Skip to content

Commit

Permalink
Address code review comments. Use JSON-templates for testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
vchimishuk committed Feb 14, 2020
1 parent 08053af commit 286d9fd
Show file tree
Hide file tree
Showing 11 changed files with 257 additions and 237 deletions.
37 changes: 25 additions & 12 deletions adapters/adoppler/adoppler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ import (
"github.com/prebid/prebid-server/openrtb_ext"
)

var bidHeaders http.Header = map[string][]string{
"Accept": {"application/json"},
"Content-Type": {"application/json;charset=utf-8"},
"X-OpenRTB-Version": {"2.5"},
}

type adsVideoExt struct {
Duration int `json:"duration"`
}
Expand Down Expand Up @@ -40,10 +46,12 @@ func (ads *Adoppler) MakeRequests(
}

var datas []*adapters.RequestData
var errs []error
for _, imp := range req.Imp {
ext, err := unmarshalExt(imp.Ext)
if err != nil {
return nil, []error{&errortypes.BadInput{err.Error()}}
errs = append(errs, &errortypes.BadInput{err.Error()})
continue
}

var r openrtb.BidRequest = *req
Expand All @@ -52,25 +60,22 @@ func (ads *Adoppler) MakeRequests(

body, err := json.Marshal(r)
if err != nil {
return nil, []error{err}
errs = append(errs, err)
continue
}

uri := fmt.Sprintf("%s/processHeaderBid/%s",
ads.endpoint, ext.AdUnit)
headers := http.Header{}
headers.Add("Accept", "application/json")
headers.Add("Content-Type", "application/json;charset=utf-8")
headers.Add("X-OpenRTB-Version", "2.5")
data := &adapters.RequestData{
Method: "POST",
Uri: uri,
Body: body,
Headers: headers,
Headers: bidHeaders,
}
datas = append(datas, data)
}

return datas, nil
return datas, errs
}

