Exemplo n.º 1
0
func Test_WillieSessions(t *testing.T) {
	r := require.New(t)
	w := willie.New(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
		defer context.Clear(req)
		sess, _ := Store.Get(req, "my-session")
		t := sess.Values["foo"]
		fmt.Printf("t: %s\n", t)
		if t != nil {
			res.WriteHeader(200)
			fmt.Fprint(res, t)
		} else {
			sess.Values["foo"] = "bar"
			sess.Save(req, res)
			res.WriteHeader(201)
			fmt.Fprint(res, "setting session")
		}
	}))

	res := w.Get("/", nil)
	r.Equal(201, res.Code)
	r.Equal("setting session", res.Body.String())

	res = w.Get("/", nil)
	r.Equal(200, res.Code)
	r.Equal("bar", res.Body.String())
}
Exemplo n.º 2
0
func Test_Willie(t *testing.T) {
	a := require.New(t)
	w := willie.New(app())

	m := map[string]methodHandler{
		"get":    w.JSONGet,
		"post":   w.JSONPost,
		"put":    w.JSONPut,
		"delete": w.JSONDelete,
	}
	for k, h := range m {
		res := h("/"+k, []string{"a", "b"})
		r := &record{}
		res.Bind(r)
		a.Equal(strings.ToUpper(k), r.Method)
		a.Equal(`["a","b"]`, string(r.Body))
	}
}