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

Function TestRecoverWithConfig

middleware/recover_test.go:94–150  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

92}
93
94func TestRecoverWithConfig(t *testing.T) {
95 var testCases = []struct {
96 name string
97 givenNoPanic bool
98 whenConfig RecoverConfig
99 expectErrContain string
100 expectErr string
101 }{
102 {
103 name: "ok, default config",
104 whenConfig: DefaultRecoverConfig,
105 expectErrContain: "[PANIC RECOVER] testPANIC goroutine",
106 },
107 {
108 name: "ok, no panic",
109 givenNoPanic: true,
110 whenConfig: DefaultRecoverConfig,
111 expectErrContain: "",
112 },
113 {
114 name: "ok, DisablePrintStack",
115 whenConfig: RecoverConfig{
116 DisablePrintStack: true,
117 },
118 expectErr: "testPANIC",
119 },
120 }
121
122 for _, tc := range testCases {
123 t.Run(tc.name, func(t *testing.T) {
124 e := echo.New()
125
126 req := httptest.NewRequest(http.MethodGet, "/", nil)
127 rec := httptest.NewRecorder()
128 c := e.NewContext(req, rec)
129
130 config := tc.whenConfig
131 h := RecoverWithConfig(config)(func(c *echo.Context) error {
132 if tc.givenNoPanic {
133 return nil
134 }
135 panic("testPANIC")
136 })
137
138 err := h(c)
139
140 if tc.expectErrContain != "" {
141 assert.Contains(t, err.Error(), tc.expectErrContain)
142 } else if tc.expectErr != "" {
143 assert.Contains(t, err.Error(), tc.expectErr)
144 } else {
145 assert.NoError(t, err)
146 }
147 assert.Equal(t, http.StatusOK, rec.Code) // status is still untouched. err is returned from middleware chain
148 })
149 }
150}

Callers

nothing calls this directly

Calls 3

RecoverWithConfigFunction · 0.85
NewContextMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…