httpserver

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2022 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package httpserver provides functions that are useful for HTTP servers. These functions are meant to be used primarily in tests. Provides functions for determining an available port and checking to see when a server is "ready" based on the response provided by an endpoint.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AvailablePort

func AvailablePort() (port int, rErr error)

AvailablePort returns a best-effort determination of an available port. Does so by opening a TCP listener on localhost, determining the port used by that listener, closing the listener and returning the address that was used by the listener. This is best-effort because there is no way to guarantee that another process will not take the port between the time when the listener is closed and the returned port is used by the caller.

func Ready

func Ready(readyCall func() (*http.Response, error), params ...ReadyParam) <-chan bool

Ready returns a channel that is sent "true" when the provided readyCall returns a nil error and a response that returns "true" when provided to readyResp. The readyCall is invoked once every tick duration until it either returns a nil error and readyResp returns true for the response or the timeout duration is reached, in which case "false" is sent on the channel.

readyCall should by a function that returns quickly. At most one readyCall will be running at a particular time.

ReadyRespParam is used to specify the function that should be used to check if the response returned by the readyCall should be interpreted as "ready". If it is not specified, a default function that returns true if the response code is 200 is used.

ReadyRetryIntervalParam is used to specify the retry interval for the "readyCall". If it is not specified, a default value of 100ms is used.

WaitTimeoutParam is used to specify the timeout duration (the time after which the channel should return "false"). If it is not specified, a default value of 5s is used.

func URLReady

func URLReady(url string, params ...ReadyParam) <-chan bool

URLReady returns a channel that is sent "true" when an http.Get executed against the provided URL returns a response with status code http.StatusOK. This is a convenience function that calls Ready with a readyCall that consists of sending a GET request using the default HTTP client to the provided URL.

Types

type ReadyParam

type ReadyParam interface {
	// contains filtered or unexported methods
}

func ReadyRespParam

func ReadyRespParam(readyResp func(*http.Response) bool) ReadyParam

func ReadyRetryIntervalParam

func ReadyRetryIntervalParam(t time.Duration) ReadyParam

func WaitTimeoutParam

func WaitTimeoutParam(t time.Duration) ReadyParam

Jump to

Keyboard shortcuts

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