Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
47b23872
Commit
47b23872
authored
Aug 13, 2016
by
astaxie
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'develop' of
https://github.com/astaxie/beego
into develop
parents
1b4f30e1
61c9387e
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
116 additions
and
13 deletions
+116
-13
input.go
context/input.go
+8
-0
filter.go
filter.go
+1
-0
router.go
router.go
+32
-13
router_test.go
router_test.go
+75
-0
No files found.
context/input.go
View file @
47b23872
...
...
@@ -301,6 +301,14 @@ func (input *BeegoInput) SetParam(key, val string) {
input
.
pnames
=
append
(
input
.
pnames
,
key
)
}
// ResetParams clears any of the input's Params
// This function is used to clear parameters so they may be reset between filter
// passes.
func
(
input
*
BeegoInput
)
ResetParams
()
{
input
.
pnames
=
input
.
pnames
[
:
0
]
input
.
pvalues
=
input
.
pvalues
[
:
0
]
}
// Query returns input data item string by a given string.
func
(
input
*
BeegoInput
)
Query
(
key
string
)
string
{
if
val
:=
input
.
Param
(
key
);
val
!=
""
{
...
...
filter.go
View file @
47b23872
...
...
@@ -27,6 +27,7 @@ type FilterRouter struct {
tree
*
Tree
pattern
string
returnOnOutput
bool
resetParams
bool
}
// ValidRouter checks if the current request is matched by this filter.
...
...
router.go
View file @
47b23872
...
...
@@ -406,20 +406,27 @@ func (p *ControllerRegister) AddAutoPrefix(prefix string, c ControllerInterface)
}
// InsertFilter Add a FilterFunc with pattern rule and action constant.
// The bool params is for setting the returnOnOutput value (false allows multiple filters to execute)
// params is for:
// 1. setting the returnOnOutput value (false allows multiple filters to execute)
// 2. determining whether or not params need to be reset.
func
(
p
*
ControllerRegister
)
InsertFilter
(
pattern
string
,
pos
int
,
filter
FilterFunc
,
params
...
bool
)
error
{
mr
:=
new
(
FilterRouter
)
mr
.
tree
=
NewTree
()
mr
.
pattern
=
pattern
mr
.
filterFunc
=
filter
mr
:=
&
FilterRouter
{
tree
:
NewTree
(),
pattern
:
pattern
,
filterFunc
:
filter
,
returnOnOutput
:
true
,
}
if
!
BConfig
.
RouterCaseSensitive
{
pattern
=
strings
.
ToLower
(
pattern
)
mr
.
pattern
=
strings
.
ToLower
(
pattern
)
}
if
len
(
params
)
==
0
{
mr
.
returnOnOutput
=
true
}
else
{
paramsLen
:=
len
(
params
)
if
paramsLen
>
0
{
mr
.
returnOnOutput
=
params
[
0
]
}
if
paramsLen
>
1
{
mr
.
resetParams
=
params
[
1
]
}
mr
.
tree
.
AddRouter
(
pattern
,
true
)
return
p
.
insertFilterRouter
(
pos
,
mr
)
}
...
...
@@ -581,12 +588,22 @@ func (p *ControllerRegister) geturl(t *Tree, url, controllName, methodName strin
}
func
(
p
*
ControllerRegister
)
execFilter
(
context
*
beecontext
.
Context
,
urlPath
string
,
pos
int
)
(
started
bool
)
{
var
preFilterParams
map
[
string
]
string
for
_
,
filterR
:=
range
p
.
filters
[
pos
]
{
if
filterR
.
returnOnOutput
&&
context
.
ResponseWriter
.
Started
{
return
true
}
if
filterR
.
resetParams
{
preFilterParams
=
context
.
Input
.
Params
()
}
if
ok
:=
filterR
.
ValidRouter
(
urlPath
,
context
);
ok
{
filterR
.
filterFunc
(
context
)
if
filterR
.
resetParams
{
context
.
Input
.
ResetParams
()
for
k
,
v
:=
range
preFilterParams
{
context
.
Input
.
SetParam
(
k
,
v
)
}
}
}
if
filterR
.
returnOnOutput
&&
context
.
ResponseWriter
.
Started
{
return
true
...
...
@@ -810,7 +827,9 @@ Admin:
var
devInfo
string
statusCode
:=
context
.
ResponseWriter
.
Status
if
statusCode
==
0
{
statusCode
=
200
}
if
statusCode
==
0
{
statusCode
=
200
}
iswin
:=
(
runtime
.
GOOS
==
"windows"
)
statusColor
:=
logs
.
ColorByStatus
(
iswin
,
statusCode
)
...
...
@@ -819,9 +838,9 @@ Admin:
if
findRouter
{
if
routerInfo
!=
nil
{
devInfo
=
fmt
.
Sprintf
(
"|%s %3d %s|%13s|%8s|%s %s %-7s %-3s r:%s"
,
statusColor
,
statusCode
,
resetColor
,
timeDur
.
String
(),
"match"
,
methodColor
,
resetColor
,
r
.
Method
,
r
.
URL
.
Path
,
routerInfo
.
pattern
)
devInfo
=
fmt
.
Sprintf
(
"|%s %3d %s|%13s|%8s|%s %s %-7s %-3s r:%s"
,
statusColor
,
statusCode
,
resetColor
,
timeDur
.
String
(),
"match"
,
methodColor
,
resetColor
,
r
.
Method
,
r
.
URL
.
Path
,
routerInfo
.
pattern
)
}
else
{
devInfo
=
fmt
.
Sprintf
(
"|%s %3d %s|%13s|%8s|%s %s %-7s %-3s"
,
statusColor
,
statusCode
,
resetColor
,
timeDur
.
String
(),
"match"
,
methodColor
,
resetColor
,
r
.
Method
,
r
.
URL
.
Path
)
...
...
router_test.go
View file @
47b23872
...
...
@@ -420,6 +420,74 @@ func testRequest(method, path string) (*httptest.ResponseRecorder, *http.Request
return
recorder
,
request
}
// Expectation: A Filter with the correct configuration should be created given
// specific parameters.
func
TestInsertFilter
(
t
*
testing
.
T
)
{
testName
:=
"TestInsertFilter"
mux
:=
NewControllerRegister
()
mux
.
InsertFilter
(
"*"
,
BeforeRouter
,
func
(
*
context
.
Context
)
{})
if
!
mux
.
filters
[
BeforeRouter
][
0
]
.
returnOnOutput
{
t
.
Errorf
(
"%s: passing no variadic params should set returnOnOutput to true"
,
testName
)
}
if
mux
.
filters
[
BeforeRouter
][
0
]
.
resetParams
{
t
.
Errorf
(
"%s: passing no variadic params should set resetParams to false"
,
testName
)
}
mux
=
NewControllerRegister
()
mux
.
InsertFilter
(
"*"
,
BeforeRouter
,
func
(
*
context
.
Context
)
{},
false
)
if
mux
.
filters
[
BeforeRouter
][
0
]
.
returnOnOutput
{
t
.
Errorf
(
"%s: passing false as 1st variadic param should set returnOnOutput to false"
,
testName
)
}
mux
=
NewControllerRegister
()
mux
.
InsertFilter
(
"*"
,
BeforeRouter
,
func
(
*
context
.
Context
)
{},
true
,
true
)
if
!
mux
.
filters
[
BeforeRouter
][
0
]
.
resetParams
{
t
.
Errorf
(
"%s: passing true as 2nd variadic param should set resetParams to true"
,
testName
)
}
}
// Expectation: the second variadic arg should cause the execution of the filter
// to preserve the parameters from before its execution.
func
TestParamResetFilter
(
t
*
testing
.
T
)
{
testName
:=
"TestParamResetFilter"
route
:=
"/beego/*"
// splat
path
:=
"/beego/routes/routes"
mux
:=
NewControllerRegister
()
mux
.
InsertFilter
(
"*"
,
BeforeExec
,
beegoResetParams
,
true
,
true
)
mux
.
Get
(
route
,
beegoHandleResetParams
)
rw
,
r
:=
testRequest
(
"GET"
,
path
)
mux
.
ServeHTTP
(
rw
,
r
)
// The two functions, `beegoResetParams` and `beegoHandleResetParams` add
// a response header of `Splat`. The expectation here is that that Header
// value should match what the _request's_ router set, not the filter's.
headers
:=
rw
.
HeaderMap
if
len
(
headers
[
"Splat"
])
!=
1
{
t
.
Errorf
(
"%s: There was an error in the test. Splat param not set in Header"
,
testName
)
}
if
headers
[
"Splat"
][
0
]
!=
"routes/routes"
{
t
.
Errorf
(
"%s: expected `:splat` param to be [routes/routes] but it was [%s]"
,
testName
,
headers
[
"Splat"
][
0
])
}
}
// Execution point: BeforeRouter
// expectation: only BeforeRouter function is executed, notmatch output as router doesn't handle
func
TestFilterBeforeRouter
(
t
*
testing
.
T
)
{
...
...
@@ -612,3 +680,10 @@ func beegoFinishRouter1(ctx *context.Context) {
func
beegoFinishRouter2
(
ctx
*
context
.
Context
)
{
ctx
.
WriteString
(
"|FinishRouter2"
)
}
func
beegoResetParams
(
ctx
*
context
.
Context
)
{
ctx
.
ResponseWriter
.
Header
()
.
Set
(
"splat"
,
ctx
.
Input
.
Param
(
":splat"
))
}
func
beegoHandleResetParams
(
ctx
*
context
.
Context
)
{
ctx
.
ResponseWriter
.
Header
()
.
Set
(
"splat"
,
ctx
.
Input
.
Param
(
":splat"
))
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment