Skip to content

Commit

Permalink
Merge pull request #32 from appleboy/query
Browse files Browse the repository at this point in the history
Support set query string.
  • Loading branch information
appleboy authored Jul 23, 2016
2 parents 94499e3 + 73b7028 commit 272e865
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 49 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ install:
glide install

update:
glide update --all-dependencies --resolve-current
glide up

example:
cd example && go test -v -cover -covermode=count -coverprofile=coverage.txt .
Expand Down
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,42 @@ func TestPostRawData(t *testing.T) {
}
```

### Set Query String

Using `SetQuery` to generate raw data.

```go
func TestQueryString(t *testing.T) {
r := gofight.New()

r.GET("/hello").
SetQUERY(gofight.H{
"a": "1",
"b": "2",
}).
Run(BasicEngine, func(r HTTPResponse, rq HTTPRequest) {
assert.Equal(t, http.StatusOK, r.Code)
})
}
```

or append exist query parameter.

```go
func TestQueryString(t *testing.T) {
r := gofight.New()

r.GET("/hello?foo=bar").
SetQUERY(gofight.H{
"a": "1",
"b": "2",
}).
Run(BasicEngine, func(r HTTPResponse, rq HTTPRequest) {
assert.Equal(t, http.StatusOK, r.Code)
})
}
```

## Example

* Basic HTTP Router: [basic.go](example/basic.go), [basic_test.go](example/basic_test.go)
Expand Down
2 changes: 1 addition & 1 deletion example/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func basicHelloHandler(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Hello World")
}

func basicHttpHelloHandler() {
func basicHTTPHelloHandler() {
http.HandleFunc("/hello", basicHelloHandler)
}

Expand Down
2 changes: 1 addition & 1 deletion example/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestBasicHelloWorld(t *testing.T) {
}

func TestBasicHttpHelloWorld(t *testing.T) {
basicHttpHelloHandler()
basicHTTPHelloHandler()

r := gofight.New()

Expand Down
52 changes: 6 additions & 46 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions gofight.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
// "X-Version": version,
// })
//
// Set query string: Using SetQUERY to generate query string data.
//
// SetQUERY(gofight.H{
// "a": "1",
// "b": "2",
// })
//
// POST FORM Data: Using SetFORM to generate form data.
//
// SetFORM(gofight.H{
Expand Down Expand Up @@ -212,6 +219,23 @@ func (rc *RequestConfig) SetFORM(body H) *RequestConfig {
return rc
}

// SetQUERY supply query string.
func (rc *RequestConfig) SetQUERY(query H) *RequestConfig {
f := make(url.Values)

for k, v := range query {
f.Set(k, v)
}

if strings.Contains(rc.Path, "?") {
rc.Path = rc.Path + "&" + f.Encode()
} else {
rc.Path = rc.Path + "?" + f.Encode()
}

return rc
}

// SetBody supply raw body.
func (rc *RequestConfig) SetBody(body string) *RequestConfig {
if len(body) > 0 {
Expand Down
20 changes: 20 additions & 0 deletions gofight_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,23 @@ func TestEchoOptions(t *testing.T) {
assert.Equal(t, http.StatusOK, r.Status())
})
}

func TestSetQueryString(t *testing.T) {
r := New()

r.GET("/hello").
SetQUERY(H{
"a": "1",
"b": "2",
})

assert.Equal(t, "/hello?a=1&b=2", r.Path)

r.GET("/hello?foo=bar").
SetQUERY(H{
"a": "1",
"b": "2",
})

assert.Equal(t, "/hello?foo=bar&a=1&b=2", r.Path)
}

0 comments on commit 272e865

Please sign in to comment.