-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.go
More file actions
66 lines (58 loc) · 1.26 KB
/
request.go
File metadata and controls
66 lines (58 loc) · 1.26 KB
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
package httpmatter
import (
"bytes"
"io"
"net/http"
"net/http/httputil"
)
// RequestMatter is a matter that can be used to store request content and error
type RequestMatter struct {
*Matter
*http.Request
}
func NewRequestMatter(namespace, name string) *RequestMatter {
return &RequestMatter{
Matter: NewMatter(namespace, name),
}
}
func (rm *RequestMatter) Parse() error {
content, err := rm.parse()
if err != nil {
return err
}
req, err := ParseRequest(content)
if err != nil {
return err
}
rm.Request = req
return nil
}
func (rm *RequestMatter) BodyString() (string, error) {
body, err := rm.BodyBytes()
if err != nil {
return "", err
}
return string(body), nil
}
func (rm *RequestMatter) BodyBytes() ([]byte, error) {
body, err := io.ReadAll(rm.Body)
if err != nil {
return nil, err
}
// Preserve existing behavior (return the bytes) while also keeping the request
// sendable after inspection by resetting the body reader.
rm.Body = io.NopCloser(bytes.NewReader(body))
return body, nil
}
func (rm *RequestMatter) Dump(req *http.Request) error {
b, err := httputil.DumpRequest(req, true)
if err != nil {
return err
}
rm.content = string(b)
rm.Request = req
return nil
}
func (rm *RequestMatter) Save() error {
return rm.Matter.Save()
}