gomino

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 26, 2022 License: MIT Imports: 10 Imported by: 0

README

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)
}

func TestGinReadmeWithDefaults(t *testing.T) {
	gomino.TestCases{
		"ping": {
			ExpectedCode:     http.StatusOK,
			ExpectedResponse: gin.H{"message": "pong"},
		}}.
		Router(pingRouter).
		Url("/ping").
		Method(http.MethodGet).
		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)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func First

func First(a interface{}, b interface{}) interface{}

func NewMultipartFormData

func NewMultipartFormData(fieldName, fileName string) (bytes.Buffer, *multipart.Writer)

func Second

func Second(a interface{}, b interface{}) interface{}

Types

type Equal

type Equal func(*testing.T, interface{}, interface{})

Equal is the function signiture for one's favourite testing framework

type HttpHeader

type HttpHeader map[string]string

HttpHeader is a key value map for HTTP header fields such as Content-Type, Cache-Control,...

type TestCase

type TestCase struct {
	Router      func(*gin.Engine)
	Method      string
	Url         string
	Middlewares []func(c *gin.Context)

	ContentType string
	Body        interface{}

	ExpectedHeader   HttpHeader
	ExpectedCode     int
	ExpectedResponse interface{}

	Before func()
	After  func()
}

TestCase contains everything a single test needs to execute

type TestCases

type TestCases map[string]*TestCase

TestCases shall contain all test cases of a single test suite, e.g. for one particular endpoint. The key represents the test name, the value an instance of TestCase

func (TestCases) Method added in v0.0.2

func (tc TestCases) Method(method string) TestCases

Method sets the default http-method for all test cases This method does not overwrite pre-existing values.

func (TestCases) Router added in v0.0.2

func (tc TestCases) Router(r func(*gin.Engine)) TestCases

Router sets the default router for all test cases. This method does not overwrite pre-existing values.

func (TestCases) Run

func (tc TestCases) Run(t *testing.T, equal Equal)

Run executes all tests of a given TestCases object

func (TestCases) Url added in v0.0.2

func (tc TestCases) Url(url string) TestCases

Url sets the default route for all test cases This method does not overwrite pre-existing values.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL