MCPcopy
hub / github.com/go-kit/kit / TestServerErrorEncoder

Function TestServerErrorEncoder

transport/http/server_test.go:59–79  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

57}
58
59func TestServerErrorEncoder(t *testing.T) {
60 errTeapot := errors.New("teapot")
61 code := func(err error) int {
62 if errors.Is(err, errTeapot) {
63 return http.StatusTeapot
64 }
65 return http.StatusInternalServerError
66 }
67 handler := httptransport.NewServer(
68 func(context.Context, interface{}) (interface{}, error) { return struct{}{}, errTeapot },
69 func(context.Context, *http.Request) (interface{}, error) { return struct{}{}, nil },
70 func(context.Context, http.ResponseWriter, interface{}) error { return nil },
71 httptransport.ServerErrorEncoder(func(_ context.Context, err error, w http.ResponseWriter) { w.WriteHeader(code(err)) }),
72 )
73 server := httptest.NewServer(handler)
74 defer server.Close()
75 resp, _ := http.Get(server.URL)
76 if want, have := http.StatusTeapot, resp.StatusCode; want != have {
77 t.Errorf("want %d, have %d", want, have)
78 }
79}
80
81func TestServerHappyPath(t *testing.T) {
82 step, response := testServer(t)

Callers

nothing calls this directly

Calls 3

WriteHeaderMethod · 0.45
CloseMethod · 0.45
GetMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…