What's the best way for testing Gin routes in Go?

a year ago by Lena to c/golang

Hi, what's the best way for testing a gin route?

I currently have one function where the routes are defined. Here it is:

package router

import (
    "github.com/gin-gonic/gin"
    "github.com/gragorther/epigo/handlers"
    "github.com/gragorther/epigo/middlewares"
)

func Setup(userUserStore handlers.UserUserStore, groupGroupStore handlers.GroupGroupStore, groupAuthStore handlers.GroupAuthStore, messageAuthStore handlers.MessageAuthStore, messageMessageStore handlers.MessageMessageStore, middlewareUserStore middlewares.UserStore) *gin.Engine {
    r := gin.Default()
    r.Use(middlewares.ErrorHandler())

    userHandler := handlers.NewUserHandler(userUserStore)
    authHandler := middlewares.NewAuthMiddleware(middlewareUserStore)
    groupHandler := handlers.NewGroupHandler(groupGroupStore, groupAuthStore)
    messageHandler := handlers.NewMessageHandler(messageMessageStore, messageAuthStore)

    // user stuff
    r.POST("/user/register", userHandler.RegisterUser)
    r.POST("/user/login", userHandler.LoginUser)
    r.GET("/user/profile", authHandler.CheckAuth, userHandler.GetUserProfile)
    r.PUT("/user/setEmailInterval", authHandler.CheckAuth, userHandler.SetEmailInterval)

    // groups
    r.DELETE("/user/groups/delete/:id", authHandler.CheckAuth, groupHandler.DeleteGroup)
    r.POST("/user/groups/add", authHandler.CheckAuth, groupHandler.AddGroup)
    r.GET("/user/groups", authHandler.CheckAuth, groupHandler.ListGroups) // list groups
    r.PATCH("/user/groups/edit/:id", authHandler.CheckAuth, groupHandler.EditGroup)

    // lastMessages
    r.POST("/user/lastMessages/add", authHandler.CheckAuth, messageHandler.AddLastMessage)
    r.GET("/user/lastMessages", authHandler.CheckAuth, messageHandler.ListLastMessages)
    r.PATCH("/user/lastMessages/edit/:id", authHandler.CheckAuth, messageHandler.EditLastMessage)
    r.DELETE("/user/lastMessages/delete/:id", authHandler.CheckAuth, messageHandler.DeleteLastMessage)
    return r
}

so, my question is, how can I test just one route? should I run this function in every test and send a request to a route with httptest? Or should I set up my handlers like it's described at https://gin-gonic.com/en/docs/testing/

(like this)

func postUser(router *gin.Engine) *gin.Engine {
  router.POST("/user/add", func(c *gin.Context) {
    var user User
    c.BindJSON(&user)
    c.JSON(200, user)
  })
  return router
}

let me know if you have any other questions about my code.

It's available on github: https://github.com/gragorther/epigo

gravediggersbiscuit 4 points a year ago

If it was me personally I would just test the handlers separately rather than scaffolding the router entirely.

Either way, I suggest looking into using the standard lib http/httptest https://pkg.go.dev/net/http/httptest

Here's an snippet example of testing a handler

req, err := http.NewRequest(http.MethodGet, "/health", bytes.NewBuffer([]byte{}))
if err != nil {
    t.Fatal(err)
}

rr := httptest.NewRecorder()
handler := http.HandlerFunc(srv.HealthCheckHandler)
handler.ServeHTTP(rr, req)

if rr.Code != http.StatusOK {
    t.Fatalf("expect HTTP response code %d, got %d", tt.expectHttpCode, rr.Code)
}
path: 0 18454240, hotness: undefined, score: 4, children: 1
grrgyle 1 point a year ago

This is the way. Just import the handler and test the juice directly

path: 0 18454240 18461462, hotness: undefined, score: 1, children: 0
golang
golang

@programming.dev

login for more options
2783
317
26

This is a community dedicated to the go programming language.

Useful Links:

Rules:

  • Posts must be relevant to Go
  • No NSFW content
  • No hate speech, bigotry, etc
  • Try to keep discussions on topic
  • No spam of tools/companies/advertisements
  • It’s OK to post your own stuff part of the time, but the primary use of the community should not be self-promotion.

go to feed...