Skip to content

Commit

Permalink
Update code with feedback from PR #1287
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Klinkert <alex@klinkert.io>
  • Loading branch information
aklinkert committed May 25, 2020
1 parent 6bbe213 commit 02a4b5a
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 100 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
42 changes: 11 additions & 31 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,17 @@ 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)
params, err := a.parseRequest(request)
if err != nil {
return nil, []error{err}
}

bidURL, err := a.makeEndpointURL(request, mergedParams)
bidURL, err := a.makeEndpointURL(request, a.mergeParams(params))
if err != nil {
return nil, []error{err}
}
Expand All @@ -159,11 +143,11 @@ 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
Expand All @@ -173,16 +157,12 @@ func (a *YieldlabAdapter) parseRequest(request *openrtb.BidRequest) ([]*openrtb_
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)
Expand All @@ -191,7 +171,7 @@ func (a *YieldlabAdapter) parseRequest(request *openrtb.BidRequest) ([]*openrtb_
return params, nil
}

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 +185,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 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)
}
136 changes: 136 additions & 0 deletions adapters/yieldlab/yieldlabtest/exemplary/video_app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
{
"mockBidRequest": {
"id": "test-request-id",
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [
{
"w": 728,
"h": 90
}
]
},
"ext": {
"bidder": {
"adslotId": "12345",
"supplyId": "123456789",
"adSize": "728x90",
"targeting": {
"key1": "value1",
"key2": "value2"
},
"extId": "abc"
}
},
"video": {
"context": "instream",
"mimes": [
"video/mp4"
],
"playerSize": [
[
400,
600
]
],
"minduration": 1,
"maxduration": 2,
"protocols": [
1,
2
],
"w": 1,
"h": 2,
"startdelay": 1,
"placement": 1,
"playbackmethod": [
2
]
}
}
],
"user": {
"buyeruid": "34a53e82-0dc3-4815-8b7e-b725ede0361c"
},
"app": {
"publisher": {
"id": "123456789"
},
"cat": [],
"bundle": "com.app.awesome",
"name": "Awesome App",
"domain": "awesomeapp.com",
"id": "123456789"
},
"device": {
"ifa": "hello-ads",
"devicetype": 4,
"connectiontype": 6,
"geo": {
"lat": 51.499488,
"lon": -0.128953
},
"ua": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36",
"ip": "169.254.13.37",
"h": 1098,
"w": 814
}
},
"httpCalls": [
{
"expectedRequest": {
"headers": {
"Accept": [
"application/json"
],
"Cookie": [
"id=34a53e82-0dc3-4815-8b7e-b725ede0361c"
],
"User-Agent": [
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36"
],
"X-Forwarded-For": [
"169.254.13.37"
]
},
"uri": "https://ad.yieldlab.net/testing/12345?content=json&ids=ylid%3A34a53e82-0dc3-4815-8b7e-b725ede0361c&lat=51.499488&lon=-0.128953&pubappname=Awesome+App&pubbundlename=com.app.awesome&pvid=true&t=key1%3Dvalue1%26key2%3Dvalue2&ts=testing&yl_rtb_connectiontype=6&yl_rtb_devicetype=4&yl_rtb_ifa=hello-ads"
},
"mockResponse": {
"status": 200,
"body": [
{
"id": 12345,
"price": 201,
"advertiser": "yieldlab",
"adsize": "728x90",
"pid": 1234,
"did": 5678,
"pvid": "40cb3251-1e1e-4cfd-8edc-7d32dc1a21e5"
}
]
}
}
],
"expectedBidResponses": [
{
"currency": "EUR",
"bids": [
{
"bid": {
"crid": "12345123433",
"dealid": "1234",
"id": "12345",
"impid": "test-imp-id",
"nurl": "https://ad.yieldlab.net/d/12345/123456789/728x90?id=abc&ids=ylid%3A34a53e82-0dc3-4815-8b7e-b725ede0361c&pvid=40cb3251-1e1e-4cfd-8edc-7d32dc1a21e5&ts=testing",
"price": 2.01,
"w": 728,
"h": 90
},
"type": "video"
}
]
}
]
}
Loading

0 comments on commit 02a4b5a

Please sign in to comment.