-
Notifications
You must be signed in to change notification settings - Fork 2
/
Connection_test.go
161 lines (132 loc) · 4.91 KB
/
Connection_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
package goapns_test
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/tantalum73/Go-APNS"
)
func mockMessage() *goapns.Message {
m := goapns.NewMessage().Badge(42).Title("title").Body("body")
return m
}
func mockConnection(t *testing.T) *goapns.Connection {
pathToCert := "example/certificate-valid-encrypted.p12"
conn, err := goapns.NewConnection(pathToCert, "password")
assert.Nil(t, err)
assert.NotNil(t, conn)
client := http.Client{}
//Mocking the client to, because otherwise we would have to use HTTP
//for testing, which is harder to do. We also do not test security here.
conn.HTTPClient = client
return conn
}
func TestConnectionCertificateWrongPath(t *testing.T) {
pathToCert := "example/nowhere"
conn, err := goapns.NewConnection(pathToCert, "wrongPassword")
assert.Error(t, err)
assert.Nil(t, conn)
}
func TestConnectionCertificateWrongPassphrase(t *testing.T) {
pathToCert := "example/certificate-valid-encrypted.p12"
conn, err := goapns.NewConnection(pathToCert, "wrongPassword")
assert.Error(t, err)
assert.Nil(t, conn)
}
func TestConnectionCertificateCorrectPassphrase(t *testing.T) {
pathToCert := "example/certificate-valid-encrypted.p12"
conn, err := goapns.NewConnection(pathToCert, "password")
assert.Nil(t, err)
assert.NotNil(t, conn)
}
func TestConnectionToken(t *testing.T) {
conn := mockConnection(t)
token := []string{"1234567890"}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method)
assert.Equal(t, fmt.Sprintf("/3/device/%s", token[0]), r.URL.String())
}))
defer server.Close()
channel := make(chan goapns.Response, 1)
conn.Host = server.URL
conn.Push(mockMessage(), token, channel)
// for response := range channel {
// fmt.Println("Received response")
// }
}
func TestConnectionHeaderDefaults(t *testing.T) {
conn := mockConnection(t)
token := []string{"1234567890"}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "application/json; charset=utf-8", r.Header.Get("Content-Type"))
assert.Equal(t, "", r.Header.Get("apns-id"))
assert.Equal(t, "", r.Header.Get("apns-priority"))
assert.Equal(t, "", r.Header.Get("apns-topic"))
assert.Equal(t, "", r.Header.Get("apns-expiration"))
}))
defer server.Close()
channel := make(chan goapns.Response, 1)
conn.Host = server.URL
conn.Push(mockMessage(), token, channel)
}
func TestConnectionHeader(t *testing.T) {
conn := mockConnection(t)
collapseID := "com.example.euroApp.scroreChanged"
token := []string{"1234567890"}
message := mockMessage().APNSID("102").PriorityHigh().Topic("topic").Expiration(time.Now()).CollapseID(collapseID)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, message.Header.APNSID, r.Header.Get("apns-id"))
assert.Equal(t, "10", message.Header.Priority, r.Header.Get("apns-priority"))
assert.Equal(t, message.Header.Topic, r.Header.Get("apns-topic"))
assert.Equal(t, fmt.Sprintf("%v", message.Header.Expiration.Unix()), r.Header.Get("apns-expiration"))
assert.Equal(t, collapseID, "apns-collapse-id")
}))
defer server.Close()
channel := make(chan goapns.Response, 1)
conn.Host = server.URL
conn.Push(message, token, channel)
}
func TestConnectionTokenExpired(t *testing.T) {
conn := mockConnection(t)
token := []string{"12345678912"}
message := mockMessage().APNSID("102").PriorityHigh().Topic("topic").Expiration(time.Now())
expired := time.Now().UTC().Unix()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("apns-id", message.Header.APNSID)
w.WriteHeader(http.StatusGone)
responseJSON := fmt.Sprintf("{\"reason\":\"Unregistered\" ,\"timestamp\":%v}", expired)
w.Write([]byte(responseJSON))
}))
defer server.Close()
channel := make(chan goapns.Response, 1)
conn.Host = server.URL
conn.Push(message, token, channel)
for response := range channel {
//Timestamp set correctly
assert.Equal(t, response.TimestempNumber, expired)
//Reason set correctly
assert.Equal(t, goapns.ErrorUnregistered, response.Error)
assert.False(t, response.Sent())
}
}
func TestConnectionBadPriority(t *testing.T) {
conn := mockConnection(t)
token := []string{"12345678912"}
message := mockMessage().APNSID("102")
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Write([]byte(`{"reason": "BadPriority"}`))
}))
defer server.Close()
channel := make(chan goapns.Response, 1)
conn.Host = server.URL
conn.Push(message, token, channel)
for response := range channel {
assert.False(t, response.Sent())
assert.Equal(t, goapns.ErrorBadPriority, response.Error)
}
}