github.com/prebid/prebid-server/v2@v2.18.0/adapters/amx/amx_test.go (about) 1 package amx 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "testing" 7 8 "github.com/prebid/openrtb/v20/openrtb2" 9 "github.com/prebid/prebid-server/v2/adapters" 10 "github.com/prebid/prebid-server/v2/config" 11 "github.com/prebid/prebid-server/v2/openrtb_ext" 12 "github.com/stretchr/testify/assert" 13 14 "github.com/prebid/prebid-server/v2/adapters/adapterstest" 15 ) 16 17 const ( 18 amxTestEndpoint = "http://pbs-dev.amxrtb.com/auction/openrtb" 19 sampleVastADM = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><VAST version=\"2.0\"><Ad id=\"128a6.44d74.46b3\"><InLine><Error><![CDATA[http://example.net/hbx/verr?e=]]></Error><Impression><![CDATA[http://example.net/hbx/vimp?lid=test&aid=testapp]]></Impression><Creatives><Creative sequence=\"1\"><Linear><Duration>00:00:15</Duration><TrackingEvents><Tracking event=\"firstQuartile\"><![CDATA[https://example.com?event=first_quartile]]></Tracking></TrackingEvents><VideoClicks><ClickThrough><![CDATA[http://example.com]]></ClickThrough></VideoClicks><MediaFiles><MediaFile delivery=\"progressive\" width=\"16\" height=\"9\" type=\"video/mp4\" bitrate=\"800\"><![CDATA[https://example.com/media.mp4]]></MediaFile></MediaFiles></Linear></Creative></Creatives></InLine></Ad></VAST>" 20 sampleDisplayADM = "<img src='https://example.com/300x250.png' height='250' width='300'/>" 21 ) 22 23 func TestJsonSamples(t *testing.T) { 24 bidder, buildErr := Builder(openrtb_ext.BidderAMX, config.Adapter{ 25 Endpoint: amxTestEndpoint}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"}) 26 27 if buildErr != nil { 28 t.Fatalf("Builder returned unexpected error %v", buildErr) 29 } 30 31 adapterstest.RunJSONBidderTest(t, "amxtest", bidder) 32 } 33 34 func TestEndpointMalformed(t *testing.T) { 35 _, buildErr := Builder(openrtb_ext.BidderAMX, config.Adapter{ 36 Endpoint: " http://leading.space.is.invalid"}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"}) 37 38 assert.Error(t, buildErr) 39 } 40 41 func TestEndpointQueryStringMalformed(t *testing.T) { 42 _, buildErr := Builder(openrtb_ext.BidderAMX, config.Adapter{ 43 Endpoint: "http://invalid.query.from.go.docs/page?%gh&%ij"}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"}) 44 45 assert.Error(t, buildErr) 46 } 47 48 func TestMakeRequestsTagID(t *testing.T) { 49 var w, h int = 300, 250 50 var width, height int64 = int64(w), int64(h) 51 52 bidder, buildErr := Builder(openrtb_ext.BidderAMX, config.Adapter{ 53 Endpoint: amxTestEndpoint}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"}) 54 55 if buildErr != nil { 56 t.Fatalf("Builder returned unexpected error %v", buildErr) 57 } 58 59 type testCase struct { 60 tagID string 61 extAdUnitID string 62 expectedTagID string 63 blankNil bool 64 } 65 66 tests := []testCase{ 67 {tagID: "tag-id", extAdUnitID: "ext.adUnitID", expectedTagID: "ext.adUnitID", blankNil: false}, 68 {tagID: "tag-id", extAdUnitID: "", expectedTagID: "tag-id", blankNil: false}, 69 {tagID: "tag-id", extAdUnitID: "", expectedTagID: "tag-id", blankNil: true}, 70 {tagID: "", extAdUnitID: "", expectedTagID: "", blankNil: true}, 71 {tagID: "", extAdUnitID: "", expectedTagID: "", blankNil: false}, 72 {tagID: "", extAdUnitID: "ext.adUnitID", expectedTagID: "ext.adUnitID", blankNil: true}, 73 {tagID: "", extAdUnitID: "ext.adUnitID", expectedTagID: "ext.adUnitID", blankNil: false}, 74 } 75 76 for _, tc := range tests { 77 imp1 := openrtb2.Imp{ 78 ID: "sample_imp_1", 79 Banner: &openrtb2.Banner{ 80 W: &width, 81 H: &height, 82 Format: []openrtb2.Format{ 83 {W: 300, H: 250}, 84 }, 85 }} 86 87 if tc.extAdUnitID != "" || !tc.blankNil { 88 imp1.Ext = json.RawMessage( 89 fmt.Sprintf(`{"bidder":{"adUnitId":"%s"}}`, tc.extAdUnitID)) 90 } 91 92 if tc.tagID != "" || !tc.blankNil { 93 imp1.TagID = tc.tagID 94 } 95 96 inputRequest := openrtb2.BidRequest{ 97 User: &openrtb2.User{}, 98 Imp: []openrtb2.Imp{imp1}, 99 Site: &openrtb2.Site{}, 100 } 101 102 actualAdapterRequests, err := bidder.MakeRequests(&inputRequest, &adapters.ExtraRequestInfo{}) 103 assert.Len(t, actualAdapterRequests, 1) 104 assert.Empty(t, err) 105 var body openrtb2.BidRequest 106 assert.Nil(t, json.Unmarshal(actualAdapterRequests[0].Body, &body)) 107 assert.Equal(t, tc.expectedTagID, body.Imp[0].TagID) 108 } 109 } 110 111 func TestMakeRequestsPublisherId(t *testing.T) { 112 var w, h int = 300, 250 113 var width, height int64 = int64(w), int64(h) 114 115 bidder, buildErr := Builder(openrtb_ext.BidderAMX, config.Adapter{ 116 Endpoint: amxTestEndpoint}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"}) 117 118 if buildErr != nil { 119 t.Fatalf("Builder returned unexpected error %v", buildErr) 120 } 121 122 type testCase struct { 123 publisherID string 124 extTagID string 125 expectedPublisherID string 126 blankNil bool 127 } 128 129 tests := []testCase{ 130 {publisherID: "publisher.id", extTagID: "ext.tagId", expectedPublisherID: "ext.tagId", blankNil: false}, 131 {publisherID: "publisher.id", extTagID: "", expectedPublisherID: "publisher.id", blankNil: false}, 132 {publisherID: "", extTagID: "ext.tagId", expectedPublisherID: "ext.tagId", blankNil: false}, 133 {publisherID: "", extTagID: "ext.tagId", expectedPublisherID: "ext.tagId", blankNil: true}, 134 {publisherID: "publisher.id", extTagID: "", expectedPublisherID: "publisher.id", blankNil: false}, 135 {publisherID: "publisher.id", extTagID: "", expectedPublisherID: "publisher.id", blankNil: true}, 136 } 137 138 for _, tc := range tests { 139 imp1 := openrtb2.Imp{ 140 ID: "sample_imp_1", 141 Banner: &openrtb2.Banner{ 142 W: &width, 143 H: &height, 144 Format: []openrtb2.Format{ 145 {W: 300, H: 250}, 146 }, 147 }} 148 149 if tc.extTagID != "" || !tc.blankNil { 150 imp1.Ext = json.RawMessage( 151 fmt.Sprintf(`{"bidder":{"tagId":"%s"}}`, tc.extTagID)) 152 } 153 154 inputRequest := openrtb2.BidRequest{ 155 User: &openrtb2.User{ID: "example_user_id"}, 156 Imp: []openrtb2.Imp{imp1}, 157 Site: &openrtb2.Site{}, 158 ID: "1234", 159 } 160 161 if tc.publisherID != "" || !tc.blankNil { 162 inputRequest.Site.Publisher = &openrtb2.Publisher{ 163 ID: tc.publisherID, 164 } 165 } 166 167 actualAdapterRequests, err := bidder.MakeRequests(&inputRequest, &adapters.ExtraRequestInfo{}) 168 assert.Len(t, actualAdapterRequests, 1) 169 assert.Empty(t, err) 170 var body openrtb2.BidRequest 171 assert.Nil(t, json.Unmarshal(actualAdapterRequests[0].Body, &body)) 172 assert.Equal(t, tc.expectedPublisherID, body.Site.Publisher.ID) 173 } 174 } 175 176 func TestMakeBids(t *testing.T) { 177 bidder, buildErr := Builder(openrtb_ext.BidderAMX, config.Adapter{ 178 Endpoint: amxTestEndpoint}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"}) 179 180 if buildErr != nil { 181 t.Fatalf("Failed to build bidder: %v", buildErr) 182 } 183 184 type testCase struct { 185 bidType openrtb_ext.BidType 186 adm string 187 extRaw string 188 valid bool 189 } 190 191 tests := []testCase{ 192 {openrtb_ext.BidTypeNative, `{"assets":[]}`, `{"ct":10}`, true}, 193 {openrtb_ext.BidTypeBanner, sampleDisplayADM, `{"ct": 1}`, true}, 194 {openrtb_ext.BidTypeBanner, sampleDisplayADM, `{"ct": "invalid"}`, false}, 195 {openrtb_ext.BidTypeBanner, sampleDisplayADM, `{}`, true}, 196 {openrtb_ext.BidTypeVideo, sampleVastADM, `{"startdelay": 1}`, true}, 197 {openrtb_ext.BidTypeBanner, sampleVastADM, `{"ct": 1}`, true}, // the server shouldn't do this 198 } 199 200 for _, test := range tests { 201 bid := openrtb2.Bid{ 202 AdM: test.adm, 203 Price: 1, 204 Ext: json.RawMessage(test.extRaw), 205 } 206 207 sb := openrtb2.SeatBid{ 208 Bid: []openrtb2.Bid{bid}, 209 } 210 211 resp := openrtb2.BidResponse{ 212 SeatBid: []openrtb2.SeatBid{sb}, 213 } 214 215 respJson, jsonErr := json.Marshal(resp) 216 if jsonErr != nil { 217 t.Fatalf("Failed to serialize test bid %v: %v", test, jsonErr) 218 } 219 220 bids, errs := bidder.MakeBids(nil, nil, &adapters.ResponseData{ 221 StatusCode: 200, 222 Body: respJson, 223 }) 224 225 if !test.valid { 226 assert.Len(t, errs, 1) 227 continue 228 } 229 230 if len(errs) > 0 { 231 t.Fatalf("Failed to make bids: %v", errs) 232 } 233 234 assert.Len(t, bids.Bids, 1) 235 assert.Equal(t, test.bidType, bids.Bids[0].BidType) 236 } 237 238 }