Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions adapters/trustx/params_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package trustx

import (
"encoding/json"
"testing"

"github.com/prebid/prebid-server/v3/openrtb_ext"
"github.com/stretchr/testify/require"
)

// This file actually intends to test static/bidder-params/trustx.json
//
// These also validate the format of the external API: request.imp[i].ext.prebid.bidder.trustx

// TestValidParams makes sure that the trustx schema accepts all imp.ext fields which we intend to support.
func TestValidParams(t *testing.T) {
validator, err := openrtb_ext.NewBidderParamsValidator("../../static/bidder-params")
require.NoError(t, err, "Failed to fetch the json-schemas")

for _, validParam := range validParams {
err := validator.Validate(openrtb_ext.BidderTrustX, json.RawMessage(validParam))
require.NoError(t, err, "Schema rejected trustx params: %s", validParam)
}
}

// TestInvalidParams makes sure that the trustx schema rejects all the imp.ext fields we don't support.
func TestInvalidParams(t *testing.T) {
validator, err := openrtb_ext.NewBidderParamsValidator("../../static/bidder-params")
require.NoError(t, err, "Failed to fetch the json-schemas")

for _, invalidParam := range invalidParams {
err := validator.Validate(openrtb_ext.BidderTrustX, json.RawMessage(invalidParam))
require.Error(t, err, "Schema allowed unexpected params: %s", invalidParam)
}
}

var validParams = []string{
`{}`,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: is a default Uid being 0 acceptable here? Is there a reason why we are not making Uid required?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, 0 is acceptable, as publishers can also pass the value in other fields (e.g. imp.tagid)

`{"uid": 1234}`,
`{"uid": 1234, "keywords":{"site": {}, "user": {}}}`,
}
var invalidParams = []string{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add other test cases where we also check data types of Uid and Keywords?
Ex: passing a "string" Uid and similarly for Keywords?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added 2 test cases.

``,
`null`,
`true`,
`5`,
`4.2`,
`[]`,
}
209 changes: 209 additions & 0 deletions adapters/trustx/trustx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
package trustx

import (
"encoding/json"
"fmt"
"net/http"

"github.com/prebid/openrtb/v20/openrtb2"
"github.com/prebid/prebid-server/v3/adapters"
"github.com/prebid/prebid-server/v3/config"
"github.com/prebid/prebid-server/v3/errortypes"
"github.com/prebid/prebid-server/v3/openrtb_ext"
"github.com/prebid/prebid-server/v3/util/jsonutil"
)

type impExt struct {
Prebid *openrtb_ext.ExtImpPrebid `json:"prebid,omitempty"`
Bidder json.RawMessage `json:"bidder"`
Data *impExtData `json:"data,omitempty"`
Gpid string `json:"gpid,omitempty"`
}

type impExtData struct {
PbAdslot string `json:"pbadslot,omitempty"`
AdServer *impExtDataAdServer `json:"adserver,omitempty"`
}

type impExtDataAdServer struct {
Name string `json:"name,omitempty"`
AdSlot string `json:"adslot,omitempty"`
}

type bidExt struct {
Bidder bidExtBidder `json:"bidder,omitempty"`
}

type bidExtBidder struct {
TrustX bidExtBidderTrustX `json:"trustx,omitempty"`
}

type bidExtBidderTrustX struct {
NetworkName string `json:"networkName,omitempty"`
}

type adapter struct {
endpoint string
}

// Builder builds a new instance of the TRUSTX 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 (a *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
var errs []error
for i := range request.Imp {
setImpExtData(&request.Imp[i])
}

body, err := jsonutil.Marshal(request)
if err != nil {
errs = append(errs, err)
return nil, errs
}

reqs := make([]*adapters.RequestData, 0, 1)
requestData := &adapters.RequestData{
Method: http.MethodPost,
Uri: a.endpoint,
Body: body,
Headers: getHeaders(request),
ImpIDs: openrtb_ext.GetImpIDs(request.Imp),
}
reqs = append(reqs, requestData)

return reqs, errs
}

func (a *adapter) MakeBids(internalRequest *openrtb2.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) {
if adapters.IsResponseStatusCodeNoContent(response) {
return nil, nil
}

if err := adapters.CheckResponseStatusCodeForErrors(response); err != nil {
return nil, []error{err}
}

var resp openrtb2.BidResponse
if err := jsonutil.Unmarshal(response.Body, &resp); err != nil {
return nil, []error{&errortypes.BadServerResponse{
Message: "Bad Server Response",
}}
}

var bidErrors []error
bidderResponse := adapters.NewBidderResponseWithBidsCapacity(1)
for i := range resp.SeatBid {
seatBid := &resp.SeatBid[i]
for j := range seatBid.Bid {
bid := &seatBid.Bid[j]
typedBid, err := getTypedBid(bid)
if err != nil {
bidErrors = append(bidErrors, err)
continue
}
bidderResponse.Bids = append(bidderResponse.Bids, typedBid)
}
}

return bidderResponse, bidErrors
}

