Skip to content

Commit

Permalink
Added mType validation
Browse files Browse the repository at this point in the history
  • Loading branch information
vsolovei committed Mar 14, 2023
1 parent a3ddf5e commit 46018e3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
13 changes: 10 additions & 3 deletions exchange/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -1280,7 +1280,7 @@ func listBiddersWithRequests(bidderRequests []BidderRequest) []openrtb_ext.Bidde
return liveAdapters
}

func getMediaTypeForBid(bid openrtb2.Bid) (openrtb_ext.BidType, error) {
func getPrebidMediaTypeForBid(bid openrtb2.Bid) (openrtb_ext.BidType, error) {
if bid.Ext != nil {
var bidExt openrtb_ext.ExtBid
err := json.Unmarshal(bid.Ext, &bidExt)
Expand Down Expand Up @@ -1317,10 +1317,17 @@ func buildStoredAuctionResponse(storedAuctionResponses map[string]json.RawMessag
mType := seat.Bid[i].MType
var bidType openrtb_ext.BidType
if mType > 0 {
bidType = openrtb_ext.BidTypes()[mType-1]
if mType <= 4 {
//1 = Banner, 2 = Video, 3 = Audio, 4 = Native
bidType = openrtb_ext.BidTypes()[mType-1]
} else {
return nil, nil, nil, &errortypes.BadServerResponse{
Message: fmt.Sprintf("Failed to parse bid mType for impression \"%s\"", seat.Bid[i].ImpID),
}
}
} else {
var err error
bidType, err = getMediaTypeForBid(seat.Bid[i])
bidType, err = getPrebidMediaTypeForBid(seat.Bid[i])
if err != nil {
return nil, nil, nil, err
}
Expand Down
33 changes: 30 additions & 3 deletions exchange/exchange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3972,9 +3972,10 @@ func TestBuildStoredAuctionResponses(t *testing.T) {
}

testCases := []struct {
desc string
in testIn
expected testResults
desc string
in testIn
expected testResults
errorMessage string
}{
{
desc: "Single imp with single stored response bid",
Expand Down Expand Up @@ -4146,10 +4147,36 @@ func TestBuildStoredAuctionResponses(t *testing.T) {
liveAdapters: []openrtb_ext.BidderName{openrtb_ext.BidderName("appnexus")},
},
},
{
desc: "Single imp with single stored response bid with incorrect bid.mtype",
in: testIn{
StoredAuctionResponses: map[string]json.RawMessage{
"impression-id": json.RawMessage(`[{"bid": [{"id": "bid_id", "mtype": 10, "ext": {"prebid": {"type": "native"}}}],"seat": "appnexus"}]`),
},
},
expected: testResults{
adapterBids: map[openrtb_ext.BidderName]*entities.PbsOrtbSeatBid{
openrtb_ext.BidderName("appnexus"): {
Bids: []*entities.PbsOrtbBid{
{
Bid: &openrtb2.Bid{ID: "bid_id", ImpID: "impression-id", MType: 2, Ext: []byte(`{"prebid": {"type": "native"}}`)},
BidType: openrtb_ext.BidTypeVideo,
},
},
},
},
liveAdapters: []openrtb_ext.BidderName{openrtb_ext.BidderName("appnexus")},
},
errorMessage: "Failed to parse bid mType for impression \"impression-id\"",
},
}
for _, test := range testCases {

bids, fledge, adapters, err := buildStoredAuctionResponse(test.in.StoredAuctionResponses)
if len(test.errorMessage) > 0 {
assert.Equal(t, test.errorMessage, err.Error(), " incorrect expected error")
continue
}
assert.NoErrorf(t, err, "%s. HoldAuction error: %v \n", test.desc, err)

assert.ElementsMatch(t, test.expected.liveAdapters, adapters, "Incorrect adapter list")
Expand Down

0 comments on commit 46018e3

Please sign in to comment.