Skip to content

Commit f972e6e

Browse files
authored
Merge pull request #14 from gobuffalo/json-improvements
added functions to allow for any http verb
2 parents 67fa649 + f38c7bf commit f972e6e

File tree

4 files changed

+57
-13
lines changed

4 files changed

+57
-13
lines changed

file.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ func (r *Request) MultiPartPost(body interface{}, files ...File) (*Response, err
1818
if err != nil {
1919
return nil, err
2020
}
21-
return r.perform(req), nil
21+
return r.Perform(req), nil
2222
}
2323

2424
func (r *Request) MultiPartPut(body interface{}, files ...File) (*Response, error) {
2525
req, err := newMultipart(r.URL, "PUT", body, files...)
2626
if err != nil {
2727
return nil, err
2828
}
29-
return r.perform(req), nil
29+
return r.Perform(req), nil
3030
}
3131

3232
// this helper method was inspired by this blog post by Matt Aimonetti:

json.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,45 @@ func (r *JSONResponse) Bind(x interface{}) {
2727

2828
func (r *JSON) Get() *JSONResponse {
2929
req, _ := http.NewRequest("GET", r.URL, nil)
30-
return r.perform(req)
30+
return r.Perform(req)
3131
}
3232

3333
func (r *JSON) Delete() *JSONResponse {
3434
req, _ := http.NewRequest("DELETE", r.URL, nil)
35-
return r.perform(req)
35+
return r.Perform(req)
3636
}
3737

3838
func (r *JSON) Post(body interface{}) *JSONResponse {
3939
b, _ := json.Marshal(body)
4040
req, _ := http.NewRequest("POST", r.URL, bytes.NewReader(b))
41-
return r.perform(req)
41+
return r.Perform(req)
4242
}
4343

4444
func (r *JSON) Put(body interface{}) *JSONResponse {
4545
b, _ := json.Marshal(body)
4646
req, _ := http.NewRequest("PUT", r.URL, bytes.NewReader(b))
47-
return r.perform(req)
47+
return r.Perform(req)
4848
}
4949

5050
func (r *JSON) Patch(body interface{}) *JSONResponse {
5151
b, _ := json.Marshal(body)
5252
req, _ := http.NewRequest("PATCH", r.URL, bytes.NewReader(b))
53-
return r.perform(req)
53+
return r.Perform(req)
5454
}
5555

56-
func (r *JSON) perform(req *http.Request) *JSONResponse {
56+
func (r *JSON) Do(method string, body interface{}) (*JSONResponse, error) {
57+
b, err := json.Marshal(body)
58+
if err != nil {
59+
return nil, err
60+
}
61+
req, err := http.NewRequest(method, r.URL, bytes.NewReader(b))
62+
if err != nil {
63+
return nil, err
64+
}
65+
return r.Perform(req), nil
66+
}
67+
68+
func (r *JSON) Perform(req *http.Request) *JSONResponse {
5769
if r.handler.HmaxSecret != "" {
5870
hmax.SignRequest(req, []byte(r.handler.HmaxSecret))
5971
}

json_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ func JSONApp() http.Handler {
2525
Message: "Hello from Get!",
2626
})
2727
})
28+
p.Handle("HEAD", "/head", func(res http.ResponseWriter, req *http.Request) {
29+
res.WriteHeader(418)
30+
json.NewEncoder(res).Encode(jBody{
31+
Method: req.Method,
32+
Message: "Hello from Head!",
33+
})
34+
})
2835
p.Handle("DELETE", "/delete", func(res http.ResponseWriter, req *http.Request) {
2936
res.WriteHeader(201)
3037
json.NewEncoder(res).Encode(jBody{
@@ -97,6 +104,23 @@ func Test_JSON_Get(t *testing.T) {
97104
r.Equal("Hello from Get!", jb.Message)
98105
}
99106

107+
func Test_JSON_Head(t *testing.T) {
108+
r := require.New(t)
109+
w := New(JSONApp())
110+
111+
c := w.JSON("/head")
112+
r.Equal("/head", c.URL)
113+
114+
res, err := c.Do("HEAD", nil)
115+
r.NoError(err)
116+
r.Equal(418, res.Code)
117+
118+
jb := &jBody{}
119+
res.Bind(jb)
120+
r.Equal("HEAD", jb.Method)
121+
r.Equal("Hello from Head!", jb.Message)
122+
}
123+
100124
func Test_JSON_Delete(t *testing.T) {
101125
r := require.New(t)
102126
w := New(JSONApp())

request.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,35 @@ func (r *Request) SetBasicAuth(username, password string) {
2828

2929
func (r *Request) Get() *Response {
3030
req, _ := http.NewRequest("GET", r.URL, nil)
31-
return r.perform(req)
31+
return r.Perform(req)
3232
}
3333

3434
func (r *Request) Delete() *Response {
3535
req, _ := http.NewRequest("DELETE", r.URL, nil)
36-
return r.perform(req)
36+
return r.Perform(req)
3737
}
3838

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

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

51-
func (r *Request) perform(req *http.Request) *Response {
51+
func (r *Request) Do(method string, body interface{}) (*Response, error) {
52+
req, err := http.NewRequest(method, r.URL, toReader(body))
53+
if err != nil {
54+
return nil, err
55+
}
56+
return r.Perform(req), nil
57+
}
58+
59+
func (r *Request) Perform(req *http.Request) *Response {
5260
if r.handler.HmaxSecret != "" {
5361
hmax.SignRequest(req, []byte(r.handler.HmaxSecret))
5462
}

0 commit comments

Comments
 (0)