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