-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock.go
57 lines (48 loc) · 1.45 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
package web
import (
"bytes"
"encoding/json"
"io"
"net/http"
)
// Parameters for creating a mock request for uses in tests
type MockRequestParameters struct {
// User data to be passed into the handler. May be nil.
UserData interface{}
// URL parameters (not query parameters) to be populated into the request.Params object in the handler. May be nil.
Parameters map[string]string
// Object to be encoded with JSON as the body. May be nil. Exclusive to Body.
JSONBody interface{}
// Body data. May be nil. Exclusive to JSONBody.
Body io.ReadCloser
// Optional HTTP request to pass to the handler.
Request *http.Request
}
// MockRequest will generate a mock request for testing your handlers. Will panic for invalid parameters.
func MockRequest(parameters MockRequestParameters) Request {
var httpRequest *http.Request
if parameters.Request != nil {
httpRequest = parameters.Request
} else {
httpRequest = &http.Request{}
}
httpRequest.RemoteAddr = "[::1]:65535"
if parameters.JSONBody != nil && parameters.Body != nil {
panic("cannot provide both JSON and data body")
}
if parameters.JSONBody != nil {
b := &bytes.Buffer{}
if err := json.NewEncoder(b).Encode(parameters.JSONBody); err != nil {
panic(err)
}
httpRequest.Body = io.NopCloser(b)
}
if parameters.Body != nil {
httpRequest.Body = parameters.Body
}
return Request{
HTTP: httpRequest,
Parameters: parameters.Parameters,
UserData: parameters.UserData,
}
}