/
servehttp.go
113 lines (91 loc) · 2.23 KB
/
servehttp.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
package mockns1
import (
"fmt"
"io/ioutil"
"net/http"
"github.com/stretchr/testify/assert"
)
// ServeHTTP is the request handler for the mock service. This
// should not be called directly in unit tests.
func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.stopTimer()
defer s.startTimer()
if _, exists := s.tests[r.Method]; !exists {
notFoundResponse(w, "method")
return
}
tests, exists := s.tests[r.Method][r.RequestURI]
if !exists {
notFoundResponse(w, "uri")
return
}
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte( // nolint: errcheck
fmt.Sprintf(`{"message": "unable to read request body: %s`, err),
))
return
}
var test *testCase
for _, t := range tests {
if compareBody(t, body) && compareHeaders(t.request.headers, r.Header) {
test = t
break
}
}
if test == nil {
notFoundResponse(w, "no test")
return
}
for k, vals := range test.response.headers {
w.Header().Set(k, vals[0])
for _, v := range vals[1:] {
w.Header().Add(k, v)
}
}
w.WriteHeader(test.status)
w.Write(test.response.body) // nolint: errcheck
}
func compareBody(test *testCase, body []byte) bool {
if !test.request.json {
return assert.Equal(new(testifyT), test.request.body, body)
}
return assert.JSONEq(new(testifyT), string(test.request.body), string(body))
}
func notFoundResponse(w http.ResponseWriter, reason string) {
msg := fmt.Sprintf(`{"message": "request not found: %s"}`, reason)
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(msg)) // nolint: errcheck
}
func compareHeaders(a, b http.Header) bool {
for key := range a {
if !compareHeader(key, a, b) {
return false
}
}
return true
}
func compareHeader(key string, a, b http.Header) bool {
if _, exists := b[key]; !exists {
return false
}
for _, v := range a[key] {
if !inList(v, b[key]) {
return false
}
}
return true
}
func inList(needle string, haystack []string) bool {
for _, straw := range haystack {
if straw == needle {
return true
}
}
return false
}
// Stub "T" for use with github.com/stretchr/testify/assert tests
type testifyT struct{}
func (t *testifyT) Errorf(format string, args ...interface{}) {}