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
3 changes: 3 additions & 0 deletions content_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
ContentTypeEventStream
)

// GetContentType returns the ContentType parsed from the given MIME media type string.
func GetContentType(s string) ContentType {
s = strings.TrimSpace(strings.Split(s, ";")[0])
switch s {
Expand Down Expand Up @@ -64,6 +65,8 @@ func GetRequestContentType(r *http.Request) ContentType {
return GetContentType(r.Header.Get("Content-Type"))
}

// GetAcceptedContentType returns the ContentType requested by the client based on
// the context or the request Accept header.
func GetAcceptedContentType(r *http.Request) ContentType {
if contentType, ok := r.Context().Value(ContentTypeCtxKey).(ContentType); ok {
return contentType
Expand Down
62 changes: 62 additions & 0 deletions content_type_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package render

import (
"net/http"
"net/http/httptest"
"testing"
)

func TestGetContentType(t *testing.T) {
tests := []struct {
input string
want ContentType
}{
{"text/plain", ContentTypePlainText},
{"text/html", ContentTypeHTML},
{"application/xhtml+xml", ContentTypeHTML},
{"application/json", ContentTypeJSON},
{"text/javascript", ContentTypeJSON},
{"text/xml", ContentTypeXML},
{"application/xml", ContentTypeXML},
{"application/x-www-form-urlencoded", ContentTypeForm},
{"text/event-stream", ContentTypeEventStream},
{"application/json; charset=utf-8", ContentTypeJSON},
{"unknown/type", ContentTypeUnknown},
}

for _, tt := range tests {
got := GetContentType(tt.input)
if got != tt.want {
t.Errorf("GetContentType(%q) = %v, want %v", tt.input, got, tt.want)
}
}
}

func TestGetAcceptedContentType(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
req.Header.Set("Accept", "application/json, text/plain")

if got := GetAcceptedContentType(req); got != ContentTypeJSON {
t.Errorf("GetAcceptedContentType() = %v, want %v", got, ContentTypeJSON)
}

reqEmpty := httptest.NewRequest("GET", "/", nil)
if got := GetAcceptedContentType(reqEmpty); got != ContentTypePlainText {
t.Errorf("GetAcceptedContentType() for empty Accept = %v, want %v", got, ContentTypePlainText)
}
}

func TestSetContentType(t *testing.T) {
handler := SetContentType(ContentTypeJSON)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if got := GetRequestContentType(r); got != ContentTypeJSON {
t.Errorf("GetRequestContentType inside handler = %v, want %v", got, ContentTypeJSON)
}
if got := GetAcceptedContentType(r); got != ContentTypeJSON {
t.Errorf("GetAcceptedContentType inside handler = %v, want %v", got, ContentTypeJSON)
}
}))

req := httptest.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
}
2 changes: 1 addition & 1 deletion render.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func Bind(r *http.Request, v Binder) error {
return binder(r, v)
}

// Render renders a single payload and respond to the client request.
// Render renders a single payload and responds to the client request.
func Render(w http.ResponseWriter, r *http.Request, v Renderer) error {
if err := renderer(w, r, v); err != nil {
return err
Expand Down