context_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright 2016 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 context
  15. import (
  16. "net/http"
  17. "net/http/httptest"
  18. "testing"
  19. )
  20. func TestXsrfReset_01(t *testing.T) {
  21. r := &http.Request{}
  22. c := NewContext()
  23. c.Request = r
  24. c.ResponseWriter = &Response{}
  25. c.ResponseWriter.reset(httptest.NewRecorder())
  26. c.Output.Reset(c)
  27. c.Input.Reset(c)
  28. c.XSRFToken("key", 16)
  29. if c._xsrfToken == "" {
  30. t.FailNow()
  31. }
  32. token := c._xsrfToken
  33. c.Reset(&Response{ResponseWriter: httptest.NewRecorder()}, r)
  34. if c._xsrfToken != "" {
  35. t.FailNow()
  36. }
  37. c.XSRFToken("key", 16)
  38. if c._xsrfToken == "" {
  39. t.FailNow()
  40. }
  41. if token == c._xsrfToken {
  42. t.FailNow()
  43. }
  44. }