/
cases-header.go
56 lines (51 loc) · 1.36 KB
/
cases-header.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
// Copyright 2017 Aleksey Blinov. All rights reserved.
package apns2mock
import (
"strconv"
"github.com/satori/go.uuid"
)
// HeaderHandlers deal with request headers:
//
// Invalid UUID format in APNS Id is 400, "BadMessageId".
//
// Priority that is not empty, 5 or 10 is 400, "BadPriority".
//
// Empty topic is 400, "MissingTopic".
//
// Topics starting with 'd' are 400, "TopicDisallowed".
//
// Collapse Ids longer than 64 are 400, "BadCollapseId".
//
// Expiration date that cannot be parsed is 400, "BadExpirationDate".
var HeaderHandlers []HadlerFunc
func init() {
HeaderHandlers = []HadlerFunc{
func(req *APNSRequest) (int, string) {
if h := req.Header.Get("apns-id"); h != "" {
if _, err := uuid.FromString(h); err != nil {
return 400, "BadMessageId"
}
}
switch h := req.Header.Get("apns-priority"); h {
case "", "5", "10":
default:
return 400, "BadPriority"
}
if h := req.Header.Get("apns-topic"); h == "" {
return 400, "MissingTopic"
}
if h := req.Header.Get("apns-topic"); h != "" && h[0] == 'd' {
return 400, "TopicDisallowed"
}
if h := req.Header.Get("apns-collapse-id"); len(h) > 64 {
return 400, "BadCollapseId"
}
if h := req.Header.Get("apns-expiration"); h != "" {
if _, err := strconv.ParseInt(h, 10, 64); err != nil {
return 400, "BadExpirationDate"
}
}
return 0, ""
},
}
}