Repository: gobuffalo/buffalo
Pull request #1234 (2018-08-16 20:07:46)

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

codecov[bot]: # [Codecov](https://codecov.io/gh/gobuffalo/buffalo/pull/1234?src=pr&el=h1) Report
> Merging [#1234](https://codecov.io/gh/gobuffalo/buffalo/pull/1234?src=pr&el=desc) into [development](https://codecov.io/gh/gobuffalo/buffalo/commit/e1f428b673cc3ff6f115983a503c9a6cb96ec696?src=pr&el=desc) will **increase** coverage by `0.12%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/gobuffalo/buffalo/pull/1234/graphs/tree.svg?width=650&height=150&src=pr&token=1Yyy8wHMMl)](https://codecov.io/gh/gobuffalo/buffalo/pull/1234?src=pr&el=tree)

```diff
@@               Coverage Diff               @@
##           development    #1234      +/-   ##
===============================================
+ Coverage         53.8%   53.92%   +0.12%     
===============================================
  Files               74       74              
  Lines             3394     3397       +3     
===============================================
+ Hits              1826     1832       +6     
+ Misses            1471     1469       -2     
+ Partials            97       96       -1
```


| [Impacted Files](https://codecov.io/gh/gobuffalo/buffalo/pull/1234?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [render/render.go](https://codecov.io/gh/gobuffalo/buffalo/pull/1234/diff?src=pr&el=tree#diff-cmVuZGVyL3JlbmRlci5nbw==) | `100% <100%> (ø)` | :arrow_up: |
| [render/auto.go](https://codecov.io/gh/gobuffalo/buffalo/pull/1234/diff?src=pr&el=tree#diff-cmVuZGVyL2F1dG8uZ28=) | `91.26% <100%> (+2.91%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/gobuffalo/buffalo/pull/1234?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/gobuffalo/buffalo/pull/1234?src=pr&el=footer). Last update [e1f428b...fbe54e4](https://codecov.io/gh/gobuffalo/buffalo/pull/1234?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/generators/newapp/templates/actions/render.go.tmpl b/generators/newapp/templates/actions/render.go.tmpl
index 0d7e69d7..91f9a58f 100644
--- a/generators/newapp/templates/actions/render.go.tmpl
+++ b/generators/newapp/templates/actions/render.go.tmpl
@@ -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",
diff --git a/render/auto.go b/render/auto.go
index d6a104cd..a4886351 100644
--- a/render/auto.go
+++ b/render/auto.go
@@ -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)
diff --git a/render/options.go b/render/options.go
index de1c2a9a..19513568 100644
--- a/render/options.go
+++ b/render/options.go
@@ -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
 }
diff --git a/render/render.go b/render/render.go
index 898a7dd5..93843a0d 100644
--- a/render/render.go
+++ b/render/render.go
@@ -41,6 +41,10 @@ func New(opts Options) *Engine {
 		opts.TemplateEngines["tmpl"] = GoTemplateEngine
 	}
 
+	if opts.DefaultContentType == "" {
+		opts.DefaultContentType = "text/html"
+	}
+
 	e := &Engine{
 		Options: opts,
 	}


Test patch:
diff --git a/buffalo/cmd/filetests/apiapp.json b/buffalo/cmd/filetests/apiapp.json
index c30bf231..07136254 100644
--- a/buffalo/cmd/filetests/apiapp.json
+++ b/buffalo/cmd/filetests/apiapp.json
@@ -16,6 +16,9 @@
   ]
 }, {
   "path": "actions/render.go",
+  "contains": [
+    "DefaultContentType: \"application/json\","
+  ],
   "!contains": [
     "HTMLLayout",
     "TemplatesBox",
diff --git a/render/auto_test.go b/render/auto_test.go
index 5b47f366..3dd944db 100644
--- a/render/auto_test.go
+++ b/render/auto_test.go
@@ -1,6 +1,7 @@
 package render_test
 
 import (
+	"net/http/httptest"
 	"os"
 	"path/filepath"
 	"strings"
@@ -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)