Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions responder.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,32 +59,37 @@ func DefaultResponder(w http.ResponseWriter, r *http.Request, v interface{}) {
}
}

// writeStatus writes a previously stored Status() hint. A nil request is
// allowed so wrappers can call JSON/XML/etc. after writing the header themselves.
func writeStatus(w http.ResponseWriter, r *http.Request) {
if r == nil {
return
}
if status, ok := r.Context().Value(StatusCtxKey).(int); ok {
w.WriteHeader(status)
}
}

// PlainText writes a string to the response, setting the Content-Type as
// text/plain.
func PlainText(w http.ResponseWriter, r *http.Request, v string) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
if status, ok := r.Context().Value(StatusCtxKey).(int); ok {
w.WriteHeader(status)
}
writeStatus(w, r)
w.Write([]byte(v)) //nolint:errcheck
}

// Data writes raw bytes to the response, setting the Content-Type as
// application/octet-stream.
func Data(w http.ResponseWriter, r *http.Request, v []byte) {
w.Header().Set("Content-Type", "application/octet-stream")
if status, ok := r.Context().Value(StatusCtxKey).(int); ok {
w.WriteHeader(status)
}
writeStatus(w, r)
w.Write(v) //nolint:errcheck
}

// HTML writes a string to the response, setting the Content-Type as text/html.
func HTML(w http.ResponseWriter, r *http.Request, v string) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if status, ok := r.Context().Value(StatusCtxKey).(int); ok {
w.WriteHeader(status)
}
writeStatus(w, r)
w.Write([]byte(v)) //nolint:errcheck
}

Expand All @@ -100,9 +105,7 @@ func JSON(w http.ResponseWriter, r *http.Request, v interface{}) {
}

w.Header().Set("Content-Type", "application/json")
if status, ok := r.Context().Value(StatusCtxKey).(int); ok {
w.WriteHeader(status)
}
writeStatus(w, r)
w.Write(buf.Bytes()) //nolint:errcheck
}

Expand All @@ -117,9 +120,7 @@ func XML(w http.ResponseWriter, r *http.Request, v interface{}) {
}

w.Header().Set("Content-Type", "application/xml; charset=utf-8")
if status, ok := r.Context().Value(StatusCtxKey).(int); ok {
w.WriteHeader(status)
}
writeStatus(w, r)

// Try to find <?xml header in first 100 bytes (just in case there're some XML comments).
findHeaderUntil := len(b)
Expand Down
70 changes: 70 additions & 0 deletions responder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package render

import (
"encoding/xml"
"net/http"
"net/http/httptest"
"strings"
"testing"
)

func TestJSONNilRequest(t *testing.T) {
w := httptest.NewRecorder()
w.WriteHeader(http.StatusCreated)
JSON(w, nil, map[string]string{"field": "test"})
if w.Code != http.StatusCreated {
t.Fatalf("status=%d want %d", w.Code, http.StatusCreated)
}
got := strings.TrimSpace(w.Body.String())
if got != `{"field":"test"}` {
t.Fatalf("body=%q", got)
}
if ct := w.Header().Get("Content-Type"); ct != "application/json" {
t.Fatalf("Content-Type=%q", ct)
}
}

func TestXMLNilRequest(t *testing.T) {
type payload struct {
XMLName xml.Name `xml:"item"`
Field string `xml:"field"`
}
w := httptest.NewRecorder()
XML(w, nil, payload{Field: "test"})
if w.Code != http.StatusOK {
t.Fatalf("status=%d want %d", w.Code, http.StatusOK)
}
if !strings.Contains(w.Body.String(), "<field>test</field>") {
t.Fatalf("body=%q", w.Body.String())
}
}

func TestPlainTextDataHTMLNilRequest(t *testing.T) {
w := httptest.NewRecorder()
PlainText(w, nil, "hello")
if w.Body.String() != "hello" {
t.Fatalf("plaintext body=%q", w.Body.String())
}

w = httptest.NewRecorder()
Data(w, nil, []byte("raw"))
if w.Body.String() != "raw" {
t.Fatalf("data body=%q", w.Body.String())
}

w = httptest.NewRecorder()
HTML(w, nil, "<p>ok</p>")
if w.Body.String() != "<p>ok</p>" {
t.Fatalf("html body=%q", w.Body.String())
}
}

func TestJSONStillHonorsStatusHint(t *testing.T) {
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/", nil)
Status(r, http.StatusAccepted)
JSON(w, r, map[string]string{"ok": "yes"})
if w.Code != http.StatusAccepted {
t.Fatalf("status=%d want %d", w.Code, http.StatusAccepted)
}
}