-
Notifications
You must be signed in to change notification settings - Fork 7
/
http.go
46 lines (41 loc) · 979 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
package mock
import (
"bytes"
"context"
"io"
"io/ioutil"
"net/http"
"net/url"
)
// HTTPRequest encapsulates an HTTP request.
type HTTPRequest struct {
IsTLS bool
Method string
URL *url.URL
Header http.Header
Body io.Reader
Form url.Values
Context context.Context
Flusher http.Flusher
}
// PeekBody will return the full body and swap the reader with a
// new one which will allow other users to continue to use it.
func (r *HTTPRequest) PeekBody() []byte {
data, _ := ioutil.ReadAll(r.Body)
r.Body = bytes.NewReader(data)
return data
}
// HTTPResponse encapsulates an HTTP response.
type HTTPResponse struct {
StatusCode int
Header http.Header
Body io.Reader
Streaming bool
}
// PeekBody will return the full body and swap the reader with a
// new one which will allow other users to continue to use it.
func (r *HTTPResponse) PeekBody() []byte {
data, _ := ioutil.ReadAll(r.Body)
r.Body = bytes.NewReader(data)
return data
}