-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapi_testing.go
107 lines (98 loc) · 3.61 KB
/
api_testing.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package forest
import (
"net/http"
)
// APITesting provides functions to call a REST api and validate its responses.
type APITesting struct {
BaseURL string
client *http.Client
}
// NewClient returns a new ApiTesting that can be used to send Http requests.
func NewClient(baseURL string, httpClient *http.Client) *APITesting {
return &APITesting{
BaseURL: baseURL,
client: httpClient,
}
}
// GET sends a Http request using a config (headers,...)
// The request is logged and any sending error will fail the test.
func (a *APITesting) GET(t T, config *RequestConfig) *http.Response {
t.Helper()
httpReq, err := config.Build(http.MethodGet, a.BaseURL)
if err != nil {
logfatal(t, sfatalf("GET: invalid Url:%s", a.BaseURL+config.pathAndQuery()))
}
if config.logRequestLine {
Logf(t, "\n%v %v %v", httpReq.Method, httpReq.URL, headersString(httpReq.Header))
}
resp, err := a.client.Do(httpReq)
CheckError(t, err)
return ensureResponse(httpReq, resp)
}
// POST sends a Http request using a config (headers,body,...)
// The request is logged and any sending error will fail the test.
func (a *APITesting) POST(t T, config *RequestConfig) *http.Response {
t.Helper()
httpReq, err := config.Build(http.MethodPost, a.BaseURL)
if err != nil {
logfatal(t, sfatalf("POST: invalid Url:%s", a.BaseURL+config.pathAndQuery()))
}
if config.logRequestLine {
Logf(t, "\n%v %v %v", httpReq.Method, httpReq.URL, headersString(httpReq.Header))
}
resp, err := a.client.Do(httpReq)
CheckError(t, err)
return ensureResponse(httpReq, resp)
}
// PUT sends a Http request using a config (headers,body,...)
// The request is logged and any sending error will fail the test.
func (a *APITesting) PUT(t T, config *RequestConfig) *http.Response {
httpReq, err := config.Build(http.MethodPut, a.BaseURL)
if err != nil {
logfatal(t, sfatalf("PUT: invalid Url:%s", a.BaseURL+config.pathAndQuery()))
}
if config.logRequestLine {
Logf(t, "\n%v %v %v", httpReq.Method, httpReq.URL, headersString(httpReq.Header))
}
resp, err := a.client.Do(httpReq)
CheckError(t, err)
return ensureResponse(httpReq, resp)
}
// DELETE sends a Http request using a config (headers,...)
// The request is logged and any sending error will fail the test.
func (a *APITesting) DELETE(t T, config *RequestConfig) *http.Response {
httpReq, err := config.Build(http.MethodDelete, a.BaseURL)
if err != nil {
logfatal(t, sfatalf("DELETE: invalid Url:%s", a.BaseURL+config.pathAndQuery()))
}
if config.logRequestLine {
Logf(t, "\n%v %v %v", httpReq.Method, httpReq.URL, headersString(httpReq.Header))
}
resp, err := a.client.Do(httpReq)
CheckError(t, err)
return ensureResponse(httpReq, resp)
}
// PATCH sends a Http request using a config (headers,...)
// The request is logged and any sending error will fail the test.
func (a *APITesting) PATCH(t T, config *RequestConfig) *http.Response {
httpReq, err := config.Build(http.MethodPatch, a.BaseURL)
if err != nil {
logfatal(t, sfatalf("PATCH: invalid Url:%s", a.BaseURL+config.pathAndQuery()))
}
if config.logRequestLine {
Logf(t, "\n%v %v %v", httpReq.Method, httpReq.URL, headersString(httpReq.Header))
}
resp, err := a.client.Do(httpReq)
CheckError(t, err)
return ensureResponse(httpReq, resp)
}
// Do sends a Http request using a Http method (GET,PUT,POST,....) and config (headers,...)
// The request is not logged and any URL build error or send error will be returned.
func (a *APITesting) Do(method string, config *RequestConfig) (*http.Response, error) {
httpReq, err := config.Build(method, a.BaseURL)
if err != nil {
return nil, err
}
resp, err := a.client.Do(httpReq)
return ensureResponse(httpReq, resp), err
}