PageRenderTime 28ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/session_test.go

https://gitlab.com/tamasd/ab
Go | 130 lines | 93 code | 24 blank | 13 comment | 1 complexity | 6c70e415548ff33eb6a103409c32acc9 MD5 | raw file
  1. // Copyright 2015 Tamás Demeter-Haludka
  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 ab
  15. import (
  16. "crypto/rand"
  17. "encoding/hex"
  18. "io"
  19. "io/ioutil"
  20. "net/http"
  21. "net/http/cookiejar"
  22. "strings"
  23. "testing"
  24. "time"
  25. . "github.com/smartystreets/goconvey/convey"
  26. "gitlab.com/tamasd/ab/lib/log"
  27. "golang.org/x/net/publicsuffix"
  28. )
  29. func getSecretKey() SecretKey {
  30. buf := make([]byte, 32)
  31. rand.Read(buf)
  32. return SecretKey(buf)
  33. }
  34. func randomString(length int) string {
  35. buf := make([]byte, length)
  36. rand.Read(buf)
  37. return hex.EncodeToString(buf)
  38. }
  39. func getBody(body io.ReadCloser) string {
  40. data, err := ioutil.ReadAll(body)
  41. So(err, ShouldBeNil)
  42. return string(data)
  43. }
  44. func TestEncDec(t *testing.T) {
  45. Convey("Tests encoding and decoding the cookie data", t, func() {
  46. key := getSecretKey()
  47. sess := Session{}
  48. sess["foo"] = "bar"
  49. sess["bar"] = "baz"
  50. c := sess.cookie(key, "", nil, time.Hour)
  51. data := c.Value
  52. s, err := readCookie(data, key)
  53. So(err, ShouldBeNil)
  54. So(s, ShouldResemble, sess)
  55. })
  56. }
  57. func TestHTTPScenario(t *testing.T) {
  58. Convey("Given a simple HTTP scenario", t, func() {
  59. key := getSecretKey()
  60. srv := NewServer(nil)
  61. srv.Use(DefaultLoggerMiddleware(log.LOG_OFF))
  62. srv.Use(SessionMiddleware("", key, nil, time.Hour))
  63. srv.Post("/set", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  64. data, _ := ioutil.ReadAll(r.Body)
  65. s := GetSession(r)
  66. s["data"] = string(data)
  67. }))
  68. srv.Get("/get", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  69. s := GetSession(r)
  70. io.WriteString(w, s["data"])
  71. }))
  72. srv.Get("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  73. s := GetSession(r)
  74. io.WriteString(w, s.Id())
  75. }))
  76. go http.ListenAndServe("localhost:31337", srv.Handler())
  77. <-time.After(time.Second)
  78. jar, _ := cookiejar.New(&cookiejar.Options{
  79. PublicSuffixList: publicsuffix.List,
  80. })
  81. baseURL := "http://localhost:31337"
  82. c := &http.Client{
  83. Jar: jar,
  84. }
  85. data := randomString(128)
  86. Convey("A session ID should be created", func() {
  87. r, err := c.Get(baseURL + "/")
  88. So(err, ShouldBeNil)
  89. sid := getBody(r.Body)
  90. Convey("And that session ID should be the same over different requests", func() {
  91. r, err := c.Get(baseURL + "/")
  92. So(err, ShouldBeNil)
  93. So(getBody(r.Body), ShouldEqual, sid)
  94. })
  95. })
  96. Convey("When a request is made to the post endpoint", func() {
  97. _, err := c.Post(baseURL+"/set", "text/plain", strings.NewReader(data))
  98. So(err, ShouldBeNil)
  99. Convey("The result at the get endpoint should match it", func() {
  100. r, err := c.Get(baseURL + "/get")
  101. So(err, ShouldBeNil)
  102. So(getBody(r.Body), ShouldEqual, data)
  103. })
  104. })
  105. })
  106. }