Skip to content

Commit

Permalink
Update code with feedback from PR prebid#1287
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Klinkert <[email protected]>
  • Loading branch information
aklinkert committed May 27, 2020
1 parent 6bbe213 commit 63c0252
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 111 deletions.
9 changes: 8 additions & 1 deletion adapters/yieldlab/usersync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@ import (
"text/template"

"github.com/stretchr/testify/assert"

"github.com/prebid/prebid-server/privacy"
"github.com/prebid/prebid-server/privacy/gdpr"
)

func TestYieldlabSyncer(t *testing.T) {
temp := template.Must(template.New("sync-template").Parse("https://ad.yieldlab.net/mr?t=2&pid=9140838&gdpr={{.GDPR}}&gdpr_consent={{.GDPRConsent}}&redirectUri=http%3A%2F%2Flocalhost%2F%2Fsetuid%3Fbidder%3Dyieldlab%26gdpr%3D{{.GDPR}}%26gdpr_consent%3D{{.GDPRConsent}}%26uid%3D%25%25YL_UID%25%25"))
syncer := NewYieldlabSyncer(temp)
syncInfo, err := syncer.GetUsersyncInfo("0", "")
syncInfo, err := syncer.GetUsersyncInfo(privacy.Policies{
GDPR: gdpr.Policy{
Signal: "0",
},
})
assert.NoError(t, err)
assert.Equal(t, "https://ad.yieldlab.net/mr?t=2&pid=9140838&gdpr=0&gdpr_consent=&redirectUri=http%3A%2F%2Flocalhost%2F%2Fsetuid%3Fbidder%3Dyieldlab%26gdpr%3D0%26gdpr_consent%3D%26uid%3D%25%25YL_UID%25%25", syncInfo.URL)
assert.Equal(t, "redirect", syncInfo.Type)
Expand Down
56 changes: 14 additions & 42 deletions adapters/yieldlab/yieldlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"golang.org/x/text/currency"

"github.com/prebid/prebid-server/adapters"
"github.com/prebid/prebid-server/errortypes"
"github.com/prebid/prebid-server/openrtb_ext"
)

Expand All @@ -33,20 +32,6 @@ func NewYieldlabBidder(endpoint string) *YieldlabAdapter {
}
}

// MakeRequests prepares bid requests to the Yieldlab API
func (a *YieldlabAdapter) MakeRequests(request *openrtb.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
var errs []error
var adapterRequests []*adapters.RequestData

adapterReq, errors := a.makeRequest(request)
if adapterReq != nil {
adapterRequests = append(adapterRequests, adapterReq)
}
errs = append(errs, errors...)

return adapterRequests, errors
}

