-
-
Notifications
You must be signed in to change notification settings - Fork 936
/
http.go
84 lines (65 loc) · 2.12 KB
/
http.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
package testhelpers
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/url"
"testing"
"github.com/stretchr/testify/require"
)
func NewDebugClient(t *testing.T) *http.Client {
return &http.Client{Transport: NewTransportWithLogger(http.DefaultTransport, t)}
}
func NewRequest(t *testing.T, isAPI bool, method string, url string, payload io.Reader) *http.Request {
req, err := http.NewRequest("POST", url, payload)
require.NoError(t, err)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Accept", "text/html")
if isAPI {
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
}
return req
}
func NewHTTPGetJSONRequest(t *testing.T, url string) *http.Request {
req, err := http.NewRequest("GET", url, nil)
require.NoError(t, err)
// req.Header.Set("Content-Type", "application/json;charset=utf-8")
req.Header.Set("Accept", "application/json")
return req
}
func NewHTTPDeleteJSONRequest(t *testing.T, url string, in interface{}) *http.Request {
var body bytes.Buffer
require.NoError(t, json.NewEncoder(&body).Encode(in))
req, err := http.NewRequest("DELETE", url, &body)
require.NoError(t, err)
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
return req
}
func HTTPRequestJSON(t *testing.T, client *http.Client, method string, url string, in interface{}) ([]byte, *http.Response) {
var body bytes.Buffer
require.NoError(t, json.NewEncoder(&body).Encode(in))
req, err := http.NewRequest(method, url, &body)
require.NoError(t, err)
req.Header.Set("Content-Type", "application/json")
res, err := client.Do(req)
require.NoError(t, err)
defer res.Body.Close()
payload, err := ioutil.ReadAll(res.Body)
require.NoError(t, err)
return payload, res
}
func HTTPPostForm(t *testing.T, client *http.Client, remote string, in *url.Values) ([]byte, *http.Response) {
if in == nil {
in = new(url.Values)
}
res, err := client.PostForm(remote, *in)
require.NoError(t, err)
defer res.Body.Close()
payload, err := ioutil.ReadAll(res.Body)
require.NoError(t, err)
return payload, res
}