github.com/prebid/prebid-server/v2@v2.18.0/adapters/kidoz/kidoz_test.go (about)

     1  package kidoz
     2  
     3  import (
     4  	"math"
     5  	"net/http"
     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/adapters/adapterstest"
    11  	"github.com/prebid/prebid-server/v2/config"
    12  	"github.com/prebid/prebid-server/v2/openrtb_ext"
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestJsonSamples(t *testing.T) {
    17  	bidder, buildErr := Builder(openrtb_ext.BidderKidoz, config.Adapter{
    18  		Endpoint: "http://example.com/prebid"}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"})
    19  
    20  	if buildErr != nil {
    21  		t.Fatalf("Builder returned unexpected error %v", buildErr)
    22  	}
    23  
    24  	adapterstest.RunJSONBidderTest(t, "kidoztest", bidder)
    25  }
    26  
    27  func makeBidRequest() *openrtb2.BidRequest {
    28  	request := &openrtb2.BidRequest{
    29  		ID: "test-req-id-0",
    30  		Imp: []openrtb2.Imp{
    31  			{
    32  				ID: "test-imp-id-0",
    33  				Banner: &openrtb2.Banner{
    34  					Format: []openrtb2.Format{
    35  						{
    36  							W: 320,
    37  							H: 50,
    38  						},
    39  					},
    40  				},
    41  				Ext: []byte(`{"bidder":{"access_token":"token-0","publisher_id":"pub-0"}}`),
    42  			},
    43  		},
    44  	}
    45  	return request
    46  }
    47  
    48  func TestMakeRequests(t *testing.T) {
    49  	bidder, buildErr := Builder(openrtb_ext.BidderKidoz, config.Adapter{
    50  		Endpoint: "http://example.com/prebid"}, 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  	t.Run("Handles Request marshal failure", func(t *testing.T) {
    57  		request := makeBidRequest()
    58  		request.Imp[0].BidFloor = math.Inf(1) // cant be marshalled
    59  		extra := &adapters.ExtraRequestInfo{}
    60  		reqs, errs := bidder.MakeRequests(request, extra)
    61  		// cant assert message its different on different versions of go
    62  		assert.Equal(t, 1, len(errs))
    63  		assert.Contains(t, errs[0].Error(), "json")
    64  		assert.Equal(t, 0, len(reqs))
    65  	})
    66  }
    67  
    68  func TestMakeBids(t *testing.T) {
    69  	bidder, buildErr := Builder(openrtb_ext.BidderKidoz, config.Adapter{
    70  		Endpoint: "http://example.com/prebid"}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"})
    71  
    72  	if buildErr != nil {
    73  		t.Fatalf("Builder returned unexpected error %v", buildErr)
    74  	}
    75  
    76  	t.Run("Handles response marshal failure", func(t *testing.T) {
    77  		request := makeBidRequest()
    78  		requestData := &adapters.RequestData{}
    79  		responseData := &adapters.ResponseData{
    80  			StatusCode: http.StatusOK,
    81  		}
    82  
    83  		resp, errs := bidder.MakeBids(request, requestData, responseData)
    84  		// cant assert message its different on different versions of go
    85  		assert.Equal(t, 1, len(errs))
    86  		assert.Contains(t, errs[0].Error(), "JSON")
    87  		assert.Nil(t, resp)
    88  	})
    89  }
    90  
    91  func TestGetMediaTypeForImp(t *testing.T) {
    92  	imps := []openrtb2.Imp{
    93  		{
    94  			ID:     "1",
    95  			Banner: &openrtb2.Banner{},
    96  		},
    97  		{
    98  			ID:    "2",
    99  			Video: &openrtb2.Video{},
   100  		},
   101  		{
   102  			ID:     "3",
   103  			Native: &openrtb2.Native{},
   104  		},
   105  		{
   106  			ID:    "4",
   107  			Audio: &openrtb2.Audio{},
   108  		},
   109  	}
   110  
   111  	t.Run("Bid not found is type empty string", func(t *testing.T) {
   112  		actual := GetMediaTypeForImp("ARGLE_BARGLE", imps)
   113  		assert.Equal(t, UndefinedMediaType, actual)
   114  	})
   115  	t.Run("Can find banner type", func(t *testing.T) {
   116  		actual := GetMediaTypeForImp("1", imps)
   117  		assert.Equal(t, openrtb_ext.BidTypeBanner, actual)
   118  	})
   119  	t.Run("Can find video type", func(t *testing.T) {
   120  		actual := GetMediaTypeForImp("2", imps)
   121  		assert.Equal(t, openrtb_ext.BidTypeVideo, actual)
   122  	})
   123  	t.Run("Can find native type", func(t *testing.T) {
   124  		actual := GetMediaTypeForImp("3", imps)
   125  		assert.Equal(t, openrtb_ext.BidTypeNative, actual)
   126  	})
   127  	t.Run("Can find audio type", func(t *testing.T) {
   128  		actual := GetMediaTypeForImp("4", imps)
   129  		assert.Equal(t, openrtb_ext.BidTypeAudio, actual)
   130  	})
   131  }