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

adds new functions to convert a buffalo.Handler to an http.Handler or http.HandlerFunc #1177

Merged
merged 2 commits into from
Jul 16, 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 Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ test:
ci-test:
$(GO_BIN) test -tags ${TAGS} -race -v ./...
docker build .

lint:
gometalinter --vendor ./... --deadline=1m --skip=internal
8 changes: 5 additions & 3 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,16 @@ type ParamValues interface {
}

func (a *App) newContext(info RouteInfo, res http.ResponseWriter, req *http.Request) Context {
ws := res.(*Response)
if ws, ok := res.(*Response); ok {
res = ws
}
params := req.URL.Query()
vars := mux.Vars(req)
for k, v := range vars {
params.Set(k, v)
}

session := a.getSession(req, ws)
session := a.getSession(req, res)

ct := httpx.ContentType(req)
contextData := map[string]interface{}{
Expand All @@ -70,7 +72,7 @@ func (a *App) newContext(info RouteInfo, res http.ResponseWriter, req *http.Requ
return &DefaultContext{
Context: req.Context(),
contentType: ct,
response: ws,
response: res,
request: req,
params: params,
logger: a.Logger,
Expand Down
17 changes: 17 additions & 0 deletions wrappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,20 @@ func WrapHandler(h http.Handler) Handler {
func WrapHandlerFunc(h http.HandlerFunc) Handler {
return WrapHandler(h)
}

// WrapBuffaloHandler wraps a buffalo.Handler to
// standard http.Handler
func WrapBuffaloHandler(h Handler) http.Handler {
a := New(Options{})
// it doesn't matter what we actually map it
// GET, POST, etc... we just need the underlying
// RouteInfo, which implements http.Handler
ri := a.GET("/", h)
return ri
}

// WrapBuffaloHandlerFunc wraps a buffalo.Handler to
// standard http.HandlerFunc
func WrapBuffaloHandlerFunc(h Handler) http.HandlerFunc {
return WrapBuffaloHandler(h).ServeHTTP
}
66 changes: 66 additions & 0 deletions wrappers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package buffalo

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/gobuffalo/buffalo/render"
"github.com/markbates/willie"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -35,3 +37,67 @@ func Test_WrapHandler(t *testing.T) {

r.Equal("hello", res.Body.String())
}

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

tt := []struct {
verb string
path string
status int
}{
{"GET", "/", 1},
{"GET", "/foo", 2},
{"POST", "/", 3},
{"POST", "/foo", 4},
}
for _, x := range tt {
bf := func(c Context) error {
req := c.Request()
return c.Render(x.status, render.String(req.Method+req.URL.Path))
}

h := WrapBuffaloHandler(bf)
r.NotNil(h)

req := httptest.NewRequest(x.verb, x.path, nil)
res := httptest.NewRecorder()

h.ServeHTTP(res, req)

r.Equal(x.status, res.Code)
r.Contains(res.Body.String(), x.verb+x.path)
}
}

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

tt := []struct {
verb string
path string
status int
}{
{"GET", "/", 1},
{"GET", "/foo", 2},
{"POST", "/", 3},
{"POST", "/foo", 4},
}
for _, x := range tt {
bf := func(c Context) error {
req := c.Request()
return c.Render(x.status, render.String(req.Method+req.URL.Path))
}

h := WrapBuffaloHandlerFunc(bf)
r.NotNil(h)

req := httptest.NewRequest(x.verb, x.path, nil)
res := httptest.NewRecorder()

h(res, req)

r.Equal(x.status, res.Code)
r.Contains(res.Body.String(), x.verb+x.path)
}
}