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

Add image upload test #392

Merged
merged 2 commits into from
Dec 11, 2022
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
1 change: 1 addition & 0 deletions chopper/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ dev_dependencies:
http_parser: ^4.0.0
lints: ^2.0.0
test: ^1.16.4
transparent_image: ^2.0.0
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hooray, I will use it in my projects. Cool!

chopper_generator:
path: ../chopper_generator
24 changes: 24 additions & 0 deletions chopper/test/multipart_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:http/http.dart' as http;
import 'package:http/testing.dart';
import 'package:http_parser/http_parser.dart';
import 'package:test/test.dart';
import 'package:transparent_image/transparent_image.dart';

import 'test_service.dart';

Expand Down Expand Up @@ -66,6 +67,29 @@ void main() {

chopper.dispose();
});

test('image', () async {
final httpClient = MockClient((http.Request req) async {
final String body = String.fromCharCodes(req.bodyBytes);

expect(req.headers['Content-Type'], contains('multipart/form-data;'));
expect(body, contains('content-type: application/octet-stream'));
expect(body, contains('content-disposition: form-data; name="image"'));
expect(
body,
contains(String.fromCharCodes(kTransparentImage)),
);

return http.Response('ok', 200);
});

final chopper = ChopperClient(client: httpClient);
final service = HttpTestService.create(chopper);

await service.postImage(kTransparentImage);

chopper.dispose();
});
});

test('file with MultipartFile', () async {
Expand Down
19 changes: 19 additions & 0 deletions chopper/test/test_service.chopper.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions chopper/test/test_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ abstract class HttpTestService extends ChopperService {
@PartFile('file') List<int> bytes,
);

@Post(path: 'image')
@multipart
Future<Response> postImage(
@PartFile('image') List<int> imageData,
);

@Post(path: 'file')
@multipart
Future<Response> postMultipartFile(
Expand Down