github.com/prebid/prebid-server@v0.275.0/adapters/ucfunnel/ucfunnel_test.go (about) 1 package ucfunnel 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "testing" 7 8 "github.com/prebid/openrtb/v19/openrtb2" 9 "github.com/prebid/prebid-server/adapters" 10 "github.com/prebid/prebid-server/config" 11 "github.com/prebid/prebid-server/openrtb_ext" 12 ) 13 14 func TestMakeRequests(t *testing.T) { 15 16 imp := openrtb2.Imp{ 17 ID: "1234", 18 Banner: &openrtb2.Banner{}, 19 } 20 imp2 := openrtb2.Imp{ 21 ID: "1235", 22 Video: &openrtb2.Video{}, 23 } 24 25 imp3 := openrtb2.Imp{ 26 ID: "1236", 27 Audio: &openrtb2.Audio{}, 28 } 29 30 imp4 := openrtb2.Imp{ 31 ID: "1237", 32 Native: &openrtb2.Native{}, 33 } 34 imp5 := openrtb2.Imp{ 35 ID: "1237", 36 Native: &openrtb2.Native{}, 37 } 38 39 internalRequest01 := openrtb2.BidRequest{Imp: []openrtb2.Imp{}} 40 internalRequest02 := openrtb2.BidRequest{Imp: []openrtb2.Imp{imp, imp2, imp3, imp4, imp5}} 41 internalRequest03 := openrtb2.BidRequest{Imp: []openrtb2.Imp{imp, imp2, imp3, imp4, imp5}} 42 43 internalRequest03.Imp[0].Ext = []byte(`{"bidder": {"adunitid": "ad-488663D474E44841E8A293379892348","partnerid": "par-7E6D2DB9A8922AB07B44A444D2BA67"}}`) 44 internalRequest03.Imp[1].Ext = []byte(`{"bidder": {"adunitid": "ad-488663D474E44841E8A293379892348","partnerid": "par-7E6D2DB9A8922AB07B44A444D2BA67"}}`) 45 internalRequest03.Imp[2].Ext = []byte(`{"bidder": {"adunitid": "ad-488663D474E44841E8A293379892348","partnerid": "par-7E6D2DB9A8922AB07B44A444D2BA67"}}`) 46 internalRequest03.Imp[3].Ext = []byte(`{"bidder": {"adunitid": "ad-488663D474E44841E8A293379892348","partnerid": "par-7E6D2DB9A8922AB07B44A444D2BA67"}}`) 47 internalRequest03.Imp[4].Ext = []byte(`{"bidder": {"adunitid": "aa","partnerid": ""}}`) 48 49 bidder, buildErr := Builder(openrtb_ext.BidderUcfunnel, config.Adapter{ 50 Endpoint: "http://localhost/bid"}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"}) 51 52 if buildErr != nil { 53 t.Fatalf("Builder returned unexpected error %v", buildErr) 54 } 55 56 var testCases = []struct { 57 in []openrtb2.BidRequest 58 out1 [](int) 59 out2 [](bool) 60 }{ 61 { 62 in: []openrtb2.BidRequest{internalRequest01, internalRequest02, internalRequest03}, 63 out1: [](int){1, 1, 0}, 64 out2: [](bool){false, false, true}, 65 }, 66 } 67 68 for idx := range testCases { 69 for i := range testCases[idx].in { 70 RequestData, err := bidder.MakeRequests(&testCases[idx].in[i], nil) 71 if ((RequestData == nil) == testCases[idx].out2[i]) && (len(err) == testCases[idx].out1[i]) { 72 t.Errorf("actual = %v expected = %v", len(err), testCases[idx].out1[i]) 73 } 74 } 75 } 76 } 77 78 func TestMakeBids(t *testing.T) { 79 imp := openrtb2.Imp{ 80 ID: "1234", 81 Banner: &openrtb2.Banner{}, 82 } 83 imp2 := openrtb2.Imp{ 84 ID: "1235", 85 Video: &openrtb2.Video{}, 86 } 87 88 imp3 := openrtb2.Imp{ 89 ID: "1236", 90 Audio: &openrtb2.Audio{}, 91 } 92 93 imp4 := openrtb2.Imp{ 94 ID: "1237", 95 Native: &openrtb2.Native{}, 96 } 97 imp5 := openrtb2.Imp{ 98 ID: "1237", 99 Native: &openrtb2.Native{}, 100 } 101 102 internalRequest03 := openrtb2.BidRequest{Imp: []openrtb2.Imp{imp, imp2, imp3, imp4, imp5}} 103 internalRequest04 := openrtb2.BidRequest{Imp: []openrtb2.Imp{imp}} 104 105 internalRequest03.Imp[0].Ext = []byte(`{"bidder": {"adunitid": "ad-488663D474E44841E8A293379892348","partnerid": "par-7E6D2DB9A8922AB07B44A444D2BA67"}}`) 106 internalRequest03.Imp[1].Ext = []byte(`{"bidder": {"adunitid": "ad-488663D474E44841E8A293379892348","partnerid": "par-7E6D2DB9A8922AB07B44A444D2BA67"}}`) 107 internalRequest03.Imp[2].Ext = []byte(`{"bidder": {"adunitid": "ad-488663D474E44841E8A293379892348","partnerid": "par-7E6D2DB9A8922AB07B44A444D2BA67"}}`) 108 internalRequest03.Imp[3].Ext = []byte(`{"bidder": {"adunitid": "ad-488663D474E44841E8A293379892348","partnerid": "par-7E6D2DB9A8922AB07B44A444D2BA67"}}`) 109 internalRequest03.Imp[4].Ext = []byte(`{"bidder": {"adunitid": "aa","partnerid": ""}}`) 110 internalRequest04.Imp[0].Ext = []byte(`{"bidder": {"adunitid": "0"}}`) 111 112 mockResponse200 := adapters.ResponseData{StatusCode: 200, Body: json.RawMessage(`{"seatbid": [{"bid": [{"impid": "1234"}]},{"bid": [{"impid": "1235"}]},{"bid": [{"impid": "1236"}]},{"bid": [{"impid": "1237"}]}]}`)} 113 mockResponse203 := adapters.ResponseData{StatusCode: 203, Body: json.RawMessage(`{"seatbid":[{"bid":[{"impid":"1234"}]},{"bid":[{"impid":"1235"}]}]}`)} 114 mockResponse204 := adapters.ResponseData{StatusCode: 204, Body: json.RawMessage(`{"seatbid":[{"bid":[{"impid":"1234"}]},{"bid":[{"impid":"1235"}]}]}`)} 115 mockResponse400 := adapters.ResponseData{StatusCode: 400, Body: json.RawMessage(`{"seatbid":[{"bid":[{"impid":"1234"}]},{"bid":[{"impid":"1235"}]}]}`)} 116 mockResponseError := adapters.ResponseData{StatusCode: 200, Body: json.RawMessage(`{"seatbid":[{"bid":[{"im236"}],{"bid":[{"impid":"1237}]}`)} 117 118 RequestData01 := adapters.RequestData{Method: "POST", Body: []byte(`{"imp":[{"id":"1234","banner":{}},{"id":"1235","video":{}},{"id":"1236","audio":{}},{"id":"1237","native":{}}]}`)} 119 RequestData02 := adapters.RequestData{Method: "POST", Body: []byte(`{"imp":[{"id":"1234","banne"1235","video":{}},{"id":"1236","audio":{}},{"id":"1237","native":{}}]}`)} 120 121 bidder, buildErr := Builder(openrtb_ext.BidderUcfunnel, config.Adapter{ 122 Endpoint: "http://localhost/bid"}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"}) 123 124 if buildErr != nil { 125 t.Fatalf("Builder returned unexpected error %v", buildErr) 126 } 127 128 var testCases = []struct { 129 in1 []openrtb2.BidRequest 130 in2 []adapters.RequestData 131 in3 []adapters.ResponseData 132 out1 [](bool) 133 out2 [](bool) 134 }{ 135 { 136 in1: []openrtb2.BidRequest{internalRequest03, internalRequest03, internalRequest03, internalRequest03, internalRequest03, internalRequest04}, 137 in2: []adapters.RequestData{RequestData01, RequestData01, RequestData01, RequestData01, RequestData01, RequestData02}, 138 in3: []adapters.ResponseData{mockResponse200, mockResponse203, mockResponse204, mockResponse400, mockResponseError, mockResponse200}, 139 out1: [](bool){true, false, false, false, false, false}, 140 out2: [](bool){false, true, false, true, true, true}, 141 }, 142 } 143 144 for idx := range testCases { 145 for i := range testCases[idx].in1 { 146 BidderResponse, err := bidder.MakeBids(&testCases[idx].in1[i], &testCases[idx].in2[i], &testCases[idx].in3[i]) 147 148 if (BidderResponse == nil) == testCases[idx].out1[i] { 149 fmt.Println(i) 150 fmt.Println("BidderResponse") 151 t.Errorf("actual = %t expected == %v", (BidderResponse == nil), testCases[idx].out1[i]) 152 } 153 154 if (err == nil) == testCases[idx].out2[i] { 155 fmt.Println(i) 156 fmt.Println("error") 157 t.Errorf("actual = %t expected == %v", err, testCases[idx].out2[i]) 158 } 159 } 160 } 161 }