forked from googlearchive/go-gcm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_test.go
153 lines (140 loc) · 4.49 KB
/
http_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
package gcm
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("HTTP Client", func() {
var (
ids = []string{"4", "8", "15", "16", "23", "42"}
expectedResp = `
{"multicast_id": 123456789012345,
"success": 5,
"failure": 1,
"canonical_ids": 1,
"results": [
{ "message_id": "10408" },
{ "message_id": "10409" },
{ "message_id": "11516" },
{ "message_id": "11517" },
{ "message_id": "12342", "registration_id": "32" },
{ "error": "NotRegistered"}
]
}`
multicastReply = `
{ "multicast_id": 216,
"success": 3,
"failure": 3,
"canonical_ids": 1,
"results": [
{ "message_id": "1:0408" },
{ "error": "Unavailable" },
{ "error": "InternalServerError" },
{ "message_id": "1:1517" },
{ "message_id": "1:2342", "registration_id": "32" },
{ "error": "NotRegistered"}
]
}`
)
Context("initializing", func() {
It("should init successfully", func() {
c := newHTTPClient("key", true)
Expect(c).To(BeAssignableToTypeOf(&gcmHTTP{}))
gc := c.(*gcmHTTP)
Expect(gc.apiKey).To(Equal("key"))
Expect(gc.httpClient).NotTo(BeNil())
})
})
Context("infrastructure", func() {
It("should transform recipient to an array of strings", func() {
singleTargetMessage := HTTPMessage{To: "recipient"}
targets, err := messageTargetAsStringsArray(singleTargetMessage)
Expect(err).NotTo(HaveOccurred())
Expect(targets).To(Equal([]string{"recipient"}))
multipleTargetMessage := HTTPMessage{RegistrationIDs: ids}
targets, err = messageTargetAsStringsArray(multipleTargetMessage)
Expect(err).NotTo(HaveOccurred())
Expect(targets).To(Equal(ids))
})
It("should check http results", func() {
response := &HTTPResponse{}
json.Unmarshal([]byte(multicastReply), &response)
resultsState := &multicastResultsState{}
doRetry, toRetry, err := checkResults(response.Results, ids, *resultsState)
Expect(doRetry).To(BeTrue())
Expect(err).NotTo(HaveOccurred())
Expect(toRetry).To(Equal([]string{"8", "15"}))
expectedResultState := &multicastResultsState{
"4": &HTTPResult{MessageID: "1:0408"},
"8": &HTTPResult{Error: "Unavailable"},
"15": &HTTPResult{Error: "InternalServerError"},
"16": &HTTPResult{MessageID: "1:1517"},
"23": &HTTPResult{MessageID: "1:2342", RegistrationID: "32"},
"42": &HTTPResult{Error: "NotRegistered"},
}
Expect(resultsState).To(Equal(expectedResultState))
})
It("should build multicast response", func() {
response := &HTTPResponse{}
json.Unmarshal([]byte(multicastReply), &response)
resultsState := multicastResultsState{
"4": &HTTPResult{MessageID: "1:0408"},
"8": &HTTPResult{Error: "Unavailable"},
"15": &HTTPResult{Error: "InternalServerError"},
"16": &HTTPResult{MessageID: "1:1517"},
"23": &HTTPResult{MessageID: "1:2342", RegistrationID: "32"},
"42": &HTTPResult{Error: "NotRegistered"},
}
resp := buildRespForMulticast(ids, resultsState, 216)
Expect(resp).To(Equal(response))
})
It("should parse retryAfter correctly", func() {
Expect(parseRetryAfter("10")).To(Equal(time.Second * 10))
Expect(parseRetryAfter("Fri, 31 Dec 2030 23:59:59 GMT")).NotTo(BeZero())
Expect(parseRetryAfter("Fri, 31 Dec 2000 23:59:59 GMT")).To(BeZero())
})
})
Context("sending", func() {
var (
server *httptest.Server
c httpC
authHeader string
)
BeforeEach(func() {
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(http.CanonicalHeaderKey("Content-Type"), "application/json")
w.Header().Set(http.CanonicalHeaderKey("Retry-After"), "10")
w.WriteHeader(200)
fmt.Fprintln(w, expectedResp)
}))
transport := &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
authHeader = req.Header.Get(http.CanonicalHeaderKey("Authorization"))
return url.Parse(server.URL)
},
}
c = &gcmHTTP{
GCMURL: server.URL,
apiKey: "apiKey",
httpClient: &http.Client{Transport: transport},
}
})
AfterEach(func() {
server.Close()
})
It("should send successfully", func() {
m := HTTPMessage{RegistrationIDs: ids}
resp, err := c.Send(m)
Expect(err).NotTo(HaveOccurred())
expResp := HTTPResponse{}
json.Unmarshal([]byte(expectedResp), &expResp)
Expect(authHeader).To(Equal("key=apiKey"))
Expect(resp).To(Equal(&expResp))
})
})
})