简单测试alexedwards/scs,仅使用公共API。

huangapple go评论140阅读模式
英文:

Simple test for alexedwards/scs using only the public api

问题

这个问题是对这个问题的后续问题。我想要一个简单的测试来验证我想要使用的函数,测试的网址是https://pkg.go.dev/github.com/alexedwards/scs/v2@v2.5.1。基本上,我只想把一些数据放入会话中,取出来,并验证它是否相同。类似这样的代码:

  1. func TestScs_SetGetIsSame1(t *testing.T) {
  2. sessionManager := scs.New()
  3. req := httptest.NewRequest("GET", "/", nil)
  4. key := "boolean"
  5. expected := false
  6. sessionManager.Put(req.Context(), key, expected)
  7. actual := sessionManager.GetBool(req.Context(), key)
  8. assert.Equal(t, expected, actual)
  9. }

这会导致 panic: scs: no session data in context。

我尝试复制httptest 包中的示例,但是 httptest.Server 没有实现 http.Handler(缺少 ServeHTTP 方法)。

  1. func TestScs_SetGetIsSame2(t *testing.T) {
  2. sessionManager := scs.New()
  3. ts := httptest.NewServer((http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  4. fmt.Fprintln(w, "Hello, client")
  5. expected := true
  6. sessionManager.Put(r.Context(), "key", expected)
  7. actual := sessionManager.GetBool(r.Context(), "key")
  8. assert.Equal(t, expected, actual)
  9. })))
  10. defer ts.Close()
  11. res, err := http.Get(ts.URL)
  12. if err != nil {
  13. log.Fatal(err)
  14. }
  15. greeting, err := io.ReadAll(res.Body)
  16. res.Body.Close()
  17. if err != nil {
  18. log.Fatal(err)
  19. }
  20. fmt.Printf("%s", greeting)
  21. sessionManager.LoadAndSave(ts)
  22. }

我还查看了 alexedwards/scs 仓库中的测试,但它们使用了一个私有函数 addSessionDataToContext。

在 Go 中,测试会话的最佳、最简单的方法是什么?

英文:

This question is a follow-up question to this one. I would like a simple test for https://pkg.go.dev/github.com/alexedwards/scs/v2@v2.5.1 to verify the functions I would like to use. Basically, I just want to put some data into the session, pull it out, and verify it is the same. Something like:

  1. func TestScs_SetGetIsSame1(t *testing.T) {
  2. sessionManager := scs.New()
  3. req := httptest.NewRequest("GET", "/", nil)
  4. key := "boolean"
  5. expected := false
  6. sessionManager.Put(req.Context(), key, expected)
  7. actual := sessionManager.GetBool(req.Context(), key)
  8. assert.Equal(t, expected, actual)
  9. }

This gives panic: scs: no session data in context.

I tried copying the example from the httptest package, but httptest.Server does not implement http.Handler (missing method ServeHTTP).

  1. func TestScs_SetGetIsSame2(t *testing.T) {
  2. sessionManager := scs.New()
  3. ts := httptest.NewServer((http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  4. fmt.Fprintln(w, "Hello, client")
  5. expected := true
  6. sessionManager.Put(r.Context(), "key", expected)
  7. actual := sessionManager.GetBool(r.Context(), "key")
  8. assert.Equal(t, expected, actual)
  9. })))
  10. defer ts.Close()
  11. res, err := http.Get(ts.URL)
  12. if err != nil {
  13. log.Fatal(err)
  14. }
  15. greeting, err := io.ReadAll(res.Body)
  16. res.Body.Close()
  17. if err != nil {
  18. log.Fatal(err)
  19. }
  20. fmt.Printf("%s", greeting)
  21. sessionManager.LoadAndSave(ts)
  22. }

I also looked at the tests in the alexedwards/scs repository, but they use a private function addSessionDataToContext.

What is the best, most simple way to test sessions in go?

答案1

得分: 1

我在你的代码中看到了以下内容:

  1. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  2. // ...
  3. }))
  4. sessionManager.LoadAndSave(ts) // 这一行是错误的

你试图将测试服务器 (ts) 传递给 sessionManager.LoadAndSave。这样是行不通的,因为 sessionManager.LoadAndSave 需要一个 http.Handler,而 ts 的类型是 *httptest.Server,而不是 http.Handler