func setImpExtData(imp *openrtb2.Imp) {
var ext impExt
if err := jsonutil.Unmarshal(imp.Ext, &ext); err != nil {
return

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want to log these errors?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, as the data read from the imp ext (adslot) is optional to us, and the request is valid without it.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense but i'd recommend adding a test case for this

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a test case for this!

}
if ext.Data != nil && ext.Data.AdServer != nil && ext.Data.AdServer.AdSlot != "" {
ext.Gpid = ext.Data.AdServer.AdSlot
extJSON, err := jsonutil.Marshal(ext)
if err == nil {
imp.Ext = extJSON
}
}
}

func getHeaders(request *openrtb2.BidRequest) http.Header {
headers := http.Header{}
headers.Set("Content-Type", "application/json;charset=utf-8")
headers.Set("Accept", "application/json")
headers.Set("X-Openrtb-Version", "2.6")

if request.Site != nil {
if request.Site.Ref != "" {
headers.Set("Referer", request.Site.Ref)
}
if request.Site.Domain != "" {
headers.Set("Origin", request.Site.Domain)
}
}

if request.Device != nil {
if len(request.Device.IP) > 0 {
headers.Set("X-Forwarded-For", request.Device.IP)
}

if len(request.Device.IPv6) > 0 {
headers.Set("X-Forwarded-For", request.Device.IPv6)
}

if len(request.Device.UA) > 0 {
headers.Set("User-Agent", request.Device.UA)
}
}

return headers
}

func getTypedBid(bid *openrtb2.Bid) (*adapters.TypedBid, error) {
var bidType openrtb_ext.BidType
switch bid.MType {
case openrtb2.MarkupBanner:
bidType = openrtb_ext.BidTypeBanner
case openrtb2.MarkupVideo:
bidType = openrtb_ext.BidTypeVideo
default:
return nil, &errortypes.BadServerResponse{
Message: fmt.Sprintf("Unsupported MType: %v", bid.MType),
}
}

var extBidPrebidVideo *openrtb_ext.ExtBidPrebidVideo
if bidType == openrtb_ext.BidTypeVideo {
extBidPrebidVideo = &openrtb_ext.ExtBidPrebidVideo{}
if len(bid.Cat) > 0 {
extBidPrebidVideo.PrimaryCategory = bid.Cat[0]
}
if bid.Dur > 0 {
extBidPrebidVideo.Duration = int(bid.Dur)
}
}
return &adapters.TypedBid{
Bid: bid,
BidType: bidType,
BidVideo: extBidPrebidVideo,
BidMeta: getBidMeta(bid.Ext),
}, nil
}

func getBidMeta(ext json.RawMessage) *openrtb_ext.ExtBidPrebidMeta {
if ext == nil {
return nil
}
var be bidExt

if err := jsonutil.Unmarshal(ext, &be); err != nil {
return nil
}
var bidMeta *openrtb_ext.ExtBidPrebidMeta
if be.Bidder.TrustX.NetworkName != "" {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can there be a case where TrustX is empty? Won't this logic break in such cases?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, there can be a case where we don't return the network name, but TrustX isn't a pointer so nothing will break, bid meta will just be nil.

bidMeta = &openrtb_ext.ExtBidPrebidMeta{
NetworkName: be.Bidder.TrustX.NetworkName,
}
}
return bidMeta
}
18 changes: 18 additions & 0 deletions adapters/trustx/trustx_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package trustx

import (
"testing"

"github.com/prebid/prebid-server/v3/adapters/adapterstest"
"github.com/prebid/prebid-server/v3/config"
"github.com/prebid/prebid-server/v3/openrtb_ext"
"github.com/stretchr/testify/require"
)

func TestJsonSamples(t *testing.T) {
bidder, buildErr := Builder(openrtb_ext.BidderTrustX, config.Adapter{
Endpoint: "https://test.localhost.com"}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"})

require.NoError(t, buildErr, "Builder returned unexpected error")
adapterstest.RunJSONBidderTest(t, "trustxtest", bidder)
}
Loading
Loading