(t testing.TB)
| 167 | } |
| 168 | |
| 169 | func (scenario *ApiScenario) test(t testing.TB) { |
| 170 | var testApp *TestApp |
| 171 | if scenario.TestAppFactory != nil { |
| 172 | testApp = scenario.TestAppFactory(t) |
| 173 | if testApp == nil { |
| 174 | t.Fatal("TestAppFactory must return a non-nill app instance") |
| 175 | } |
| 176 | } else { |
| 177 | var testAppErr error |
| 178 | testApp, testAppErr = NewTestApp() |
| 179 | if testAppErr != nil { |
| 180 | t.Fatalf("Failed to initialize the test app instance: %v", testAppErr) |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | // https://github.com/pocketbase/pocketbase/discussions/7267 |
| 185 | if scenario.TestAppFactory == nil || !scenario.DisableTestAppCleanup { |
| 186 | defer testApp.Cleanup() |
| 187 | } |
| 188 | |
| 189 | baseRouter, err := apis.NewRouter(testApp) |
| 190 | if err != nil { |
| 191 | t.Fatal(err) |
| 192 | } |
| 193 | |
| 194 | // manually trigger the serve event to ensure that custom app routes and middlewares are registered |
| 195 | serveEvent := new(core.ServeEvent) |
| 196 | serveEvent.App = testApp |
| 197 | serveEvent.Router = baseRouter |
| 198 | |
| 199 | serveErr := testApp.OnServe().Trigger(serveEvent, func(e *core.ServeEvent) error { |
| 200 | if scenario.BeforeTestFunc != nil { |
| 201 | scenario.BeforeTestFunc(t, testApp, e) |
| 202 | } |
| 203 | |
| 204 | // reset the event counters in case a hook was triggered from a before func (eg. db save) |
| 205 | testApp.ResetEventCalls() |
| 206 | |
| 207 | // add middleware to timeout long-running requests (eg. keep-alive routes) |
| 208 | e.Router.Bind(&hook.Handler[*core.RequestEvent]{ |
| 209 | Func: func(re *core.RequestEvent) error { |
| 210 | slowTimer := time.AfterFunc(3*time.Second, func() { |
| 211 | t.Logf("[WARN] Long running test %q", scenario.Name) |
| 212 | }) |
| 213 | defer slowTimer.Stop() |
| 214 | |
| 215 | if scenario.Timeout > 0 { |
| 216 | ctx, cancelFunc := context.WithTimeout(re.Request.Context(), scenario.Timeout) |
| 217 | defer cancelFunc() |
| 218 | re.Request = re.Request.Clone(ctx) |
| 219 | } |
| 220 | |
| 221 | return re.Next() |
| 222 | }, |
| 223 | Priority: -9999, |
| 224 | }) |
| 225 | |
| 226 | recorder := httptest.NewRecorder() |
no test coverage detected