MCPcopy
hub / github.com/labstack/echo / TestContextTimeoutErrorOutInHandler

Function TestContextTimeoutErrorOutInHandler

middleware/context_timeout_test.go:54–79  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

52}
53
54func TestContextTimeoutErrorOutInHandler(t *testing.T) {
55 t.Parallel()
56 m := ContextTimeoutWithConfig(ContextTimeoutConfig{
57 // Timeout has to be defined or the whole flow for timeout middleware will be skipped
58 Timeout: 10 * time.Millisecond,
59 })
60
61 req := httptest.NewRequest(http.MethodGet, "/", nil)
62 rec := httptest.NewRecorder()
63
64 e := echo.New()
65 c := e.NewContext(req, rec)
66
67 rec.Code = 1 // we want to be sure that even 200 will not be sent
68 err := m(func(c *echo.Context) error {
69 // this error must not be written to the client response. Middlewares upstream of timeout middleware must be able
70 // to handle returned error and this can be done only then handler has not yet committed (written status code)
71 // the response.
72 return echo.NewHTTPError(http.StatusTeapot, "err")
73 })(c)
74
75 assert.Error(t, err)
76 assert.EqualError(t, err, "code=418, message=err")
77 assert.Equal(t, 1, rec.Code)
78 assert.Equal(t, "", rec.Body.String())
79}
80
81func TestContextTimeoutSuccessfulRequest(t *testing.T) {
82 t.Parallel()

Callers

nothing calls this directly

Calls 4

ContextTimeoutWithConfigFunction · 0.85
NewContextMethod · 0.80
ErrorMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…