This repository has been archived by the owner on Jan 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
124 lines (106 loc) · 2.89 KB
/
server.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
114
115
116
117
118
119
120
121
122
123
124
package net
import (
"fmt"
mr "github.com/tjarratt/mr_t"
"net/http"
"net/http/httptest"
"strings"
)
type TestRequest struct {
Method string
Path string
Header http.Header
Matcher RequestMatcher
Response TestResponse
}
type RequestMatcher func(mr.TestingT, *http.Request)
type TestResponse struct {
Body string
Status int
Header http.Header
}
type TestHandler struct {
Requests []TestRequest
CallCount int
T mr.TestingT
}
func (h *TestHandler) AllRequestsCalled() bool {
if h.CallCount == len(h.Requests) {
return true
}
fmt.Print("Failed to call requests:\n")
for i := h.CallCount; i < len(h.Requests); i++ {
fmt.Printf("%#v\n", h.Requests[i])
}
fmt.Print("\n\n")
return false
}
func (h *TestHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if len(h.Requests) <= h.CallCount {
h.logError("Index out of range! Test server called too many times. Final Request:", r.Method, r.RequestURI)
return
}
tester := h.Requests[h.CallCount]
h.CallCount++
// match method
if tester.Method != r.Method {
h.logError("Method does not match.\nExpected: %s\nActual: %s", tester.Method, r.Method)
}
// match path
paths := strings.Split(tester.Path, "?")
if paths[0] != r.URL.Path {
h.logError("Path does not match.\nExpected: %s\nActual: %s", paths[0], r.URL.Path)
}
// match query string
if len(paths) > 1 {
if !strings.Contains(r.URL.RawQuery, paths[1]) {
h.logError("Query string does not match.\nExpected: %s\nActual: %s", paths[1], r.URL.RawQuery)
}
}
for key, values := range tester.Header {
key = http.CanonicalHeaderKey(key)
actualValues := strings.Join(r.Header[key], ";")
expectedValues := strings.Join(values, ";")
if key == "Authorization" && !strings.Contains(actualValues, expectedValues) {
h.logError("%s header is not contained in actual value.\nExpected: %s\nActual: %s", key, expectedValues, actualValues)
}
if key != "Authorization" && actualValues != expectedValues {
h.logError("%s header did not match.\nExpected: %s\nActual: %s", key, expectedValues, actualValues)
}
}
// match custom request matcher
if tester.Matcher != nil {
tester.Matcher(h.T, r)
}
// set response headers
header := w.Header()
for name, values := range tester.Response.Header {
if len(values) < 1 {
continue
}
header.Set(name, values[0])
}
// write response
w.WriteHeader(tester.Response.Status)
fmt.Fprintln(w, tester.Response.Body)
}
func NewServer(t mr.TestingT, requests []TestRequest) (s *httptest.Server, h *TestHandler) {
h = &TestHandler{
Requests: requests,
T: t,
}
s = httptest.NewServer(h)
return
}
func NewTLSServer(t mr.TestingT, requests []TestRequest) (s *httptest.Server, h *TestHandler) {
h = &TestHandler{
Requests: requests,
T: t,
}
s = httptest.NewTLSServer(h)
return
}
func (h *TestHandler) logError(msg string, args ...interface{}) {
h.T.Logf(msg, args...)
h.T.Fail()
}