-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
55 lines (44 loc) · 909 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
package apptest
import (
"io"
"net/http"
"net/http/httptest"
"sync"
)
var RedirectCodes = []int{
http.StatusMovedPermanently,
http.StatusFound,
http.StatusSeeOther,
http.StatusTemporaryRedirect,
http.StatusPermanentRedirect,
}
type Response struct {
RawResponse *httptest.ResponseRecorder
bodyCache []byte
mu sync.Mutex
}
func (r *Response) Code() int {
return r.RawResponse.Code
}
func (r *Response) IsRedirect() bool {
for _, redirectCode := range RedirectCodes {
if r.Code() == redirectCode {
return true
}
}
return false
}
func (r *Response) IsOK() bool {
return r.Code() == http.StatusOK
}
func (r *Response) Body() string {
r.mu.Lock()
defer r.mu.Unlock()
if r.bodyCache == nil {
r.bodyCache, _ = io.ReadAll(r.RawResponse.Result().Body)
}
return string(r.bodyCache)
}
func (r *Response) Header() http.Header {
return r.RawResponse.Result().Header
}