feat: add per-call ContentType override via CallOptions by caueasantos · Pull Request #111 · unrolled/render · GitHub
Skip to content
Merged
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
54 changes: 41 additions & 13 deletions render.go
21 changes: 21 additions & 0 deletions render_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,24 @@ func TestDataCustomContentType(t *testing.T) {
expect(t, res.Header().Get(ContentType), "image/png")
expect(t, res.Body.String(), "..png data..")
}

func TestDataPerCallContentType(t *testing.T) {
render := New()

var err error

h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
err = render.Data(w, http.StatusOK, []byte("..jpeg data.."), CallOptions{
ContentType: "image/jpeg",
})
})

res := httptest.NewRecorder()
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "/foo", nil)
h.ServeHTTP(res, req)

expectNil(t, err)
expect(t, res.Code, http.StatusOK)
expect(t, res.Header().Get(ContentType), "image/jpeg")
expect(t, res.Body.String(), "..jpeg data..")
}
23 changes: 23 additions & 0 deletions render_html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,3 +501,26 @@ func TestHTMLTemplateOptionError(t *testing.T) {
expectNotNil(t, err)
expect(t, strings.Contains(err.Error(), "map has no entry for key"), true)
}

func TestHTMLPerCallContentType(t *testing.T) {
render := New(Options{
Directory: "testdata/basic",
})

var err error

h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
err = render.HTML(w, http.StatusOK, "hello", "gophers", HTMLOptions{
ContentType: "application/xhtml+xml",
})
})

res := httptest.NewRecorder()
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "/foo", nil)
h.ServeHTTP(res, req)

expectNil(t, err)
expect(t, res.Code, http.StatusOK)
expect(t, res.Header().Get(ContentType), "application/xhtml+xml")
expect(t, res.Body.String(), "<h1>Hello gophers</h1>\n")
}
21 changes: 21 additions & 0 deletions render_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,3 +412,24 @@ func TestJSONEncoderStream(t *testing.T) {
expect(t, res.Header().Get(ContentType), ContentJSON+"; charset=UTF-8")
expect(t, res.Body.String(), TestEncoder{}.String())
}

func TestJSONPerCallContentType(t *testing.T) {
render := New()

var err error

h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
err = render.JSON(w, http.StatusOK, Greeting{"hello", "world"}, CallOptions{
ContentType: "application/vnd.api+json",
})
})

res := httptest.NewRecorder()
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "/foo", nil)
h.ServeHTTP(res, req)

expectNil(t, err)
expect(t, res.Code, http.StatusOK)
expect(t, res.Header().Get(ContentType), "application/vnd.api+json")
expect(t, res.Body.String(), "{\"one\":\"hello\",\"two\":\"world\"}")
}
21 changes: 21 additions & 0 deletions render_jsonp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,24 @@ func TestJSONPDisabledCharset(t *testing.T) {
expect(t, res.Header().Get(ContentType), ContentJSONP)
expect(t, res.Body.String(), "helloCallback({\"one\":\"hello\",\"two\":\"world\"});")
}

func TestJSONPPerCallContentType(t *testing.T) {
render := New()

var err error

h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
err = render.JSONP(w, http.StatusOK, "helloCallback", GreetingP{"hello", "world"}, CallOptions{
ContentType: "application/vnd.api+json",
})
})

res := httptest.NewRecorder()
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "/foo", nil)
h.ServeHTTP(res, req)

expectNil(t, err)
expect(t, res.Code, http.StatusOK)
expect(t, res.Header().Get(ContentType), "application/vnd.api+json")
expect(t, res.Body.String(), "helloCallback({\"one\":\"hello\",\"two\":\"world\"});")
}
21 changes: 21 additions & 0 deletions render_text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,24 @@ func TestTextDisabledCharset(t *testing.T) {
expect(t, res.Header().Get(ContentType), ContentText)
expect(t, res.Body.String(), "Hello Text!")
}

func TestTextPerCallContentType(t *testing.T) {
render := New()

var err error

h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
err = render.Text(w, http.StatusOK, "Hello Text!", CallOptions{
ContentType: "text/css",
})
})

res := httptest.NewRecorder()
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "/foo", nil)
h.ServeHTTP(res, req)

expectNil(t, err)
expect(t, res.Code, http.StatusOK)
expect(t, res.Header().Get(ContentType), "text/css")
expect(t, res.Body.String(), "Hello Text!")
}
21 changes: 21 additions & 0 deletions render_xml_test.go
Loading