fihttp

package
v0.151.0 Latest Latest
Warning

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

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

Documentation

Overview

Example
package main

import (
	"context"
	"errors"
	"log"
	"net/http"

	"go.llib.dev/testcase/faultinject"
	"go.llib.dev/testcase/faultinject/fihttp"
)

func main() {
	type FaultTag struct{}

	client := &http.Client{
		Transport: fihttp.RoundTripper{
			Next:        http.DefaultTransport,
			ServiceName: "xy-external-service-name",
		},
	}

	myHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// if clients inject the "mapped-fault-name" then we will detect it here.
		if err := r.Context().Value(FaultTag{}).(error); err != nil {
			const code = http.StatusInternalServerError
			http.Error(w, http.StatusText(code), code)
			return
		}

		// outbound request will have faults injected which is not meant to our service
		outboundRequest, err := http.NewRequestWithContext(r.Context(), http.MethodGet, "http://example.com/", nil)
		if err != nil {
			const code = http.StatusInternalServerError
			http.Error(w, http.StatusText(code), code)
			return
		}
		_, _ = client.Do(outboundRequest)

		w.WriteHeader(http.StatusTeapot)
	})

	myHandlerWithFaultInjectionMiddleware := fihttp.Handler{
		Next:        myHandler,
		ServiceName: "our-service-name",
		FaultsMapping: fihttp.FaultsMapping{
			"mapped-fault-name": func(ctx context.Context) context.Context {
				return faultinject.Inject(ctx, FaultTag{}, errors.New("boom"))
			},
		},
	}

	if err := http.ListenAndServe(":8080", myHandlerWithFaultInjectionMiddleware); err != nil {
		log.Fatal(err.Error())
	}
}
Output:

Index

Examples

Constants

View Source
const Header = `Fault-Inject`

Variables

This section is empty.

Functions

func Propagate

func Propagate(ctx context.Context, fs ...Fault) context.Context

Types

type Fault

type Fault struct {
	ServiceName string `json:"service_name,omitempty"`
	Name        string `json:"name"`
}

type FaultsMapping

type FaultsMapping map[string]InjectFn

type Handler

type Handler struct {
	Next          http.Handler
	ServiceName   string
	FaultsMapping FaultsMapping
}

func (Handler) ServeHTTP

func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type InjectFn

type InjectFn func(context.Context) context.Context

type RoundTripper

type RoundTripper struct {
	Next http.RoundTripper
	// ServiceName is the name of the service of which this http.Client meant to do requests.
	ServiceName string
}
Example
package main

import (
	"context"
	"net/http"
	"strings"

	"go.llib.dev/testcase/faultinject/fihttp"
)

func main() {
	const serviceName = "xy-service"
	c := &http.Client{
		Transport: fihttp.RoundTripper{
			Next:        http.DefaultTransport,
			ServiceName: serviceName,
		},
	}

	ctx := context.Background()

	ctx = fihttp.Propagate(ctx, fihttp.Fault{
		ServiceName: serviceName,
		Name:        "fault-name",
	})

	req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://localhost:8080", strings.NewReader(""))
	if err != nil {
		panic(err)
	}

	response, err := c.Do(req)
	if err != nil {
		panic(err)
	}

	_ = response
}
Output:

func (RoundTripper) RoundTrip

func (rt RoundTripper) RoundTrip(r *http.Request) (*http.Response, error)

Jump to

Keyboard shortcuts

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