Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

this option allows for users to set the default content type for Auto instead of the default "text/html". #1234

Merged
merged 3 commits into from
Aug 17, 2018
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
3 changes: 3 additions & 0 deletions buffalo/cmd/filetests/apiapp.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
]
}, {
"path": "actions/render.go",
"contains": [
"DefaultContentType: \"application/json\","
],
"!contains": [
"HTMLLayout",
"TemplatesBox",
Expand Down
3 changes: 3 additions & 0 deletions generators/newapp/templates/actions/render.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ var assetsBox = packr.NewBox("../public")

func init() {
r = render.New(render.Options{
{{ if .opts.AsAPI -}}
DefaultContentType: "application/json",
{{ end -}}
{{ if .opts.AsWeb -}}
// HTML layout to be used for all HTML requests:
HTMLLayout: "application.html",
Expand Down
8 changes: 4 additions & 4 deletions render/auto.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ PUT /users/edit - (redirect to /users/id or render user/edit.html)
DELETE /users/id - redirect to /users
*/
func (e *Engine) Auto(ctx context.Context, i interface{}) Renderer {
ct, ok := ctx.Value("contentType").(string)
if !ok {
ct = "text/html"
ct, _ := ctx.Value("contentType").(string)
if ct == "" {
ct = e.DefaultContentType
}
ct = strings.ToLower(ct)
ct = strings.TrimSpace(strings.ToLower(ct))

if strings.Contains(ct, "json") {
return e.JSON(i)
Expand Down
19 changes: 19 additions & 0 deletions render/auto_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package render_test

import (
"net/http/httptest"
"os"
"path/filepath"
"strings"
Expand All @@ -20,6 +21,24 @@ type Car struct {

type Cars []Car

func Test_Auto_DefaultContentType(t *testing.T) {
r := require.New(t)

re := render.New(render.Options{
DefaultContentType: "application/json",
})
app := buffalo.New(buffalo.Options{})
app.GET("/cars", func(c buffalo.Context) error {
return c.Render(200, re.Auto(c, []string{"Honda", "Toyota", "Ford", "Chevy"}))
})

res := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/cars", nil)
app.ServeHTTP(res, req)

r.Equal(`["Honda","Toyota","Ford","Chevy"]`, strings.TrimSpace(res.Body.String()))
}

func Test_Auto_JSON(t *testing.T) {
r := require.New(t)

Expand Down
4 changes: 4 additions & 0 deletions render/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ type Options struct {

// TemplateEngine to be used for rendering HTML templates
TemplateEngines map[string]TemplateEngine

// DefaultContentType instructs the engine what it should fall back to if
// the "content-type" is unknown
DefaultContentType string
}
4 changes: 4 additions & 0 deletions render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func New(opts Options) *Engine {
opts.TemplateEngines["tmpl"] = GoTemplateEngine
}

if opts.DefaultContentType == "" {
opts.DefaultContentType = "text/html"
}

e := &Engine{
Options: opts,
}
Expand Down