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
49513148
Commit
49513148
authored
Mar 14, 2014
by
Jared Folkins
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added FlashName,FlashSeperator, & Tests
parent
439b1afb
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
76 additions
and
11 deletions
+76
-11
.gitignore
.gitignore
+2
-0
config.go
config.go
+13
-0
config_test.go
config_test.go
+15
-0
flash.go
flash.go
+6
-11
flash_test.go
flash_test.go
+40
-0
No files found.
.gitignore
View file @
49513148
.DS_Store
*.swp
*.swo
config.go
View file @
49513148
...
...
@@ -58,6 +58,8 @@ var (
EnableAdmin
bool
// flag of enable admin module to log every request info.
AdminHttpAddr
string
// http server configurations for admin module.
AdminHttpPort
int
FlashName
string
// name of the flash variable found in response header and cookie
FlashSeperator
string
// used to seperate flash key:value
)
func
init
()
{
...
...
@@ -123,6 +125,9 @@ func init() {
AdminHttpAddr
=
"127.0.0.1"
AdminHttpPort
=
8088
FlashName
=
"BEEGO_FLASH"
FlashSeperator
=
"BEEGOFLASH"
runtime
.
GOMAXPROCS
(
runtime
.
NumCPU
())
// init BeeLogger
...
...
@@ -271,6 +276,14 @@ func ParseConfig() (err error) {
BeegoServerName
=
serverName
}
if
flashname
:=
AppConfig
.
String
(
"FlashName"
);
flashname
!=
""
{
FlashName
=
flashname
}
if
flashseperator
:=
AppConfig
.
String
(
"FlashSeperator"
);
flashseperator
!=
""
{
FlashSeperator
=
flashseperator
}
if
sd
:=
AppConfig
.
String
(
"StaticDir"
);
sd
!=
""
{
for
k
:=
range
StaticDir
{
delete
(
StaticDir
,
k
)
...
...
config_test.go
0 → 100644
View file @
49513148
package
beego
import
(
"testing"
)
func
TestDefaults
(
t
*
testing
.
T
)
{
if
FlashName
!=
"BEEGO_FLASH"
{
t
.
Errorf
(
"FlashName was not set to default."
)
}
if
FlashSeperator
!=
"BEEGOFLASH"
{
t
.
Errorf
(
"FlashName was not set to default."
)
}
}
flash.go
View file @
49513148
...
...
@@ -6,9 +6,6 @@ import (
"strings"
)
// the separation string when encoding flash data.
const
BEEGO_FLASH_SEP
=
"#BEEGOFLASH#"
// FlashData is a tools to maintain data when using across request.
type
FlashData
struct
{
Data
map
[
string
]
string
...
...
@@ -54,29 +51,27 @@ func (fd *FlashData) Store(c *Controller) {
c
.
Data
[
"flash"
]
=
fd
.
Data
var
flashValue
string
for
key
,
value
:=
range
fd
.
Data
{
flashValue
+=
"
\x00
"
+
key
+
BEEGO_FLASH_SEP
+
value
+
"
\x00
"
flashValue
+=
"
\x00
"
+
key
+
"
\x23
"
+
FlashSeperator
+
"
\x23
"
+
value
+
"
\x00
"
}
c
.
Ctx
.
SetCookie
(
"BEEGO_FLASH"
,
url
.
QueryEscape
(
flashValue
),
0
,
"/"
)
c
.
Ctx
.
SetCookie
(
FlashName
,
url
.
QueryEscape
(
flashValue
),
0
,
"/"
)
}
// ReadFromRequest parsed flash data from encoded values in cookie.
func
ReadFromRequest
(
c
*
Controller
)
*
FlashData
{
flash
:=
&
FlashData
{
Data
:
make
(
map
[
string
]
string
),
}
if
cookie
,
err
:=
c
.
Ctx
.
Request
.
Cookie
(
"BEEGO_FLASH"
);
err
==
nil
{
flash
:=
NewFlash
()
if
cookie
,
err
:=
c
.
Ctx
.
Request
.
Cookie
(
FlashName
);
err
==
nil
{
v
,
_
:=
url
.
QueryUnescape
(
cookie
.
Value
)
vals
:=
strings
.
Split
(
v
,
"
\x00
"
)
for
_
,
v
:=
range
vals
{
if
len
(
v
)
>
0
{
kv
:=
strings
.
Split
(
v
,
BEEGO_FLASH_SEP
)
kv
:=
strings
.
Split
(
v
,
FlashSeperator
)
if
len
(
kv
)
==
2
{
flash
.
Data
[
kv
[
0
]]
=
kv
[
1
]
}
}
}
//read one time then delete it
c
.
Ctx
.
SetCookie
(
"BEEGO_FLASH"
,
""
,
-
1
,
"/"
)
c
.
Ctx
.
SetCookie
(
FlashName
,
""
,
-
1
,
"/"
)
}
c
.
Data
[
"flash"
]
=
flash
.
Data
return
flash
...
...
flash_test.go
0 → 100644
View file @
49513148
package
beego
import
(
"net/http"
"net/http/httptest"
"strings"
"testing"
)
type
TestFlashController
struct
{
Controller
}
func
(
this
*
TestFlashController
)
TestWriteFlash
()
{
flash
:=
NewFlash
()
flash
.
Notice
(
"TestFlashString"
)
flash
.
Store
(
&
this
.
Controller
)
// we choose to serve json because we don't want to load a template html file
this
.
ServeJson
(
true
)
}
func
TestFlashHeader
(
t
*
testing
.
T
)
{
// create fake GET request
r
,
_
:=
http
.
NewRequest
(
"GET"
,
"/"
,
nil
)
w
:=
httptest
.
NewRecorder
()
// setup the handler
handler
:=
NewControllerRegistor
()
handler
.
Add
(
"/"
,
&
TestFlashController
{},
"get:TestWriteFlash"
)
handler
.
ServeHTTP
(
w
,
r
)
// get the Set-Cookie value
sc
:=
w
.
Header
()
.
Get
(
"Set-Cookie"
)
// match for the expected header
res
:=
strings
.
Contains
(
sc
,
"BEEGO_FLASH=%00notice%23BEEGOFLASH%23TestFlashString%00"
)
// validate the assertion
if
res
!=
true
{
t
.
Errorf
(
"TestFlashHeader() unable to validate flash message"
)
}
}
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