inprocesshttp

package
v0.0.53 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: Apache-2.0 Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package inprocesshttp serves HTTP client requests straight from an http.Handler in the same process, without a network listener.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewClient

func NewClient(handler http.Handler, admit func(*url.URL) error) *http.Client

NewClient returns a client that serves every request admit accepts from handler, with the request's Host set to its URL's host, and does not follow redirects. A request admit refuses has its body closed and fails with admit's error, which should name only the URL's scheme, host, and path. A request whose context ends before the handler finishes returns the context's error, so a client Timeout bounds the call; the abandoned handler finishes into its own buffer. A handler panic is re-raised on the caller's goroutine.

It suits short request/response calls such as a token endpoint's: the whole response is buffered and returned only after the handler finishes, so it cannot carry a streamed response, such as an MCP gateway call's; NewStreamingClient does.

func NewStreamingClient

func NewStreamingClient(handler http.Handler, admit func(*url.URL) error) *http.Client

NewStreamingClient returns a client like NewClient's whose responses stream. As net/http's server does, the writer buffers the response: it is sent, and RoundTrip returns, at the handler's first Flush, once 4 KiB is written, or when the handler returns, and each later Flush sends what was written since. Sending only then keeps a handler that finishes its work after writing a reply from racing the caller's next request.

The handler's request context ends with the caller's, or when the caller closes the body; a body read after the caller's context ended fails with its error unless the handler had already finished. The client has no timeout. A handler panic, http.ErrAbortHandler included, drops the response: before anything was sent RoundTrip fails, after that the body fails with io.ErrUnexpectedEOF; any other panic before a response is re-raised on the caller's goroutine, as NewClient does.

Types

type Recorder

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

Recorder collects a handler's response in memory. The first status written wins; a handler that writes none gets 200.

func NewRecorder

func NewRecorder() *Recorder

NewRecorder returns an empty Recorder.

func (*Recorder) Body

func (b *Recorder) Body() []byte

Body returns the recorded body.

func (*Recorder) Header

func (b *Recorder) Header() http.Header

Header returns the recorded response headers.

func (*Recorder) Status

func (b *Recorder) Status() int

Status returns the recorded status, 200 if the handler wrote none.

func (*Recorder) Write

func (b *Recorder) Write(p []byte) (int, error)

Write appends p to the recorded body.

func (*Recorder) WriteHeader

func (b *Recorder) WriteHeader(status int)

WriteHeader records status unless one was already written.

Jump to

Keyboard shortcuts

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