Skip to content

Commit

Permalink
feature/add-callback-and-async-callback (#157)
Browse files Browse the repository at this point in the history
* feature/add-callback-and-async-callback

* Fix unit tests and add new cases
  • Loading branch information
sebastianbuechler committed Aug 23, 2023
1 parent 26eb57d commit a03d15b
Show file tree
Hide file tree
Showing 5 changed files with 244 additions and 106 deletions.
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);

0 comments on commit a03d15b

Please sign in to comment.