cookies使用

1.基础cookies操作

目录结构

主目录basic

  1. —— main.go
  2. —— main_test.go

代码示例

main.go

  1. package main
  2. import "github.com/kataras/iris"
  3. func newApp() *iris.Application {
  4. app := iris.New()
  5. app.Get("/cookies/{name}/{value}", func(ctx iris.Context) {
  6. name := ctx.Params().Get("name")
  7. value := ctx.Params().Get("value")
  8. ctx.SetCookieKV(name, value) // <-- 设置一个Cookie
  9. // 另外也可以用: ctx.SetCookie(&http.Cookie{...})
  10. // 如果要设置自定义存放路径:
  11. // ctx.SetCookieKV(name, value, iris.CookiePath("/custom/path/cookie/will/be/stored"))
  12. ctx.Request().Cookie(name)
  13. //如果您希望仅对当前请求路径可见:
  14. //(请注意,如果服务器发送空cookie的路径,所有浏览器都兼容,将会使用客户端自定义路径)
  15. // ctx.SetCookieKV(name, value, iris.CookieCleanPath /* or iris.CookiePath("") */)
  16. // 学习更多:
  17. // iris.CookieExpires(time.Duration)
  18. // iris.CookieHTTPOnly(false)
  19. ctx.Writef("cookie added: %s = %s", name, value)
  20. })
  21. app.Get("/cookies/{name}", func(ctx iris.Context) {
  22. name := ctx.Params().Get("name")
  23. value := ctx.GetCookie(name) // <-- 检索,获取Cookie
  24. //判断命名cookie不存在,再获取值
  25. // cookie, err := ctx.Request().Cookie(name)
  26. // if err != nil {
  27. // handle error.
  28. // }
  29. ctx.WriteString(value)
  30. })
  31. app.Delete("/cookies/{name}", func(ctx iris.Context) {
  32. name := ctx.Params().Get("name")
  33. ctx.RemoveCookie(name) // <-- 删除Cookie
  34. //如果要设置自定义路径:
  35. // ctx.SetCookieKV(name, value, iris.CookiePath("/custom/path/cookie/will/be/stored"))
  36. ctx.Writef("cookie %s removed", name)
  37. })
  38. return app
  39. }
  40. func main() {
  41. app := newApp()
  42. // GET: http://localhost:8080/cookies/my_name/my_value
  43. // GET: http://localhost:8080/cookies/my_name
  44. // DELETE: http://localhost:8080/cookies/my_name
  45. app.Run(iris.Addr(":8080"))
  46. }

main_test.go

  1. package main
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/kataras/iris/httptest"
  6. )
  7. func TestCookiesBasic(t *testing.T) {
  8. app := newApp()
  9. e := httptest.New(t, app, httptest.URL("http://example.com"))
  10. cookieName, cookieValue := "my_cookie_name", "my_cookie_value"
  11. // Test Set A Cookie.
  12. t1 := e.GET(fmt.Sprintf("/cookies/%s/%s", cookieName, cookieValue)).Expect().Status(httptest.StatusOK)
  13. t1.Cookie(cookieName).Value().Equal(cookieValue) // validate cookie's existence, it should be there now.
  14. t1.Body().Contains(cookieValue)
  15. // Test Retrieve A Cookie.
  16. t2 := e.GET(fmt.Sprintf("/cookies/%s", cookieName)).Expect().Status(httptest.StatusOK)
  17. t2.Body().Equal(cookieValue)
  18. // Test Remove A Cookie.
  19. t3 := e.DELETE(fmt.Sprintf("/cookies/%s", cookieName)).Expect().Status(httptest.StatusOK)
  20. t3.Body().Contains(cookieName)
  21. t4 := e.GET(fmt.Sprintf("/cookies/%s", cookieName)).Expect().Status(httptest.StatusOK)
  22. t4.Cookies().Empty()
  23. t4.Body().Empty()
  24. }

2.cookies加密

目录结构

主目录securecookie

  1. —— main.go
  2. —— main_test.go

代码示例

main.go

  1. package main
  2. //开发人员可以使用任何库添加自定义cookie编码器/解码器。
  3. //在这个例子中,我们使用gorilla的securecookie包:
  4. // $ go get github.com/gorilla/securecookie
  5. // $ go run main.go
  6. import (
  7. "github.com/kataras/iris"
  8. "github.com/gorilla/securecookie"
  9. )
  10. var (
  11. // AES仅支持16,24或32字节的密钥大小。
  12. //您需要准确提供该密钥字节大小,或者从您键入的内容中获取密钥。
  13. hashKey = []byte("the-big-and-secret-fash-key-here")
  14. blockKey = []byte("lot-secret-of-characters-big-too")
  15. sc = securecookie.New(hashKey, blockKey)
  16. )
  17. func newApp() *iris.Application {
  18. app := iris.New()
  19. app.Get("/cookies/{name}/{value}", func(ctx iris.Context) {
  20. name := ctx.Params().Get("name")
  21. value := ctx.Params().Get("value")
  22. //加密值
  23. ctx.SetCookieKV(name, value, iris.CookieEncode(sc.Encode)) // <--设置一个Cookie
  24. ctx.Writef("cookie added: %s = %s", name, value)
  25. })
  26. app.Get("/cookies/{name}", func(ctx iris.Context) {
  27. name := ctx.Params().Get("name")
  28. //解密值
  29. value := ctx.GetCookie(name, iris.CookieDecode(sc.Decode)) // <--检索Cookie
  30. ctx.WriteString(value)
  31. })
  32. app.Delete("/cookies/{name}", func(ctx iris.Context) {
  33. name := ctx.Params().Get("name")
  34. ctx.RemoveCookie(name) // <-- 删除Cookie
  35. ctx.Writef("cookie %s removed", name)
  36. })
  37. return app
  38. }
  39. func main() {
  40. app := newApp()
  41. app.Run(iris.Addr(":8080"))
  42. }

main_test.go

  1. package main
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/kataras/iris/httptest"
  6. )
  7. func TestCookiesBasic(t *testing.T) {
  8. app := newApp()
  9. e := httptest.New(t, app, httptest.URL("http://example.com"))
  10. cookieName, cookieValue := "my_cookie_name", "my_cookie_value"
  11. // Test Set A Cookie.
  12. t1 := e.GET(fmt.Sprintf("/cookies/%s/%s", cookieName, cookieValue)).Expect().Status(httptest.StatusOK)
  13. // note that this will not work because it doesn't always returns the same value:
  14. // cookieValueEncoded, _ := sc.Encode(cookieName, cookieValue)
  15. t1.Cookie(cookieName).Value().NotEqual(cookieValue) // validate cookie's existence and value is not on its raw form.
  16. t1.Body().Contains(cookieValue)
  17. // Test Retrieve A Cookie.
  18. t2 := e.GET(fmt.Sprintf("/cookies/%s", cookieName)).Expect().Status(httptest.StatusOK)
  19. t2.Body().Equal(cookieValue)
  20. // Test Remove A Cookie.
  21. t3 := e.DELETE(fmt.Sprintf("/cookies/%s", cookieName)).Expect().Status(httptest.StatusOK)
  22. t3.Body().Contains(cookieName)
  23. t4 := e.GET(fmt.Sprintf("/cookies/%s", cookieName)).Expect().Status(httptest.StatusOK)
  24. t4.Cookies().Empty()
  25. t4.Body().Empty()
  26. }