-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwares-test[router]_test.go
145 lines (135 loc) · 3.75 KB
/
wares-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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// 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 wares_test
import (
"encoding/json"
"errors"
"io"
"net/http"
"github.com/ursiform/bear"
"github.com/ursiform/forest"
"github.com/ursiform/forest-wares"
)
// implements Populater
type postBody struct {
Foo string `json:"foo"`
}
func (pb *postBody) Populate(body io.ReadCloser) error {
return json.NewDecoder(body).Decode(pb)
}
type responseFormat struct {
Foo string `json:"foo"`
}
type router struct{ *forest.App }
func (app *router) authenticate(ctx *bear.Context) {
ctx.Set(forest.SessionID, sessionIDExistent)
ctx.Set(forest.SessionUserID, sessionUserID)
ctx.Next()
}
func (app *router) customSafeErrorFilterFailure(ctx *bear.Context) {
ctx.Set(forest.Error, errors.New(customUnsafeErrorMessage))
app.Ware("ServerError")(ctx)
}
func (app *router) customSafeErrorFilterSuccess(ctx *bear.Context) {
ctx.Set(forest.Error, errors.New(customSafeErrorMessage))
app.Ware("ServerError")(ctx)
}
func (app *router) initPostParse(ctx *bear.Context) {
ctx.Set(forest.Body, new(postBody)).Next()
}
func (app *router) respondSuccess(ctx *bear.Context) {
data := &responseFormat{Foo: "foo"}
app.Response(
ctx,
http.StatusOK,
forest.Success,
forest.NoMessage).Write(data)
}
func (app *router) sessionCreateError(ctx *bear.Context) {
ctx.Set("testerror", true).Next()
}
func (app *router) sessionDelIntercept(ctx *bear.Context) {
sessionID := ctx.Get(forest.SessionID).(string)
if sessionID == sessionIDWithSelfDestruct {
ctx.Set(forest.SessionID, nil)
}
if sessionID == sessionIDWithUserDestruct {
ctx.Set(forest.SessionUserID, nil)
}
ctx.Next()
}
func (app *router) sessionVerify(ctx *bear.Context) {
_, ok := ctx.Get(forest.SessionID).(string)
if !ok {
ctx.Set(forest.Error, errors.New("sessionVerify failed"))
app.Ware("ServerError")(ctx)
return
} else {
ctx.Next()
}
}
func (app *router) Route(path string) {
app.On("GET", path,
app.respondSuccess)
app.On("GET", path+"/authenticate/failure",
app.Ware("Authenticate"),
app.respondSuccess)
app.On("GET", path+"/authenticate/success",
app.authenticate,
app.Ware("Authenticate"),
app.respondSuccess)
app.On("GET", path+"/bad-request",
app.Ware("BadRequest"))
app.On("GET", path+"/conflict",
app.Ware("Conflict"))
app.On("GET", path+"/csrf",
app.authenticate,
app.Ware("CSRF"),
app.respondSuccess)
app.On("GET", path+"/not-found",
app.Ware("NotFound"))
app.On("GET", path+"/safe-error/failure",
app.customSafeErrorFilterFailure)
app.On("GET", path+"/safe-error/success",
app.customSafeErrorFilterSuccess)
app.On("GET", path+"/server-error",
app.Ware("ServerError"))
app.On("GET", path+"/session-del",
app.Ware("SessionGet"),
app.sessionDelIntercept,
app.Ware("SessionDel"),
app.respondSuccess)
app.On("GET", path+"/session-get",
app.Ware("SessionGet"),
app.sessionVerify,
app.respondSuccess)
app.On("GET", path+"/session-get/create-error",
app.sessionCreateError,
app.Ware("SessionGet"),
app.sessionVerify,
app.respondSuccess)
app.On("GET", path+"/session-set",
app.Ware("SessionGet"),
app.Ware("SessionSet"),
app.respondSuccess)
app.On("GET", path+"/unauthorized",
app.Ware("Unauthorized"))
app.On("POST", path+"/body-parser/failure/no-init",
app.Ware("BodyParser"),
app.respondSuccess)
app.On("POST", path+"/body-parser/success",
app.initPostParse,
app.Ware("BodyParser"),
app.respondSuccess)
app.On("*", path,
app.Ware("MethodNotAllowed"))
}
func newRouter(parent *forest.App) *router {
manager := new(sessionManager)
wares.InstallBodyParser(parent)
wares.InstallErrorWares(parent)
wares.InstallSecurityWares(parent)
wares.InstallSessionWares(parent, manager)
return &router{parent}
}