httpshort

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

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

Go to latest
Published: Apr 20, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

README

Go library for short-cutting HTTP clients to HTTP handlers

Go Reference

Your client code uses an http.Client. Your server code contains an http.Handler.

This package implements an http.RoundTripper that allows a HTTP client to call a HTTP handler directly without starting a HTTP server.

mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln(w, "Hello world")
}

client := &http.Client{
    Transport: Transport{
        Handler: mux,
    },
}

resp, err := client.Get("http://example.com")

There is also a helper function to create a http.Client from a handler:

mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln(w, "Hello world")
}

client := Client(nil, mux)

resp, err := client.Get("http://example.com")

Copyright Mikhail Gusarov. Licensed under Apache 2.0 license.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Client

func Client(ctx context.Context, handler http.Handler) *http.Client

Client is a helper function that returns an http.Client. The client is configured with the Transport from this package.

Example
mux := http.NewServeMux()
mux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintln(w, "index")
})

client := Client(nil, mux)

resp := must.OK1(client.Get("/"))
defer resp.Body.Close()

fmt.Println("GET / response status:", resp.Status)
fmt.Println("GET / response body:", string(must.OK1(io.ReadAll(resp.Body))))
Output:

GET / response status: 200 OK
GET / response body: index

Types

type Transport

type Transport struct {
	// Context, when specified, limits the lifetime of handler
	// invocation. It ensures that any request passed to the handler
	// gets cancelled if this context is cancelled.
	//
	// Deadlines and values from this context are disregarded.
	//
	// This is particularly useful for timely and clean shutdown of tests.
	Context context.Context //nolint:containedctx

	// Handler is the handler to be invoked
	Handler http.Handler
}

Transport is an http.RoundTripper that directly calls the HTTP handler

This transport is useful for creating http.Client instances for testing: either short-cutting clients to existing HTTP server handlers, or mocking external HTTP endpoints.

Example
mux := http.NewServeMux()
mux.HandleFunc("POST /size", func(w http.ResponseWriter, r *http.Request) {
	// Check passed header
	if r.Header.Get("Content-Type") != "text/plain" {
		http.Error(w, "", http.StatusUnsupportedMediaType)
		return
	}

	body, err := io.ReadAll(r.Body)
	if err != nil {
		http.Error(w, "", http.StatusInternalServerError)
		return
	}

	w.Header().Set("Content-Type", "x-text/digits")
	fmt.Fprintf(w, "%d", len(body))
})

// Create a HTTP client that uses mux

client := &http.Client{
	Transport: Transport{
		Handler: mux,
	},
}

respPost := must.OK1(client.Post("/size", "text/plain", strings.NewReader("1234")))
defer respPost.Body.Close()

fmt.Println("POST /size response status:", respPost.Status)
fmt.Println("POST /size response Content-Type:", respPost.Header.Get("Content-Type"))
fmt.Println("POST /size response body:", string(must.OK1(io.ReadAll(respPost.Body))))
Output:

POST /size response status: 200 OK
POST /size response Content-Type: x-text/digits
POST /size response body: 4

func (Transport) RoundTrip

func (t Transport) RoundTrip(req *http.Request) (*http.Response, error)

Jump to

Keyboard shortcuts

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