github.com/prebid/prebid-server/v2@v2.18.0/adapters/avocet/avocet_test.go (about)

     1  package avocet
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  	"reflect"
     7  	"testing"
     8  
     9  	"github.com/prebid/openrtb/v20/adcom1"
    10  	"github.com/prebid/openrtb/v20/openrtb2"
    11  	"github.com/prebid/prebid-server/v2/adapters"
    12  	"github.com/prebid/prebid-server/v2/adapters/adapterstest"
    13  	"github.com/prebid/prebid-server/v2/config"
    14  	"github.com/prebid/prebid-server/v2/errortypes"
    15  	"github.com/prebid/prebid-server/v2/openrtb_ext"
    16  )
    17  
    18  func TestJsonSamples(t *testing.T) {
    19  	bidder, buildErr := Builder(openrtb_ext.BidderAvocet, config.Adapter{
    20  		Endpoint: "https://bid.staging.avct.cloud/ortb/bid/5e722ee9bd6df11d063a8013"}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"})
    21  
    22  	if buildErr != nil {
    23  		t.Fatalf("Builder returned unexpected error %v", buildErr)
    24  	}
    25  
    26  	adapterstest.RunJSONBidderTest(t, "avocettest", bidder)
    27  }
    28  
    29  func TestAvocetAdapter_MakeRequests(t *testing.T) {
    30  	type fields struct {
    31  		Endpoint string
    32  	}
    33  	type args struct {
    34  		request *openrtb2.BidRequest
    35  		reqInfo *adapters.ExtraRequestInfo
    36  	}
    37  	type reqData []*adapters.RequestData
    38  	tests := []struct {
    39  		name     string
    40  		fields   fields
    41  		args     args
    42  		want     []*adapters.RequestData
    43  		wantErrs []error
    44  	}{
    45  		{
    46  			name:   "return nil if zero imps",
    47  			fields: fields{Endpoint: "https://bid.avct.cloud"},
    48  			args: args{
    49  				&openrtb2.BidRequest{},
    50  				nil,
    51  			},
    52  			want:     nil,
    53  			wantErrs: nil,
    54  		},
    55  		{
    56  			name:   "makes POST request with JSON content",
    57  			fields: fields{Endpoint: "https://bid.avct.cloud"},
    58  			args: args{
    59  				&openrtb2.BidRequest{Imp: []openrtb2.Imp{{}}},
    60  				nil,
    61  			},
    62  			want: reqData{
    63  				&adapters.RequestData{
    64  					Method: http.MethodPost,
    65  					Uri:    "https://bid.avct.cloud",
    66  					Body:   []byte(`{"id":"","imp":[{"id":""}]}`),
    67  					Headers: map[string][]string{
    68  						"Accept":       {"application/json"},
    69  						"Content-Type": {"application/json;charset=utf-8"},
    70  					},
    71  					ImpIDs: []string{""},
    72  				},
    73  			},
    74  			wantErrs: nil,
    75  		},
    76  	}
    77  	for _, tt := range tests {
    78  		t.Run(tt.name, func(t *testing.T) {
    79  			a := &AvocetAdapter{
    80  				Endpoint: tt.fields.Endpoint,
    81  			}
    82  			got, got1 := a.MakeRequests(tt.args.request, tt.args.reqInfo)
    83  			if len(got) != len(tt.want) {
    84  				t.Errorf("AvocetAdapter.MakeRequests() got %v requests, wanted %v requests", len(got), len(tt.want))
    85  			}
    86  			if len(got) == len(tt.want) {
    87  				for i := range tt.want {
    88  					if !reflect.DeepEqual(got[i], tt.want[i]) {
    89  						t.Errorf("AvocetAdapter.MakeRequests() got = %v, want %v", got[i], tt.want[i])
    90  					}
    91  				}
    92  			}
    93  			if !reflect.DeepEqual(got1, tt.wantErrs) {
    94  				t.Errorf("AvocetAdapter.MakeRequests() got1 = %v, want %v", got1, tt.wantErrs)
    95  			}
    96  		})
    97  	}
    98  }
    99  
   100  func TestAvocetAdapter_MakeBids(t *testing.T) {
   101  	type fields struct {
   102  		Endpoint string
   103  	}
   104  	type args struct {
   105  		internalRequest *openrtb2.BidRequest
   106  		externalRequest *adapters.RequestData
   107  		response        *adapters.ResponseData
   108  	}
   109  	tests := []struct {
   110  		name   string
   111  		fields fields
   112  		args   args
   113  		want   *adapters.BidderResponse
   114  		errs   []error
   115  	}{
   116  		{
   117  			name:   "204 No Content indicates no bids",
   118  			fields: fields{Endpoint: "https://bid.avct.cloud"},
   119  			args: args{
   120  				nil,
   121  				nil,
   122  				&adapters.ResponseData{StatusCode: http.StatusNoContent},
   123  			},
   124  			want: nil,
   125  			errs: nil,
   126  		},
   127  		{
   128  			name:   "Non-200 return error",
   129  			fields: fields{Endpoint: "https://bid.avct.cloud"},
   130  			args: args{
   131  				nil,
   132  				nil,
   133  				&adapters.ResponseData{StatusCode: http.StatusBadRequest, Body: []byte("message")},
   134  			},
   135  			want: nil,
   136  			errs: []error{&errortypes.BadServerResponse{Message: "received status code: 400 error: message"}},
   137  		},
   138  		{
   139  			name:   "200 response containing banner bids",
   140  			fields: fields{Endpoint: "https://bid.avct.cloud"},
   141  			args: args{
   142  				nil,
   143  				nil,
   144  				&adapters.ResponseData{StatusCode: http.StatusOK, Body: validBannerBidResponseBody},
   145  			},
   146  			want: &adapters.BidderResponse{
   147  				Currency: "USD",
   148  				Bids: []*adapters.TypedBid{
   149  					{
   150  						Bid:     &validBannerBid,
   151  						BidType: openrtb_ext.BidTypeBanner,
   152  					},
   153  				},
   154  			},
   155  			errs: nil,
   156  		},
   157  		{
   158  			name:   "200 response containing video bids",
   159  			fields: fields{Endpoint: "https://bid.avct.cloud"},
   160  			args: args{
   161  				nil,
   162  				nil,
   163  				&adapters.ResponseData{StatusCode: http.StatusOK, Body: validVideoBidResponseBody},
   164  			},
   165  			want: &adapters.BidderResponse{
   166  				Currency: "USD",
   167  				Bids: []*adapters.TypedBid{
   168  					{
   169  						Bid:     &validVideoBid,
   170  						BidType: openrtb_ext.BidTypeVideo,
   171  						BidVideo: &openrtb_ext.ExtBidPrebidVideo{
   172  							Duration: 30,
   173  						},
   174  					},
   175  				},
   176  			},
   177  			errs: nil,
   178  		},
   179  	}
   180  	for _, tt := range tests {
   181  		t.Run(tt.name, func(t *testing.T) {
   182  			a := &AvocetAdapter{
   183  				Endpoint: tt.fields.Endpoint,
   184  			}
   185  			got, got1 := a.MakeBids(tt.args.internalRequest, tt.args.externalRequest, tt.args.response)
   186  			if !reflect.DeepEqual(got, tt.want) {
   187  				gotb, _ := json.Marshal(got)
   188  				wantb, _ := json.Marshal(tt.want)
   189  				t.Errorf("AvocetAdapter.MakeBids() got = %s, want %s", string(gotb), string(wantb))
   190  			}
   191  			if !reflect.DeepEqual(got1, tt.errs) {
   192  				t.Errorf("AvocetAdapter.MakeBids() got1 = %v, want %v", got1, tt.errs)
   193  			}
   194  		})
   195  	}
   196  }
   197  
   198  func Test_getBidType(t *testing.T) {
   199  	type args struct {
   200  		bid openrtb2.Bid
   201  		ext avocetBidExt
   202  	}
   203  	tests := []struct {
   204  		name string
   205  		args args
   206  		want openrtb_ext.BidType
   207  	}{
   208  		{
   209  			name: "VPAID 1.0",
   210  			args: args{openrtb2.Bid{API: adcom1.APIVPAID10}, avocetBidExt{}},
   211  			want: openrtb_ext.BidTypeVideo,
   212  		},
   213  		{
   214  			name: "VPAID 2.0",
   215  			args: args{openrtb2.Bid{API: adcom1.APIVPAID20}, avocetBidExt{}},
   216  			want: openrtb_ext.BidTypeVideo,
   217  		},
   218  		{
   219  			name: "other",
   220  			args: args{openrtb2.Bid{}, avocetBidExt{}},
   221  			want: openrtb_ext.BidTypeBanner,
   222  		},
   223  	}
   224  	for _, tt := range tests {
   225  		t.Run(tt.name, func(t *testing.T) {
   226  			if got := getBidType(tt.args.bid, tt.args.ext); !reflect.DeepEqual(got, tt.want) {
   227  				t.Errorf("getBidType() = %v, want %v", got, tt.want)
   228  			}
   229  		})
   230  	}
   231  }
   232  
   233  var validBannerBid = openrtb2.Bid{
   234  	AdM:      "<iframe src=\"http://ads.staging.avct.cloud/sv?pp=${AUCTION_PRICE}&uuid=0df2c449-6d85-4179-b5d5-37f2f91caa24&ty=h&crid=5b51e49634f2021f127ff7c9&tacid=5b51e4ed89654741306813a8&aid=749d36d7-c993-455f-aefd-ffd8a7e3ccf_0&accid=5b51dd1634f2021f127ff7c0&brid=5b51e20f34f2021f127ff7c4&ioid=5b51e22089654741306813a1&caid=5b51e2d689654741306813a4&it=1&iobsid=496e8cff35b2c0110029534d&ext_aid=749d36d7-c993-455f-aefd-ffd8a7e3ccf_0&bp=15.64434783&bt=1591874537316649768&h=250&w=300&vpr=0&vdp=0&domain=example.com&gco=54510b3b816269000061a0f7&stid=542d2c1615e3c013de53a6e2&glat=0&glong=0&bip4=3232238090&ext_siid=5ea89200c865f911007f1b0e&ext_pid=1&ext_sid=5ea84df8c865f911007f1ade&ext_plid=5ea9601ac865f911007f1b6a&optv=latest:latest&invsrc=5e722ee9bd6df11d063a8013&ug=0d&ca=0&biid=requestd-54644474bf-l7gx4|eu-central-1-staging&reg=eu-central-1&ck=1_5d99a849\" height=\"250\" width=\"300\" marginwidth=0 marginheight=0 hspace=0 vspace=0 frameborder=0 scrolling=\"no\"></iframe>",
   235  	ADomain:  []string{"avocet.io"},
   236  	CID:      "5b51e2d689654741306813a4",
   237  	CrID:     "5b51e49634f2021f127ff7c9",
   238  	H:        250,
   239  	ID:       "bc708396-9202-437b-b726-08b9864cb8b8",
   240  	ImpID:    "test-imp-id",
   241  	IURL:     "https://cdn.staging.avocet.io/snapshots/5b51dd1634f2021f127ff7c0/5b51e49634f2021f127ff7c9.jpeg",
   242  	Language: "en",
   243  	Price:    15.64434783,
   244  	W:        300,
   245  }
   246  
   247  var validBannerBidResponseBody = []byte(`{
   248  	"bidid": "dd87f80c-16a0-43c8-a673-b94b3ea4d417",
   249  	"id": "test-request-id",
   250  	"seatbid": [
   251  		{
   252  			"bid": [
   253  				{
   254  					"adm": "<iframe src=\"http://ads.staging.avct.cloud/sv?pp=${AUCTION_PRICE}&uuid=0df2c449-6d85-4179-b5d5-37f2f91caa24&ty=h&crid=5b51e49634f2021f127ff7c9&tacid=5b51e4ed89654741306813a8&aid=749d36d7-c993-455f-aefd-ffd8a7e3ccf_0&accid=5b51dd1634f2021f127ff7c0&brid=5b51e20f34f2021f127ff7c4&ioid=5b51e22089654741306813a1&caid=5b51e2d689654741306813a4&it=1&iobsid=496e8cff35b2c0110029534d&ext_aid=749d36d7-c993-455f-aefd-ffd8a7e3ccf_0&bp=15.64434783&bt=1591874537316649768&h=250&w=300&vpr=0&vdp=0&domain=example.com&gco=54510b3b816269000061a0f7&stid=542d2c1615e3c013de53a6e2&glat=0&glong=0&bip4=3232238090&ext_siid=5ea89200c865f911007f1b0e&ext_pid=1&ext_sid=5ea84df8c865f911007f1ade&ext_plid=5ea9601ac865f911007f1b6a&optv=latest:latest&invsrc=5e722ee9bd6df11d063a8013&ug=0d&ca=0&biid=requestd-54644474bf-l7gx4|eu-central-1-staging&reg=eu-central-1&ck=1_5d99a849\" height=\"250\" width=\"300\" marginwidth=0 marginheight=0 hspace=0 vspace=0 frameborder=0 scrolling=\"no\"></iframe>",
   255  					"adomain": ["avocet.io"],
   256  					"cid": "5b51e2d689654741306813a4",
   257  					"crid": "5b51e49634f2021f127ff7c9",
   258  					"h": 250,
   259  					"id": "bc708396-9202-437b-b726-08b9864cb8b8",
   260  					"impid": "test-imp-id",
   261  					"iurl": "https://cdn.staging.avocet.io/snapshots/5b51dd1634f2021f127ff7c0/5b51e49634f2021f127ff7c9.jpeg",
   262  					"language": "en",
   263  					"price": 15.64434783,
   264  					"w": 300
   265  				}
   266  			],
   267  			"seat": "TEST_SEAT_ID"
   268  		}
   269  	]
   270  }`)
   271  
   272  var validVideoBid = openrtb2.Bid{
   273  	AdM:      "<VAST version=\"3.0\"><Ad id=\"5ec530e32d57fe1100f17d87\"><Wrapper><AdSystem>Avocet</AdSystem><VASTAdTagURI><![CDATA[http://ads.staging.avct.cloud/vast?x=1&uuid=0df2c449-6d85-4179-b5d5-37f2f91caa24&ty=h&crid=5ec530e32d57fe1100f17d87&tacid=5ec531d32d57fe1100f17d89&aid=749d36d7-c993-455f-aefd-ffd8a7e3ccf_0&accid=5b51dd1634f2021f127ff7c0&brid=5b51e20f34f2021f127ff7c4&ioid=5b51e22089654741306813a1&caid=5b51e2d689654741306813a4&it=2&iobsid=496e8cff35b2c0110029534d&ext_aid=749d36d7-c993-455f-aefd-ffd8a7e3ccf_0&bp=15.64434783&bt=1591875033134290518&h=396&w=600&vpr=0&vdp=0&domain=example.com&gco=54510b3b816269000061a0f7&stid=542d2c1615e3c013de53a6e2&glat=0&glong=0&bip4=3232238090&ext_siid=5ea89200c865f911007f1b0e&ext_pid=1&ext_sid=5ea84df8c865f911007f1ade&ext_plid=5ea9601ac865f911007f1b6a&optv=latest:latest&invsrc=5e722ee9bd6df11d063a8013&ug=0d&ca=0&biid=requestd-54644474bf-l7gx4|eu-central-1-staging&reg=eu-central-1&pixel=1&ck=1_c343bf14]]></VASTAdTagURI><Impression><![CDATA[http://ads.staging.avct.cloud/sv?pp=${AUCTION_PRICE}&uuid=0df2c449-6d85-4179-b5d5-37f2f91caa24&ty=h&crid=5ec530e32d57fe1100f17d87&tacid=5ec531d32d57fe1100f17d89&aid=749d36d7-c993-455f-aefd-ffd8a7e3ccf_0&accid=5b51dd1634f2021f127ff7c0&brid=5b51e20f34f2021f127ff7c4&ioid=5b51e22089654741306813a1&caid=5b51e2d689654741306813a4&it=2&iobsid=496e8cff35b2c0110029534d&ext_aid=749d36d7-c993-455f-aefd-ffd8a7e3ccf_0&bp=15.64434783&bt=1591875033134290518&h=396&w=600&vpr=0&vdp=0&domain=example.com&gco=54510b3b816269000061a0f7&stid=542d2c1615e3c013de53a6e2&glat=0&glong=0&bip4=3232238090&ext_siid=5ea89200c865f911007f1b0e&ext_pid=1&ext_sid=5ea84df8c865f911007f1ade&ext_plid=5ea9601ac865f911007f1b6a&optv=latest:latest&invsrc=5e722ee9bd6df11d063a8013&ug=0d&ca=0&biid=requestd-54644474bf-l7gx4|eu-central-1-staging&reg=eu-central-1&pixel=1&ck=1_c343bf14]]></Impression><Creatives><Creative AdId=\"5ec530e32d57fe1100f17d87\"><Linear><TrackingEvents></TrackingEvents><VideoClicks></VideoClicks></Linear></Creative></Creatives></Wrapper></Ad></VAST>",
   274  	ADomain:  []string{"avocet.io"},
   275  	CID:      "5b51e2d689654741306813a4",
   276  	CrID:     "5ec530e32d57fe1100f17d87",
   277  	H:        396,
   278  	ID:       "3d4c2d45-5a8c-43b8-9e15-4f48ac45204f",
   279  	ImpID:    "dfp-ad--top-above-nav",
   280  	IURL:     "https://cdn.staging.avocet.io/snapshots/5b51dd1634f2021f127ff7c0/5ec530e32d57fe1100f17d87.jpeg",
   281  	Language: "en",
   282  	Price:    15.64434783,
   283  	W:        600,
   284  	Ext:      []byte(`{"avocet":{"duration":30}}`),
   285  }
   286  
   287  var validVideoBidResponseBody = []byte(`{
   288  	"bidid": "dd87f80c-16a0-43c8-a673-b94b3ea4d417",
   289  	"id": "test-request-id",
   290  	"seatbid": [
   291  		{
   292  			"bid": [
   293  				{
   294  					"adm": "<VAST version=\"3.0\"><Ad id=\"5ec530e32d57fe1100f17d87\"><Wrapper><AdSystem>Avocet</AdSystem><VASTAdTagURI><![CDATA[http://ads.staging.avct.cloud/vast?x=1&uuid=0df2c449-6d85-4179-b5d5-37f2f91caa24&ty=h&crid=5ec530e32d57fe1100f17d87&tacid=5ec531d32d57fe1100f17d89&aid=749d36d7-c993-455f-aefd-ffd8a7e3ccf_0&accid=5b51dd1634f2021f127ff7c0&brid=5b51e20f34f2021f127ff7c4&ioid=5b51e22089654741306813a1&caid=5b51e2d689654741306813a4&it=2&iobsid=496e8cff35b2c0110029534d&ext_aid=749d36d7-c993-455f-aefd-ffd8a7e3ccf_0&bp=15.64434783&bt=1591875033134290518&h=396&w=600&vpr=0&vdp=0&domain=example.com&gco=54510b3b816269000061a0f7&stid=542d2c1615e3c013de53a6e2&glat=0&glong=0&bip4=3232238090&ext_siid=5ea89200c865f911007f1b0e&ext_pid=1&ext_sid=5ea84df8c865f911007f1ade&ext_plid=5ea9601ac865f911007f1b6a&optv=latest:latest&invsrc=5e722ee9bd6df11d063a8013&ug=0d&ca=0&biid=requestd-54644474bf-l7gx4|eu-central-1-staging&reg=eu-central-1&pixel=1&ck=1_c343bf14]]></VASTAdTagURI><Impression><![CDATA[http://ads.staging.avct.cloud/sv?pp=${AUCTION_PRICE}&uuid=0df2c449-6d85-4179-b5d5-37f2f91caa24&ty=h&crid=5ec530e32d57fe1100f17d87&tacid=5ec531d32d57fe1100f17d89&aid=749d36d7-c993-455f-aefd-ffd8a7e3ccf_0&accid=5b51dd1634f2021f127ff7c0&brid=5b51e20f34f2021f127ff7c4&ioid=5b51e22089654741306813a1&caid=5b51e2d689654741306813a4&it=2&iobsid=496e8cff35b2c0110029534d&ext_aid=749d36d7-c993-455f-aefd-ffd8a7e3ccf_0&bp=15.64434783&bt=1591875033134290518&h=396&w=600&vpr=0&vdp=0&domain=example.com&gco=54510b3b816269000061a0f7&stid=542d2c1615e3c013de53a6e2&glat=0&glong=0&bip4=3232238090&ext_siid=5ea89200c865f911007f1b0e&ext_pid=1&ext_sid=5ea84df8c865f911007f1ade&ext_plid=5ea9601ac865f911007f1b6a&optv=latest:latest&invsrc=5e722ee9bd6df11d063a8013&ug=0d&ca=0&biid=requestd-54644474bf-l7gx4|eu-central-1-staging&reg=eu-central-1&pixel=1&ck=1_c343bf14]]></Impression><Creatives><Creative AdId=\"5ec530e32d57fe1100f17d87\"><Linear><TrackingEvents></TrackingEvents><VideoClicks></VideoClicks></Linear></Creative></Creatives></Wrapper></Ad></VAST>",
   295  					"adomain": ["avocet.io"],
   296  					"cid": "5b51e2d689654741306813a4",
   297  					"crid": "5ec530e32d57fe1100f17d87",
   298  					"h": 396,
   299  					"id": "3d4c2d45-5a8c-43b8-9e15-4f48ac45204f",
   300  					"impid": "dfp-ad--top-above-nav",
   301  					"iurl": "https://cdn.staging.avocet.io/snapshots/5b51dd1634f2021f127ff7c0/5ec530e32d57fe1100f17d87.jpeg",
   302  					"language": "en",
   303  					"price": 15.64434783,
   304  					"w": 600,
   305  					"ext": {"avocet":{"duration":30}}
   306  				}
   307  			],
   308  			"seat": "TEST_SEAT_ID"
   309  		}
   310  	]
   311  }`)