Skip to content

Commit

Permalink
Support form body and plain body
Browse files Browse the repository at this point in the history
  • Loading branch information
cizixs committed Nov 13, 2016
1 parent f6691fc commit 25cdd01
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 6 deletions.
51 changes: 46 additions & 5 deletions gohttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"net/http"
"path/filepath"
"strings"

"github.com/google/go-querystring/query"
)
Expand Down Expand Up @@ -181,14 +182,54 @@ func (jbd jsonBodyData) Body() (io.Reader, error) {
return buf, nil
}

// JSON sets data in body, and send it as application/json
// JSON accepts a struct as data, and sets it as body, and send it as application/json
// If the actual method does not support body or json data, such as `GET`, `HEAD`,
// it will be simply omitted.
func (c *Client) JSON(bodyJSON interface{}) *Client {
c.Header(contentType, jsonContentType)
//TODO: how to handle error
body, _ := jsonBodyData{payload: bodyJSON}.Body()
c.body = body
if bodyJSON != nil {
c.Header(contentType, jsonContentType)
//TODO: how to handle error
body, _ := jsonBodyData{payload: bodyJSON}.Body()
c.body = body
}
return c
}

type formBodyData struct {
payload interface{}
}

// Body returns io.Reader from form data.
// Form data is a collection of many key-value pairs,
// so we use go-querystring to parse it to string, then
// create a io.Reader interface.
func (fbd formBodyData) Body() (io.Reader, error) {
values, err := query.Values(fbd.payload)
if err != nil {
return nil, err
}
return strings.NewReader(values.Encode()), nil
}

// Form accepts a struct, uses it as body data, and sent it as application/www-x-form-urlencoded
// If the actual method does not support body or form data, such as `GET`, `HEAD`,
// it will be simply omitted.
func (c *Client) Form(bodyForm interface{}) *Client {
if bodyForm != nil {
c.Header(contentType, formContentType)
body, _ := formBodyData{payload: bodyForm}.Body()
c.body = body
}
return c
}

// Body accepts `io.Reader`, will read data from it and use it as request body.
// This doee not set `Content-Type` header, so users should use `Header(key, value)`
// to specify it if necessary.
func (c *Client) Body(body io.Reader) *Client {
if body != nil {
c.body = body
}
return c
}

Expand Down
36 changes: 35 additions & 1 deletion gohttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,36 @@ func TestGetWithHeader(t *testing.T) {
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)

Expand All @@ -111,7 +141,11 @@ func TestPostJSON(t *testing.T) {

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
data, _ := ioutil.ReadAll(r.Body)
fmt.Fprint(w, string(data))
if r.Header.Get("Content-Type") != "application/json" {
w.Write([]byte("No json data"))
} else {
fmt.Fprint(w, string(data))
}
}))
defer ts.Close()

Expand Down

0 comments on commit 25cdd01

Please sign in to comment.