-
Notifications
You must be signed in to change notification settings - Fork 14
/
mock.go
165 lines (146 loc) · 4.4 KB
/
mock.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package fastglue
import (
"fmt"
"log"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/valyala/fasthttp"
)
// MockServer is a mock HTTP server. It uses an httptest.Server mock server
// that can take an HTTP request and respond with a mock response.
type MockServer struct {
Server *httptest.Server
handles map[string]MockResponse
}
// MockResponse represents a mock response produced by the mock server.
type MockResponse struct {
StatusCode int
ContentType string
Body []byte
}
// MockRequest represents a single mock request.
type MockRequest struct {
server *MockServer
req *Request
assert *assert.Assertions
}
// NewMockServer initializes a mock HTTP server against which any request be sent,
// and the request can be responded to with a mock response.
func NewMockServer() *MockServer {
m := &MockServer{
handles: make(map[string]MockResponse),
}
s := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Check if the URI is registered.
if _, ok := m.handles[r.RequestURI]; !ok {
w.WriteHeader(http.StatusNotFound)
logerr(w.Write([]byte("not found")))
return
}
// Check if the method+URI is registered.
out, ok := m.handles[r.Method+r.RequestURI]
if !ok {
w.WriteHeader(http.StatusMethodNotAllowed)
logerr(w.Write([]byte("method not allowed")))
return
}
// Write the status code.
if out.StatusCode == 0 {
w.WriteHeader(200)
} else {
w.WriteHeader(out.StatusCode)
}
if out.ContentType != "" {
w.Header().Set("Content-Type", out.ContentType)
}
if len(out.Body) > 0 {
logerr(w.Write(out.Body))
}
}),
)
m.Server = s
return m
}
// Handle registers a mock response handler.
func (m *MockServer) Handle(method, uri string, r MockResponse) {
key := method + uri
_, ok := m.handles[key]
if ok {
panic(fmt.Sprintf("handle already registered: %v:%v", method, uri))
}
m.handles[key] = r
m.handles[uri] = r
}
// Reset resets existing registered mock response handlers.
func (m *MockServer) Reset() {
m.handles = make(map[string]MockResponse)
}
// URL returns the URL of the mock server that can be used as the mock
// upstream server.
func (m *MockServer) URL() string {
return m.Server.URL
}
// NewFastglueReq returns an empty fastglue.Request that can be filled
// and used to pass to actual fastglue handlers to mock incoming HTTP requests.
func (m *MockServer) NewFastglueReq() *Request {
return &Request{
RequestCtx: &fasthttp.RequestCtx{Request: *fasthttp.AcquireRequest()},
}
}
// Do returns a new request handler with which a mock request is made.
// It takes an HTTP handler and executes it against the given request.
// The assert.Assertions is optional.
//
// Since there's no real HTTP server+handler that originates the request to the
// handler, an artificial request (req) context has to be provided.
//
// upstreamResp is the body with which the mock server should respond to the
// request.
//
// Example:
// req := &fastglue.Request{
// RequestCtx: &fasthttp.RequestCtx{Request: *fasthttp.AcquireRequest()},
// Context: app,
// }
// req.RequestCtx.Request.SetRequestURI("/fake/path/to/simulate")
// req.RequestCtx.SetUserValue("user", authUser{
// UserID: testUser,
// AppID: 1,
// })
//
func (m *MockServer) Do(h FastRequestHandler, req *Request, t *testing.T) *MockRequest {
mr := &MockRequest{
req: req,
server: m,
assert: assert.New(t),
}
mr.assert.NoError(h(req), "error executing mock request")
return mr
}
// GetReq returns the underlying fastglue.Request that's set on the MockRequest.
func (mr *MockRequest) GetReq() *Request {
return mr.req
}
// AssertStatus asserts the response code of the request against the given code.
func (mr *MockRequest) AssertStatus(code int) {
mr.assert.Equal(code, mr.req.RequestCtx.Response.StatusCode(),
"status code doesn't match")
}
// AssertBody asserts the response body of the request against the given body.
func (mr *MockRequest) AssertBody(body []byte) {
mr.assert.Equal(body, mr.req.RequestCtx.Response.Body(),
"response body doesn't match")
}
// AssertJSON asserts the JSON response of the body of the request against the given body.
func (mr *MockRequest) AssertJSON(body []byte) {
mr.assert.JSONEq(string(body), string(mr.req.RequestCtx.Response.Body()),
"response body doesn't match")
}
func logerr(n int, err error) {
if err != nil {
log.Printf("Write failed: %v", err)
}
}