-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
73 lines (62 loc) · 1.58 KB
/
response.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
package batch
import (
"encoding/json"
"net/http"
)
// Response is a batch response.
type Response struct {
Responses []ResponsePart `json:"responses"`
}
// ResponsePart contains sub-response information of a batch response.
type ResponsePart struct {
Status int `json:"status"`
Path string `json:"path"`
Body json.RawMessage `json:"body,omitempty"`
Headers map[string]string `json:"headers,omitempty"`
}
// ResponseRecorder records responses.
type ResponseRecorder struct {
code int
header http.Header
offset int
body []byte
}
// Reset resets the recorder.
func (w *ResponseRecorder) Reset() {
*w = ResponseRecorder{body: w.body[:0]}
}
// WriteHeader implements http.ResponseWriter interface.
func (w *ResponseRecorder) WriteHeader(code int) {
w.code = code
w.offset = len(w.body)
}
// Header implements http.ResponseWriter interface.
func (w *ResponseRecorder) Header() http.Header {
if w.header == nil {
w.header = make(http.Header)
}
return w.header
}
// Write implements http.ResponseWriter interface.
func (w *ResponseRecorder) Write(p []byte) (int, error) {
if w.code == 0 {
w.WriteHeader(http.StatusOK)
}
w.body = append(w.body, p...)
return len(p), nil
}
func (w *ResponseRecorder) appendTo(r *Response, path string) {
code := w.code
w.code = 0
headers := make(map[string]string, len(w.header))
for key := range w.header {
headers[key] = w.header.Get(key)
delete(w.header, key)
}
r.Responses = append(r.Responses, ResponsePart{
Status: code,
Path: path,
Body: w.body[w.offset:len(w.body)],
Headers: headers,
})
}