Skip to content

Commit 272e865

Browse files
authored
Merge pull request #32 from appleboy/query
Support set query string.
2 parents 94499e3 + 73b7028 commit 272e865

File tree

7 files changed

+89
-49
lines changed

7 files changed

+89
-49
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ install:
88
glide install
99

1010
update:
11-
glide update --all-dependencies --resolve-current
11+
glide up
1212

1313
example:
1414
cd example && go test -v -cover -covermode=count -coverprofile=coverage.txt .

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,42 @@ func TestPostRawData(t *testing.T) {
177177
}
178178
```
179179

180+
### Set Query String
181+
182+
Using `SetQuery` to generate raw data.
183+
184+
```go
185+
func TestQueryString(t *testing.T) {
186+
r := gofight.New()
187+
188+
r.GET("/hello").
189+
SetQUERY(gofight.H{
190+
"a": "1",
191+
"b": "2",
192+
}).
193+
Run(BasicEngine, func(r HTTPResponse, rq HTTPRequest) {
194+
assert.Equal(t, http.StatusOK, r.Code)
195+
})
196+
}
197+
```
198+
199+
or append exist query parameter.
200+
201+
```go
202+
func TestQueryString(t *testing.T) {
203+
r := gofight.New()
204+
205+
r.GET("/hello?foo=bar").
206+
SetQUERY(gofight.H{
207+
"a": "1",
208+
"b": "2",
209+
}).
210+
Run(BasicEngine, func(r HTTPResponse, rq HTTPRequest) {
211+
assert.Equal(t, http.StatusOK, r.Code)
212+
})
213+
}
214+
```
215+
180216
## Example
181217

182218
* Basic HTTP Router: [basic.go](example/basic.go), [basic_test.go](example/basic_test.go)

example/basic.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ func basicHelloHandler(w http.ResponseWriter, r *http.Request) {
99
io.WriteString(w, "Hello World")
1010
}
1111

12-
func basicHttpHelloHandler() {
12+
func basicHTTPHelloHandler() {
1313
http.HandleFunc("/hello", basicHelloHandler)
1414
}
1515

example/basic_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestBasicHelloWorld(t *testing.T) {
2626
}
2727

2828
func TestBasicHttpHelloWorld(t *testing.T) {
29-
basicHttpHelloHandler()
29+
basicHTTPHelloHandler()
3030

3131
r := gofight.New()
3232

glide.lock

Lines changed: 6 additions & 46 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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)