forked from prebid/prebid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pbs_light_test.go
112 lines (95 loc) · 2.62 KB
/
pbs_light_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
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"github.com/julienschmidt/httprouter"
"github.com/prebid/prebid-server/config"
"github.com/prebid/prebid-server/pbs"
"net/http"
"net/http/httptest"
"testing"
)
func TestCookieSyncNoCookies(t *testing.T) {
cfg, err := config.New()
if err != nil {
t.Fatalf("Unable to config: %v", err)
}
setupExchanges(cfg)
router := httprouter.New()
router.POST("/cookie_sync", cookieSync)
csreq := cookieSyncRequest{
UUID: "abcdefg",
Bidders: []string{"appnexus", "audienceNetwork", "random"},
}
csbuf := new(bytes.Buffer)
err = json.NewEncoder(csbuf).Encode(&csreq)
if err != nil {
t.Fatalf("Encode csr failed: %v", err)
}
req, _ := http.NewRequest("POST", "/cookie_sync", csbuf)
rr := httptest.NewRecorder()
router.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("Wrong status: %d", rr.Code)
}
csresp := cookieSyncResponse{}
err = json.Unmarshal(rr.Body.Bytes(), &csresp)
if err != nil {
t.Fatalf("Unmarshal response failed: %v", err)
}
if csresp.UUID != csreq.UUID {
t.Errorf("UUIDs didn't match")
}
if csresp.Status != "no_cookie" {
t.Errorf("Expected status = no_cookie; got %s", csresp.Status)
}
if len(csresp.BidderStatus) != 2 {
t.Errorf("Expected 2 bidder status rows; got %d", len(csresp.BidderStatus))
}
}
func TestCookieSyncHasCookies(t *testing.T) {
cfg, err := config.New()
if err != nil {
t.Fatalf("Unable to config: %v", err)
}
setupExchanges(cfg)
router := httprouter.New()
router.POST("/cookie_sync", cookieSync)
csreq := cookieSyncRequest{
UUID: "abcdefg",
Bidders: []string{"appnexus", "audienceNetwork", "random"},
}
csbuf := new(bytes.Buffer)
err = json.NewEncoder(csbuf).Encode(&csreq)
if err != nil {
t.Fatalf("Encode csr failed: %v", err)
}
req, _ := http.NewRequest("POST", "/cookie_sync", csbuf)
pcs := pbs.ParseUIDCookie(req)
pcs.UIDs["adnxs"] = "1234"
pcs.UIDs["audienceNetwork"] = "2345"
j, _ := json.Marshal(pcs)
b64 := base64.URLEncoding.EncodeToString(j)
uid_cookie := http.Cookie{Name: "uids", Value: b64}
req.AddCookie(&uid_cookie)
rr := httptest.NewRecorder()
router.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("Wrong status: %d", rr.Code)
}
csresp := cookieSyncResponse{}
err = json.Unmarshal(rr.Body.Bytes(), &csresp)
if err != nil {
t.Fatalf("Unmarshal response failed: %v", err)
}
if csresp.UUID != csreq.UUID {
t.Errorf("UUIDs didn't match")
}
if csresp.Status != "ok" {
t.Errorf("Expected status = ok; got %s", csresp.Status)
}
if len(csresp.BidderStatus) != 0 {
t.Errorf("Expected 0 bidder status rows; got %d", len(csresp.BidderStatus))
}
}