MCPcopy
hub / github.com/openfga/openfga / TestCustomHTTPErrorHandler

Function TestCustomHTTPErrorHandler

pkg/middleware/http/handler_test.go:20–47  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

18)
19
20func TestCustomHTTPErrorHandler(t *testing.T) {
21 req := httptest.NewRequest(http.MethodGet, "/upper?word=abc", nil)
22 w := httptest.NewRecorder()
23 e := errors.NewEncodedError(int32(openfgav1.ErrorCode_assertions_too_many_items), "some error")
24 metaData := runtime.ServerMetadata{
25 HeaderMD: metadata.New(map[string]string{
26 "foo": "boo",
27 }),
28 TrailerMD: metadata.New(map[string]string{}),
29 }
30 ctx := runtime.NewServerMetadataContext(context.Background(), metaData)
31 CustomHTTPErrorHandler(ctx, w, req, e)
32 res := w.Result()
33 defer res.Body.Close()
34 require.Equal(t, http.StatusBadRequest, res.StatusCode)
35
36 header := res.Header.Get("Foo")
37 require.Equal(t, "boo", header)
38
39 contentType := res.Header.Get("Content-Type")
40 require.Equal(t, "application/json", contentType)
41
42 data, err := io.ReadAll(res.Body)
43 require.NoError(t, err)
44
45 expectedData := "{\"code\":\"assertions_too_many_items\",\"message\":\"some error\"}"
46 require.Equal(t, expectedData, strings.TrimSpace(string(data)))
47}
48
49func TestCustomHTTPErrorHandlerSpecialEncoding(t *testing.T) {
50 req := httptest.NewRequest(http.MethodGet, "/upper?word=abc", nil)

Callers

nothing calls this directly

Calls 5

NewEncodedErrorFunction · 0.92
CustomHTTPErrorHandlerFunction · 0.85
EqualMethod · 0.80
CloseMethod · 0.65
GetMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…