Skip to content

Commit

Permalink
Marshaler handles when Response pointer is nil.
Browse files Browse the repository at this point in the history
  • Loading branch information
babo committed Jun 15, 2014
1 parent 987c09e commit 11a58d4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion marshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func (m *Marshaler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}
w.WriteHeader(code)
if nil != rs && http.StatusNoContent != code {
if nil != rs && http.StatusNoContent != code && (out[2].Kind() != reflect.Ptr || !out[2].IsNil()) {
if err := json.NewEncoder(w).Encode(rs); nil != err {
log.Println(err)
}
Expand Down
15 changes: 15 additions & 0 deletions marshaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,21 @@ func TestNoContent(t *testing.T) {
}
}

func TestNilContent(t *testing.T) {
w := &testResponseWriter{}
r, _ := http.NewRequest("GET", "http://example.com/foo", nil)
r.Header.Set("Accept", "application/json")
Marshaled(func(u *url.URL, h http.Header, rq *testRequest) (int, http.Header, *testResponse, error) {
return http.StatusOK, nil, nil, nil
}).ServeHTTP(w, r)
if http.StatusOK != w.StatusCode {
t.Fatal(w.StatusCode)
}
if "" != w.Body.String() {
t.Fatal(w.Body.String())
}
}

func TestHeader(t *testing.T) {
w := &testResponseWriter{}
r, _ := http.NewRequest("GET", "http://example.com/foo", nil)
Expand Down

0 comments on commit 11a58d4

Please sign in to comment.