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
49513148
authored
2014-03-13 22:30:12 -0700
by
Jared Folkins
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
added FlashName,FlashSeperator, & Tests
1 parent
439b1afb
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
76 additions
and
11 deletions
.gitignore
config.go
config_test.go
flash.go
flash_test.go
.gitignore
View file @
4951314
.DS_Store
*.swp
*.swo
...
...
config.go
View file @
4951314
...
...
@@ -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 @
4951314
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 @
4951314
...
...
@@ -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 @
4951314
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
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