Documentation
¶
Overview ¶
Package httpsrv provides net/http middleware that subjects inbound requests to chaos. The Middleware mounts in any net/http chain.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Middleware ¶
Middleware returns an http middleware that consults eng on every request. If eng is nil or has no rules, the wrapper is a near-zero-cost passthrough.
Example ¶
package main
import (
"errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
"github.com/ag4r/chaotic/adapter/httpsrv"
"github.com/ag4r/chaotic/engine"
"github.com/ag4r/chaotic/fault"
)
func main() {
eng := engine.New().AddRule(engine.NewRule(
engine.MatchKind(engine.OpHTTPServer),
engine.Times(1),
engine.WithFault(fault.Error(errors.New("overloaded"))),
).Named("inbound"))
var h http.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprintln(w, "handled")
})
h = httpsrv.Middleware(eng)(h)
srv := httptest.NewServer(h)
defer srv.Close()
get := func() int {
resp, err := http.Get(srv.URL)
if err != nil {
return -1
}
defer func() { _ = resp.Body.Close() }()
_, _ = io.Copy(io.Discard, resp.Body)
return resp.StatusCode
}
fmt.Println("request 1 status:", get()) // chaos fault -> 500 before the handler
fmt.Println("request 2 status:", get()) // chaos exhausted -> handler runs
}
Output: request 1 status: 500 request 2 status: 200
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.