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

Commit

Permalink
working on #3
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Nov 24, 2016
1 parent 6538db2 commit 2ddffe5
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 16 deletions.
3 changes: 1 addition & 2 deletions context.go
Expand Up @@ -18,8 +18,7 @@ type Context interface {
LogField(string, interface{})
LogFields(map[string]interface{})
Logger() Logger

// render functions
Bind(interface{}) error
Render(int, render.Renderer) error
Error(int, error) error
NoContent(int) error
Expand Down
19 changes: 19 additions & 0 deletions default_context.go
@@ -1,12 +1,16 @@
package buffalo

import (
"encoding/json"
"encoding/xml"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"time"

"github.com/gorilla/schema"
"github.com/markbates/buffalo/render"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -76,6 +80,21 @@ func (d *DefaultContext) Render(status int, rr render.Renderer) error {
return err
}

func (d *DefaultContext) Bind(value interface{}) error {
switch strings.ToLower(d.request.Header.Get("Content-Type")) {
case "application/json":
return json.NewDecoder(d.request.Body).Decode(value)
case "application/xml":
return xml.NewDecoder(d.request.Body).Decode(value)
default:
err := d.request.ParseForm()
if err != nil {
return err
}
return schema.NewDecoder().Decode(value, d.request.PostForm)
}
}

func (d *DefaultContext) NoContent(status int) error {
d.response.WriteHeader(status)
return nil
Expand Down
80 changes: 80 additions & 0 deletions default_context_test.go
Expand Up @@ -7,6 +7,8 @@ import (

"github.com/Sirupsen/logrus"
"github.com/markbates/buffalo/render"
"github.com/markbates/willie"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -68,3 +70,81 @@ func Test_DefaultContext_Render(t *testing.T) {
r.Equal(123, res.Code)
r.Equal("Hello Mark!", res.Body.String())
}

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

user := struct {
FirstName string `schema:"first_name"`
}{}

a := New(Options{})
a.POST("/", func(c Context) error {
err := c.Bind(&user)
if err != nil {
return errors.WithStack(err)
}
return c.NoContent(201)
})

w := willie.New(a)
uv := url.Values{"first_name": []string{"Mark"}}
res := w.Request("/").Post(uv)
r.Equal(201, res.Code)

r.Equal("Mark", user.FirstName)
}

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

user := struct {
FirstName string `json:"first_name"`
}{}

a := New(Options{})
a.POST("/", func(c Context) error {
err := c.Bind(&user)
if err != nil {
return errors.WithStack(err)
}
return c.NoContent(201)
})

w := willie.New(a)
res := w.JSON("/").Post(map[string]string{
"first_name": "Mark",
})
r.Equal(201, res.Code)

r.Equal("Mark", user.FirstName)
}

// func Test_DefaultContext_Bind_XML(t *testing.T) {
// r := require.New(t)
//
// user := struct {
// FirstName string `json:"first_name"`
// }{}
//
// a := New(Options{})
// a.POST("/", func(c Context) error {
// err := c.Bind(&user)
// if err != nil {
// return errors.WithStack(err)
// }
// return c.NoContent(201)
// })
//
// w := willie.New(a)
// req := w.Request("/")
// req.Headers["Content-Type"] = "application/xml"
// b, err := xml.Marshal(map[string]string{
// "first_name": "Mark",
// })
// r.NoError(err)
// res := req.Post(bytes.NewReader(b))
// r.Equal(201, res.Code)
//
// r.Equal("Mark", user.FirstName)
// }
6 changes: 3 additions & 3 deletions middleware_test.go
Expand Up @@ -3,7 +3,7 @@ package buffalo
import (
"testing"

"github.com/markbates/going/willy"
"github.com/markbates/willie"
"github.com/stretchr/testify/require"
)

Expand All @@ -26,7 +26,7 @@ func Test_App_Use(t *testing.T) {
return nil
})

w := willy.New(a)
w := willie.New(a)
w.Request("/").Get()
r.Len(log, 3)
r.Equal([]string{"start", "handler", "end"}, log)
Expand Down Expand Up @@ -70,7 +70,7 @@ func Test_App_Skip(t *testing.T) {

a.Skip(mw2, h2)

w := willy.New(a)
w := willie.New(a)

w.Request("/h2").Get()
r.Len(log, 3)
Expand Down
10 changes: 5 additions & 5 deletions not_found_test.go
Expand Up @@ -5,7 +5,7 @@ import (
"net/http"
"testing"

"github.com/markbates/going/willy"
"github.com/markbates/willie"
"github.com/stretchr/testify/require"
)

Expand All @@ -15,7 +15,7 @@ func Test_App_Dev_NotFound(t *testing.T) {
a := New(Options{Env: "development"})
a.GET("/foo", func(c Context) error { return nil })

w := willy.New(a)
w := willie.New(a)
res := w.Request("/bad").Get()
r.Equal(404, res.Code)

Expand All @@ -30,7 +30,7 @@ func Test_App_Dev_NotFound_JSON(t *testing.T) {
a := New(Options{Env: "development"})
a.GET("/foo", func(c Context) error { return nil })

w := willy.New(a)
w := willie.New(a)
res := w.JSON("/bad").Get()
r.Equal(404, res.Code)

Expand All @@ -48,7 +48,7 @@ func Test_App_Prod_NotFound(t *testing.T) {
a := New(Options{Env: "production"})
a.GET("/foo", func(c Context) error { return nil })

w := willy.New(a)
w := willie.New(a)
res := w.Request("/bad").Get()
r.Equal(404, res.Code)

Expand All @@ -68,7 +68,7 @@ func Test_App_Override_NotFound(t *testing.T) {
})
a.GET("/foo", func(c Context) error { return nil })

w := willy.New(a)
w := willie.New(a)
res := w.Request("/bad").Get()
r.Equal(404, res.Code)

Expand Down
6 changes: 3 additions & 3 deletions router_test.go
Expand Up @@ -9,7 +9,7 @@ import (
"testing"

"github.com/markbates/buffalo/render"
"github.com/markbates/going/willy"
"github.com/markbates/willie"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -64,7 +64,7 @@ func Test_Router_Group(t *testing.T) {
return c.NoContent(201)
})

w := willy.New(a)
w := willie.New(a)
res := w.Request("/api/v1/users").Get()
r.Equal(201, res.Code)
}
Expand Down Expand Up @@ -98,7 +98,7 @@ func Test_Router_ServeFiles(t *testing.T) {
a := New(Options{})
a.ServeFiles("/assets", http.Dir(filepath.Dir(tmpFile.Name())))

w := willy.New(a)
w := willie.New(a)
res := w.Request("/assets/%s", filepath.Base(tmpFile.Name())).Get()

r.Equal(200, res.Code)
Expand Down
6 changes: 3 additions & 3 deletions wrappers_test.go
Expand Up @@ -4,7 +4,7 @@ import (
"net/http"
"testing"

"github.com/markbates/going/willy"
"github.com/markbates/willie"
"github.com/stretchr/testify/require"
)

Expand All @@ -16,7 +16,7 @@ func Test_WrapHandlerFunc(t *testing.T) {
res.Write([]byte("hello"))
}))

w := willy.New(a)
w := willie.New(a)
res := w.Request("/foo").Get()

r.Equal("hello", res.Body.String())
Expand All @@ -30,7 +30,7 @@ func Test_WrapHandler(t *testing.T) {
res.Write([]byte("hello"))
})))

w := willy.New(a)
w := willie.New(a)
res := w.Request("/foo").Get()

r.Equal("hello", res.Body.String())
Expand Down

0 comments on commit 2ddffe5

Please sign in to comment.