-
Notifications
You must be signed in to change notification settings - Fork 0
/
forest-test[router]_test.go
48 lines (40 loc) · 1.15 KB
/
forest-test[router]_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright 2015 Afshin Darian. All rights reserved.
// Use of this source code is governed by The MIT License
// that can be found in the LICENSE file.
package forest_test
import (
"github.com/ursiform/bear"
"github.com/ursiform/forest"
"github.com/ursiform/forest-wares"
"net/http"
)
type responseFormat struct {
Foo string `json:"foo"`
}
type router struct{ *forest.App }
func (app *router) respondSuccess(
_ http.ResponseWriter, _ *http.Request, ctx *bear.Context) {
data := &responseFormat{Foo: "foo"}
app.Response(ctx,
http.StatusOK, forest.Success, forest.NoMessage).Write(data)
}
func (app *router) setCookie(
_ http.ResponseWriter, _ *http.Request, ctx *bear.Context) {
path := "/"
cookieName := "foo"
cookieValue := "bar"
app.SetCookie(ctx, path, cookieName, cookieValue, app.Duration("Cookie"))
ctx.Next()
}
func (app *router) Route(path string) {
app.On("GET", path+"/nonexistent",
app.Ware("NonExistent"), app.respondSuccess)
app.On("GET", path,
app.setCookie, app.respondSuccess)
app.On("*", path,
app.Ware("MethodNotAllowed"))
}
func newRouter(parent *forest.App) *router {
wares.InstallErrorWares(parent)
return &router{parent}
}