reqb

package module
v0.0.0-...-bab1744 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2024 License: MIT Imports: 5 Imported by: 0

README

github.com/akm/reqb

Overview

reqb means request builder. The official pronunciation is 'rek-b'. It 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/reqb

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/reqb"
)

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

Options allows you to create requests with a set of some options. This is useful when you need to create multiple requests with the same base configuration.

defaultOpts := reqb.Options{reqb.BaseUrl("http://example.com")}

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

Example Test

Here is an example of how to use reqb in a test:

package reqb

import (
    "net/http"
    "testing"

    "github.com/akm/reqb"
    "github.com/stretchr/testify/assert"
    "github.com/stretchr/testify/require"
)

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

    factory := reqb.Options{reqb.BaseUrl(testServer.URL)}

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

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

This example demonstrates how to use reqb 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

This section is empty.

Functions

This section is empty.

Types

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 ...builder.Option) Func

CONNECT creates a new CONNECT request with the specified options.

func DELETE

func DELETE(v ...builder.Option) Func

DELETE creates a new DELETE request with the specified options.

func GET

func GET(v ...builder.Option) Func

GET creates a new GET request with the specified options.

func HEAD(v ...builder.Option) Func

HEAD creates a new HEAD request with the specified options.

func New

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

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

func OPTIONS

func OPTIONS(v ...builder.Option) Func

OPTIONS creates a new OPTIONS request with the specified options.

func PATCH

func PATCH(v ...builder.Option) Func

PATCH creates a new PATCH request with the specified options.

func POST

func POST(v ...builder.Option) Func

POST creates a new POST request with the specified options.

func PUT

func PUT(v ...builder.Option) Func

PUT creates a new PUT request with the specified options.

func TRACE

func TRACE(v ...builder.Option) Func

TRACE creates a new TRACE request with the specified options.

type Option

type Option = builder.Option

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.

type Options

type Options []Option

func (Options) Append

func (o Options) Append(opts ...Option) Options

func (Options) CONNECT

func (o Options) CONNECT(v ...Option) Func

func (Options) DELETE

func (o Options) DELETE(v ...Option) Func

func (Options) GET

func (o Options) GET(v ...Option) Func

func (Options) HEAD

func (o Options) HEAD(v ...Option) Func

func (Options) OPTIONS

func (o Options) OPTIONS(v ...Option) Func

func (Options) PATCH

func (o Options) PATCH(v ...Option) Func

func (Options) POST

func (o Options) POST(v ...Option) Func

func (Options) PUT

func (o Options) PUT(v ...Option) Func

func (Options) TRACE

func (o Options) TRACE(v ...Option) Func

func (Options) With

func (o Options) With(opts ...Option) Options

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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