github.com/prebid/prebid-server/v2@v2.18.0/adapters/yieldlab/yieldlab_test.go (about) 1 package yieldlab 2 3 import ( 4 "encoding/json" 5 "strconv" 6 "testing" 7 8 "github.com/prebid/openrtb/v20/openrtb2" 9 "github.com/stretchr/testify/assert" 10 11 "github.com/prebid/prebid-server/v2/adapters/adapterstest" 12 "github.com/prebid/prebid-server/v2/config" 13 "github.com/prebid/prebid-server/v2/openrtb_ext" 14 ) 15 16 const testURL = "https://ad.yieldlab.net/testing/" 17 18 var testCacheBuster cacheBuster = func() string { 19 return "testing" 20 } 21 22 var testWeekGenerator weekGenerator = func() string { 23 return "33" 24 } 25 26 func newTestYieldlabBidder(endpoint string) *YieldlabAdapter { 27 return &YieldlabAdapter{ 28 endpoint: endpoint, 29 cacheBuster: testCacheBuster, 30 getWeek: testWeekGenerator, 31 } 32 } 33 34 func TestNewYieldlabBidder(t *testing.T) { 35 bidder, buildErr := Builder(openrtb_ext.BidderYieldlab, config.Adapter{ 36 Endpoint: testURL}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"}) 37 38 assert.NoError(t, buildErr) 39 assert.NotNil(t, bidder) 40 41 bidderYieldlab := bidder.(*YieldlabAdapter) 42 assert.Equal(t, testURL, bidderYieldlab.endpoint) 43 assert.NotNil(t, bidderYieldlab.cacheBuster) 44 assert.NotNil(t, bidderYieldlab.getWeek) 45 } 46 47 func TestJsonSamples(t *testing.T) { 48 adapterstest.RunJSONBidderTest(t, "yieldlabtest", newTestYieldlabBidder(testURL)) 49 } 50 51 func Test_splitSize(t *testing.T) { 52 type args struct { 53 size string 54 } 55 tests := []struct { 56 name string 57 args args 58 want uint64 59 want1 uint64 60 wantErr bool 61 }{ 62 { 63 name: "valid", 64 args: args{ 65 size: "300x800", 66 }, 67 want: 300, 68 want1: 800, 69 wantErr: false, 70 }, 71 { 72 name: "empty", 73 args: args{ 74 size: "", 75 }, 76 want: 0, 77 want1: 0, 78 wantErr: false, 79 }, 80 { 81 name: "invalid", 82 args: args{ 83 size: "test", 84 }, 85 want: 0, 86 want1: 0, 87 wantErr: false, 88 }, 89 { 90 name: "invalid_height", 91 args: args{ 92 size: "200xtest", 93 }, 94 want: 0, 95 want1: 0, 96 wantErr: true, 97 }, 98 { 99 name: "invalid_width", 100 args: args{ 101 size: "testx200", 102 }, 103 want: 0, 104 want1: 0, 105 wantErr: true, 106 }, 107 { 108 name: "invalid_separator", 109 args: args{ 110 size: "200y200", 111 }, 112 want: 0, 113 want1: 0, 114 wantErr: false, 115 }, 116 } 117 for _, tt := range tests { 118 t.Run(tt.name, func(t *testing.T) { 119 got, got1, err := splitSize(tt.args.size) 120 if (err != nil) != tt.wantErr { 121 t.Errorf("splitSize() error = %v, wantErr %v", err, tt.wantErr) 122 return 123 } 124 if got != tt.want { 125 t.Errorf("splitSize() got = %v, want %v", got, tt.want) 126 } 127 if got1 != tt.want1 { 128 t.Errorf("splitSize() got1 = %v, want %v", got1, tt.want1) 129 } 130 }) 131 } 132 } 133 134 func Test_makeNodeValue(t *testing.T) { 135 int8TestCase := int8(8) 136 tests := []struct { 137 name string 138 nodeParam interface{} 139 expected string 140 }{ 141 { 142 name: "string with special characters", 143 nodeParam: "AZ09-._~:/?#[]@!$%&'()*+,;=", 144 expected: "AZ09-._~%3A%2F%3F%23%5B%5D%40%21%24%25%26%27%28%29%2A%2B%2C%3B%3D", 145 }, 146 { 147 name: "int8 pointer", 148 nodeParam: &int8TestCase, 149 expected: "8", 150 }, 151 { 152 name: "int", 153 nodeParam: 8, 154 expected: "8", 155 }, 156 { 157 name: "free form data", 158 nodeParam: json.RawMessage(`{"foo":"bar"}`), 159 expected: "%7B%22foo%22%3A%22bar%22%7D", 160 }, 161 { 162 name: "unknown type (bool)", 163 nodeParam: true, 164 expected: "", 165 }, 166 } 167 for _, test := range tests { 168 t.Run(test.name, func(t *testing.T) { 169 actual := makeNodeValue(test.nodeParam) 170 assert.Equal(t, test.expected, actual) 171 }) 172 } 173 } 174 175 func Test_makeSupplyChain(t *testing.T) { 176 hp := int8(1) 177 tests := []struct { 178 name string 179 param openrtb2.SupplyChain 180 expected string 181 }{ 182 { 183 name: "No nodes", 184 param: openrtb2.SupplyChain{ 185 Ver: "1.0", 186 Complete: 1, 187 }, 188 expected: "", 189 }, 190 { 191 name: "Not all fields", 192 param: openrtb2.SupplyChain{ 193 Ver: "1.0", 194 Complete: 1, 195 Nodes: []openrtb2.SupplyChainNode{ 196 { 197 ASI: "exchange1.com", 198 SID: "12345", 199 HP: &hp, 200 }, 201 }, 202 }, 203 204 expected: "1.0,1!exchange1.com,12345,1,,,,", 205 }, 206 { 207 name: "All fields handled in correct order", 208 param: openrtb2.SupplyChain{ 209 Ver: "1.0", 210 Complete: 1, 211 Nodes: []openrtb2.SupplyChainNode{ 212 { 213 ASI: "exchange1.com", 214 SID: "12345", 215 HP: &hp, 216 RID: "bid-request-1", 217 Name: "publisher", 218 Domain: "publisher.com", 219 Ext: []byte("{\"ext\":\"test\"}"), 220 }, 221 }, 222 }, 223 expected: "1.0,1!exchange1.com,12345,1,bid-request-1,publisher,publisher.com,%7B%22ext%22%3A%22test%22%7D", 224 }, 225 { 226 name: "handle simple node.ext type (string)", 227 param: openrtb2.SupplyChain{ 228 Ver: "1.0", 229 Complete: 1, 230 Nodes: []openrtb2.SupplyChainNode{ 231 { 232 Ext: []byte("\"ext\""), 233 }, 234 }, 235 }, 236 expected: "1.0,1!,,,,,,%22ext%22", 237 }, 238 { 239 name: "handle simple node.ext type (int)", 240 param: openrtb2.SupplyChain{ 241 Ver: "1.0", 242 Complete: 1, 243 Nodes: []openrtb2.SupplyChainNode{ 244 { 245 Ext: []byte(strconv.Itoa(1)), 246 }, 247 }, 248 }, 249 expected: "1.0,1!,,,,,,1", 250 }, 251 } 252 for _, test := range tests { 253 t.Run(test.name, func(t *testing.T) { 254 actual := makeSupplyChain(test.param) 255 assert.Equal(t, test.expected, actual) 256 }) 257 } 258 } 259 260 func Test_makeDSATransparencyUrlParam(t *testing.T) { 261 tests := []struct { 262 name string 263 transparencies []dsaTransparency 264 expected string 265 }{ 266 { 267 name: "No transparency objects", 268 transparencies: []dsaTransparency{}, 269 expected: "", 270 }, 271 { 272 name: "Nil transparency", 273 transparencies: nil, 274 expected: "", 275 }, 276 { 277 name: "Params without a domain", 278 transparencies: []dsaTransparency{ 279 { 280 Params: []int{1, 2}, 281 }, 282 }, 283 expected: "", 284 }, 285 { 286 name: "Params without a params", 287 transparencies: []dsaTransparency{ 288 { 289 Domain: "domain.com", 290 }, 291 }, 292 expected: "domain.com", 293 }, 294 { 295 name: "One object; No Params", 296 transparencies: []dsaTransparency{ 297 { 298 Domain: "domain.com", 299 Params: []int{}, 300 }, 301 }, 302 expected: "domain.com", 303 }, 304 { 305 name: "One object; One Param", 306 transparencies: []dsaTransparency{ 307 { 308 Domain: "domain.com", 309 Params: []int{1}, 310 }, 311 }, 312 expected: "domain.com~1", 313 }, 314 { 315 name: "Three domain objects", 316 transparencies: []dsaTransparency{ 317 { 318 Domain: "domain1.com", 319 Params: []int{1, 2}, 320 }, 321 { 322 Domain: "domain2.com", 323 Params: []int{3, 4}, 324 }, 325 { 326 Domain: "domain3.com", 327 Params: []int{5, 6}, 328 }, 329 }, 330 expected: "domain1.com~1_2~~domain2.com~3_4~~domain3.com~5_6", 331 }, 332 } 333 for _, test := range tests { 334 t.Run(test.name, func(t *testing.T) { 335 actual := makeDSATransparencyURLParam(test.transparencies) 336 assert.Equal(t, test.expected, actual) 337 }) 338 } 339 } 340 341 func Test_getDSA_invalidRequestExt(t *testing.T) { 342 req := &openrtb2.BidRequest{ 343 Regs: &openrtb2.Regs{Ext: json.RawMessage(`{"DSA":"wrongValueType"}`)}, 344 } 345 346 dsa, err := getDSA(req) 347 348 assert.NotNil(t, err) 349 assert.Nil(t, dsa) 350 } 351 352 func TestYieldlabAdapter_makeEndpointURL_invalidEndpoint(t *testing.T) { 353 bidder, buildErr := Builder(openrtb_ext.BidderYieldlab, config.Adapter{ 354 Endpoint: "test$:/somethingĀ§"}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"}) 355 356 if buildErr != nil { 357 t.Fatalf("Builder returned unexpected error %v", buildErr) 358 } 359 360 bidderYieldlab := bidder.(*YieldlabAdapter) 361 _, err := bidderYieldlab.makeEndpointURL(nil, nil) 362 assert.Error(t, err) 363 }