httptestutil

package
v0.2.0 Latest Latest
Warning

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

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

Documentation

Overview

Package httptestutil contains utilities for use in HTTP tests, particular when using httptest.Server

Example
package main

import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"strconv"

	"github.com/theopenlane/httpsling"
	"github.com/theopenlane/httpsling/httptestutil"
)

func main() {
	mux := http.NewServeMux()
	mux.Handle("/echo", httpsling.MockHandler(201, httpsling.Body("pong")))

	ts := httptest.NewServer(mux)
	defer ts.Close()

	// inspect server traffic
	is := httptestutil.Inspect(ts)

	// construct a pre-configured Requester
	r := httptestutil.Requester(ts)

	var out string
	resp, _ := r.Receive(&out, httpsling.Get("/echo"), httpsling.Body("ping"))

	ex := is.LastExchange()
	fmt.Println("server received: " + ex.RequestBody.String())
	fmt.Println("server sent: " + strconv.Itoa(ex.StatusCode))
	fmt.Println("server sent: " + ex.ResponseBody.String())
	fmt.Println("client received: " + strconv.Itoa(resp.StatusCode))
	fmt.Println("client received: " + out)

}
Output:

server received: ping
server sent: 201
server sent: pong
client received: 201
client received: pong

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dump

func Dump(ts *httptest.Server, to io.Writer)

Dump writes requests and responses to the writer

func DumpTo

func DumpTo(handler http.Handler, writer io.Writer) http.Handler

DumpTo wraps an http.Handler in a new handler the new handler dumps requests and responses to a writer, using the httputil.DumpRequest and httputil.DumpResponse functions

func DumpToLog

func DumpToLog(ts *httptest.Server, logf func(a ...interface{}))

DumpToLog writes requests and responses to a logging function

func DumpToStdout

func DumpToStdout(ts *httptest.Server)

DumpToStdout writes requests and responses to os.Stdout

func Requester

func Requester(ts *httptest.Server, options ...httpsling.Option) *httpsling.Requester

Requester creates a Requester instance which is pre-configured to send requests to the test server

Types

type Exchange

type Exchange struct {
	// Request is the request sent to the server
	Request *http.Request
	// RequestBody is the request body
	RequestBody *bytes.Buffer
	// StatusCode is the status code returned by the server
	StatusCode int
	// Header is the response header
	Header http.Header
	// ResponseBody is the response body
	ResponseBody *bytes.Buffer
}

Exchange is a snapshot of one request/response exchange with the server

type Inspector

type Inspector struct {
	Exchanges chan Exchange
}

Inspector is server-side middleware which captures server exchanges in a buffer

func Inspect

func Inspect(ts *httptest.Server) *Inspector

Inspect installs and returns an Inspector; the Inspector captures exchanges with the test server

func NewInspector

func NewInspector(size int) *Inspector

NewInspector creates a new Inspector with the requested channel buffer size

func (*Inspector) Clear

func (b *Inspector) Clear()

Clear drains the channel

func (*Inspector) Drain

func (b *Inspector) Drain() []*Exchange

Drain reads all buffered exchanges from the channel

func (*Inspector) LastExchange

func (b *Inspector) LastExchange() *Exchange

LastExchange receives the most recent exchange from channel; this also has the side effect of draining the channel completely

func (*Inspector) NextExchange

func (b *Inspector) NextExchange() *Exchange

NextExchange receives the next exchange from the channel, or returns nil if no exchange is ready

func (*Inspector) Wrap

func (b *Inspector) Wrap(next http.Handler) http.Handler

Wrap installs the inspector in an HTTP server by wrapping the server's Handler

Jump to

Keyboard shortcuts

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