(t *testing.T)
| 92 | } |
| 93 | |
| 94 | func 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 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…