ginTestContext

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

README

gin-test-context

Test your gin handler easily!

Install

go get -u github.com/laggu/gin-test-context

Features

gin-test-context makes gin.Context which is set with various variables you need

What you can do
  • setting headers
  • setting uri params
  • setting queries
  • setting body

Examples

Headers
by struct
header := struct{
    Foo string `header:"foo"`
    Bar string `header:"bar"`
}{
    Foo: "abc",
    Bar: "xyz",
}

builder := ginTestContext.NewBuilder()
builder.SetHeaders(header)

context, err := builder.GetContext()
require.NoError(t, err)

yourHandler(context)
by map
header := map[string]string
header["foo"] = "abc"
header["bar"] = "xyz"

builder := ginTestContext.NewBuilder()
builder.SetHeaders(header)

context, err := builder.GetContext()
require.NoError(t, err)

yourHandler(context)
URI Params
by struct
params := struct{
    Foo string `uri:"foo"`
    Bar string `uri:"bar"`
}{
    Foo: "abc",
    Bar: "xyz",
}

builder := ginTestContext.NewBuilder()
builder.SetURIParams(params)

context, err := builder.GetContext()
require.NoError(t, err)

yourHandler(context)
by map
params := map[string]string
params["foo"] = "abc"
params["bar"] = "xyz"

builder := ginTestContext.NewBuilder()
builder.SetURIParams(paramsß)

context, err := builder.GetContext()
require.NoError(t, err)

yourHandler(context)
Queries
by struct
queries := struct{
    Foo string `form:"foo"`
    Bar string `form:"bar"`
}{
    Foo: "abc",
    Bar: "xyz",
}

builder := ginTestContext.NewBuilder()
builder.SetQueries(queries)

context, err := builder.GetContext()
require.NoError(t, err)

yourHandler(context)
by map
queries := map[string]string
queries["foo"] = "abc"
queries["bar"] = "xyz"

builder := ginTestContext.NewBuilder()
builder.SetQueries(queries)

context, err := builder.GetContext()
require.NoError(t, err)

yourHandler(context)
Body
by struct
body := struct{
    Foo string `json:"foo"`
    Bar string `json:"bar"`
}{
    Foo: "abc",
    Bar: "xyz",
}

builder := ginTestContext.NewBuilder()
builder.SetBody(body)

context, err := builder.GetContext()
require.NoError(t, err)

yourHandler(context)
by map
body := map[string]string
body["foo"] = "abc"
body["bar"] = "xyz"

builder := ginTestContext.NewBuilder()
builder.SetBody(body)

context, err := builder.GetContext()
require.NoError(t, err)

yourHandler(context)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnsupportedBodyType = errors.New("unsupported body type")
)
View Source
var (
	ErrUnsupportedHeaderType = errors.New("unsupported header type")
)
View Source
var (
	ErrUnsupportedQueryType = errors.New("unsupported query type")
)
View Source
var (
	ErrUnsupportedURIParamType = errors.New("unsupported uri param type")
)

Functions

func NewTestContext

func NewTestContext(input ContextInput) (*gin.Context, error)

Types

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

func NewBuilder

func NewBuilder() *Builder

func (*Builder) GetContext

func (b *Builder) GetContext() (*gin.Context, error)

func (*Builder) SetBody

func (b *Builder) SetBody(body interface{})

func (*Builder) SetHeaders

func (h *Builder) SetHeaders(headers interface{})

func (*Builder) SetQueries

func (q *Builder) SetQueries(queries interface{})

func (*Builder) SetURIParams

func (u *Builder) SetURIParams(uriParams interface{})

type ContextInput

type ContextInput struct {
	Headers   any
	URIParams any
	Queries   any
	Body      any
}

Jump to

Keyboard shortcuts

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