-
Notifications
You must be signed in to change notification settings - Fork 492
/
talktest.go
57 lines (51 loc) · 940 Bytes
/
talktest.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
package talktest
import (
"encoding/json"
"net/http"
"net/http/httptest"
"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) {
tr := Request{
URL: r.URL.String(),
}
dec := json.NewDecoder(r.Body)
dec.Decode(&tr.PostData)
s.mu.Lock()
s.requests = append(s.requests, tr)
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 {
URL string
PostData PostData
}
type PostData struct {
Title string `json:"title"`
Text string `json:"text"`
AuthorName string `json:"authorName"`
}