testrequest

package module
v0.0.0-...-1ad1588 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2024 License: MIT Imports: 8 Imported by: 0

README

github.com/akm/go-testrequest

Overview

go-testrequest is a Go package that simplifies the creation and testing of HTTP requests. It provides a flexible and extensible way to build HTTP requests using a set of options and a factory pattern.

Installation

go get github.com/akm/go-testrequest

Usage

Creating Requests

You can create HTTP requests using the provided functions such as GET, POST, PUT, etc. Each function accepts a list of options to configure the request.

import (
    "github.com/akm/go-testrequest"
)

req := testrequest.GET(
    testrequest.BaseUrl("http://example.com"),
    testrequest.Path("/users"),
    testrequest.Header("Authorization", "Bearer token"),
)
Using Factories

Factories allow you to create requests with a set of default options. This is useful when you need to create multiple requests with the same base configuration.

factory := testrequest.NewFactory(testrequest.BaseUrl("http://example.com"))

req := factory.POST(
    testrequest.Path("/users"),
    testrequest.BodyString(`{"name":"John Doe"}`),
)

Example Test

Here is an example of how to use go-testrequest in a test:

package testrequest

import (
    "net/http"
    "testing"

    "github.com/akm/go-testrequest"
    "github.com/stretchr/testify/assert"
    "github.com/stretchr/testify/require"
)

func TestClientWithServer(t *testing.T) {
    testServer := startEchoServer(t)
    testServer.Start()
    defer testServer.Close()

    factory := testrequest.NewFactory(testrequest.BaseUrl(testServer.URL))

    client := &http.Client{}
    resp, err := client.Do(factory.GET(testrequest.Path("/foo")))
    require.NoError(t, err)
    defer resp.Body.Close()

    assert.Equal(t, http.StatusOK, resp.StatusCode)
}

This example demonstrates how to use go-testrequest to create and test HTTP requests in a Go test. See tests/client_test.go for more details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultFactory = NewFactory()

Functions

This section is empty.

Types

type Factory

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

Factory is a struct that holds default options for creating HTTP requests.

func NewFactory

func NewFactory(defaultOptions ...Option) *Factory

NewFactory creates a new Factory with the given default options.

func (*Factory) CONNECT

func (f *Factory) CONNECT(v ...Option) Func

CONNECT creates a new HTTP CONNECT request function with the specified options.

func (*Factory) DELETE

func (f *Factory) DELETE(v ...Option) Func

DELETE creates a new HTTP DELETE request function with the specified options.

func (*Factory) GET

func (f *Factory) GET(v ...Option) Func

GET creates a new HTTP GET request function with the specified options.

func (*Factory) HEAD

func (f *Factory) HEAD(v ...Option) Func

HEAD creates a new HTTP HEAD request function with the specified options.

func (*Factory) New

func (f *Factory) New(method string, options ...Option) Func

New creates a new HTTP request function with the specified method and options.

func (*Factory) OPTIONS

func (f *Factory) OPTIONS(v ...Option) Func

OPTIONS creates a new HTTP OPTIONS request function with the specified options.

func (*Factory) PATCH

func (f *Factory) PATCH(v ...Option) Func

PATCH creates a new HTTP PATCH request function with the specified options.

func (*Factory) POST

func (f *Factory) POST(v ...Option) Func

POST creates a new HTTP POST request function with the specified options.

func (*Factory) PUT

func (f *Factory) PUT(v ...Option) Func

PUT creates a new HTTP PUT request function with the specified options.

func (*Factory) TRACE

func (f *Factory) TRACE(v ...Option) Func

TRACE creates a new HTTP TRACE request function with the specified options.

type Func

type Func = func(*testing.T) *http.Request

Func is a type alias for a function that takes a *testing.T and returns an *http.Request.

func CONNECT

func CONNECT(v ...Option) Func

CONNECT creates a new CONNECT request with the specified options.

func DELETE

func DELETE(v ...Option) Func

DELETE creates a new DELETE request with the specified options.

func GET

func GET(v ...Option) Func

GET creates a new GET request with the specified options.

func HEAD(v ...Option) Func

HEAD creates a new HEAD request with the specified options.

func New

func New(method string, options ...Option) Func

New creates a new request with the specified method and options.

func OPTIONS

func OPTIONS(v ...Option) Func

OPTIONS creates a new OPTIONS request with the specified options.

func PATCH

func PATCH(v ...Option) Func

PATCH creates a new PATCH request with the specified options.

func POST

func POST(v ...Option) Func

POST creates a new POST request with the specified options.

func PUT

func PUT(v ...Option) Func

PUT creates a new PUT request with the specified options.

func TRACE

func TRACE(v ...Option) Func

TRACE creates a new TRACE request with the specified options.

type Option

type Option = func(*builder)

Option is a function that modifies the builder.

func BaseUrl

func BaseUrl(v string) Option

BaseUrl sets the base URL for the request.

func Body

func Body(v *io.Reader) Option

Body sets the body of the request from an io.Reader.

func BodyBytes

func BodyBytes(v []byte) Option

BodyBytes sets the body of the request from a byte slice.

func BodyString

func BodyString(v string) Option

BodyString sets the body of the request from a string.

func Context

func Context(v context.Context) Option

Context sets the context for the request.

func Cookie(v *http.Cookie) Option

Cookie adds a cookie to the request.

func Header(k, v string) Option

Header adds a header to the request.

func Host

func Host(v string) Option

Host sets the host for the request.

func Path

func Path(v string, args ...interface{}) Option

Path sets the path for the request, with optional formatting arguments.

func Port

func Port(v int) Option

Port sets the port for the request as an integer.

func PortString

func PortString(v string) Option

PortString sets the port for the request as a string.

func Query

func Query(k, v string) Option

Query adds a query parameter to the request.

func Scheme

func Scheme(v string) Option

Scheme sets the URL scheme (http or https) for the request.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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