-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmethods_test.go
94 lines (84 loc) · 2.13 KB
/
methods_test.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
package qst_test
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/broothie/qst"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMethods(t *testing.T) {
type testCase struct {
new func(url string, options ...qst.Option) (*http.Request, error)
do func(url string, options ...qst.Option) (*http.Response, error)
clientDo func(options ...qst.Option) (*http.Response, error)
}
client := http.DefaultClient
var methods = map[string]testCase{
http.MethodGet: {
new: qst.NewGet,
do: qst.Get,
clientDo: qst.NewClient(client).Get,
},
http.MethodHead: {
new: qst.NewHead,
do: qst.Head,
clientDo: qst.NewClient(client).Head,
},
http.MethodPost: {
new: qst.NewPost,
do: qst.Post,
clientDo: qst.NewClient(client).Post,
},
http.MethodPut: {
new: qst.NewPut,
do: qst.Put,
clientDo: qst.NewClient(client).Put,
},
http.MethodPatch: {
new: qst.NewPatch,
do: qst.Patch,
clientDo: qst.NewClient(client).Patch,
},
http.MethodDelete: {
new: qst.NewDelete,
do: qst.Delete,
clientDo: qst.NewClient(client).Delete,
},
http.MethodConnect: {
new: qst.NewConnect,
do: qst.Connect,
clientDo: qst.NewClient(client).Connect,
},
http.MethodOptions: {
new: qst.NewOptions,
do: qst.Options,
clientDo: qst.NewClient(client).Options,
},
http.MethodTrace: {
new: qst.NewTrace,
do: qst.Trace,
clientDo: qst.NewClient(client).Trace,
},
}
for method, tc := range methods {
server := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
assert.Equal(t, method, r.Method)
}))
t.Run("new", func(t *testing.T) {
req, err := tc.new(server.URL)
assert.NoError(t, err)
_, err = http.DefaultClient.Do(req)
require.NoError(t, err)
})
t.Run("do", func(t *testing.T) {
_, err := tc.do(server.URL)
require.NoError(t, err)
})
t.Run("clientDo", func(t *testing.T) {
_, err := tc.clientDo(qst.URL(server.URL))
require.NoError(t, err)
})
server.Close()
}
}