相反,在创建测试服务器之前,你应该将实际的处理程序传递给 sessionManager.LoadAndSave

为此,你需要使用 sessionManager.LoadAndSave 将实际的处理程序函数包装起来(它返回一个 http.Handler),然后将其传递给 httptest.NewServer

  1. handler := sessionManager.LoadAndSave(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  2. // ... 在这里编写逻辑 ...
  3. }))
  4. ts := httptest.NewServer(handler)

这样就通过使用 sessionManager.LoadAndSave 中间件将实际的处理程序函数包装成了一个新的处理程序。然后将这个包装后的处理程序传递给 httptest.NewServer

将所有这些组合在一起:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/alexedwards/scs/v2"
  5. "github.com/stretchr/testify/assert"
  6. "io"
  7. "log"
  8. "net/http"
  9. "net/http/httptest"
  10. "testing"
  11. )
  12. func TestScs_SetGetIsSame2(t *testing.T) {
  13. sessionManager := scs.New()
  14. // 创建一个处理程序并使用 sessionManager.LoadAndSave 进行包装
  15. handler := sessionManager.LoadAndSave(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  16. fmt.Fprintln(w, "Hello, client")
  17. expected := true
  18. sessionManager.Put(r.Context(), "key", expected)
  19. actual := sessionManager.GetBool(r.Context(), "key")
  20. assert.Equal(t, expected, actual)
  21. }))
  22. // 使用处理程序创建一个测试服务器
  23. ts := httptest.NewServer(handler)
  24. defer ts.Close()
  25. // 向服务器发起请求
  26. res, err := http.Get(ts.URL)
  27. if err != nil {
  28. log.Fatal(err)
  29. }
  30. // 读取响应体
  31. greeting, err := io.ReadAll(res.Body)
  32. res.Body.Close()
  33. if err != nil {
  34. log.Fatal(err)
  35. }
  36. // 打印响应体
  37. fmt.Printf("%s", greeting)
  38. }

希望对你有所帮助!

英文:

I see in your code

  1. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  2. // ...
  3. }))
  4. sessionManager.LoadAndSave(ts) // this line is incorrect

You are trying to pass the test server (ts) to sessionManager.LoadAndSave. This will not work because sessionManager.LoadAndSave expects an http.Handler, and ts is of type *httptest.Server, not http.Handler.

Instead, you should pass the actual handler to sessionManager.LoadAndSave before creating the test server.

For that, you would wrap the actual handler function with sessionManager.LoadAndSave (which does return a http.Handler) before passing it to httptest.NewServer.

  1. handler := sessionManager.LoadAndSave(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  2. // ... logic here ...
  3. }))
  4. ts := httptest.NewServer(handler)

That creates a new handler by wrapping your actual handler function with the sessionManager.LoadAndSave middleware. This wrapped handler is then passed to httptest.NewServer.

If you bring it all together:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/alexedwards/scs/v2"
  5. "github.com/stretchr/testify/assert"
  6. "io"
  7. "log"
  8. "net/http"
  9. "net/http/httptest"
  10. "testing"
  11. )
  12. func TestScs_SetGetIsSame2(t *testing.T) {
  13. sessionManager := scs.New()
  14. // Create a handler and wrap it using sessionManager.LoadAndSave
  15. handler := sessionManager.LoadAndSave(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  16. fmt.Fprintln(w, "Hello, client")
  17. expected := true
  18. sessionManager.Put(r.Context(), "key", expected)
  19. actual := sessionManager.GetBool(r.Context(), "key")
  20. assert.Equal(t, expected, actual)
  21. }))
  22. // Create a test server with the handler
  23. ts := httptest.NewServer(handler)
  24. defer ts.Close()
  25. // Make a request to the server
  26. res, err := http.Get(ts.URL)
  27. if err != nil {
  28. log.Fatal(err)
  29. }
  30. // Read the response body
  31. greeting, err := io.ReadAll(res.Body)
  32. res.Body.Close()
  33. if err != nil {
  34. log.Fatal(err)
  35. }
  36. // Print the response body
  37. fmt.Printf("%s", greeting)
  38. }

huangapple
  • 本文由 发表于 2023年7月4日 02:23:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76607300.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定