1
0

flash_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2014 beego Author. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package beego
  15. import (
  16. "net/http"
  17. "net/http/httptest"
  18. "strings"
  19. "testing"
  20. )
  21. type TestFlashController struct {
  22. Controller
  23. }
  24. func (t *TestFlashController) TestWriteFlash() {
  25. flash := NewFlash()
  26. flash.Notice("TestFlashString")
  27. flash.Store(&t.Controller)
  28. // we choose to serve json because we don't want to load a template html file
  29. t.ServeJSON(true)
  30. }
  31. func TestFlashHeader(t *testing.T) {
  32. // create fake GET request
  33. r, _ := http.NewRequest("GET", "/", nil)
  34. w := httptest.NewRecorder()
  35. // setup the handler
  36. handler := NewControllerRegister()
  37. handler.Add("/", &TestFlashController{}, "get:TestWriteFlash")
  38. handler.ServeHTTP(w, r)
  39. // get the Set-Cookie value
  40. sc := w.Header().Get("Set-Cookie")
  41. // match for the expected header
  42. res := strings.Contains(sc, "BEEGO_FLASH=%00notice%23BEEGOFLASH%23TestFlashString%00")
  43. // validate the assertion
  44. if res != true {
  45. t.Errorf("TestFlashHeader() unable to validate flash message")
  46. }
  47. }