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