(t *testing.T)
| 150 | } |
| 151 | |
| 152 | func 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 | |
| 179 | func TestCanRejectNonPostRequest(t *testing.T) { |
| 180 | ecm := jsonrpc.EndpointCodecMap{} |
nothing calls this directly
no test coverage detected
searching dependent graphs…