|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/labstack/echo/v4" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +// logRecord is one captured logf call. |
| 15 | +type logRecord struct { |
| 16 | + status int |
| 17 | + message string |
| 18 | +} |
| 19 | + |
| 20 | +func newMiddlewareTestEcho() (*echo.Echo, *[]logRecord) { |
| 21 | + records := &[]logRecord{} |
| 22 | + e := echo.New() |
| 23 | + e.Use(errorLogMiddlewareLogf(func(status int, format string, args ...any) { |
| 24 | + *records = append(*records, logRecord{status: status, message: fmt.Sprintf(format, args...)}) |
| 25 | + })) |
| 26 | + return e, records |
| 27 | +} |
| 28 | + |
| 29 | +func doRequest(e *echo.Echo, method, path string) *httptest.ResponseRecorder { |
| 30 | + req := httptest.NewRequest(method, path, nil) |
| 31 | + rec := httptest.NewRecorder() |
| 32 | + e.ServeHTTP(rec, req) |
| 33 | + return rec |
| 34 | +} |
| 35 | + |
| 36 | +func TestErrorLogMiddleware(t *testing.T) { |
| 37 | + apiPath := V0Prefix + "test" |
| 38 | + |
| 39 | + t.Run("success response is passed through and not logged", func(t *testing.T) { |
| 40 | + e, records := newMiddlewareTestEcho() |
| 41 | + bigBody := strings.Repeat("x", 3*errorBodyLogLimit) |
| 42 | + e.GET(apiPath, func(c echo.Context) error { |
| 43 | + return c.String(http.StatusOK, bigBody) |
| 44 | + }) |
| 45 | + |
| 46 | + rec := doRequest(e, http.MethodGet, apiPath) |
| 47 | + require.Equal(t, http.StatusOK, rec.Code) |
| 48 | + require.Equal(t, bigBody, rec.Body.String()) |
| 49 | + require.Empty(t, *records) |
| 50 | + }) |
| 51 | + |
| 52 | + t.Run("handler-written 4xx on API path is logged with body", func(t *testing.T) { |
| 53 | + e, records := newMiddlewareTestEcho() |
| 54 | + e.POST(apiPath, func(c echo.Context) error { |
| 55 | + return c.JSON(http.StatusBadRequest, ErrorMessage("invalid peer id")) |
| 56 | + }) |
| 57 | + |
| 58 | + rec := doRequest(e, http.MethodPost, apiPath) |
| 59 | + require.Equal(t, http.StatusBadRequest, rec.Code) |
| 60 | + require.Len(t, *records, 1) |
| 61 | + require.Equal(t, http.StatusBadRequest, (*records)[0].status) |
| 62 | + require.Contains(t, (*records)[0].message, "POST "+apiPath+": 400:") |
| 63 | + require.Contains(t, (*records)[0].message, "invalid peer id") |
| 64 | + }) |
| 65 | + |
| 66 | + t.Run("4xx outside API prefix is not logged", func(t *testing.T) { |
| 67 | + e, records := newMiddlewareTestEcho() |
| 68 | + e.GET("/static.js", func(c echo.Context) error { |
| 69 | + return c.String(http.StatusNotFound, "404 page not found") |
| 70 | + }) |
| 71 | + |
| 72 | + rec := doRequest(e, http.MethodGet, "/static.js") |
| 73 | + require.Equal(t, http.StatusNotFound, rec.Code) |
| 74 | + require.Empty(t, *records) |
| 75 | + }) |
| 76 | + |
| 77 | + t.Run("5xx outside API prefix is still logged", func(t *testing.T) { |
| 78 | + e, records := newMiddlewareTestEcho() |
| 79 | + e.GET("/broken", func(c echo.Context) error { |
| 80 | + return c.JSON(http.StatusInternalServerError, ErrorMessage("boom")) |
| 81 | + }) |
| 82 | + |
| 83 | + doRequest(e, http.MethodGet, "/broken") |
| 84 | + require.Len(t, *records, 1) |
| 85 | + require.Equal(t, http.StatusInternalServerError, (*records)[0].status) |
| 86 | + }) |
| 87 | + |
| 88 | + t.Run("echo HTTPError is logged with its status and rendered by echo", func(t *testing.T) { |
| 89 | + e, records := newMiddlewareTestEcho() |
| 90 | + e.GET(apiPath, func(c echo.Context) error { |
| 91 | + return echo.NewHTTPError(http.StatusUnauthorized, "bad credentials") |
| 92 | + }) |
| 93 | + |
| 94 | + rec := doRequest(e, http.MethodGet, apiPath) |
| 95 | + require.Equal(t, http.StatusUnauthorized, rec.Code) |
| 96 | + require.Contains(t, rec.Body.String(), "bad credentials") |
| 97 | + require.Len(t, *records, 1) |
| 98 | + require.Equal(t, http.StatusUnauthorized, (*records)[0].status) |
| 99 | + require.Contains(t, (*records)[0].message, "bad credentials") |
| 100 | + }) |
| 101 | + |
| 102 | + t.Run("plain error from handler is logged as 500", func(t *testing.T) { |
| 103 | + e, records := newMiddlewareTestEcho() |
| 104 | + e.GET(apiPath, func(c echo.Context) error { |
| 105 | + return fmt.Errorf("database exploded") |
| 106 | + }) |
| 107 | + |
| 108 | + rec := doRequest(e, http.MethodGet, apiPath) |
| 109 | + require.Equal(t, http.StatusInternalServerError, rec.Code) |
| 110 | + require.Len(t, *records, 1) |
| 111 | + require.Equal(t, http.StatusInternalServerError, (*records)[0].status) |
| 112 | + require.Contains(t, (*records)[0].message, "database exploded") |
| 113 | + }) |
| 114 | + |
| 115 | + t.Run("route not found under API prefix is logged", func(t *testing.T) { |
| 116 | + e, records := newMiddlewareTestEcho() |
| 117 | + |
| 118 | + rec := doRequest(e, http.MethodGet, V0Prefix+"no/such/route") |
| 119 | + require.Equal(t, http.StatusNotFound, rec.Code) |
| 120 | + require.Len(t, *records, 1) |
| 121 | + require.Equal(t, http.StatusNotFound, (*records)[0].status) |
| 122 | + }) |
| 123 | + |
| 124 | + t.Run("oversized error body is truncated in the log, intact on the wire", func(t *testing.T) { |
| 125 | + e, records := newMiddlewareTestEcho() |
| 126 | + bigError := strings.Repeat("e", 2*errorBodyLogLimit) |
| 127 | + e.GET(apiPath, func(c echo.Context) error { |
| 128 | + return c.String(http.StatusBadRequest, bigError) |
| 129 | + }) |
| 130 | + |
| 131 | + rec := doRequest(e, http.MethodGet, apiPath) |
| 132 | + require.Equal(t, bigError, rec.Body.String()) |
| 133 | + require.Len(t, *records, 1) |
| 134 | + message := (*records)[0].message |
| 135 | + require.Contains(t, message, "(body truncated)") |
| 136 | + // message = "GET <path>: 400: <capped body> (body truncated)" — the |
| 137 | + // captured part must be capped at errorBodyLogLimit, not the full 2x. |
| 138 | + require.Less(t, len(message), errorBodyLogLimit+200) |
| 139 | + }) |
| 140 | + |
| 141 | + t.Run("committed response with error returned is logged once", func(t *testing.T) { |
| 142 | + e, records := newMiddlewareTestEcho() |
| 143 | + e.GET(apiPath, func(c echo.Context) error { |
| 144 | + _ = c.JSON(http.StatusBadRequest, ErrorMessage("written and returned")) |
| 145 | + return fmt.Errorf("handler also returned an error") |
| 146 | + }) |
| 147 | + |
| 148 | + rec := doRequest(e, http.MethodGet, apiPath) |
| 149 | + require.Equal(t, http.StatusBadRequest, rec.Code) |
| 150 | + require.Len(t, *records, 1) |
| 151 | + require.Contains(t, (*records)[0].message, "written and returned") |
| 152 | + }) |
| 153 | + |
| 154 | + t.Run("Flush is passed through to the underlying writer", func(t *testing.T) { |
| 155 | + e, _ := newMiddlewareTestEcho() |
| 156 | + e.GET(apiPath, func(c echo.Context) error { |
| 157 | + _ = c.String(http.StatusOK, "chunk") |
| 158 | + c.Response().Flush() |
| 159 | + return nil |
| 160 | + }) |
| 161 | + |
| 162 | + rec := doRequest(e, http.MethodGet, apiPath) |
| 163 | + require.Equal(t, http.StatusOK, rec.Code) |
| 164 | + require.True(t, rec.Flushed) |
| 165 | + }) |
| 166 | +} |
0 commit comments