Skip to content

Commit e1f9223

Browse files
committed
Support set query string.
Signed-off-by: Bo-Yi Wu <[email protected]>
1 parent 94499e3 commit e1f9223

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

gofight.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
// "X-Version": version,
1515
// })
1616
//
17+
// Set query string: Using SetQUERY to generate query string data.
18+
//
19+
// SetQUERY(gofight.H{
20+
// "a": "1",
21+
// "b": "2",
22+
// })
23+
//
1724
// POST FORM Data: Using SetFORM to generate form data.
1825
//
1926
// SetFORM(gofight.H{
@@ -212,6 +219,23 @@ func (rc *RequestConfig) SetFORM(body H) *RequestConfig {
212219
return rc
213220
}
214221

222+
// SetQUERY supply query string.
223+
func (rc *RequestConfig) SetQUERY(query H) *RequestConfig {
224+
f := make(url.Values)
225+
226+
for k, v := range query {
227+
f.Set(k, v)
228+
}
229+
230+
if strings.Contains(rc.Path, "?") {
231+
rc.Path = rc.Path + "&" + f.Encode()
232+
} else {
233+
rc.Path = rc.Path + "?" + f.Encode()
234+
}
235+
236+
return rc
237+
}
238+
215239
// SetBody supply raw body.
216240
func (rc *RequestConfig) SetBody(body string) *RequestConfig {
217241
if len(body) > 0 {

gofight_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,3 +380,23 @@ func TestEchoOptions(t *testing.T) {
380380
assert.Equal(t, http.StatusOK, r.Status())
381381
})
382382
}
383+
384+
func TestSetQueryString(t *testing.T) {
385+
r := New()
386+
387+
r.GET("/hello").
388+
SetQUERY(H{
389+
"a": "1",
390+
"b": "2",
391+
})
392+
393+
assert.Equal(t, "/hello?a=1&b=2", r.Path)
394+
395+
r.GET("/hello?foo=bar").
396+
SetQUERY(H{
397+
"a": "1",
398+
"b": "2",
399+
})
400+
401+
assert.Equal(t, "/hello?foo=bar&a=1&b=2", r.Path)
402+
}

0 commit comments

Comments
 (0)