added FlashName,FlashSeperator, & Tests
Showing
5 changed files
with
76 additions
and
11 deletions
| ... | @@ -58,6 +58,8 @@ var ( | ... | @@ -58,6 +58,8 @@ var ( |
| 58 | EnableAdmin bool // flag of enable admin module to log every request info. | 58 | EnableAdmin bool // flag of enable admin module to log every request info. |
| 59 | AdminHttpAddr string // http server configurations for admin module. | 59 | AdminHttpAddr string // http server configurations for admin module. |
| 60 | AdminHttpPort int | 60 | AdminHttpPort int |
| 61 | FlashName string // name of the flash variable found in response header and cookie | ||
| 62 | FlashSeperator string // used to seperate flash key:value | ||
| 61 | ) | 63 | ) |
| 62 | 64 | ||
| 63 | func init() { | 65 | func init() { |
| ... | @@ -123,6 +125,9 @@ func init() { | ... | @@ -123,6 +125,9 @@ func init() { |
| 123 | AdminHttpAddr = "127.0.0.1" | 125 | AdminHttpAddr = "127.0.0.1" |
| 124 | AdminHttpPort = 8088 | 126 | AdminHttpPort = 8088 |
| 125 | 127 | ||
| 128 | FlashName = "BEEGO_FLASH" | ||
| 129 | FlashSeperator = "BEEGOFLASH" | ||
| 130 | |||
| 126 | runtime.GOMAXPROCS(runtime.NumCPU()) | 131 | runtime.GOMAXPROCS(runtime.NumCPU()) |
| 127 | 132 | ||
| 128 | // init BeeLogger | 133 | // init BeeLogger |
| ... | @@ -271,6 +276,14 @@ func ParseConfig() (err error) { | ... | @@ -271,6 +276,14 @@ func ParseConfig() (err error) { |
| 271 | BeegoServerName = serverName | 276 | BeegoServerName = serverName |
| 272 | } | 277 | } |
| 273 | 278 | ||
| 279 | if flashname := AppConfig.String("FlashName"); flashname != "" { | ||
| 280 | FlashName = flashname | ||
| 281 | } | ||
| 282 | |||
| 283 | if flashseperator := AppConfig.String("FlashSeperator"); flashseperator != "" { | ||
| 284 | FlashSeperator = flashseperator | ||
| 285 | } | ||
| 286 | |||
| 274 | if sd := AppConfig.String("StaticDir"); sd != "" { | 287 | if sd := AppConfig.String("StaticDir"); sd != "" { |
| 275 | for k := range StaticDir { | 288 | for k := range StaticDir { |
| 276 | delete(StaticDir, k) | 289 | delete(StaticDir, k) | ... | ... |
config_test.go
0 → 100644
| ... | @@ -6,9 +6,6 @@ import ( | ... | @@ -6,9 +6,6 @@ import ( |
| 6 | "strings" | 6 | "strings" |
| 7 | ) | 7 | ) |
| 8 | 8 | ||
| 9 | // the separation string when encoding flash data. | ||
| 10 | const BEEGO_FLASH_SEP = "#BEEGOFLASH#" | ||
| 11 | |||
| 12 | // FlashData is a tools to maintain data when using across request. | 9 | // FlashData is a tools to maintain data when using across request. |
| 13 | type FlashData struct { | 10 | type FlashData struct { |
| 14 | Data map[string]string | 11 | Data map[string]string |
| ... | @@ -54,29 +51,27 @@ func (fd *FlashData) Store(c *Controller) { | ... | @@ -54,29 +51,27 @@ func (fd *FlashData) Store(c *Controller) { |
| 54 | c.Data["flash"] = fd.Data | 51 | c.Data["flash"] = fd.Data |
| 55 | var flashValue string | 52 | var flashValue string |
| 56 | for key, value := range fd.Data { | 53 | for key, value := range fd.Data { |
| 57 | flashValue += "\x00" + key + BEEGO_FLASH_SEP + value + "\x00" | 54 | flashValue += "\x00" + key + "\x23" + FlashSeperator + "\x23" + value + "\x00" |
| 58 | } | 55 | } |
| 59 | c.Ctx.SetCookie("BEEGO_FLASH", url.QueryEscape(flashValue), 0, "/") | 56 | c.Ctx.SetCookie(FlashName, url.QueryEscape(flashValue), 0, "/") |
| 60 | } | 57 | } |
| 61 | 58 | ||
| 62 | // ReadFromRequest parsed flash data from encoded values in cookie. | 59 | // ReadFromRequest parsed flash data from encoded values in cookie. |
| 63 | func ReadFromRequest(c *Controller) *FlashData { | 60 | func ReadFromRequest(c *Controller) *FlashData { |
| 64 | flash := &FlashData{ | 61 | flash := NewFlash() |
| 65 | Data: make(map[string]string), | 62 | if cookie, err := c.Ctx.Request.Cookie(FlashName); err == nil { |
| 66 | } | ||
| 67 | if cookie, err := c.Ctx.Request.Cookie("BEEGO_FLASH"); err == nil { | ||
| 68 | v, _ := url.QueryUnescape(cookie.Value) | 63 | v, _ := url.QueryUnescape(cookie.Value) |
| 69 | vals := strings.Split(v, "\x00") | 64 | vals := strings.Split(v, "\x00") |
| 70 | for _, v := range vals { | 65 | for _, v := range vals { |
| 71 | if len(v) > 0 { | 66 | if len(v) > 0 { |
| 72 | kv := strings.Split(v, BEEGO_FLASH_SEP) | 67 | kv := strings.Split(v, FlashSeperator) |
| 73 | if len(kv) == 2 { | 68 | if len(kv) == 2 { |
| 74 | flash.Data[kv[0]] = kv[1] | 69 | flash.Data[kv[0]] = kv[1] |
| 75 | } | 70 | } |
| 76 | } | 71 | } |
| 77 | } | 72 | } |
| 78 | //read one time then delete it | 73 | //read one time then delete it |
| 79 | c.Ctx.SetCookie("BEEGO_FLASH", "", -1, "/") | 74 | c.Ctx.SetCookie(FlashName, "", -1, "/") |
| 80 | } | 75 | } |
| 81 | c.Data["flash"] = flash.Data | 76 | c.Data["flash"] = flash.Data |
| 82 | return flash | 77 | return flash | ... | ... |
flash_test.go
0 → 100644
| 1 | package beego | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "net/http" | ||
| 5 | "net/http/httptest" | ||
| 6 | "strings" | ||
| 7 | "testing" | ||
| 8 | ) | ||
| 9 | |||
| 10 | type TestFlashController struct { | ||
| 11 | Controller | ||
| 12 | } | ||
| 13 | |||
| 14 | func (this *TestFlashController) TestWriteFlash() { | ||
| 15 | flash := NewFlash() | ||
| 16 | flash.Notice("TestFlashString") | ||
| 17 | flash.Store(&this.Controller) | ||
| 18 | // we choose to serve json because we don't want to load a template html file | ||
| 19 | this.ServeJson(true) | ||
| 20 | } | ||
| 21 | |||
| 22 | func TestFlashHeader(t *testing.T) { | ||
| 23 | // create fake GET request | ||
| 24 | r, _ := http.NewRequest("GET", "/", nil) | ||
| 25 | w := httptest.NewRecorder() | ||
| 26 | |||
| 27 | // setup the handler | ||
| 28 | handler := NewControllerRegistor() | ||
| 29 | handler.Add("/", &TestFlashController{}, "get:TestWriteFlash") | ||
| 30 | handler.ServeHTTP(w, r) | ||
| 31 | |||
| 32 | // get the Set-Cookie value | ||
| 33 | sc := w.Header().Get("Set-Cookie") | ||
| 34 | // match for the expected header | ||
| 35 | res := strings.Contains(sc, "BEEGO_FLASH=%00notice%23BEEGOFLASH%23TestFlashString%00") | ||
| 36 | // validate the assertion | ||
| 37 | if res != true { | ||
| 38 | t.Errorf("TestFlashHeader() unable to validate flash message") | ||
| 39 | } | ||
| 40 | } |
-
Please register or sign in to post a comment