contextfactory

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2023 License: MIT Imports: 4 Imported by: 0

README

Context Factory

The Context Factory provides a straightforward method for testing Gin Handlers by generating a test context.

This project has been developped by the Aloe team and is now open source.

tests Go Reference

Overview

The module provides:

  • A simple method to create gin.Context
  • An easy way to test your gin.Handlers

Concept

Emulate an API call by populating a gin.Context thanks to the ContextOptions structure:

type ContextOptions struct {
	Method      string
	Path        string
	Body        io.Reader
	PathParams  gin.Params
	QueryParams map[string]string
	Headers     map[string]string
	ContextVars map[string]interface{}
}

Usage

Use the factory method to create a gin.Context and a httptest.ResponseRecorder inside a unit test:

    func TestGetDog(t *testing.T) {
        options := contextfactory.ContextOptions{
            Path: "/kid",
            QueryParams: map[string]string{
                "age":      3,
            }
        }

        context, writer := contextfactory.BuildGinTestContext(testCase.Context)

        GetDogs(context)

        assert.Equal(t, http.StatusOK, writer.Code)
    }

Contributing

This section will be added soon.

License

Client is released under the MIT license. See LICENSE.txt.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildGinTestContext

func BuildGinTestContext(opt ContextOptions) (*gin.Context, *httptest.ResponseRecorder)

BuildGinTestContext creates a test context for Gin framework with the provided ContextOptions. It returns a *gin.Context and *httptest.ResponseRecorder, which can be used to simulate HTTP requests and test Gin handlers.

Types

type ContextOptions

type ContextOptions struct {
	Method      string
	Path        string
	Body        io.Reader
	PathParams  gin.Params
	QueryParams map[string]string
	Headers     map[string]string
	ContextVars map[string]interface{}
}

ContextOptions for the context factory method.

Jump to

Keyboard shortcuts

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