-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
http.go
51 lines (42 loc) · 884 Bytes
/
http.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
package testing
import (
"bytes"
"net/http"
)
type MockResponseWriter struct {
//mock.Mock
Bytes []byte
statusCode int
Headers http.Header
}
func (m *MockResponseWriter) Header() http.Header {
return m.Headers
}
func (m *MockResponseWriter) Write(p []byte) (int, error) {
m.Bytes = p
return len(p), nil
}
func (m *MockResponseWriter) WriteHeader(statusCode int) {
m.statusCode = statusCode
}
func (m *MockResponseWriter) StatusCode() int {
return m.statusCode
}
func NewMockResponseWriter() http.ResponseWriter {
return &MockResponseWriter{
Headers: make(http.Header),
}
}
type MockRequest struct {
Request *http.Request
}
func NewMockRequest(method, url, body string) *MockRequest {
buffer := bytes.NewBuffer([]byte(body))
req, err := http.NewRequest(method, url, buffer)
if err != nil {
panic(err)
}
return &MockRequest{
Request: req,
}
}