Test your gin handler easily!
go get -u github.com/laggu/gin-test-context
gin-test-context makes gin.Context which is set with various variables you need
- setting headers
- setting uri params
- setting queries
- setting body
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)
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)
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)
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 := 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)
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 := 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)
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)