func (ads *Adoppler) MakeBids(
Expand Down Expand Up @@ -105,6 +110,11 @@ func (ads *Adoppler) MakeBids(

impTypes := make(map[string]openrtb_ext.BidType)
for _, imp := range intReq.Imp {
if _, ok := impTypes[imp.ID]; ok {
return nil, []error{&errortypes.BadInput{
fmt.Sprintf("duplicate $.imp.id %s", imp.ID),
}}
}
if imp.Banner != nil {
impTypes[imp.ID] = openrtb_ext.BidTypeBanner
} else if imp.Video != nil {
Expand All @@ -113,6 +123,10 @@ func (ads *Adoppler) MakeBids(
impTypes[imp.ID] = openrtb_ext.BidTypeAudio
} else if imp.Native != nil {
impTypes[imp.ID] = openrtb_ext.BidTypeNative
} else {
return nil, []error{&errortypes.BadInput{
"one of $.imp.banner, $.imp.video, $.imp.audio and $.imp.native field required",
}}
}
}

Expand All @@ -133,7 +147,7 @@ func (ads *Adoppler) MakeBids(
if err != nil {
return nil, []error{&errortypes.BadServerResponse{err.Error()}}
}
if adsExt.Video == nil {
if adsExt == nil || adsExt.Video == nil {
return nil, []error{&errortypes.BadServerResponse{
"$.seatbid.bid.ext.ads.video required",
}}
Expand All @@ -143,12 +157,11 @@ func (ads *Adoppler) MakeBids(
PrimaryCategory: head(bid.Cat),
}
}
tb := &adapters.TypedBid{
bids = append(bids, &adapters.TypedBid{
Bid: &bid,
BidType: tp,
BidVideo: bidVideo,
}
bids = append(bids, tb)
})
}
}

Expand Down
229 changes: 4 additions & 225 deletions adapters/adoppler/adoppler_test.go
Original file line number Diff line number Diff line change
@@ -1,233 +1,12 @@
package adoppler

import (
"encoding/json"
"net/http"
"reflect"
"testing"

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

var bidRequest *openrtb.BidRequest = &openrtb.BidRequest{
ID: "req1",
Imp: []openrtb.Imp{
{
ID: "imp1",
Banner: &openrtb.Banner{},
Ext: json.RawMessage([]byte(
`{"bidder": {"adunit": "10"}}`)),
},
{
ID: "imp2",
Video: &openrtb.Video{},
Ext: json.RawMessage([]byte(
`{"bidder": {"adunit": "20"}}`)),
},
},
}

func TestMakeRequests(t *testing.T) {
ads := NewAdopplerBidder("http://adoppler.com")
datas, errs := ads.MakeRequests(bidRequest, nil)
if len(errs) != 0 {
t.Fatal(errs[0])
}

exps := []*adapters.RequestData{
{
Method: "POST",
Uri: "http://adoppler.com/processHeaderBid/10",
Body: []byte(`{"id":"req1-10","imp":[{"id":"imp1","banner":{},"ext":{"bidder":{"adunit":"10"}}}]}`),
Headers: http.Header{
"Accept": []string{"application/json"},
"Content-Type": []string{"application/json;charset=utf-8"},
"X-Openrtb-Version": []string{"2.5"},
},
},
{
Method: "POST",
Uri: "http://adoppler.com/processHeaderBid/20",
Body: []byte(`{"id":"req1-20","imp":[{"id":"imp2","video":{"mimes":null},"ext":{"bidder":{"adunit":"20"}}}]}`),
Headers: http.Header{
"Accept": []string{"application/json"},
"Content-Type": []string{"application/json;charset=utf-8"},
"X-Openrtb-Version": []string{"2.5"},
},
},
}
if len(exps) != len(datas) {
t.Fatalf("%d != %d", len(exps), len(datas))
}
assertRequestData(t, exps[0], datas[0])
assertRequestData(t, exps[1], datas[1])
}

func TestMakeBidsBadRequest(t *testing.T) {
ads := NewAdopplerBidder("http://adoppler.com")
datas, errs := ads.MakeRequests(bidRequest, nil)
if len(errs) != 0 {
t.Fatal(errs[0])
}
resp := &adapters.ResponseData{
StatusCode: http.StatusBadRequest,
}

_, errs = ads.MakeBids(bidRequest, datas[0], resp)
if len(errs) != 1 {
t.Fatalf("%d != %d", 1, len(errs))
}
if _, ok := errs[0].(*errortypes.BadInput); !ok {
t.Fatalf("%v is not *errortypes.BadInput type", errs[0])
}
}

func TestMakeBidsServerError(t *testing.T) {
ads := NewAdopplerBidder("http://adoppler.com")
datas, errs := ads.MakeRequests(bidRequest, nil)
if len(errs) != 0 {
t.Fatal(errs[0])
}
resp := &adapters.ResponseData{
StatusCode: http.StatusInternalServerError,
}

_, errs = ads.MakeBids(bidRequest, datas[0], resp)
if len(errs) != 1 {
t.Fatalf("%d != %d", 1, len(errs))
}
if _, ok := errs[0].(*errortypes.BadServerResponse); !ok {
t.Fatalf("%v is not *errortypes.BadServerResponse type", errs[0])
}
}

func TestMakeBidsNoContent(t *testing.T) {
ads := NewAdopplerBidder("http://adoppler.com")
datas, errs := ads.MakeRequests(bidRequest, nil)
if len(errs) != 0 {
t.Fatal(errs[0])
}
resp := &adapters.ResponseData{
StatusCode: http.StatusNoContent,
}

bidResp, errs := ads.MakeBids(bidRequest, datas[0], resp)
if len(errs) != 0 {
t.Fatalf("%d != %d", 0, len(errs))
}
if bidResp != nil {
t.Fatalf("%v != nil", bidResp)
}
}

func TestMakeBidsOKBanner(t *testing.T) {
ads := NewAdopplerBidder("http://adoppler.com")
datas, errs := ads.MakeRequests(bidRequest, nil)
if len(errs) != 0 {
t.Fatal(errs[0])
}
resp := &adapters.ResponseData{
StatusCode: http.StatusOK,
Body: []byte(`
{"id": "resp1",
"seatbid": [{"bid": [{"id": "bid1",
"impid": "imp1",
"price": 1.50,
"adm": "<b>a banner</b>"}]}]}
`),
}

bidResp, errs := ads.MakeBids(bidRequest, datas[0], resp)
if len(errs) != 0 {
t.Fatal(errs[0])
}
if bidResp == nil {
t.Fatalf("nil == %v", bidResp)
}
assertIntEqual(t, 1, len(bidResp.Bids))
bid := bidResp.Bids[0]
assertStringEqual(t, string(openrtb_ext.BidTypeBanner),
string(bid.BidType))
if bid.BidVideo != nil {
t.Fatalf("nil != %v", bid.BidVideo)
}
assertStringEqual(t, "bid1", bid.Bid.ID)
assertStringEqual(t, "<b>a banner</b>", bid.Bid.AdM)
}

func TestMakeBidsOKVideo(t *testing.T) {
ads := NewAdopplerBidder("http://adoppler.com")
datas, errs := ads.MakeRequests(bidRequest, nil)
if len(errs) != 0 {
t.Fatal(errs[0])
}
resp := &adapters.ResponseData{
StatusCode: http.StatusOK,
Body: []byte(`
{"id": "resp1",
"seatbid": [{"bid": [{"id": "bid1",
"impid": "imp2",
"price": 1.50,
"adm": "<VAST />",
"ext": {
"ads": {
"video": {
"duration": 90
}
}
}}]}]}
`),
}

bidResp, errs := ads.MakeBids(bidRequest, datas[1], resp)
if len(errs) != 0 {
t.Fatal(errs[0])
}
if bidResp == nil {
t.Fatalf("nil == %v", bidResp)
}
assertIntEqual(t, 1, len(bidResp.Bids))
bid := bidResp.Bids[0]
assertStringEqual(t, string(openrtb_ext.BidTypeVideo),
string(bid.BidType))
if bid.BidVideo == nil {
t.Fatalf("nil == %v", bid.BidVideo)
}
assertIntEqual(t, 90, bid.BidVideo.Duration)
assertStringEqual(t, "bid1", bid.Bid.ID)
assertStringEqual(t, "<VAST />", bid.Bid.AdM)
}

func assertRequestData(t *testing.T, exp *adapters.RequestData,
act *adapters.RequestData) {

if exp.Method != act.Method {
t.Fatalf("%v != %v", exp.Method, act.Method)
}
if exp.Uri != act.Uri {
t.Fatalf("%v != %v", exp.Uri, act.Uri)
}
if string(exp.Body) != string(act.Body) {
t.Fatalf("%v != %v", string(exp.Body), string(act.Body))
}
if !reflect.DeepEqual(exp.Headers, act.Headers) {
t.Fatalf("%v != %v", exp.Headers, act.Headers)
}
}

func assertIntEqual(t *testing.T, exp int, act int) {
t.Helper()
if exp != act {
t.Fatalf("%v != %v", exp, act)
}
}

func assertStringEqual(t *testing.T, exp string, act string) {
t.Helper()
if exp != act {
t.Fatalf("%v != %v", exp, act)
}
func TestJsonSamples(t *testing.T) {
bidder := NewAdopplerBidder("http://adoppler.com")
adapterstest.RunJSONBidderTest(t, "adopplertest", bidder)
}
Loading

0 comments on commit 286d9fd

Please sign in to comment.