forked from felangel/mason
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dart_frog): multipart/form-data (felangel#551)
Co-authored-by: Felix Angelov <felangelov@gmail.com> Co-authored-by: VuiTv <tangvuicntt@gmail.com>
- Loading branch information
1 parent
eb98fa2
commit 1358a9f
Showing
9 changed files
with
562 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:dart_frog/dart_frog.dart'; | ||
|
||
final contentTypePng = ContentType('image', 'png'); | ||
|
||
Future<Response> onRequest(RequestContext context) async { | ||
final formData = await context.request.formData(); | ||
final photo = formData.files['photo']; | ||
|
||
if (photo == null || photo.contentType.mimeType != contentTypePng.mimeType) { | ||
return Response(statusCode: HttpStatus.badRequest); | ||
} | ||
|
||
return Response.json( | ||
body: {'message': 'Successfully uploaded ${photo.name}'}, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// ignore_for_file: prefer_const_constructors | ||
|
||
import 'dart:io'; | ||
|
||
import 'package:dart_frog/dart_frog.dart'; | ||
import 'package:mocktail/mocktail.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import '../../../routes/photos/upload.dart' as route; | ||
|
||
class _MockRequestContext extends Mock implements RequestContext {} | ||
|
||
class _MockRequest extends Mock implements Request {} | ||
|
||
void main() { | ||
group('POST /upload', () { | ||
test('responds with a 400 when file extension is not .png', () async { | ||
final context = _MockRequestContext(); | ||
final request = _MockRequest(); | ||
when(() => context.request).thenReturn(request); | ||
|
||
final formData = FormData( | ||
fields: {}, | ||
files: { | ||
'photo': UploadedFile( | ||
'file.txt', | ||
ContentType.text, | ||
Stream.fromIterable([[]]), | ||
) | ||
}, | ||
); | ||
when(request.formData).thenAnswer((_) async => formData); | ||
|
||
final response = await route.onRequest(context); | ||
expect(response.statusCode, equals(HttpStatus.badRequest)); | ||
}); | ||
|
||
test('responds with a 200', () async { | ||
final context = _MockRequestContext(); | ||
final request = _MockRequest(); | ||
when(() => context.request).thenReturn(request); | ||
|
||
final formData = FormData( | ||
fields: {}, | ||
files: { | ||
'photo': UploadedFile( | ||
'picture.png', | ||
ContentType('image', 'png'), | ||
Stream.fromIterable([[]]), | ||
) | ||
}, | ||
); | ||
when(request.formData).thenAnswer((_) async => formData); | ||
|
||
final response = await route.onRequest(context); | ||
expect(response.statusCode, equals(HttpStatus.ok)); | ||
expect( | ||
response.json(), | ||
completion( | ||
equals({'message': 'Successfully uploaded picture.png'}), | ||
), | ||
); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.