Skip to content

Commit

Permalink
Add support for replying byte data (#146)
Browse files Browse the repository at this point in the history
* Add support for replying byte data

* fix format
  • Loading branch information
maple135790 committed Apr 30, 2023
1 parent af264ae commit e5488a5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/src/handlers/request_handler.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:convert';
import 'dart:typed_data';

import 'package:dio/dio.dart';
import 'package:http_mock_adapter/http_mock_adapter.dart';
Expand Down Expand Up @@ -50,6 +51,16 @@ class RequestHandler implements MockServer {
false;

mockResponse = (requestOptions) {
if (data is Uint8List) {
return MockResponseBody.fromBytes(
data,
statusCode,
headers: headers,
statusMessage: statusMessage,
isRedirect: isRedirect,
delay: delay,
);
}
var rawData = data;
if (data is MockDataCallback) {
rawData = data(requestOptions);
Expand Down
17 changes: 17 additions & 0 deletions lib/src/response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,21 @@ class MockResponseBody extends ResponseBody implements MockResponse {
isRedirect: isRedirect,
delay: delay,
);

static MockResponseBody fromBytes(
Uint8List bytes,
int statusCode, {
required Map<String, List<String>> headers,
String? statusMessage,
required bool isRedirect,
Duration? delay,
}) =>
MockResponseBody(
Stream.value(bytes),
statusCode,
headers: headers,
statusMessage: statusMessage,
isRedirect: isRedirect,
delay: delay,
);
}
24 changes: 24 additions & 0 deletions test/handlers/request_handler_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'dart:ffi';
import 'dart:io';
import 'dart:typed_data';

import 'package:dio/dio.dart';
import 'package:http_mock_adapter/http_mock_adapter.dart';
Expand All @@ -13,6 +15,28 @@ void main() {
setUp(() {
requestHandler = RequestHandler();
});
test('sets response data for a status with Uint8List', () async {
const statusCode = HttpStatus.ok;
final inputData = Uint8List.fromList([1, 2, 3, 4, 5]);

requestHandler.reply(
statusCode,
inputData,
);

final statusHandler = requestHandler.mockResponse;

expect(statusHandler, isNotNull);

final mockResponseBody =
statusHandler(RequestOptions(path: '')) as MockResponseBody;
final resolvedData = await BackgroundTransformer().transformResponse(
RequestOptions(path: '', responseType: ResponseType.bytes),
mockResponseBody,
);

expect(resolvedData, inputData);
});

test('sets response data for a status with JSON by default', () async {
const statusCode = HttpStatus.ok;
Expand Down

2 comments on commit e5488a5

@maple135790
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, is publish error something I need to fix?

@LukaGiorgadze
Copy link
Member

@LukaGiorgadze LukaGiorgadze commented on e5488a5 May 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, is publish error something I need to fix?

Hey @maple135790 !
Yeah, it seems analyze is failing - https://github.com/lomsa-dev/http-mock-adapter/actions/runs/4845618541/jobs/8634640938#step:6:88

Also, this analyze needs to be added in Github Action PR if it's not there, since it was OK during PR.

Screenshot 2023-05-01 at 14 43 36

Please sign in to comment.