Skip to content

Commit

Permalink
Merge pull request #14 from gobuffalo/json-improvements
Browse files Browse the repository at this point in the history
added functions to allow for any http verb
  • Loading branch information
markbates authored Feb 28, 2020
2 parents 67fa649 + f38c7bf commit f972e6e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 13 deletions.
4 changes: 2 additions & 2 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ func (r *Request) MultiPartPost(body interface{}, files ...File) (*Response, err
if err != nil {
return nil, err
}
return r.perform(req), nil
return r.Perform(req), nil
}

func (r *Request) MultiPartPut(body interface{}, files ...File) (*Response, error) {
req, err := newMultipart(r.URL, "PUT", body, files...)
if err != nil {
return nil, err
}
return r.perform(req), nil
return r.Perform(req), nil
}

// this helper method was inspired by this blog post by Matt Aimonetti:
Expand Down
24 changes: 18 additions & 6 deletions json.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,45 @@ func (r *JSONResponse) Bind(x interface{}) {

func (r *JSON) Get() *JSONResponse {
req, _ := http.NewRequest("GET", r.URL, nil)
return r.perform(req)
return r.Perform(req)
}

func (r *JSON) Delete() *JSONResponse {
req, _ := http.NewRequest("DELETE", r.URL, nil)
return r.perform(req)
return r.Perform(req)
}

func (r *JSON) Post(body interface{}) *JSONResponse {
b, _ := json.Marshal(body)
req, _ := http.NewRequest("POST", r.URL, bytes.NewReader(b))
return r.perform(req)
return r.Perform(req)
}

func (r *JSON) Put(body interface{}) *JSONResponse {
b, _ := json.Marshal(body)
req, _ := http.NewRequest("PUT", r.URL, bytes.NewReader(b))
return r.perform(req)
return r.Perform(req)
}

func (r *JSON) Patch(body interface{}) *JSONResponse {
b, _ := json.Marshal(body)
req, _ := http.NewRequest("PATCH", r.URL, bytes.NewReader(b))
return r.perform(req)
return r.Perform(req)
}

func (r *JSON) perform(req *http.Request) *JSONResponse {
func (r *JSON) Do(method string, body interface{}) (*JSONResponse, error) {
b, err := json.Marshal(body)
if err != nil {
return nil, err
}
req, err := http.NewRequest(method, r.URL, bytes.NewReader(b))
if err != nil {
return nil, err
}
return r.Perform(req), nil
}

func (r *JSON) Perform(req *http.Request) *JSONResponse {
if r.handler.HmaxSecret != "" {
hmax.SignRequest(req, []byte(r.handler.HmaxSecret))
}
Expand Down
24 changes: 24 additions & 0 deletions json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ func JSONApp() http.Handler {
Message: "Hello from Get!",
})
})
p.Handle("HEAD", "/head", func(res http.ResponseWriter, req *http.Request) {
res.WriteHeader(418)
json.NewEncoder(res).Encode(jBody{
Method: req.Method,
Message: "Hello from Head!",
})
})
p.Handle("DELETE", "/delete", func(res http.ResponseWriter, req *http.Request) {
res.WriteHeader(201)
json.NewEncoder(res).Encode(jBody{
Expand Down Expand Up @@ -97,6 +104,23 @@ func Test_JSON_Get(t *testing.T) {
r.Equal("Hello from Get!", jb.Message)
}

func Test_JSON_Head(t *testing.T) {
r := require.New(t)
w := New(JSONApp())

c := w.JSON("/head")
r.Equal("/head", c.URL)

res, err := c.Do("HEAD", nil)
r.NoError(err)
r.Equal(418, res.Code)

jb := &jBody{}
res.Bind(jb)
r.Equal("HEAD", jb.Method)
r.Equal("Hello from Head!", jb.Message)
}

func Test_JSON_Delete(t *testing.T) {
r := require.New(t)
w := New(JSONApp())
Expand Down
18 changes: 13 additions & 5 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,35 @@ func (r *Request) SetBasicAuth(username, password string) {

func (r *Request) Get() *Response {
req, _ := http.NewRequest("GET", r.URL, nil)
return r.perform(req)
return r.Perform(req)
}

func (r *Request) Delete() *Response {
req, _ := http.NewRequest("DELETE", r.URL, nil)
return r.perform(req)
return r.Perform(req)
}

func (r *Request) Post(body interface{}) *Response {
req, _ := http.NewRequest("POST", r.URL, toReader(body))
r.Headers["Content-Type"] = "application/x-www-form-urlencoded"
return r.perform(req)
return r.Perform(req)
}

func (r *Request) Put(body interface{}) *Response {
req, _ := http.NewRequest("PUT", r.URL, toReader(body))
r.Headers["Content-Type"] = "application/x-www-form-urlencoded"
return r.perform(req)
return r.Perform(req)
}

func (r *Request) perform(req *http.Request) *Response {
func (r *Request) Do(method string, body interface{}) (*Response, error) {
req, err := http.NewRequest(method, r.URL, toReader(body))
if err != nil {
return nil, err
}
return r.Perform(req), nil
}

func (r *Request) Perform(req *http.Request) *Response {
if r.handler.HmaxSecret != "" {
hmax.SignRequest(req, []byte(r.handler.HmaxSecret))
}
Expand Down

0 comments on commit f972e6e

Please sign in to comment.