httptest

package
v0.0.0-...-0bbbf19 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get(t testing.TB, url string) (resp *http.Response)

Get issues a GET to the specified URL. If the response is one of the following redirect codes, Get follows the redirect, up to a maximum of 10 redirects:

301 (Moved Permanently)
302 (Found)
303 (See Other)
307 (Temporary Redirect)
308 (Permanent Redirect)

An error is returned if there were too many redirects or if there was an HTTP protocol error. A non-2xx response doesn't cause an error. Any returned error will be of type *url.Error. The url.Error value's Timeout method will report true if the request timed out.

When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.

Get is a wrapper around DefaultClient.Get.

To make a request with custom headers, use NewRequest and DefaultClient.Do.

To make a request with a specified context.Context, use NewRequestWithContext and DefaultClient.Do.

func Head(t testing.TB, url string) (resp *http.Response)

Head issues a HEAD to the specified URL. If the response is one of the following redirect codes, Head follows the redirect, up to a maximum of 10 redirects:

301 (Moved Permanently)
302 (Found)
303 (See Other)
307 (Temporary Redirect)
308 (Permanent Redirect)

Head is a wrapper around DefaultClient.Head.

To make a request with a specified context.Context, use NewRequestWithContext and DefaultClient.Do.

func ListenAndServe

func ListenAndServe(t testing.TB, addr string, handler http.Handler)

ListenAndServe listens on the TCP network address addr and then calls Serve with handler to handle requests on incoming connections. Accepted connections are configured to enable TCP keep-alives.

The handler is typically nil, in which case [DefaultServeMux] is used.

ListenAndServe always returns a non-nil error.

func ListenAndServeTLS

func ListenAndServeTLS(t testing.TB, addr, certFile, keyFile string, handler http.Handler)

ListenAndServeTLS acts identically to ListenAndServe, except that it expects HTTPS connections. Additionally, files containing a certificate and matching private key for the server must be provided. If the certificate is signed by a certificate authority, the certFile should be the concatenation of the server's certificate, any intermediates, and the CA's certificate.

func NewRequest

func NewRequest(t testing.TB, method, url string, body io.Reader) *http.Request

NewRequest wraps NewRequestWithContext using context.Background.

func NewRequestWithContext

func NewRequestWithContext(t testing.TB, ctx context.Context, method, url string, body io.Reader) *http.Request

NewRequestWithContext returns a new [Request] given a method, URL, and optional body.

If the provided body is also an io.Closer, the returned [Request.Body] is set to body and will be closed (possibly asynchronously) by the Client methods Do, Post, and PostForm, and [Transport.RoundTrip].

NewRequestWithContext returns a Request suitable for use with [Client.Do] or [Transport.RoundTrip]. To create a request for use with testing a Server Handler, either use the NewRequest function in the net/http/httptest package, use ReadRequest, or manually update the Request fields. For an outgoing client request, the context controls the entire lifetime of a request and its response: obtaining a connection, sending the request, and reading the response headers and body. See the Request type's documentation for the difference between inbound and outbound request fields.

If body is of type *bytes.Buffer, *bytes.Reader, or *strings.Reader, the returned request's ContentLength is set to its exact value (instead of -1), GetBody is populated (so 307 and 308 redirects can replay the body), and Body is set to [NoBody] if the ContentLength is 0.

func ParseCookie

func ParseCookie(t testing.TB, line string) []*http.Cookie

ParseCookie parses a Cookie header value and returns all the cookies which were set in it. Since the same cookie name can appear multiple times the returned Values can contain more than one value for a given key.

func ParseSetCookie

func ParseSetCookie(t testing.TB, line string) *http.Cookie

ParseSetCookie parses a Set-Cookie header value and returns a cookie. It returns an error on syntax error.

func ParseTime

func ParseTime(t testing.TB, text string) time.Time

ParseTime parses a time header (such as the Date: header), trying each of the three formats allowed by HTTP/1.1: [TimeFormat], time.RFC850, and time.ANSIC.

func Post

func Post(t testing.TB, url, contentType string, body io.Reader) (resp *http.Response)

Post issues a POST to the specified URL.

Caller should close resp.Body when done reading from it.

If the provided body is an io.Closer, it is closed after the request.

Post is a wrapper around DefaultClient.Post.

To set custom headers, use NewRequest and DefaultClient.Do.

See the [Client.Do] method documentation for details on how redirects are handled.

To make a request with a specified context.Context, use NewRequestWithContext and DefaultClient.Do.

func PostForm

func PostForm(t testing.TB, url string, data url.Values) (resp *http.Response)

PostForm issues a POST to the specified URL, with data's keys and values URL-encoded as the request body.

The Content-Type header is set to application/x-www-form-urlencoded. To set other headers, use NewRequest and DefaultClient.Do.

When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.

PostForm is a wrapper around DefaultClient.PostForm.

See the [Client.Do] method documentation for details on how redirects are handled.

To make a request with a specified context.Context, use NewRequestWithContext and DefaultClient.Do.

func ProxyFromEnvironment

func ProxyFromEnvironment(t testing.TB, req *http.Request) *url.URL

ProxyFromEnvironment returns the URL of the proxy to use for a given request, as indicated by the environment variables HTTP_PROXY, HTTPS_PROXY and NO_PROXY (or the lowercase versions thereof). Requests use the proxy from the environment variable matching their scheme, unless excluded by NO_PROXY.

The environment values may be either a complete URL or a "host[:port]", in which case the "http" scheme is assumed. An error is returned if the value is a different form.

A nil URL and nil error are returned if no proxy is defined in the environment, or a proxy should not be used for the given request, as defined by NO_PROXY.

As a special case, if req.URL.Host is "localhost" (with or without a port number), then a nil URL and nil error will be returned.

func ReadRequest

func ReadRequest(t testing.TB, b *bufio.Reader) *http.Request

ReadRequest reads and parses an incoming request from b.

ReadRequest is a low-level function and should only be used for specialized applications; most code should use the [Server] to read requests and handle them via the [Handler] interface. ReadRequest only supports HTTP/1.x requests. For HTTP/2, use golang.org/x/net/http2.

func ReadResponse

func ReadResponse(t testing.TB, r *bufio.Reader, req *http.Request) *http.Response

ReadResponse reads and returns an HTTP response from r. The req parameter optionally specifies the [Request] that corresponds to this [Response]. If nil, a GET request is assumed. Clients must call resp.Body.Close when finished reading resp.Body. After that call, clients can inspect resp.Trailer to find key/value pairs included in the response trailer.

func Serve

func Serve(t testing.TB, l net.Listener, handler http.Handler)

Serve accepts incoming HTTP connections on the listener l, creating a new service goroutine for each. The service goroutines read requests and then call handler to reply to them.

The handler is typically nil, in which case [DefaultServeMux] is used.

HTTP/2 support is only enabled if the Listener returns [*tls.Conn] connections and they were configured with "h2" in the TLS Config.NextProtos.

Serve always returns a non-nil error.

func ServeTLS

func ServeTLS(t testing.TB, l net.Listener, handler http.Handler, certFile, keyFile string)

ServeTLS accepts incoming HTTPS connections on the listener l, creating a new service goroutine for each. The service goroutines read requests and then call handler to reply to them.

The handler is typically nil, in which case [DefaultServeMux] is used.

Additionally, files containing a certificate and matching private key for the server must be provided. If the certificate is signed by a certificate authority, the certFile should be the concatenation of the server's certificate, any intermediates, and the CA's certificate.

ServeTLS always returns a non-nil error.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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