-
Notifications
You must be signed in to change notification settings - Fork 710
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
19 changed files
with
1,240 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package theadx | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/prebid/prebid-server/v2/openrtb_ext" | ||
) | ||
|
||
// This file actually intends to test static/bidder-params/theadx.json | ||
// | ||
// These also validate the format of the external API: request.imp[i].ext.prebid.bidder.theadx | ||
|
||
// TestValidParams makes sure that the theadx schema accepts all imp.ext fields which we intend to support. | ||
func TestValidParams(t *testing.T) { | ||
validator, err := openrtb_ext.NewBidderParamsValidator("../../static/bidder-params") | ||
if err != nil { | ||
t.Fatalf("Failed to fetch the json-schemas. %v", err) | ||
} | ||
|
||
for _, validParam := range validParams { | ||
if err := validator.Validate(openrtb_ext.BidderTheadx, json.RawMessage(validParam)); err != nil { | ||
t.Errorf("Schema rejected theadx params: %s", validParam) | ||
} | ||
} | ||
} | ||
|
||
// TestInvalidParams makes sure that the theadx schema rejects all the imp.ext fields we don't support. | ||
func TestInvalidParams(t *testing.T) { | ||
validator, err := openrtb_ext.NewBidderParamsValidator("../../static/bidder-params") | ||
if err != nil { | ||
t.Fatalf("Failed to fetch the json-schemas. %v", err) | ||
} | ||
|
||
for _, invalidParam := range invalidParams { | ||
if err := validator.Validate(openrtb_ext.BidderTheadx, json.RawMessage(invalidParam)); err == nil { | ||
t.Errorf("Schema allowed unexpected params: %s", invalidParam) | ||
} | ||
} | ||
} | ||
|
||
var validParams = []string{ | ||
`{"tagid":321}`, | ||
`{"tagid":321,"wid":"456"}`, | ||
`{"tagid":321,"pid":"12345"}`, | ||
`{"tagid":321,"pname":"plc_mobile_300x250"}`, | ||
`{"tagid":321,"inv":321,"mname":"pcl1"}`, | ||
`{"tagid":"123","wid":"456","pid":"12345","pname":"plc_mobile_300x250"}`, | ||
} | ||
|
||
var invalidParams = []string{ | ||
``, | ||
`null`, | ||
`true`, | ||
`5`, | ||
`4.2`, | ||
`[]`, | ||
`{}`, | ||
`{"notmid":"123"}`, | ||
`{"mid":"placementID"}`, | ||
`{"inv":321,"mname":12345}`, | ||
`{"inv":321}`, | ||
`{"inv":"321"}`, | ||
`{"mname":"12345"}`, | ||
`{"mid":"123","priceType":"GROSS"}`, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
package theadx | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/prebid/openrtb/v20/openrtb2" | ||
"github.com/prebid/prebid-server/v2/adapters" | ||
"github.com/prebid/prebid-server/v2/config" | ||
"github.com/prebid/prebid-server/v2/errortypes" | ||
"github.com/prebid/prebid-server/v2/openrtb_ext" | ||
) | ||
|
||
type adapter struct { | ||
endpoint string | ||
} | ||
|
||
// Builder builds a new instance of the theadx adapter for the given bidder with the given config. | ||
func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) { | ||
bidder := &adapter{ | ||
endpoint: config.Endpoint, | ||
} | ||
return bidder, nil | ||
} | ||
func getHeaders(request *openrtb2.BidRequest) http.Header { | ||
headers := http.Header{} | ||
headers.Add("Content-Type", "application/json;charset=utf-8") | ||
headers.Add("Accept", "application/json") | ||
headers.Add("X-Openrtb-Version", "2.5") | ||
headers.Add("X-TEST", "1") | ||
|
||
if request.Device != nil { | ||
if len(request.Device.UA) > 0 { | ||
headers.Add("X-Device-User-Agent", request.Device.UA) | ||
} | ||
|
||
if len(request.Device.IPv6) > 0 { | ||
headers.Add("X-Real-IP", request.Device.IPv6) | ||
} | ||
|
||
if len(request.Device.IP) > 0 { | ||
headers.Add("X-Real-IP", request.Device.IP) | ||
} | ||
} | ||
|
||
return headers | ||
} | ||
func (a *adapter) MakeRequests(request *openrtb2.BidRequest, requestInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) { | ||
var errors []error | ||
var validImps = make([]openrtb2.Imp, 0, len(request.Imp)) | ||
|
||
for _, imp := range request.Imp { | ||
var bidderExt adapters.ExtImpBidder | ||
if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil { | ||
errors = append(errors, &errortypes.BadInput{ | ||
Message: err.Error(), | ||
}) | ||
continue | ||
} | ||
|
||
var theadxImpExt openrtb_ext.ExtImpTheadx | ||
if err := json.Unmarshal(bidderExt.Bidder, &theadxImpExt); err != nil { | ||
errors = append(errors, &errortypes.BadInput{ | ||
Message: err.Error(), | ||
}) | ||
continue | ||
} | ||
|
||
imp.TagID = theadxImpExt.TagID.String() | ||
validImps = append(validImps, imp) | ||
} | ||
|
||
request.Imp = validImps | ||
|
||
requestJSON, err := json.Marshal(request) | ||
if err != nil { | ||
errors = append(errors, err) | ||
return nil, errors | ||
} | ||
|
||
requestData := &adapters.RequestData{ | ||
Method: "POST", | ||
Uri: a.endpoint, | ||
Body: requestJSON, | ||
Headers: getHeaders(request), | ||
} | ||
|
||
return []*adapters.RequestData{requestData}, errors | ||
} | ||
|
||
func (a *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) { | ||
if responseData.StatusCode == http.StatusNoContent { | ||
return nil, nil | ||
} | ||
|
||
if responseData.StatusCode == http.StatusBadRequest { | ||
err := &errortypes.BadInput{ | ||
Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", responseData.StatusCode), | ||
} | ||
return nil, []error{err} | ||
} | ||
|
||
if responseData.StatusCode != http.StatusOK { | ||
err := &errortypes.BadServerResponse{ | ||
Message: fmt.Sprintf("Unexpected status code: %d.", responseData.StatusCode), | ||
} | ||
return nil, []error{err} | ||
} | ||
|
||
var response openrtb2.BidResponse | ||
if err := json.Unmarshal(responseData.Body, &response); err != nil { | ||
return nil, []error{err} | ||
} | ||
|
||
bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp)) | ||
bidResponse.Currency = response.Cur | ||
var errors []error | ||
for _, seatBid := range response.SeatBid { | ||
for i, bid := range seatBid.Bid { | ||
bidType, err := getMediaTypeForBid(bid) | ||
if err != nil { | ||
errors = append(errors, err) | ||
continue | ||
} | ||
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{ | ||
Bid: &seatBid.Bid[i], | ||
BidType: bidType, | ||
}) | ||
} | ||
} | ||
|
||
return bidResponse, errors | ||
} | ||
|
||
func getMediaTypeForBid(bid openrtb2.Bid) (openrtb_ext.BidType, error) { | ||
if bid.Ext != nil { | ||
var bidExt openrtb_ext.ExtBid | ||
err := json.Unmarshal(bid.Ext, &bidExt) | ||
if err == nil && bidExt.Prebid != nil { | ||
return openrtb_ext.ParseBidType(string(bidExt.Prebid.Type)) | ||
} | ||
} | ||
|
||
return "", &errortypes.BadServerResponse{ | ||
Message: fmt.Sprintf("Failed to parse impression \"%s\" mediatype", bid.ImpID), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package theadx | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/prebid/prebid-server/v2/adapters/adapterstest" | ||
"github.com/prebid/prebid-server/v2/config" | ||
"github.com/prebid/prebid-server/v2/openrtb_ext" | ||
) | ||
|
||
func TestJsonSamples(t *testing.T) { | ||
bidder, buildErr := Builder(openrtb_ext.BidderTheadx, config.Adapter{ | ||
Endpoint: "https://ssp.theadx.com/request"}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"}) | ||
|
||
if buildErr != nil { | ||
t.Fatalf("Builder returned unexpected error %v", buildErr) | ||
} | ||
|
||
adapterstest.RunJSONBidderTest(t, "theadxtest", bidder) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
{ | ||
"mockBidRequest": { | ||
"id": "test-request-id", | ||
"imp": [{ | ||
"id": "test-imp-id", | ||
"ext": { | ||
"bidder": { | ||
"tagid": "123", | ||
"pname": "placement" | ||
} | ||
}, | ||
"banner": { | ||
"format": [{ | ||
"w": 300, | ||
"h": 250 | ||
}] | ||
} | ||
}, { | ||
"id": "test-imp-id2", | ||
"ext": { | ||
"bidder": { | ||
"tagid": "123", | ||
"wid": 456, | ||
"pname": "placement1" | ||
} | ||
}, | ||
"banner": { | ||
"format": [{ | ||
"w": 300, | ||
"h": 300 | ||
}] | ||
} | ||
}], | ||
"site": { | ||
"publisher": { | ||
"id": "1" | ||
}, | ||
"page": "some-page-url" | ||
}, | ||
"device": { | ||
"w": 1920, | ||
"h": 800 | ||
} | ||
}, | ||
"httpCalls": [{ | ||
"expectedRequest": { | ||
"uri": "https://ssp.theadx.com/request", | ||
"body": { | ||
"id": "test-request-id", | ||
"imp": [{ | ||
"id": "test-imp-id", | ||
"tagid": "123", | ||
"ext": { | ||
"bidder": { | ||
"tagid": "123", | ||
"pname": "placement" | ||
} | ||
}, | ||
"banner": { | ||
"format": [{ | ||
"w": 300, | ||
"h": 250 | ||
}] | ||
} | ||
}, { | ||
"id": "test-imp-id2", | ||
"tagid": "123", | ||
"ext": { | ||
"bidder": { | ||
"tagid": "123", | ||
"wid": 456, | ||
"pname": "placement1" | ||
} | ||
}, | ||
"banner": { | ||
"format": [{ | ||
"w": 300, | ||
"h": 300 | ||
}] | ||
} | ||
}], | ||
"site": { | ||
"publisher": { | ||
"id": "1" | ||
}, | ||
"page": "some-page-url" | ||
}, | ||
"device": { | ||
"w": 1920, | ||
"h": 800 | ||
} | ||
} | ||
}, | ||
"mockResponse": { | ||
"status": 204 | ||
} | ||
}], | ||
"expectedMakeRequestsErrors": [], | ||
"expectedBidResponses": [] | ||
} |
Oops, something went wrong.