-
Notifications
You must be signed in to change notification settings - Fork 16
/
request_test.go
187 lines (146 loc) · 4.31 KB
/
request_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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package ngamux
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/golang-must/must"
)
func TestReq(t *testing.T) {
must := must.New(t)
req := httptest.NewRequest(http.MethodGet, "/", nil)
expected := &Request{req}
result := Req(req)
must.NotNil(result)
must.Equal(expected, result)
}
func TestParams(t *testing.T) {
must := must.New(t)
req := httptest.NewRequest(http.MethodGet, "/", nil)
req = req.WithContext(context.WithValue(req.Context(), KeyContextParams, [][]string{{"id", "1"}}))
result := Req(req).Params("id")
must.Equal("1", result)
result = Req(req).Params("slug")
must.Equal("", result)
}
func TestQuery(t *testing.T) {
must := must.New(t)
req := Req(httptest.NewRequest(http.MethodGet, "/?id=1", nil))
result := req.Query("id")
must.Equal("1", result)
result = req.Query("slug", "undefined")
must.Equal("undefined", result)
result = req.Query("slug")
must.Equal("", result)
}
func TestJSON(t *testing.T) {
t.Run("positive", func(t *testing.T) {
must := must.New(t)
input := strings.NewReader(`{"id": 1}`)
req := Req(httptest.NewRequest(http.MethodGet, "/?id=1", input))
var data map[string]any
err := req.JSON(&data)
must.Nil(err)
must.NotNil(data["id"])
id, ok := data["id"].(float64)
must.True(ok)
must.Equal(float64(1), id)
})
t.Run("negative", func(t *testing.T) {
must := must.New(t)
input := strings.NewReader(`id`)
req := Req(httptest.NewRequest(http.MethodGet, "/?id=1", input))
var data map[string]any
err := req.JSON(&data)
must.NotNil(err)
must.Nil(data)
})
}
func TestFormValue(t *testing.T) {
t.Run("can supply default value", func(t *testing.T) {
must := must.New(t)
req := Req(httptest.NewRequest(http.MethodPost, "/", strings.NewReader("")))
result := req.FormValue("idontknow", "1")
must.Equal("1", result)
})
}
type Key string
const KeyID Key = "id"
func TestLocals(t *testing.T) {
must := must.New(t)
req := Req(httptest.NewRequest(http.MethodGet, "/", nil))
req.Locals(string(KeyID), 1)
id := req.Locals(string(KeyID))
must.Equal(1, id)
slug := req.Locals("slug")
must.Nil(slug)
}
func TestIsLocalhost(t *testing.T) {
must := must.New(t)
req := httptest.NewRequest(http.MethodGet, "/", nil)
result := Req(req).IsLocalhost()
must.False(result)
req.Host = "localhost"
result = Req(req).IsLocalhost()
must.True(result)
}
func TestGetIpAddress(t *testing.T) {
must := must.New(t)
req := httptest.NewRequest(http.MethodGet, "/", nil)
// Inject IP Local
req.RemoteAddr = "127.0.0.1"
result := Req(req).GetIPAdress()
must.Equal("127.0.0.1", result)
}
type queryStruct struct {
Foo string `query:"foo"`
Bar string `query:"bar"`
}
type queryStructWithNested struct {
FirstName string `query:"first_name"`
LastName string `query:"last_name"`
Age int `query:"age"`
Wallet float64 `query:"wallet"`
IsHuman bool `query:"is_human"`
Address struct {
City string `query:"city"`
Country string `query:"country"`
} `query:"address"`
}
type queryStructNotValid struct {
FieldNotValid interface{} `query:"field_not_valid"` // <- dont use interface{} to data types
}
func TestQueriesParser(t *testing.T) {
t.Run("parse query url to struct", func(t *testing.T) {
must := must.New(t)
req := httptest.NewRequest(http.MethodGet, "/q?foo=foo&bar=bar", nil)
var qs queryStruct
err := Req(req).QueriesParser(&qs)
must.Nil(err)
must.Equal(qs.Foo, "foo")
must.Equal(qs.Bar, "bar")
})
t.Run("parse query url to nested struct", func(t *testing.T) {
must := must.New(t)
req := httptest.NewRequest(http.MethodGet, "/q?age=19&is_human=true&wallet=69.8&first_name=farda&last_name=nurfatika&address=city%3Dsemarang%26country%3Dindonesia", nil)
var qswn queryStructWithNested
err := Req(req).QueriesParser(&qswn)
must.Nil(err)
must.Equal(qswn.FirstName, "farda")
must.Equal(qswn.LastName, "nurfatika")
must.Equal(qswn.Age, 19)
must.Equal(qswn.IsHuman, true)
must.Equal(qswn.Wallet, 69.8)
must.Equal(qswn.Address.City, "semarang")
must.Equal(qswn.Address.Country, "indonesia")
})
t.Run("parse query to struct error", func(t *testing.T) {
must := must.New(t)
req := httptest.NewRequest(http.MethodGet, "/q?field_not_valid=xixixixi", nil)
var qs queryStructNotValid
err := Req(req).QueriesParser(&qs)
must.NotNil(err)
must.Equal(qs.FieldNotValid, nil)
})
}