github.com/prebid/prebid-server/v2@v2.18.0/adapters/adot/adot_test.go (about)

     1  package adot
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/prebid/openrtb/v20/openrtb2"
     8  	"github.com/prebid/prebid-server/v2/adapters"
     9  	"github.com/prebid/prebid-server/v2/adapters/adapterstest"
    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  const testsBidderEndpoint = "https://dsp.adotmob.com/headerbidding{PUBLISHER_PATH}/bidrequest"
    16  
    17  func TestJsonSamples(t *testing.T) {
    18  	bidder, buildErr := Builder(openrtb_ext.BidderAdot, config.Adapter{
    19  		Endpoint: testsBidderEndpoint}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"})
    20  
    21  	if buildErr != nil {
    22  		t.Fatalf("Builder returned unexpected error %v", buildErr)
    23  	}
    24  
    25  	adapterstest.RunJSONBidderTest(t, "adottest", bidder)
    26  }
    27  
    28  func TestMediaTypeError(t *testing.T) {
    29  	_, err := getMediaTypeForBid(nil)
    30  
    31  	assert.Error(t, err)
    32  
    33  	byteInvalid, _ := json.Marshal(&adotBidExt{Adot: bidExt{"invalid"}})
    34  	_, err = getMediaTypeForBid(&openrtb2.Bid{Ext: json.RawMessage(byteInvalid)})
    35  
    36  	assert.Error(t, err)
    37  }
    38  
    39  func TestBidResponseNoContent(t *testing.T) {
    40  	bidder, buildErr := Builder(openrtb_ext.BidderAdot, config.Adapter{
    41  		Endpoint: "https://dsp.adotmob.com/headerbidding{PUBLISHER_PATH}/bidrequest"}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"})
    42  
    43  	if buildErr != nil {
    44  		t.Fatalf("Builder returned unexpected error %v", buildErr)
    45  	}
    46  
    47  	bidResponse, err := bidder.MakeBids(nil, nil, &adapters.ResponseData{StatusCode: 204})
    48  	if bidResponse != nil {
    49  		t.Fatalf("the bid response should be nil since the bidder status is No Content")
    50  	} else if err != nil {
    51  		t.Fatalf("the error should be nil since the bidder status is 204 : No Content")
    52  	}
    53  }
    54  
    55  func TestMediaTypeForBid(t *testing.T) {
    56  	byteBanner, _ := json.Marshal(&adotBidExt{Adot: bidExt{"banner"}})
    57  	byteVideo, _ := json.Marshal(&adotBidExt{Adot: bidExt{"video"}})
    58  	byteNative, _ := json.Marshal(&adotBidExt{Adot: bidExt{"native"}})
    59  
    60  	bidTypeBanner, _ := getMediaTypeForBid(&openrtb2.Bid{Ext: json.RawMessage(byteBanner)})
    61  	if bidTypeBanner != openrtb_ext.BidTypeBanner {
    62  		t.Errorf("the type is not the valid one. actual: %v, expected: %v", bidTypeBanner, openrtb_ext.BidTypeBanner)
    63  	}
    64  
    65  	bidTypeVideo, _ := getMediaTypeForBid(&openrtb2.Bid{Ext: json.RawMessage(byteVideo)})
    66  	if bidTypeVideo != openrtb_ext.BidTypeVideo {
    67  		t.Errorf("the type is not the valid one. actual: %v, expected: %v", bidTypeVideo, openrtb_ext.BidTypeVideo)
    68  	}
    69  
    70  	bidTypeNative, _ := getMediaTypeForBid(&openrtb2.Bid{Ext: json.RawMessage(byteNative)})
    71  	if bidTypeNative != openrtb_ext.BidTypeNative {
    72  		t.Errorf("the type is not the valid one. actual: %v, expected: %v", bidTypeNative, openrtb_ext.BidTypeVideo)
    73  	}
    74  }
    75  
    76  func TestResolveMacros(t *testing.T) {
    77  	bid := &openrtb2.Bid{AdM: "adm:imp_${AUCTION_PRICE} amd:creativeview_${AUCTION_PRICE}", NURL: "nurl_${AUCTION_PRICE}", Price: 123.45}
    78  	resolveMacros(bid)
    79  	assert.Equal(t, "adm:imp_123.45 amd:creativeview_123.45", bid.AdM)
    80  	assert.Equal(t, "nurl_123.45", bid.NURL)
    81  }
    82  
    83  func TestGetImpAdotExt(t *testing.T) {
    84  	ext := &openrtb2.Imp{Ext: json.RawMessage(`{"bidder":{"publisherPath": "/hubvisor"}}`)}
    85  	adotExt := getImpAdotExt(ext)
    86  	assert.Equal(t, adotExt.PublisherPath, "/hubvisor")
    87  
    88  	emptyBidderExt := &openrtb2.Imp{Ext: json.RawMessage(`{"bidder":{}}`)}
    89  	emptyAdotBidderExt := getImpAdotExt(emptyBidderExt)
    90  	assert.NotNil(t, emptyAdotBidderExt)
    91  	assert.Equal(t, emptyAdotBidderExt.PublisherPath, "")
    92  
    93  	emptyExt := &openrtb2.Imp{Ext: json.RawMessage(`{}`)}
    94  	emptyAdotExt := getImpAdotExt(emptyExt)
    95  	assert.Nil(t, emptyAdotExt)
    96  }