github.com/corylanou/buffalo@v0.8.0/middleware/content_type_test.go (about)

     1  package middleware_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/gobuffalo/buffalo"
     7  	"github.com/gobuffalo/buffalo/middleware"
     8  	"github.com/gobuffalo/buffalo/render"
     9  	"github.com/markbates/willie"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func ctApp() *buffalo.App {
    14  	h := func(c buffalo.Context) error {
    15  		return c.Render(200, render.String(c.Request().Header.Get("Content-Type")))
    16  	}
    17  	a := buffalo.Automatic(buffalo.Options{})
    18  	a.GET("/set", middleware.SetContentType("application/json")(h))
    19  	a.GET("/add", middleware.AddContentType("application/json")(h))
    20  	return a
    21  }
    22  
    23  func Test_SetContentType(t *testing.T) {
    24  	r := require.New(t)
    25  
    26  	w := willie.New(ctApp())
    27  	res := w.Request("/set").Get()
    28  	r.Equal("application/json", res.Body.String())
    29  
    30  	req := w.Request("/set")
    31  	req.Headers["Content-Type"] = "text/plain"
    32  
    33  	res = req.Get()
    34  	r.Equal("application/json", res.Body.String())
    35  }
    36  
    37  func Test_AddContentType(t *testing.T) {
    38  	r := require.New(t)
    39  
    40  	w := willie.New(ctApp())
    41  	res := w.Request("/add").Get()
    42  	r.Equal("application/json", res.Body.String())
    43  
    44  	req := w.Request("/add")
    45  	req.Headers["Content-Type"] = "text/plain"
    46  
    47  	res = req.Get()
    48  	r.Equal("text/plain", res.Body.String())
    49  }