-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
61 lines (54 loc) · 1.38 KB
/
main.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
package net
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
)
// HTTPClientInterface helps mocking http.Client in tests
type HTTPClientInterface interface {
PostForm(url string, data url.Values) (resp *http.Response, err error)
Get(url string) (resp *http.Response, err error)
}
// BuildHTTPResponse is used in tests
func BuildHTTPResponse(statusCode int, body string) *http.Response {
return &http.Response{
StatusCode: statusCode,
Body: ioutil.NopCloser(bytes.NewBufferString(body)),
}
}
// GetResponse is used in tests
func GetResponse(testServer *httptest.Server, values url.Values) (int, []byte) {
res, err := http.PostForm(testServer.URL, values)
if err != nil {
panic(err)
}
response, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
panic(err)
}
return res.StatusCode, response
}
// JSONGetResponse is used in tests
func JSONGetResponse(testServer *httptest.Server, data map[string]interface{}) (int, []byte) {
j, err := json.Marshal(data)
if err != nil {
panic(err)
}
req, err := http.NewRequest("POST", testServer.URL, bytes.NewBuffer(j))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
panic(err)
}
response, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
panic(err)
}
return res.StatusCode, response
}