/
response.go
57 lines (44 loc) · 999 Bytes
/
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
package easy
import (
"bytes"
"encoding/json"
"net/http"
"testing"
"github.com/stretchr/testify/require"
)
type Response struct {
Resp *http.Response
}
func NewResponse(resp *http.Response) *Response {
return &Response{
Resp: resp,
}
}
func (c *Response) Ok(t *testing.T) {
c.Status(t, 200)
}
func (c *Response) Status(t *testing.T, status int) {
require.Equal(t, c.Resp.StatusCode, status)
}
func (c *Response) Body() *bytes.Buffer {
buf := new(bytes.Buffer)
buf.ReadFrom(c.Resp.Body)
return buf
}
func (c *Response) EqBody(t *testing.T, body string) {
require.Equal(t, body, c.Body().String())
}
func (c *Response) EqJson(t *testing.T, obj any) {
bytes, err := json.Marshal(obj)
require.NoError(t, err)
require.Equal(t, c.Body().Bytes(), bytes)
}
// Prase json body
func (c *Response) Json(v any) error {
data := c.Body().Bytes()
return json.Unmarshal(data, v)
}
// Returns set-cookie's
func (c *Response) SetCookies() []*http.Cookie {
return c.Resp.Cookies()
}