onRequest method
Called when the request is about to be sent.
Implementation
@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
// Check if Joker is active
if (!Joker.isActive) {
// Continue with normal request
handler.next(options);
return;
}
// Try to find a matching stub
final stub = Joker.findStubForRequest(
method: options.method,
uri: options.uri,
);
if (stub == null) {
// No stub found, make real request
handler.next(options);
return;
}
// Get the stubbed response
final jokerResponse = Joker.getResponseForStub(
stub,
method: options.method,
uri: options.uri,
);
if (jokerResponse == null) {
// Shouldn't happen, but fallback to real request
handler.next(options);
return;
}
// Apply delay if specified
if (jokerResponse.delay != null) {
Future.delayed(jokerResponse.delay!).then((_) {
_resolveWithStub(jokerResponse, options, handler);
});
} else {
_resolveWithStub(jokerResponse, options, handler);
}
}