Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/add-callback-and-async-callback #157

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/src/adapters/dio_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class DioAdapter with Recording, RequestHandling implements HttpClientAdapter {
}

await setDefaultRequestHeaders(dio, requestOptions);
final response = mockResponse(requestOptions);
final response = await mockResponse(requestOptions);

// Waits for defined duration.
if (response.delay != null) await Future.delayed(response.delay!);
Expand Down
91 changes: 88 additions & 3 deletions lib/src/handlers/request_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,28 @@ abstract class MockServer {
Duration? delay,
});

void replyCallback(
int statusCode,
MockDataCallback data, {
Map<String, List<String>> headers = const {
Headers.contentTypeHeader: [Headers.jsonContentType],
},
String? statusMessage,
bool isRedirect = false,
Duration? delay,
});

void replyCallbackAsync(
int statusCode,
MockDataCallbackAsync data, {
Map<String, List<String>> headers = const {
Headers.contentTypeHeader: [Headers.jsonContentType],
},
String? statusMessage,
bool isRedirect = false,
Duration? delay,
});

void throws(
int statusCode,
DioException dioError, {
Expand All @@ -31,7 +53,7 @@ abstract class MockServer {
/// constructs the configured [MockResponse].
class RequestHandler implements MockServer {
/// This is the response.
late MockResponse Function(RequestOptions options) mockResponse;
late Future<MockResponse> Function(RequestOptions options) mockResponse;

/// Stores [MockResponse] in [mockResponse].
@override
Expand All @@ -50,7 +72,7 @@ class RequestHandler implements MockServer {
) ??
false;

mockResponse = (requestOptions) {
mockResponse = (requestOptions) async {
if (data is Uint8List) {
return MockResponseBody.fromBytes(
data,
Expand All @@ -77,9 +99,72 @@ class RequestHandler implements MockServer {
};
}

/// Stores [MockResponse] in [mockResponse].
@override
void replyCallback(
int statusCode,
MockDataCallback data, {
Map<String, List<String>> headers = const {
Headers.contentTypeHeader: [Headers.jsonContentType],
},
String? statusMessage,
bool isRedirect = false,
Duration? delay,
}) {
final isJsonContentType = headers[Headers.contentTypeHeader]?.contains(
Headers.jsonContentType,
) ??
false;

mockResponse = (requestOptions) async {
final rawData = data(requestOptions);

return MockResponseBody.from(
isJsonContentType ? jsonEncode(rawData) : rawData,
statusCode,
headers: headers,
statusMessage: statusMessage,
isRedirect: isRedirect,
delay: delay,
);
};
}

/// Stores [MockResponse] in [mockResponse].
@override
void replyCallbackAsync(
int statusCode,
MockDataCallbackAsync data, {
Map<String, List<String>> headers = const {
Headers.contentTypeHeader: [Headers.jsonContentType],
},
String? statusMessage,
bool isRedirect = false,
Duration? delay,
}) {
final isJsonContentType = headers[Headers.contentTypeHeader]?.contains(
Headers.jsonContentType,
) ??
false;

mockResponse = (requestOptions) async {
final rawData = await data(requestOptions);

return MockResponseBody.from(
isJsonContentType ? jsonEncode(rawData) : rawData,
statusCode,
headers: headers,
statusMessage: statusMessage,
isRedirect: isRedirect,
delay: delay,
);
};
}

/// Stores the [DioException] inside the [mockResponse].
@override
void throws(int statusCode, DioException dioError, {Duration? delay}) {
mockResponse = (requestOptions) => MockDioException.from(dioError, delay);
mockResponse =
(requestOptions) async => MockDioException.from(dioError, delay);
}
}
2 changes: 1 addition & 1 deletion lib/src/interceptors/dio_interceptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class DioInterceptor extends Interceptor with Recording, RequestHandling {
@override
void onRequest(requestOptions, requestInterceptorHandler) async {
await setDefaultRequestHeaders(dio, requestOptions);
final response = mockResponse(requestOptions);
final response = await mockResponse(requestOptions);

// Reject the response if type is MockDioException.
if (isMockDioException(response)) {
Expand Down
6 changes: 5 additions & 1 deletion lib/src/types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ typedef MockServerCallback = void Function(MockServer server);

/// Type for [Recording]'s [ResponseBody], which takes [RequestOptions] as a parameter
/// and compares its signature to saved [Request]'s signature and chooses right response.
typedef MockResponseBodyCallback = MockResponse Function(
typedef MockResponseBodyCallback = Future<MockResponse> Function(
RequestOptions options,
);

/// Type for expect data as function
typedef MockDataCallback = dynamic Function(RequestOptions options);

/// Type for async expect data as function
typedef MockDataCallbackAsync = Future<dynamic> Function(
RequestOptions options);
Loading