Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Derek Dowling committed Jan 24, 2016
2 parents c890b25 + af60b61 commit 0d9dae4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
31 changes: 25 additions & 6 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,22 +115,41 @@ func Do(request *http.Request) (*jsh.Document, *http.Response, error) {
request.Header.Set("Content-Length", strconv.Itoa(int(request.ContentLength)))

client := &http.Client{}
httpResponse, clientErr := client.Do(request)
response, clientErr := client.Do(request)

if clientErr != nil {
return nil, nil, fmt.Errorf(
"Error sending %s request: %s", request.Method, clientErr.Error(),
)
}

if request.Method == "DELETE" {
return nil, httpResponse, nil
doc, parseErr := ParseResponse(response)
if parseErr != nil {
return nil, response, fmt.Errorf("Error parsing response: %s", parseErr.Error())
}

document, err := Document(httpResponse)
return doc, response, parseErr
}

// ParseResponse handles parsing an HTTP response into a JSON Document if
// possible
func ParseResponse(response *http.Response) (*jsh.Document, error) {

skipCodes := []int{
http.StatusNoContent,
http.StatusNotFound,
}

for _, code := range skipCodes {
if code == response.StatusCode {
return nil, nil
}
}

document, err := Document(response)
if err != nil {
return nil, httpResponse, err
return nil, err
}

return document, httpResponse, nil
return document, nil
}
21 changes: 19 additions & 2 deletions client/client_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jsc

import (
"net/http"
"net/url"
"testing"

Expand Down Expand Up @@ -42,11 +43,27 @@ func TestClientRequest(t *testing.T) {
})
}

func TestParseResponse(t *testing.T) {

Convey("ParseResponse", t, func() {

response := &http.Response{
StatusCode: http.StatusNotFound,
}

Convey("404 response parsing should not return a 406 error", func() {
doc, err := ParseResponse(response)
So(doc, ShouldBeNil)
So(err, ShouldBeNil)
})
})
}

func TestResponseParsing(t *testing.T) {

Convey("Response Parsing Tests", t, func() {

Convey("->ParseObject()", func() {
Convey("Parse Object", func() {

obj, objErr := jsh.NewObject("123", "test", map[string]string{"test": "test"})
So(objErr, ShouldBeNil)
Expand All @@ -63,7 +80,7 @@ func TestResponseParsing(t *testing.T) {
})
})

Convey("->GetList()", func() {
Convey("Parse List", func() {

obj, objErr := jsh.NewObject("123", "test", map[string]string{"test": "test"})
So(objErr, ShouldBeNil)
Expand Down
2 changes: 1 addition & 1 deletion error.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ http://jsonapi.org/format/#error-objects
type Error struct {
Title string `json:"title"`
Detail string `json:"detail"`
Status int `json:"status"`
Status int `json:"status,string"`
Source struct {
Pointer string `json:"pointer"`
} `json:"source"`
Expand Down

0 comments on commit 0d9dae4

Please sign in to comment.