-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathgohttp_test.go
166 lines (133 loc) · 4.42 KB
/
gohttp_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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package gohttp_test
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/cizixs/gohttp"
)
func TestGet(t *testing.T) {
assert := assert.New(t)
greeting := "hello, gohttp."
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, greeting)
}))
defer ts.Close()
resp, err := gohttp.Get(ts.URL)
assert.NoError(err, "A get request should cause no error.")
assert.Equal(http.StatusOK, resp.StatusCode)
actualGreeting, err := ioutil.ReadAll(resp.Body)
assert.Equal(greeting, string(actualGreeting))
}
func TestGetWithPath(t *testing.T) {
assert := assert.New(t)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, r.URL.Path)
}))
defer ts.Close()
resp, err := gohttp.New().Path("/users").Path("cizixs").Get(ts.URL)
assert.NoError(err, "Get request with path should cause no error.")
data, _ := ioutil.ReadAll(resp.Body)
assert.Equal("/users/cizixs", string(data))
}
func TestGetWithQuery(t *testing.T) {
assert := assert.New(t)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, r.URL.RawQuery)
}))
defer ts.Close()
resp, err := gohttp.New().Query("foo", "bar").Get(ts.URL)
assert.NoError(err, "Get request with query string should cause no error.")
data, _ := ioutil.ReadAll(resp.Body)
assert.Equal("foo=bar", string(data))
resp, err = gohttp.New().Query("foo", "bar").Query("name", "cizixs").Get(ts.URL)
assert.NoError(err, "Get request with query string should cause no error.")
data, _ = ioutil.ReadAll(resp.Body)
assert.Equal("foo=bar&name=cizixs", string(data))
}
func TestGetWithQueryStruct(t *testing.T) {
assert := assert.New(t)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, r.URL.RawQuery)
}))
defer ts.Close()
options := struct {
Query string `url:"q"`
ShowAll bool `url:"all"`
Page int `url:"page"`
}{"foo", true, 2}
resp, err := gohttp.New().QueryStruct(options).Get(ts.URL)
assert.NoError(err, "Get request with query string should cause no error.")
data, _ := ioutil.ReadAll(resp.Body)
assert.Equal("all=true&page=2&q=foo", string(data))
}
func TestGetWithHeader(t *testing.T) {
assert := assert.New(t)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Header.Write(w)
}))
defer ts.Close()
userAgent := "gohttp client by cizixs"
resp, err := gohttp.New().Header("User-Agent", userAgent).Get(ts.URL)
assert.NoError(err, "Get request with header should cause no error.")
data, _ := ioutil.ReadAll(resp.Body)
assert.True(strings.Contains(string(data), "User-Agent"))
assert.True(strings.Contains(string(data), userAgent))
}
func TestPostForm(t *testing.T) {
assert := assert.New(t)
type Login struct {
Name string `json:"name,omitempty"`
Password string `json:"password,omitempty"`
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
data, _ := ioutil.ReadAll(r.Body)
if r.Header.Get("Content-Type") != "application/x-www-form-urlencoded" {
w.Write([]byte("No form data"))
} else {
fmt.Fprint(w, string(data))
}
}))
defer ts.Close()
user := Login{
Name: "cizixs",
Password: "test1234",
}
resp, err := gohttp.New().Form(user).Post(ts.URL)
assert.NoError(err, "Post request should cause no error.")
data, _ := ioutil.ReadAll(resp.Body)
assert.Equal("Name=cizixs&Password=test1234", string(data))
}
func TestPostJSON(t *testing.T) {
assert := assert.New(t)
type User struct {
Title string `json:"title,omitempty"`
Name string `json:"name,omitempty"`
Age int `json:"age,omitempty"`
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
data, _ := ioutil.ReadAll(r.Body)
if r.Header.Get("Content-Type") != "application/json" {
w.Write([]byte("No json data"))
} else {
fmt.Fprint(w, string(data))
}
}))
defer ts.Close()
user := User{
Title: "Test title",
Name: "cizixs",
}
resp, err := gohttp.New().JSON(user).Post(ts.URL)
assert.NoError(err, "Post request should cause no error.")
data, _ := ioutil.ReadAll(resp.Body)
returnedUser := User{}
json.Unmarshal(data, &returnedUser)
assert.Equal("Test title", returnedUser.Title)
assert.Equal("cizixs", returnedUser.Name)
assert.Equal(0, returnedUser.Age)
}