-
Notifications
You must be signed in to change notification settings - Fork 1
/
responsewriter.go
46 lines (39 loc) · 1.06 KB
/
responsewriter.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
// Package mock provides mocking utilities used in tests.
package mock
import (
"net/http"
)
// ResponseWriter returns a responseWriter implementing
// http.ResponseWriter.
func ResponseWriter() *responseWriter { //nolint:golint
return &responseWriter{}
}
// responseWriter implements http.ResponseWriter.
type responseWriter struct {
LastBody string
LastStatus int
header http.Header
err error
}
// Err sets the error value returned by the Write method.
// Chain it with construction: mock.ResponseWriter().Err(someErr).
func (r *responseWriter) Err(err error) *responseWriter {
r.err = err
return r
}
// Header returns the responseWriters HTTP header.
func (r *responseWriter) Header() http.Header {
if r.header == nil {
r.header = http.Header{}
}
return r.header
}
// WriteHeader writes HTTP Status code to struct field.
func (r *responseWriter) WriteHeader(status int) {
r.LastStatus = status
}
// Write writes response body to struct field.
func (r *responseWriter) Write(b []byte) (int, error) {
r.LastBody = string(b)
return len(b), r.err
}