MCPcopy
hub / github.com/pocketbase/pocketbase / test

Method test

tests/api.go:169–317  ·  view source on GitHub ↗
(t testing.TB)

Source from the content-addressed store, hash-verified

167}
168
169func (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()

Callers 15

TestMethod · 0.95
BenchmarkMethod · 0.95
tinymce.min.jsFile · 0.80
jtFunction · 0.80
ssFunction · 0.80
CsFunction · 0.80
kFunction · 0.80
OFunction · 0.80
GFunction · 0.80
yFunction · 0.80
waFunction · 0.80
RaFunction · 0.80

Calls 14

CleanupMethod · 0.95
ResetEventCallsMethod · 0.95
NewRouterFunction · 0.92
NewTestAppFunction · 0.85
TriggerMethod · 0.80
StopMethod · 0.80
BuildMuxMethod · 0.80
OnServeMethod · 0.65
ContextMethod · 0.65
NextMethod · 0.65
SetMethod · 0.65
BindMethod · 0.45

Tested by

no test coverage detected