英文:
Testing gin based REST API not getting params while using net/http/httptest
问题
我正在开发一个基于Gin Go的REST API,端点的代码如下所示:
func carsByType(c *gin.Context) {
fmt.Println("Go Request in Handler...")
carType := c.Params.ByName("type")
fmt.Println(carType)
if carType != "" {
}
c.JSON(http.StatusBadRequest, gin.H{"result": "Bad request"})
return
}
func main() {
router := gin.Default()
router.GET("/cars/:type", carsByType)
router.Run(":80")
}
当我通过浏览器和cURL请求端点时,一切正常,可以获取到carType的值。但是当我运行测试时,它返回了"bad request",并且carType的值是""。
用于测试端点的代码如下所示:
func TestGetcarsByType(t *testing.T) {
gin.SetMode(gin.TestMode)
handler := carsByType
router := gin.Default()
router.GET("/cars/1", handler)
req, err := http.NewRequest("GET", "/cars/1", nil)
if err != nil {
fmt.Println(err)
}
resp := httptest.NewRecorder()
router.ServeHTTP(resp, req)
assert.Equal(t, resp.Code, 200)
}
我做错了什么?
英文:
I am developing a REST API based on Gin Go, the endpoint looks something like below:
func carsByType(c *gin.Context) {
fmt.Println("Go Request in Handler...")
carType := c.Params.ByName("type")
fmt.Println(carType)
if carType != "" {
}
c.JSON(http.StatusBadRequest, gin.H{"result": "Bad request"})
return
}
func main() {
router := gin.Default()
router.GET("/cars/:type", carsByType)
router.Run(":80")
}
When I am making request to the endpoint via browser and cURL its just working fine, getting the carType value but when I am running the tests its returning bad request and getting carType is "".
For testing the endpoint my test code looks like this:
func TestGetcarsByType(t *testing.T) {
gin.SetMode(gin.TestMode)
handler := carsByType
router := gin.Default()
router.GET("/cars/1", handler)
req, err := http.NewRequest("GET", "/cars/1", nil)
if err != nil {
fmt.Println(err)
}
resp := httptest.NewRecorder()
router.ServeHTTP(resp, req)
assert.Equal(t, resp.Code, 200)
}
What am I doing wrong?
答案1
得分: 9
router.GET("/cars/1", handler)
应该改为 router.GET("/cars/:type", handler)
在你的测试中。
请注意,更好的做法(更易测试,减少路由的重复)是创建一个 SetupRouter() *gin.Engine
函数,该函数返回所有的路由。例如:
// package main
// server.go
// 这是你创建 gin.Default() 并添加路由的地方
func SetupRouter() *gin.Engine {
router := gin.Default()
router.GET("/cars/:type", carsByType)
return router
}
func main() {
router := SetupRouter()
router.Run()
}
// router_test.go
testRouter := SetupRouter()
req, err := http.NewRequest("GET", "/cars/1", nil)
if err != nil {
fmt.Println(err)
}
resp := httptest.NewRecorder()
testRouter.ServeHTTP(resp, req)
assert.Equal(t, resp.Code, 200)
英文:
router.GET("/cars/1", handler)
should be router.GET("/cars/:type", handler)
in your test.
Note that a better (more testable; less duplication of routes) would be to create a SetupRouter() *gin.Engine
function that returns all of your routes. e.g.
// package main
// server.go
// This is where you create a gin.Default() and add routes to it
func SetupRouter() *gin.Engine {
router := gin.Default()
router.GET("/cars/:type", carsByType)
return router
}
func main() {
router := SetupRouter()
router.Run()
}
// router_test.go
testRouter := SetupRouter()
req, err := http.NewRequest("GET", "/cars/1", nil)
if err != nil {
fmt.Println(err)
}
resp := httptest.NewRecorder()
testRouter.ServeHTTP(resp, req)
assert.Equal(t, resp.Code, 200)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论