forked from prebid/prebid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
info_test.go
227 lines (205 loc) · 7.95 KB
/
info_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package adapters_test
import (
"errors"
"strings"
"testing"
"github.com/mxmCherry/openrtb"
"github.com/prebid/prebid-server/adapters"
"github.com/prebid/prebid-server/config"
"github.com/prebid/prebid-server/errortypes"
"github.com/prebid/prebid-server/openrtb_ext"
"github.com/stretchr/testify/assert"
)
func TestAppNotSupported(t *testing.T) {
bidder := &mockBidder{}
info := adapters.BidderInfo{
Capabilities: &adapters.CapabilitiesInfo{
Site: &adapters.PlatformInfo{
MediaTypes: []openrtb_ext.BidType{openrtb_ext.BidTypeBanner},
},
},
}
constrained := adapters.EnforceBidderInfo(bidder, info)
bids, errs := constrained.MakeRequests(&openrtb.BidRequest{
Imp: []openrtb.Imp{{ID: "imp-1", Banner: &openrtb.Banner{}}},
App: &openrtb.App{},
}, &adapters.ExtraRequestInfo{})
if !assert.Len(t, errs, 1) {
return
}
assert.EqualError(t, errs[0], "this bidder does not support app requests")
assert.IsType(t, &errortypes.BadInput{}, errs[0])
assert.Len(t, bids, 0)
}
func TestSiteNotSupported(t *testing.T) {
bidder := &mockBidder{}
info := adapters.BidderInfo{
Capabilities: &adapters.CapabilitiesInfo{
App: &adapters.PlatformInfo{
MediaTypes: []openrtb_ext.BidType{openrtb_ext.BidTypeBanner},
},
},
}
constrained := adapters.EnforceBidderInfo(bidder, info)
bids, errs := constrained.MakeRequests(&openrtb.BidRequest{
Imp: []openrtb.Imp{{ID: "imp-1", Banner: &openrtb.Banner{}}},
Site: &openrtb.Site{},
}, &adapters.ExtraRequestInfo{})
if !assert.Len(t, errs, 1) {
return
}
assert.EqualError(t, errs[0], "this bidder does not support site requests")
assert.IsType(t, &errortypes.BadInput{}, errs[0])
assert.Len(t, bids, 0)
}
func TestImpFiltering(t *testing.T) {
bidder := &mockBidder{}
info := adapters.BidderInfo{
Capabilities: &adapters.CapabilitiesInfo{
Site: &adapters.PlatformInfo{
MediaTypes: []openrtb_ext.BidType{openrtb_ext.BidTypeVideo},
},
App: &adapters.PlatformInfo{
MediaTypes: []openrtb_ext.BidType{openrtb_ext.BidTypeBanner},
},
},
}
constrained := adapters.EnforceBidderInfo(bidder, info)
testCases := []struct {
description string
inBidRequest *openrtb.BidRequest
expectedErrors []error
expectedImpLen int
}{
{
description: "Empty Imp array. MakeRequest() call not expected",
inBidRequest: &openrtb.BidRequest{
Imp: []openrtb.Imp{},
Site: &openrtb.Site{},
},
expectedErrors: []error{
&errortypes.BadInput{Message: "Bid request didn't contain media types supported by the bidder"},
},
expectedImpLen: 0,
},
{
description: "Sole imp in bid request is of wrong media type. MakeRequest() call not expected",
inBidRequest: &openrtb.BidRequest{
Imp: []openrtb.Imp{{ID: "imp-1", Video: &openrtb.Video{}}},
App: &openrtb.App{},
},
expectedErrors: []error{
&errortypes.BadInput{Message: "request.imp[0] uses video, but this bidder doesn't support it"},
&errortypes.BadInput{Message: "Bid request didn't contain media types supported by the bidder"},
},
expectedImpLen: 0,
},
{
description: "All imps in bid request of wrong media type, MakeRequest() call not expected",
inBidRequest: &openrtb.BidRequest{
Imp: []openrtb.Imp{
{ID: "imp-1", Video: &openrtb.Video{}},
{ID: "imp-2", Native: &openrtb.Native{}},
{ID: "imp-3", Audio: &openrtb.Audio{}},
},
App: &openrtb.App{},
},
expectedErrors: []error{
&errortypes.BadInput{Message: "request.imp[0] uses video, but this bidder doesn't support it"},
&errortypes.BadInput{Message: "request.imp[1] uses native, but this bidder doesn't support it"},
&errortypes.BadInput{Message: "request.imp[2] uses audio, but this bidder doesn't support it"},
&errortypes.BadInput{Message: "Bid request didn't contain media types supported by the bidder"},
},
expectedImpLen: 0,
},
{
description: "Some imps with correct media type, MakeRequest() call expected",
inBidRequest: &openrtb.BidRequest{
Imp: []openrtb.Imp{
{
ID: "imp-1",
Video: &openrtb.Video{},
},
{
Native: &openrtb.Native{},
},
{
ID: "imp-2",
Video: &openrtb.Video{},
Native: &openrtb.Native{},
},
{
Banner: &openrtb.Banner{},
},
},
Site: &openrtb.Site{},
},
expectedErrors: []error{
&errortypes.BadInput{Message: "request.imp[1] uses native, but this bidder doesn't support it"},
&errortypes.BadInput{Message: "request.imp[2] uses native, but this bidder doesn't support it"},
&errortypes.BadInput{Message: "request.imp[3] uses banner, but this bidder doesn't support it"},
&errortypes.BadInput{Message: "request.imp[1] has no supported MediaTypes. It will be ignored"},
&errortypes.BadInput{Message: "request.imp[3] has no supported MediaTypes. It will be ignored"},
},
expectedImpLen: 2,
},
{
description: "All imps with correct media type, MakeRequest() call expected",
inBidRequest: &openrtb.BidRequest{
Imp: []openrtb.Imp{
{ID: "imp-1", Video: &openrtb.Video{}},
{ID: "imp-2", Video: &openrtb.Video{}},
},
Site: &openrtb.Site{},
},
expectedErrors: nil,
expectedImpLen: 2,
},
}
for _, test := range testCases {
actualAdapterRequests, actualErrs := constrained.MakeRequests(test.inBidRequest, &adapters.ExtraRequestInfo{})
// Assert the request.Imp slice was correctly filtered and if MakeRequest() was called by asserting
// the corresponding error messages were returned
for i, expectedErr := range test.expectedErrors {
assert.EqualError(t, expectedErr, actualErrs[i].Error(), "Test failed. Error[%d] in error list mismatch: %s", i, test.description)
}
// Extra MakeRequests() call check: our mockBidder returns an adapter request for every imp
assert.Len(t, actualAdapterRequests, test.expectedImpLen, "Test failed. Incorrect lenght of filtered imps: %s", test.description)
}
}
type mockBidder struct {
gotRequest *openrtb.BidRequest
}
func (m *mockBidder) MakeRequests(request *openrtb.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
var adapterRequests []*adapters.RequestData
for i := 0; i < len(request.Imp); i++ {
adapterRequests = append(adapterRequests, &adapters.RequestData{})
}
return adapterRequests, nil
}
func (m *mockBidder) MakeBids(internalRequest *openrtb.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) {
return nil, []error{errors.New("mock MakeBids error")}
}
func blankAdapterConfig(bidderName openrtb_ext.BidderName) map[string]config.Adapter {
adapters := make(map[string]config.Adapter)
adapters[strings.ToLower(string(bidderName))] = config.Adapter{}
return adapters
}
func TestParsing(t *testing.T) {
mockBidderName := openrtb_ext.BidderName("someBidder")
infos := adapters.ParseBidderInfos(blankAdapterConfig(mockBidderName), "./adapterstest/bidder-info", []openrtb_ext.BidderName{mockBidderName})
if infos[string(mockBidderName)].Maintainer.Email != "some-email@domain.com" {
t.Errorf("Bad maintainer email. Got %s", infos[string(mockBidderName)].Maintainer.Email)
}
assert.Equal(t, true, infos.IsActive(mockBidderName))
assert.Equal(t, true, infos.HasAppSupport(mockBidderName))
assert.Equal(t, true, infos.HasSiteSupport(mockBidderName))
assert.Equal(t, true, infos.SupportsAppMediaType(mockBidderName, openrtb_ext.BidTypeBanner))
assert.Equal(t, false, infos.SupportsAppMediaType(mockBidderName, openrtb_ext.BidTypeVideo))
assert.Equal(t, false, infos.SupportsAppMediaType(mockBidderName, openrtb_ext.BidTypeAudio))
assert.Equal(t, true, infos.SupportsAppMediaType(mockBidderName, openrtb_ext.BidTypeNative))
assert.Equal(t, true, infos.SupportsWebMediaType(mockBidderName, openrtb_ext.BidTypeBanner))
assert.Equal(t, true, infos.SupportsWebMediaType(mockBidderName, openrtb_ext.BidTypeVideo))
assert.Equal(t, false, infos.SupportsWebMediaType(mockBidderName, openrtb_ext.BidTypeAudio))
assert.Equal(t, true, infos.SupportsWebMediaType(mockBidderName, openrtb_ext.BidTypeNative))
}