Skip to content

Commit

Permalink
Merge pull request #26 from dghubble/json-decode-any-content-type
Browse files Browse the repository at this point in the history
Decode JSON success/failure regardless of Content-Type
  • Loading branch information
dghubble committed Oct 11, 2016
2 parents 75737a8 + c083b04 commit c961a43
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 24 deletions.
3 changes: 1 addition & 2 deletions sling.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io"
"net/http"
"net/url"
"strings"

goquery "github.com/google/go-querystring/query"
)
Expand Down Expand Up @@ -352,7 +351,7 @@ func (s *Sling) Do(req *http.Request, successV, failureV interface{}) (*http.Res
}
// when err is nil, resp contains a non-nil resp.Body which must be closed
defer resp.Body.Close()
if strings.Contains(resp.Header.Get(contentType), jsonContentType) {
if successV != nil || failureV != nil {
err = decodeResponseJSON(resp, successV, failureV)
}
return resp, err
Expand Down
21 changes: 0 additions & 21 deletions sling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,6 @@ func TestBodyFormSetter(t *testing.T) {
t.Errorf("did not expect a Content-Type header, got %s", sling.header.Get(contentType))
}
}

}

func TestBodySetter(t *testing.T) {
Expand Down Expand Up @@ -715,26 +714,6 @@ func TestDo_onFailureWithNilValue(t *testing.T) {
}
}

func TestDo_skipDecodingIfContentTypeWrong(t *testing.T) {
client, mux, server := testServer()
defer server.Close()
mux.HandleFunc("/success", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
fmt.Fprintf(w, `{"text": "Some text", "favorite_count": 24}`)
})

sling := New().Client(client)
req, _ := http.NewRequest("GET", "http://example.com/success", nil)

model := new(FakeModel)
sling.Do(req, model, nil)

expectedModel := &FakeModel{}
if !reflect.DeepEqual(expectedModel, model) {
t.Errorf("decoding should have been skipped, Content-Type was incorrect")
}
}

func TestReceive_success(t *testing.T) {
client, mux, server := testServer()
defer server.Close()
Expand Down
2 changes: 1 addition & 1 deletion test
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env bash
set -e
set -e

PKGS=$(go list ./... | grep -v /examples)
FORMATTABLE="$(find . -maxdepth 1 -type d)"
Expand Down

0 comments on commit c961a43

Please sign in to comment.