使用路径变量测试 Chi 路由

Joh*_*ohn 7 rest go go-chi

我在测试 go-chi 路线时遇到问题,特别是带有路径变量的路线。运行服务器go run main.go工作正常,并且使用路径变量请求路由的行为符合预期。

当我运行路由测试时,我总是收到 HTTP 错误:Unprocessable Entity。注销发生的情况后articleID,似乎articleCtx无法访问路径变量。不确定这是否意味着我需要articleCtx在测试中使用,但我已经尝试ArticleCtx(http.HandlerFunc(GetArticleID))并收到错误:

panic: interface conversion: interface {} is nil, not *chi.Context [recovered] panic: interface conversion: interface {} is nil, not *chi.Context

运行服务器:go run main.go

测试服务器:go test .

我的来源:

// main.go

package main

import (
    "context"
    "fmt"
    "net/http"
    "strconv"

    "github.com/go-chi/chi"
)

type ctxKey struct {
    name string
}

func main() {
    r := chi.NewRouter()

    r.Route("/articles", func(r chi.Router) {
        r.Route("/{articleID}", func(r chi.Router) {
            r.Use(ArticleCtx)
            r.Get("/", GetArticleID) // GET /articles/123
        })
    })

    http.ListenAndServe(":3333", r)
}

// ArticleCtx gives the routes using it access to the requested article ID in the path
func ArticleCtx(next http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        articleParam := chi.URLParam(r, "articleID")
        articleID, err := strconv.Atoi(articleParam)
        if err != nil {
            http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
            return
        }

        ctx := context.WithValue(r.Context(), ctxKey{"articleID"}, articleID)
        next.ServeHTTP(w, r.WithContext(ctx))
    })
}

// GetArticleID returns the article ID that the client requested
func GetArticleID(w http.ResponseWriter, r *http.Request) {
    ctx := r.Context()
    articleID, ok := ctx.Value(ctxKey{"articleID"}).(int)
    if !ok {
        http.Error(w, http.StatusText(http.StatusUnprocessableEntity), http.StatusUnprocessableEntity)
        return
    }

    w.Write([]byte(fmt.Sprintf("article ID:%d", articleID)))
}
Run Code Online (Sandbox Code Playgroud)
// main_test.go

package main

import (
    "fmt"
    "net/http"
    "net/http/httptest"
    "testing"
)

func TestGetArticleID(t *testing.T) {
    tests := []struct {
        name           string
        rec            *httptest.ResponseRecorder
        req            *http.Request
        expectedBody   string
        expectedHeader string
    }{
        {
            name:         "OK_1",
            rec:          httptest.NewRecorder(),
            req:          httptest.NewRequest("GET", "/articles/1", nil),
            expectedBody: `article ID:1`,
        },
        {
            name:         "OK_100",
            rec:          httptest.NewRecorder(),
            req:          httptest.NewRequest("GET", "/articles/100", nil),
            expectedBody: `article ID:100`,
        },
        {
            name:         "BAD_REQUEST",
            rec:          httptest.NewRecorder(),
            req:          httptest.NewRequest("PUT", "/articles/bad", nil),
            expectedBody: fmt.Sprintf("%s\n", http.StatusText(http.StatusBadRequest)),
        },
    }

    for _, test := range tests {
        t.Run(test.name, func(t *testing.T) {
            ArticleCtx(http.HandlerFunc(GetArticleID)).ServeHTTP(test.rec, test.req)

            if test.expectedBody != test.rec.Body.String() {
                t.Errorf("Got: \t\t%s\n\tExpected: \t%s\n", test.rec.Body.String(), test.expectedBody)
            }
        })
    }
}
Run Code Online (Sandbox Code Playgroud)

不知道如何继续这个。有任何想法吗?我想知道是否有net/http/httptest关于使用context测试的答案,但没有看到任何内容。

也是相当新的 go Go(和context包),因此非常感谢任何代码审查/最佳实践评论:)

Ull*_*uri 17

尽管我直接对处理程序进行单元测试,但也遇到了类似的问题。基本上,当使用强制您手动添加 url 参数时,它们似乎不会自动添加到请求上下文中httptest.NewRequest

像下面这样的东西对我有用。

w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/{key}", nil)

rctx := chi.NewRouteContext()
rctx.URLParams.Add("key", "value")

r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, rctx))

handler := func(w http.ResponseWriter, r *http.Request) {
    value := chi.URLParam(r, "key")
}
handler(w, r)
Run Code Online (Sandbox Code Playgroud)

所有功劳都归功于 soedar =)


Gau*_*rma 3

我对命名路径变量也有同样的问题。我能够解决它为我的测试设置路由器。go-chi 测试显示了一个好的样本。

使用 URL 参数进行 Go Chi 示例测试