Skip to content

Commit

Permalink
Merge pull request zino-hofmann#348 from truongsinh/fix/crashed-uploa…
Browse files Browse the repository at this point in the history
…d-file

fix(client): crashed upload file
  • Loading branch information
micimize authored Jun 28, 2019
2 parents 72d331f + aa116a2 commit 117c495
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 19 deletions.
3 changes: 3 additions & 0 deletions packages/graphql/dart_test.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
tags:
integration:

define_platforms:
chromium:
name: Chromium
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql/lib/src/link/http/link_http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ Future<BaseRequest> _prepareRequest(
// @deprecated, backward compatible only
// in case the body is io.File
// in future release, io.File will no longer be supported
if (isIoFile(body)) {
if (isIoFile(object)) {
return null;
}
return object.toJson();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,18 @@ Future<Map<String, MultipartFile>> deprecatedHelper(
return null;
}

bool isIoFile(object) => object is io.File;
bool isIoFile(object) {
final r = object is io.File;
if(r) {
print(r'''
⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️ DEPRECATION WARNING ⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️
Please do not use `File` direcly anymore. Instead, use
`MultipartFile`. There's also a utitlity method to help you
`import 'package:graphql/utilities.dart' show multipartFileFrom;`
⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️ DEPRECATION WARNING ⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️
''');
}
return r;
}
11 changes: 10 additions & 1 deletion packages/graphql/lib/src/utilities/file_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,19 @@ import 'package:http_parser/http_parser.dart';
import 'package:mime/mime.dart';
import 'package:path/path.dart';

MediaType contentType(f) {
final a = lookupMimeType(f.path);
if (a == null) {
return null;
}
final b = MediaType.parse(a);
return b;
}

Future<MultipartFile> multipartFileFrom(io.File f) async => MultipartFile(
'',
f.openRead(),
await f.length(),
contentType: MediaType.parse(lookupMimeType(f.path)),
contentType: contentType(f),
filename: basename(f.path),
);
10 changes: 2 additions & 8 deletions packages/graphql/test/in_memory_storage_io_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import 'dart:io';
import 'package:test/test.dart';
import 'package:graphql/src/cache/in_memory_io.dart';

import 'helpers.dart';

void main() {
group('In memory exception handling', () {
test('FileSystemException', overridePrint((List<String> log) async {
Expand All @@ -19,11 +21,3 @@ void main() {
}));
});
}

overridePrint(testFn(List<String> log)) => () {
final log = <String>[];
final spec = new ZoneSpecification(print: (_, __, ___, String msg) {
log.add(msg);
});
return Zone.current.fork(specification: spec).run(() => testFn(log));
};
94 changes: 94 additions & 0 deletions packages/graphql/test/multipart_upload_io_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
@TestOn("vm")

import 'package:test/test.dart';
import 'package:mockito/mockito.dart';
import 'package:http/http.dart' as http;
import 'dart:io' as io;

import 'package:graphql/client.dart';

import 'helpers.dart';

class MockHttpClient extends Mock implements http.Client {}

void main() {
HttpLink httpLink;
AuthLink authLink;
Link link;
GraphQLClient graphQLClientClient;
MockHttpClient mockHttpClient;

group(
'upload',
() {
const String uploadMutation = r'''
mutation($files: [Upload!]!) {
multipleUpload(files: $files) {
id
filename
mimetype
path
}
}
''';

setUp(() {
mockHttpClient = MockHttpClient();

when(mockHttpClient.send(any)).thenAnswer((Invocation a) async {
return simpleResponse(body: '{"data": {}}');
});

httpLink = HttpLink(
uri: 'http://localhost:3001/graphql', httpClient: mockHttpClient);

authLink = AuthLink(
getToken: () async => 'Bearer my-special-bearer-token',
);

link = authLink.concat(httpLink);

graphQLClientClient = GraphQLClient(
cache: getTestCache(),
link: link,
);
});

test(
'upload with io.File instance deprecation warning',
overridePrint((log) async {
final MutationOptions _options = MutationOptions(
document: uploadMutation,
variables: <String, dynamic>{
'files': [
io.File('pubspec.yaml'),
],
},
);
final QueryResult r = await graphQLClientClient.mutate(_options);

expect(r.errors, isNull);
expect(r.data, isNotNull);
expect(log, hasLength(5));
final warningMessage = r'''
⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️ DEPRECATION WARNING ⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️
Please do not use `File` direcly anymore. Instead, use
`MultipartFile`. There's also a utitlity method to help you
`import 'package:graphql/utilities.dart' show multipartFileFrom;`
⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️ DEPRECATION WARNING ⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️⚠️️️️️️️️
''';
expect(log[0], warningMessage);
expect(log[1], warningMessage);
expect(log[2], warningMessage);
expect(log[3], warningMessage);
expect(log[4], warningMessage);
}),
);
},
onPlatform: {
"!vm": Skip("This test is only for VM"),
},
);
}
10 changes: 6 additions & 4 deletions packages/graphql/test/socket_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@ import 'package:test/test.dart';
import 'package:graphql/src/socket_client.dart'
show SocketClient, SocketConnectionState;

import 'helpers.dart';

void main() {
group('SocketClient', () {
SocketClient socketClient;
setUp(() {
setUp(overridePrint((log) {
socketClient = SocketClient(
'ws://echo.websocket.org',
protocols: null,
randomBytesForUuid: Uint8List.fromList(
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]),
);
});
tearDown(() async {
}));
tearDown(overridePrint((log) async {
await socketClient.dispose();
});
}));
test('connection', () async {
await expectLater(
socketClient.connectionState.asBroadcastStream(),
Expand Down
10 changes: 6 additions & 4 deletions packages/graphql/test/websocket_legacy_io_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,26 @@ import 'package:graphql/src/websocket/messages.dart';

import 'package:graphql/legacy_socket_api/legacy_socket_client.dart';

import 'helpers.dart';

void main() {
group(
'SocketClient',
() {
// ignore: deprecated_member_use_from_same_package
SocketClient socketClient;
setUp(() {
setUp(overridePrint((log) {
// ignore: deprecated_member_use_from_same_package
socketClient = SocketClient(
'ws://echo.websocket.org',
protocols: null,
randomBytesForUuid: Uint8List.fromList(
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]),
);
});
tearDown(() async {
}));
tearDown(overridePrint((log) async {
await socketClient.dispose();
});
}));
test('connection', () async {
await expectLater(
socketClient.connectionState.asBroadcastStream(),
Expand Down

0 comments on commit 117c495

Please sign in to comment.