// Builds endpoint url based on adapter-specific pub settings from imp.ext
func (a *YieldlabAdapter) makeEndpointURL(req *openrtb.BidRequest, params *openrtb_ext.ExtImpYieldlab) (string, error) {
uri, err := url.Parse(a.endpoint)
Expand Down Expand Up @@ -130,18 +115,12 @@ func (a *YieldlabAdapter) makeTargetingValues(params *openrtb_ext.ExtImpYieldlab
return values.Encode()
}

func (a *YieldlabAdapter) makeRequest(request *openrtb.BidRequest) (*adapters.RequestData, []error) {
params, err := a.parseRequest(request)
if err != nil {
return nil, []error{err}
func (a *YieldlabAdapter) MakeRequests(request *openrtb.BidRequest, _ *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
if len(request.Imp) == 0 {
return nil, []error{fmt.Errorf("invalid request %+v, no Impressions given", request)}
}

mergedParams, err := a.mergeParams(params)
if err != nil {
return nil, []error{err}
}

bidURL, err := a.makeEndpointURL(request, mergedParams)
bidURL, err := a.makeEndpointURL(request, a.mergeParams(a.parseRequest(request)))
if err != nil {
return nil, []error{err}
}
Expand All @@ -159,39 +138,35 @@ func (a *YieldlabAdapter) makeRequest(request *openrtb.BidRequest) (*adapters.Re
headers.Add("Cookie", "id="+request.User.BuyerUID)
}

return &adapters.RequestData{
return []*adapters.RequestData{{
Method: "GET",
Uri: bidURL,
Headers: headers,
}, nil
}}, nil
}

// parseRequest extracts the Yieldlab request information from the request
func (a *YieldlabAdapter) parseRequest(request *openrtb.BidRequest) ([]*openrtb_ext.ExtImpYieldlab, error) {
func (a *YieldlabAdapter) parseRequest(request *openrtb.BidRequest) []*openrtb_ext.ExtImpYieldlab {
params := make([]*openrtb_ext.ExtImpYieldlab, 0)

for i := 0; i < len(request.Imp); i++ {
bidderExt := new(adapters.ExtImpBidder)
if err := json.Unmarshal(request.Imp[i].Ext, bidderExt); err != nil {
return nil, &errortypes.BadInput{
Message: err.Error(),
}
continue
}

yieldlabExt := new(openrtb_ext.ExtImpYieldlab)
if err := json.Unmarshal(bidderExt.Bidder, yieldlabExt); err != nil {
return nil, &errortypes.BadInput{
Message: err.Error(),
}
continue
}

params = append(params, yieldlabExt)
}

return params, nil
return params
}

func (a *YieldlabAdapter) mergeParams(params []*openrtb_ext.ExtImpYieldlab) (*openrtb_ext.ExtImpYieldlab, error) {
func (a *YieldlabAdapter) mergeParams(params []*openrtb_ext.ExtImpYieldlab) *openrtb_ext.ExtImpYieldlab {
var adSlotIds []string
targeting := make(map[string]string)

Expand All @@ -205,7 +180,7 @@ func (a *YieldlabAdapter) mergeParams(params []*openrtb_ext.ExtImpYieldlab) (*op
return &openrtb_ext.ExtImpYieldlab{
AdslotID: strings.Join(adSlotIds, adSlotIdSeparator),
Targeting: targeting,
}, nil
}
}

// MakeBids make the bids for the bid response.
Expand All @@ -223,10 +198,7 @@ func (a *YieldlabAdapter) MakeBids(internalRequest *openrtb.BidRequest, external
}
}

requests, err := a.parseRequest(internalRequest)
if err != nil {
return nil, []error{err}
}
params := a.parseRequest(internalRequest)

bidderResponse := &adapters.BidderResponse{
Currency: currency.EUR.String(),
Expand All @@ -239,7 +211,7 @@ func (a *YieldlabAdapter) MakeBids(internalRequest *openrtb.BidRequest, external
return nil, []error{err}
}

req := a.findBidReq(bid.ID, requests)
req := a.findBidReq(bid.ID, params)
if req == nil {
return nil, []error{
fmt.Errorf("failed to find yieldlab request for adslotID %v. This is most likely a programming issue", bid.ID),
Expand Down
103 changes: 102 additions & 1 deletion adapters/yieldlab/yieldlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ package yieldlab
import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/prebid/prebid-server/adapters/adapterstest"
)

const testURL = "https://ad.yieldlab.net/testing/"

var testCacheBuster cacheBuster = func() string {
return "testing"
}
Expand All @@ -22,6 +26,103 @@ func newTestYieldlabBidder(endpoint string) *YieldlabAdapter {
}
}

func TestNewYieldlabBidder(t *testing.T) {
bid := NewYieldlabBidder(testURL)
assert.NotNil(t, bid)
assert.Equal(t, bid.endpoint, testURL)
assert.NotNil(t, bid.cacheBuster)
assert.NotNil(t, bid.getWeek)
}

func TestJsonSamples(t *testing.T) {
adapterstest.RunJSONBidderTest(t, "yieldlabtest", newTestYieldlabBidder("https://ad.yieldlab.net/testing/"))
adapterstest.RunJSONBidderTest(t, "yieldlabtest", newTestYieldlabBidder(testURL))
}

func Test_splitSize(t *testing.T) {
type args struct {
size string
}
tests := []struct {
name string
args args
want uint64
want1 uint64
wantErr bool
}{
{
name: "valid",
args: args{
size: "300x800",
},
want: 300,
want1: 800,
wantErr: false,
},
{
name: "empty",
args: args{
size: "",
},
want: 0,
want1: 0,
wantErr: false,
},
{
name: "invalid",
args: args{
size: "test",
},
want: 0,
want1: 0,
wantErr: false,
},
{
name: "invalid_height",
args: args{
size: "200xtest",
},
want: 0,
want1: 0,
wantErr: true,
},
{
name: "invalid_width",
args: args{
size: "testx200",
},
want: 0,
want1: 0,
wantErr: true,
},
{
name: "invalid_separator",
args: args{
size: "200y200",
},
want: 0,
want1: 0,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1, err := splitSize(tt.args.size)
if (err != nil) != tt.wantErr {
t.Errorf("splitSize() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("splitSize() got = %v, want %v", got, tt.want)
}
if got1 != tt.want1 {
t.Errorf("splitSize() got1 = %v, want %v", got1, tt.want1)
}
})
}
}

func TestYieldlabAdapter_makeEndpointURL_invalidEndpoint(t *testing.T) {
bid := NewYieldlabBidder("test$:/something§")
_, err := bid.makeEndpointURL(nil, nil)
assert.Error(t, err)
}
Loading

0 comments on commit 63c0252

Please sign in to comment.