gomino

Go Report Card Go Reference

Gomino provides test-utilities for gin-gonic/gin‘s web framework.

Usage

Download and install gomino:

go get github.com/matthiasreumann/gomino

Import it in your code:

import "github.com/matthiasreumann/gomino"

Examples

Simple ‘ping’ router from Gin’s README.md

func pingRouter(r *gin.Engine) *gin.Engine {
	r.GET("/ping", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{"message": "pong"})
	})
}

func TestGinReadme(t *testing.T) {
	gomino.TestCases{
		"ping": {
			Router:           pingRouter,
			Method:           http.MethodGet,
			Url:              "/ping",
			ExpectedCode:     http.StatusOK,
			ExpectedResponse: gin.H{"message": "pong"},
		},
	}.Run(t, assert.Equal)
}

Using middlewares

func userRouter(r *gin.Engine) {
	r.GET("/user", func(c *gin.Context) {
		if c.MustGet("session-username").(string) == "hansi" {
			c.JSON(http.StatusOK, gin.H{"message": "hello hansi"})
		} else {
			c.AbortWithStatus(http.StatusForbidden)
		}
	})
}

func TestWithMiddleware(t *testing.T) {
    gomino.TestCases{
        "user hansi": {
            Router: userRouter,
            Method: http.MethodGet,
            Url:    "/user",
            Middlewares: []func(c *gin.Context){
                func(c *gin.Context) {
                    c.Set("session-username", "hansi")
                },
            },
            ExpectedCode:     http.StatusOK,
            ExpectedResponse: gin.H{"message": "hello hansi"},
        },
        "user not hansi": {
            Router: userRouter,
            Method: http.MethodGet,
            Url:    "/user",
            Middlewares: []func(c *gin.Context){
                func(c *gin.Context) {
                    c.Set("session-username", "bobby")
                },
            },
            ExpectedCode: http.StatusForbidden,
        },
    }.Run(t, assert.Equal)
}

Using router-functions with dependency injection

func loginRouter(r *gin.Engine, dao UserDao) {
	r.POST("/login", func(c *gin.Context) {
		if dao.Get() == "hansi" {
			c.Status(http.StatusOK)
		} else {
			c.AbortWithStatus(http.StatusForbidden)
		}
	})
}

func TestRouterWithDependencies(t *testing.T) {
	gomino.TestCases{
		"user hansi": {
			Router: func(r *gin.Engine) {
				loginRouter(r, NewUserDaoMock("hansi"))
			},
			Method:       http.MethodPost,
			Url:          "/login",
			ExpectedCode: http.StatusOK,
		},
		"user not hansi": {
			Router: func(r *gin.Engine) {
				loginRouter(r, NewUserDaoMock("bobby"))
			},
			Method:       http.MethodPost,
			Url:          "/login",
			ExpectedCode: http.StatusForbidden,
		},
	}.Run(t, assert.Equal)
}

GitHub

View Github