-
Notifications
You must be signed in to change notification settings - Fork 0
/
stub.go
76 lines (64 loc) · 1.58 KB
/
stub.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
package httpstub
import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/url"
"testing"
"github.com/google/go-cmp/cmp"
)
// Stub is any single request stub
type Stub struct {
URL string
Status int
Body interface{}
Config StubConfig
Receive Receive
}
type Receive struct {
Body []byte
Params url.Values
}
type StubConfig struct {
DontAssertReceive bool
}
func (s *Stub) intercept(t *testing.T) http.Handler {
t.Helper()
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if !s.Config.DontAssertReceive {
s.assertReceive(t, req)
}
res.WriteHeader(s.Status)
err := json.NewEncoder(res).Encode(s.Body)
if err != nil {
t.Fatalf("couldn't intercept due body marshalling error: %v", err)
}
})
}
func (s *Stub) assertReceive(t *testing.T, req *http.Request) {
t.Helper()
assertReceivedBody(t, req, s.Receive.Body)
if s.Receive.Params == nil {
s.Receive.Params = url.Values{}
}
assertReceivedParams(t, req, s.Receive.Params)
}
func assertReceivedBody(t *testing.T, req *http.Request, assertion []byte) {
t.Helper()
defer req.Body.Close()
body, err := ioutil.ReadAll(req.Body)
if err != nil && err != io.EOF {
t.Fatalf("couldn't read request body: %v", err)
}
if diff := cmp.Diff(string(assertion), string(body)); diff != "" {
t.Errorf("body received mismatch (-want +got): %s", diff)
}
}
func assertReceivedParams(t *testing.T, req *http.Request, assertion url.Values) {
t.Helper()
got := req.URL.Query()
if diff := cmp.Diff(assertion, got); diff != "" {
t.Errorf("params received mismatch (-want +got): %s", diff)
}
}