forked from prebid/prebid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_test.go
232 lines (215 loc) · 6.34 KB
/
request_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
228
229
230
231
232
package openrtb_ext
import (
"encoding/json"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
// Test the unmarshalling of the prebid extensions and setting default Price Granularity
func TestExtRequestTargeting(t *testing.T) {
extRequest := &ExtRequest{}
err := json.Unmarshal([]byte(ext1), extRequest)
if err != nil {
t.Errorf("ext1 Unmarshall failure: %s", err.Error())
}
if extRequest.Prebid.Targeting != nil {
t.Error("ext1 Targeting is not nil")
}
extRequest = &ExtRequest{}
err = json.Unmarshal([]byte(ext2), extRequest)
if err != nil {
t.Errorf("ext2 Unmarshall failure: %s", err.Error())
}
if extRequest.Prebid.Targeting == nil {
t.Error("ext2 Targeting is nil")
} else {
pgDense := PriceGranularityFromString("dense")
if !reflect.DeepEqual(extRequest.Prebid.Targeting.PriceGranularity, pgDense) {
t.Errorf("ext2 expected Price granularity \"dense\" (%v), found \"%v\"", pgDense, extRequest.Prebid.Targeting.PriceGranularity)
}
}
extRequest = &ExtRequest{}
err = json.Unmarshal([]byte(ext3), extRequest)
if err != nil {
t.Errorf("ext3 Unmarshall failure: %s", err.Error())
}
if extRequest.Prebid.Targeting == nil {
t.Error("ext3 Targeting is nil")
} else {
pgMed := PriceGranularityFromString("medium")
if !reflect.DeepEqual(extRequest.Prebid.Targeting.PriceGranularity, pgMed) {
t.Errorf("ext3 expected Price granularity \"medium\", found \"%v\"", extRequest.Prebid.Targeting.PriceGranularity)
}
}
}
const ext1 = `{
"prebid": {
"non_target": "some junk"
}
}
`
const ext2 = `{
"prebid": {
"targeting": {
"pricegranularity": "dense"
}
}
}`
const ext3 = `{
"prebid": {
"targeting": { }
}
}`
func TestCacheIllegal(t *testing.T) {
var bids ExtRequestPrebidCache
if err := json.Unmarshal([]byte(`{}`), &bids); err == nil {
t.Error("Unmarshal should fail when cache.bids is undefined.")
}
if err := json.Unmarshal([]byte(`{"bids":null}`), &bids); err == nil {
t.Error("Unmarshal should fail when cache.bids is null.")
}
if err := json.Unmarshal([]byte(`{"bids":true}`), &bids); err == nil {
t.Error("Unmarshal should fail when cache.bids is not an object.")
}
}
func TestCacheBids(t *testing.T) {
var bids ExtRequestPrebidCache
assert.NoError(t, json.Unmarshal([]byte(`{"bids":{}}`), &bids))
assert.NotNil(t, bids.Bids)
assert.Nil(t, bids.VastXML)
}
func TestCacheVast(t *testing.T) {
var bids ExtRequestPrebidCache
assert.NoError(t, json.Unmarshal([]byte(`{"vastxml":{}}`), &bids))
assert.Nil(t, bids.Bids)
assert.NotNil(t, bids.VastXML)
}
func TestCacheNothing(t *testing.T) {
var bids ExtRequestPrebidCache
assert.Error(t, json.Unmarshal([]byte(`{}`), &bids))
}
type granularityTestData struct {
json []byte
target PriceGranularity
}
func TestGranularityUnmarshal(t *testing.T) {
for _, test := range validGranularityTests {
var resolved PriceGranularity
err := json.Unmarshal(test.json, &resolved)
if err != nil {
t.Errorf("Failed to Unmarshall granularity: %s", err.Error())
}
if !reflect.DeepEqual(test.target, resolved) {
t.Errorf("Granularity unmarshal failed, the unmarshalled JSON did not match the target\nExpected: %v\nActual : %v", test.target, resolved)
}
}
}
var validGranularityTests []granularityTestData = []granularityTestData{
{
json: []byte(`{"precision": 4, "ranges": [{"min": 0, "max": 5, "increment": 0.1}, {"min": 5, "max":10, "increment":0.5}, {"min":10, "max":20, "increment":1}]}`),
target: PriceGranularity{
Precision: 4,
Ranges: []GranularityRange{
{Min: 0.0, Max: 5.0, Increment: 0.1},
{Min: 5.0, Max: 10.0, Increment: 0.5},
{Min: 10.0, Max: 20.0, Increment: 1.0},
},
},
},
{
json: []byte(`{"ranges":[{ "max":5, "increment": 0.05}, {"max": 10, "increment": 0.25}, {"max": 20, "increment": 0.5}]}`),
target: PriceGranularity{
Precision: 2,
Ranges: []GranularityRange{
{Min: 0.0, Max: 5.0, Increment: 0.05},
{Min: 5.0, Max: 10.0, Increment: 0.25},
{Min: 10.0, Max: 20.0, Increment: 0.5},
},
},
},
{
json: []byte(`"medium"`),
target: priceGranularityMed,
},
{
json: []byte(`{ "precision": 3, "ranges": [{"max":20, "increment":0.005}]}`),
target: PriceGranularity{
Precision: 3,
Ranges: []GranularityRange{{Min: 0.0, Max: 20.0, Increment: 0.005}},
},
},
{
json: []byte(`{"precision": 0, "ranges": [{"max":5, "increment": 1}, {"max": 10, "increment": 2}, {"max": 20, "increment": 5}]}`),
target: PriceGranularity{
Precision: 0,
Ranges: []GranularityRange{
{Min: 0.0, Max: 5.0, Increment: 1.0},
{Min: 5.0, Max: 10.0, Increment: 2.0},
{Min: 10.0, Max: 20.0, Increment: 5.0},
},
},
},
{
json: []byte(`{"precision": 2, "ranges": [{"min": 0.5, "max":5, "increment": 0.1}, {"min": 54, "max": 10, "increment": 1}, {"min": -42, "max": 20, "increment": 5}]}`),
target: PriceGranularity{
Precision: 2,
Ranges: []GranularityRange{
{Min: 0.0, Max: 5.0, Increment: 0.1},
{Min: 5.0, Max: 10.0, Increment: 1.0},
{Min: 10.0, Max: 20.0, Increment: 5.0},
},
},
},
{
json: []byte(`{}`),
target: priceGranularityMed,
},
{
json: []byte(`{"precision": 2}`),
target: priceGranularityMed,
},
{
json: []byte(`{"precision": 2, "ranges":[]}`),
target: priceGranularityMed,
},
}
func TestGranularityUnmarshalBad(t *testing.T) {
testCases := []struct {
description string
jsonPriceGranularity []byte
}{
{
"Malformed",
[]byte(`[]`),
},
{
"Negative precision",
[]byte(`{"precision": -1, "ranges": [{"max":20, "increment":0.5}]}`),
},
{
"Precision greater than MaxDecimalFigures supported",
[]byte(`{"precision": 16, "ranges": [{"max":20, "increment":0.5}]}`),
},
{
"Negative increment",
[]byte(`{"ranges":[{"max":20, "increment": -1}]}`),
},
{
"Range with non float64 max value",
[]byte(`{"ranges":[{"max":"20", "increment": "0.1"}]}`),
},
{
"Ranges in decreasing order",
[]byte(`{"ranges":[{"max":20, "increment":0.1}. {"max":10, "increment":0.02}]}`),
},
{
"Max equal to previous max",
[]byte(`{"ranges":[{"max":1.0, "increment": 0.07}, {"max" 1.0, "increment": 0.03}]}`),
},
}
for _, test := range testCases {
resolved := PriceGranularity{}
err := json.Unmarshal(test.jsonPriceGranularity, &resolved)
assert.Errorf(t, err, "Invalid granularity unmarshalled without error.\nJSON was: %s\n Resolved to: %v. Test: %s", string(test.jsonPriceGranularity), resolved, test.description)
}
}