github.com/prebid/prebid-server/v2@v2.18.0/endpoints/info/bidders_test.go (about) 1 package info 2 3 import ( 4 "io" 5 "net/http" 6 "net/http/httptest" 7 "testing" 8 9 "github.com/prebid/prebid-server/v2/config" 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestPrepareBiddersResponseAll(t *testing.T) { 14 var ( 15 enabledCore = config.BidderInfo{Disabled: false} 16 enabledAlias = config.BidderInfo{Disabled: false, AliasOf: "something"} 17 disabledCore = config.BidderInfo{Disabled: true} 18 disabledAlias = config.BidderInfo{Disabled: true, AliasOf: "something"} 19 ) 20 21 testCases := []struct { 22 name string 23 givenBidders config.BidderInfos 24 givenRequestAliases map[string]string 25 expected string 26 }{ 27 { 28 name: "none", 29 givenBidders: config.BidderInfos{}, 30 givenRequestAliases: nil, 31 expected: `[]`, 32 }, 33 { 34 name: "core-one-enabled", 35 givenBidders: config.BidderInfos{"a": enabledCore}, 36 givenRequestAliases: nil, 37 expected: `["a"]`, 38 }, 39 { 40 name: "core-one-disabled", 41 givenBidders: config.BidderInfos{"a": disabledCore}, 42 givenRequestAliases: nil, 43 expected: `["a"]`, 44 }, 45 { 46 name: "core-one-mixed", 47 givenBidders: config.BidderInfos{"a": disabledCore, "b": enabledCore}, 48 givenRequestAliases: nil, 49 expected: `["a","b"]`, 50 }, 51 { 52 name: "core-one-mixed-sorted", 53 givenBidders: config.BidderInfos{"z": enabledCore, "a": enabledCore}, 54 givenRequestAliases: nil, 55 expected: `["a","z"]`, 56 }, 57 { 58 name: "alias-one", 59 givenBidders: config.BidderInfos{"a": enabledAlias}, 60 givenRequestAliases: nil, 61 expected: `["a"]`, 62 }, 63 { 64 name: "alias-mixed", 65 givenBidders: config.BidderInfos{"a": disabledCore, "b": disabledAlias, "c": enabledCore, "d": enabledAlias}, 66 givenRequestAliases: nil, 67 expected: `["a","b","c","d"]`, 68 }, 69 { 70 name: "alias-mixed-sorted", 71 givenBidders: config.BidderInfos{"z": enabledAlias, "a": enabledCore}, 72 givenRequestAliases: nil, 73 expected: `["a","z"]`, 74 }, 75 { 76 name: "defaultrequest-one", 77 givenBidders: config.BidderInfos{"a": enabledCore}, 78 givenRequestAliases: map[string]string{"b": "a"}, 79 expected: `["a","b"]`, 80 }, 81 { 82 name: "defaultrequest-mixed", 83 givenBidders: config.BidderInfos{"a": enabledCore, "b": disabledCore}, 84 givenRequestAliases: map[string]string{"x": "a", "y": "b"}, 85 expected: `["a","b","x","y"]`, 86 }, 87 { 88 name: "defaultrequest-mixed-sorted", 89 givenBidders: config.BidderInfos{"z": enabledCore}, 90 givenRequestAliases: map[string]string{"a": "z"}, 91 expected: `["a","z"]`, 92 }, 93 { 94 name: "mixed", 95 givenBidders: config.BidderInfos{"a": disabledCore, "b": disabledAlias, "c": enabledCore, "d": enabledAlias}, 96 givenRequestAliases: map[string]string{"z": "a"}, 97 expected: `["a","b","c","d","z"]`, 98 }, 99 } 100 101 for _, test := range testCases { 102 t.Run(test.name, func(t *testing.T) { 103 result, err := prepareBiddersResponseAll(test.givenBidders, test.givenRequestAliases) 104 assert.NoError(t, err) 105 assert.Equal(t, []byte(test.expected), result) 106 }) 107 } 108 } 109 110 func TestPrepareBiddersResponseAllBaseOnly(t *testing.T) { 111 var ( 112 enabledCore = config.BidderInfo{Disabled: false} 113 enabledAlias = config.BidderInfo{Disabled: false, AliasOf: "something"} 114 disabledCore = config.BidderInfo{Disabled: true} 115 disabledAlias = config.BidderInfo{Disabled: true, AliasOf: "something"} 116 ) 117 118 testCases := []struct { 119 name string 120 givenBidders config.BidderInfos 121 expected string 122 }{ 123 { 124 name: "none", 125 givenBidders: config.BidderInfos{}, 126 expected: `[]`, 127 }, 128 { 129 name: "core-one-enabled", 130 givenBidders: config.BidderInfos{"a": enabledCore}, 131 expected: `["a"]`, 132 }, 133 { 134 name: "core-one-disabled", 135 givenBidders: config.BidderInfos{"a": disabledCore}, 136 expected: `["a"]`, 137 }, 138 { 139 name: "core-one-mixed", 140 givenBidders: config.BidderInfos{"a": disabledCore, "b": enabledCore}, 141 expected: `["a","b"]`, 142 }, 143 { 144 name: "core-one-mixed-sorted", 145 givenBidders: config.BidderInfos{"z": enabledCore, "a": enabledCore}, 146 expected: `["a","z"]`, 147 }, 148 { 149 name: "alias-one", 150 givenBidders: config.BidderInfos{"a": enabledAlias}, 151 expected: `[]`, 152 }, 153 { 154 name: "alias-mixed", 155 givenBidders: config.BidderInfos{"a": disabledCore, "b": disabledAlias, "c": enabledCore, "d": enabledAlias}, 156 expected: `["a","c"]`, 157 }, 158 } 159 160 for _, test := range testCases { 161 t.Run(test.name, func(t *testing.T) { 162 result, err := prepareBiddersResponseAllBaseOnly(test.givenBidders) 163 assert.NoError(t, err) 164 assert.Equal(t, []byte(test.expected), result) 165 }) 166 } 167 } 168 169 func TestPrepareBiddersResponseEnabledOnly(t *testing.T) { 170 var ( 171 enabledCore = config.BidderInfo{Disabled: false} 172 enabledAlias = config.BidderInfo{Disabled: false, AliasOf: "something"} 173 disabledCore = config.BidderInfo{Disabled: true} 174 disabledAlias = config.BidderInfo{Disabled: true, AliasOf: "something"} 175 ) 176 177 testCases := []struct { 178 name string 179 givenBidders config.BidderInfos 180 givenRequestAliases map[string]string 181 expected string 182 }{ 183 { 184 name: "none", 185 givenBidders: config.BidderInfos{}, 186 givenRequestAliases: nil, 187 expected: `[]`, 188 }, 189 { 190 name: "core-one-enabled", 191 givenBidders: config.BidderInfos{"a": enabledCore}, 192 givenRequestAliases: nil, 193 expected: `["a"]`, 194 }, 195 { 196 name: "core-one-disabled", 197 givenBidders: config.BidderInfos{"a": disabledCore}, 198 givenRequestAliases: nil, 199 expected: `[]`, 200 }, 201 { 202 name: "core-one-mixed", 203 givenBidders: config.BidderInfos{"a": disabledCore, "b": enabledCore}, 204 givenRequestAliases: nil, 205 expected: `["b"]`, 206 }, 207 { 208 name: "core-one-mixed-sorted", 209 givenBidders: config.BidderInfos{"z": enabledCore, "a": enabledCore}, 210 givenRequestAliases: nil, 211 expected: `["a","z"]`, 212 }, 213 { 214 name: "alias-one", 215 givenBidders: config.BidderInfos{"a": enabledAlias}, 216 givenRequestAliases: nil, 217 expected: `["a"]`, 218 }, 219 { 220 name: "alias-mixed", 221 givenBidders: config.BidderInfos{"a": disabledCore, "b": disabledAlias, "c": enabledCore, "d": enabledAlias}, 222 givenRequestAliases: nil, 223 expected: `["c","d"]`, 224 }, 225 { 226 name: "alias-mixed-sorted", 227 givenBidders: config.BidderInfos{"z": enabledAlias, "a": enabledCore}, 228 givenRequestAliases: nil, 229 expected: `["a","z"]`, 230 }, 231 { 232 name: "defaultrequest-one", 233 givenBidders: config.BidderInfos{"a": enabledCore}, 234 givenRequestAliases: map[string]string{"b": "a"}, 235 expected: `["a","b"]`, 236 }, 237 { 238 name: "defaultrequest-mixed", 239 givenBidders: config.BidderInfos{"a": enabledCore, "b": disabledCore}, 240 givenRequestAliases: map[string]string{"x": "a", "y": "b"}, 241 expected: `["a","x"]`, 242 }, 243 { 244 name: "defaultrequest-mixed-sorted", 245 givenBidders: config.BidderInfos{"z": enabledCore}, 246 givenRequestAliases: map[string]string{"a": "z"}, 247 expected: `["a","z"]`, 248 }, 249 { 250 name: "mixed", 251 givenBidders: config.BidderInfos{"a": disabledCore, "b": disabledAlias, "c": enabledCore, "d": enabledAlias}, 252 givenRequestAliases: map[string]string{"z": "a"}, 253 expected: `["c","d"]`, 254 }, 255 } 256 257 for _, test := range testCases { 258 t.Run(test.name, func(t *testing.T) { 259 result, err := prepareBiddersResponseEnabledOnly(test.givenBidders, test.givenRequestAliases) 260 assert.NoError(t, err) 261 assert.Equal(t, []byte(test.expected), result) 262 }) 263 } 264 } 265 266 func TestPrepareBiddersResponseEnabledOnlyBaseOnly(t *testing.T) { 267 var ( 268 enabledCore = config.BidderInfo{Disabled: false} 269 enabledAlias = config.BidderInfo{Disabled: false, AliasOf: "something"} 270 disabledCore = config.BidderInfo{Disabled: true} 271 disabledAlias = config.BidderInfo{Disabled: true, AliasOf: "something"} 272 ) 273 274 testCases := []struct { 275 name string 276 givenBidders config.BidderInfos 277 expected string 278 }{ 279 { 280 name: "none", 281 givenBidders: config.BidderInfos{}, 282 expected: `[]`, 283 }, 284 { 285 name: "core-one-enabled", 286 givenBidders: config.BidderInfos{"a": enabledCore}, 287 expected: `["a"]`, 288 }, 289 { 290 name: "core-one-disabled", 291 givenBidders: config.BidderInfos{"a": disabledCore}, 292 expected: `[]`, 293 }, 294 { 295 name: "core-one-mixed", 296 givenBidders: config.BidderInfos{"a": disabledCore, "b": enabledCore}, 297 expected: `["b"]`, 298 }, 299 { 300 name: "core-one-mixed-sorted", 301 givenBidders: config.BidderInfos{"z": enabledCore, "a": enabledCore}, 302 expected: `["a","z"]`, 303 }, 304 { 305 name: "alias-one", 306 givenBidders: config.BidderInfos{"a": enabledAlias}, 307 expected: `[]`, 308 }, 309 { 310 name: "alias-many", 311 givenBidders: config.BidderInfos{"a": enabledAlias, "b": enabledAlias}, 312 expected: `[]`, 313 }, 314 { 315 name: "mixed", 316 givenBidders: config.BidderInfos{"a": disabledCore, "b": disabledAlias, "c": enabledCore, "d": enabledAlias}, 317 expected: `["c"]`, 318 }, 319 } 320 321 for _, test := range testCases { 322 t.Run(test.name, func(t *testing.T) { 323 result, err := prepareBiddersResponseEnabledOnlyBaseOnly(test.givenBidders) 324 assert.NoError(t, err) 325 assert.Equal(t, []byte(test.expected), result) 326 }) 327 } 328 } 329 330 func TestBiddersHandler(t *testing.T) { 331 var ( 332 enabledCore = config.BidderInfo{Disabled: false} 333 enabledAlias = config.BidderInfo{Disabled: false, AliasOf: "something"} 334 disabledCore = config.BidderInfo{Disabled: true} 335 disabledAlias = config.BidderInfo{Disabled: true, AliasOf: "something"} 336 ) 337 338 bidders := config.BidderInfos{"a": enabledCore, "b": enabledAlias, "c": disabledCore, "d": disabledAlias} 339 aliases := map[string]string{"x": "a", "y": "c"} 340 341 testCases := []struct { 342 name string 343 givenURL string 344 expectedStatus int 345 expectedBody string 346 expectedHeaders http.Header 347 }{ 348 { 349 name: "simple", 350 givenURL: "/info/bidders", 351 expectedStatus: http.StatusOK, 352 expectedBody: `["a","b","c","d","x","y"]`, 353 expectedHeaders: http.Header{"Content-Type": []string{"application/json"}}, 354 }, 355 { 356 name: "enabledonly-false", 357 givenURL: "/info/bidders?enabledonly=false", 358 expectedStatus: http.StatusOK, 359 expectedBody: `["a","b","c","d","x","y"]`, 360 expectedHeaders: http.Header{"Content-Type": []string{"application/json"}}, 361 }, 362 { 363 name: "enabledonly-false-caseinsensitive", 364 givenURL: "/info/bidders?enabledonly=fAlSe", 365 expectedStatus: http.StatusOK, 366 expectedBody: `["a","b","c","d","x","y"]`, 367 expectedHeaders: http.Header{"Content-Type": []string{"application/json"}}, 368 }, 369 { 370 name: "enabledonly-true", 371 givenURL: "/info/bidders?enabledonly=true", 372 expectedStatus: http.StatusOK, 373 expectedBody: `["a","b","x"]`, 374 expectedHeaders: http.Header{"Content-Type": []string{"application/json"}}, 375 }, 376 { 377 name: "enabledonly-true-caseinsensitive", 378 givenURL: "/info/bidders?enabledonly=TrUe", 379 expectedStatus: http.StatusOK, 380 expectedBody: `["a","b","x"]`, 381 expectedHeaders: http.Header{"Content-Type": []string{"application/json"}}, 382 }, 383 { 384 name: "enabledonly-invalid", 385 givenURL: "/info/bidders?enabledonly=foo", 386 expectedStatus: http.StatusBadRequest, 387 expectedBody: `Invalid value for 'enabledonly' query param, must be of boolean type`, 388 expectedHeaders: http.Header{}, 389 }, 390 { 391 name: "enabledonly-missing", 392 givenURL: "/info/bidders?enabledonly=", 393 expectedStatus: http.StatusBadRequest, 394 expectedBody: `Invalid value for 'enabledonly' query param, must be of boolean type`, 395 expectedHeaders: http.Header{}, 396 }, 397 { 398 name: "baseonly-false", 399 givenURL: "/info/bidders?baseadaptersonly=false", 400 expectedStatus: http.StatusOK, 401 expectedBody: `["a","b","c","d","x","y"]`, 402 expectedHeaders: http.Header{"Content-Type": []string{"application/json"}}, 403 }, 404 { 405 name: "baseonly-false-caseinsensitive", 406 givenURL: "/info/bidders?baseadaptersonly=fAlSe", 407 expectedStatus: http.StatusOK, 408 expectedBody: `["a","b","c","d","x","y"]`, 409 expectedHeaders: http.Header{"Content-Type": []string{"application/json"}}, 410 }, 411 { 412 name: "baseonly-true", 413 givenURL: "/info/bidders?baseadaptersonly=true", 414 expectedStatus: http.StatusOK, 415 expectedBody: `["a","c"]`, 416 expectedHeaders: http.Header{"Content-Type": []string{"application/json"}}, 417 }, 418 { 419 name: "baseonly-true-caseinsensitive", 420 givenURL: "/info/bidders?baseadaptersonly=TrUe", 421 expectedStatus: http.StatusOK, 422 expectedBody: `["a","c"]`, 423 expectedHeaders: http.Header{"Content-Type": []string{"application/json"}}, 424 }, 425 { 426 name: "baseonly-invalid", 427 givenURL: "/info/bidders?baseadaptersonly=foo", 428 expectedStatus: http.StatusBadRequest, 429 expectedBody: `Invalid value for 'baseadaptersonly' query param, must be of boolean type`, 430 expectedHeaders: http.Header{}, 431 }, 432 { 433 name: "baseonly-missing", 434 givenURL: "/info/bidders?baseadaptersonly=", 435 expectedStatus: http.StatusBadRequest, 436 expectedBody: `Invalid value for 'baseadaptersonly' query param, must be of boolean type`, 437 expectedHeaders: http.Header{}, 438 }, 439 { 440 name: "enabledonly-true-baseonly-false", 441 givenURL: "/info/bidders?enabledonly=true&baseadaptersonly=false", 442 expectedStatus: http.StatusOK, 443 expectedBody: `["a","b","x"]`, 444 expectedHeaders: http.Header{"Content-Type": []string{"application/json"}}, 445 }, 446 { 447 name: "enabledonly-false-baseonly-true", 448 givenURL: "/info/bidders?enabledonly=false&baseadaptersonly=true", 449 expectedStatus: http.StatusOK, 450 expectedBody: `["a","c"]`, 451 expectedHeaders: http.Header{"Content-Type": []string{"application/json"}}, 452 }, 453 { 454 name: "enabledonly-true-baseonly-true", 455 givenURL: "/info/bidders?enabledonly=true&baseadaptersonly=true", 456 expectedStatus: http.StatusOK, 457 expectedBody: `["a"]`, 458 expectedHeaders: http.Header{"Content-Type": []string{"application/json"}}, 459 }, 460 } 461 462 for _, test := range testCases { 463 t.Run(test.name, func(t *testing.T) { 464 handler := NewBiddersEndpoint(bidders, aliases) 465 466 request := httptest.NewRequest("GET", test.givenURL, nil) 467 468 responseRecorder := httptest.NewRecorder() 469 handler(responseRecorder, request, nil) 470 471 result := responseRecorder.Result() 472 assert.Equal(t, result.StatusCode, test.expectedStatus) 473 474 resultBody, _ := io.ReadAll(result.Body) 475 assert.Equal(t, []byte(test.expectedBody), resultBody) 476 477 resultHeaders := result.Header 478 assert.Equal(t, test.expectedHeaders, resultHeaders) 479 }) 480 } 481 }