-
Notifications
You must be signed in to change notification settings - Fork 495
/
pushovertest.go
83 lines (74 loc) · 1.39 KB
/
pushovertest.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
package pushovertest
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"strconv"
"sync"
)
type Server struct {
mu sync.Mutex
ts *httptest.Server
URL string
requests []Request
closed bool
}
func NewServer() *Server {
s := new(Server)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fr := Request{}
data, _ := ioutil.ReadAll(r.Body)
v, _ := url.ParseQuery(string(data))
fr.PostData, _ = NewPostData(v)
s.mu.Lock()
s.requests = append(s.requests, fr)
s.mu.Unlock()
}))
s.ts = ts
s.URL = ts.URL
return s
}
func (s *Server) Requests() []Request {
s.mu.Lock()
defer s.mu.Unlock()
return s.requests
}
func (s *Server) Close() {
if s.closed {
return
}
s.closed = true
s.ts.Close()
}
type Request struct {
PostData PostData
}
type PostData struct {
Token string
UserKey string
Message string
Device string
Title string
URL string
URLTitle string
Sound string
Priority int
}
func NewPostData(v url.Values) (PostData, error) {
p := PostData{}
p.Token = v.Get("token")
p.UserKey = v.Get("user")
p.Message = v.Get("message")
p.Device = v.Get("device")
p.Title = v.Get("title")
p.URL = v.Get("url")
p.URLTitle = v.Get("url_title")
p.Sound = v.Get("sound")
priority, err := strconv.Atoi(v.Get("priority"))
if err != nil {
return p, err
}
p.Priority = priority
return p, nil
}