Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
张磊
/
FileStorageBeego
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
1ff0a31d
authored
2013-09-11 15:49:12 +0800
by
astaxie
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
add router test
1 parent
e7f08946
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
99 additions
and
0 deletions
router_test.go
router_test.go
0 → 100644
View file @
1ff0a31
package
beego
import
(
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"
)
type
TestController
struct
{
Controller
}
func
(
this
*
TestController
)
Get
()
{
this
.
Data
[
"Username"
]
=
"astaxie"
this
.
Ctx
.
Output
.
Body
([]
byte
(
"ok"
))
}
func
(
this
*
TestController
)
List
()
{
this
.
Ctx
.
Output
.
Body
([]
byte
(
"i am list"
))
}
func
TestUserFunc
(
t
*
testing
.
T
)
{
r
,
_
:=
http
.
NewRequest
(
"GET"
,
"/api/list"
,
nil
)
w
:=
httptest
.
NewRecorder
()
handler
:=
NewControllerRegistor
()
handler
.
Add
(
"/api/list"
,
&
TestController
{},
"*:List"
)
handler
.
ServeHTTP
(
w
,
r
)
if
w
.
Body
.
String
()
!=
"i am list"
{
t
.
Errorf
(
"user define func can't run"
)
}
}
func
TestAutoFunc
(
t
*
testing
.
T
)
{
r
,
_
:=
http
.
NewRequest
(
"GET"
,
"/test/list"
,
nil
)
w
:=
httptest
.
NewRecorder
()
handler
:=
NewControllerRegistor
()
handler
.
AddAuto
(
&
TestController
{})
handler
.
ServeHTTP
(
w
,
r
)
if
w
.
Body
.
String
()
!=
"i am list"
{
t
.
Errorf
(
"user define func can't run"
)
}
}
func
TestRouteOk
(
t
*
testing
.
T
)
{
r
,
_
:=
http
.
NewRequest
(
"GET"
,
"/person/anderson/thomas?learn=kungfu"
,
nil
)
w
:=
httptest
.
NewRecorder
()
handler
:=
NewControllerRegistor
()
handler
.
Add
(
"/person/:last/:first"
,
&
TestController
{})
handler
.
ServeHTTP
(
w
,
r
)
lastNameParam
:=
r
.
URL
.
Query
()
.
Get
(
":last"
)
firstNameParam
:=
r
.
URL
.
Query
()
.
Get
(
":first"
)
learnParam
:=
r
.
URL
.
Query
()
.
Get
(
"learn"
)
if
lastNameParam
!=
"anderson"
{
t
.
Errorf
(
"url param set to [%s]; want [%s]"
,
lastNameParam
,
"anderson"
)
}
if
firstNameParam
!=
"thomas"
{
t
.
Errorf
(
"url param set to [%s]; want [%s]"
,
firstNameParam
,
"thomas"
)
}
if
learnParam
!=
"kungfu"
{
t
.
Errorf
(
"url param set to [%s]; want [%s]"
,
learnParam
,
"kungfu"
)
}
}
func
TestNotFound
(
t
*
testing
.
T
)
{
r
,
_
:=
http
.
NewRequest
(
"GET"
,
"/"
,
nil
)
w
:=
httptest
.
NewRecorder
()
handler
:=
NewControllerRegistor
()
handler
.
ServeHTTP
(
w
,
r
)
if
w
.
Code
!=
http
.
StatusNotFound
{
t
.
Errorf
(
"Code set to [%v]; want [%v]"
,
w
.
Code
,
http
.
StatusNotFound
)
}
}
// TestStatic tests the ability to serve static
// content from the filesystem
func
TestStatic
(
t
*
testing
.
T
)
{
r
,
_
:=
http
.
NewRequest
(
"GET"
,
"/router_test.go"
,
nil
)
w
:=
httptest
.
NewRecorder
()
pwd
,
_
:=
os
.
Getwd
()
handler
:=
NewControllerRegistor
()
SetStaticPath
(
"/"
,
pwd
)
handler
.
ServeHTTP
(
w
,
r
)
testFile
,
_
:=
ioutil
.
ReadFile
(
pwd
+
"/routes_test.go"
)
if
w
.
Body
.
String
()
!=
string
(
testFile
)
{
t
.
Errorf
(
"handler.Static failed to serve file"
)
}
}
Write
Preview
Styling with
Markdown
is supported
Attach a file
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 post a comment