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

Function TestServerErrorEncoder

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

Source from the content-addressed store, hash-verified

150}
151
152func TestServerErrorEncoder(t *testing.T) {
153 errTeapot := errors.New("teapot")
154 code := func(err error) int {
155 if errors.Is(err, errTeapot) {
156 return http.StatusTeapot
157 }
158 return http.StatusInternalServerError
159 }
160 ecm := jsonrpc.EndpointCodecMap{
161 "add": jsonrpc.EndpointCodec{
162 Endpoint: func(context.Context, interface{}) (interface{}, error) { return struct{}{}, errTeapot },
163 Decode: nopDecoder,
164 Encode: nopEncoder,
165 },
166 }
167 handler := jsonrpc.NewServer(
168 ecm,
169 jsonrpc.ServerErrorEncoder(func(_ context.Context, err error, w http.ResponseWriter) { w.WriteHeader(code(err)) }),
170 )
171 server := httptest.NewServer(handler)
172 defer server.Close()
173 resp, _ := http.Post(server.URL, "application/json", addBody())
174 if want, have := http.StatusTeapot, resp.StatusCode; want != have {
175 t.Errorf("want %d, have %d", want, have)
176 }
177}
178
179func TestCanRejectNonPostRequest(t *testing.T) {
180 ecm := jsonrpc.EndpointCodecMap{}

Callers

nothing calls this directly

Calls 5

NewServerFunction · 0.92
ServerErrorEncoderFunction · 0.92
addBodyFunction · 0.85
WriteHeaderMethod · 0.45
CloseMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…