Skip to content

Commit

Permalink
Support custom "Accept" header (fluttercommunity#86)
Browse files Browse the repository at this point in the history
* Bump to Flutter 1.20.2 stable

* Support custom "Accept" header
  • Loading branch information
ened authored Sep 10, 2020
1 parent 81b0b79 commit 19ac599
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .ci/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# https://github.com/cirruslabs/docker-images-flutter/blob/master/.cirrus.yml
FROM cirrusci/flutter:1.17.5
FROM cirrusci/flutter:1.20.2

RUN yes | sdkmanager \
"platforms;android-29" \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,13 @@ public Result doWorkInternal() {
RequestBody requestBody = new CountingRequestBody(innerRequestBody, getId().toString(), this);
Request.Builder requestBuilder = new Request.Builder();

requestBuilder.addHeader("Accept", "*/*");

if (headers != null) {
for (String key : headers.keySet()) {
String header = headers.get(key);
if (header != null && !header.isEmpty()) {
requestBuilder = requestBuilder.addHeader(key, header);
requestBuilder = requestBuilder.header(key, header);
}
}
}
Expand All @@ -214,8 +216,6 @@ public Result doWorkInternal() {
null));
}

requestBuilder.addHeader("Accept", "application/json; charset=utf-8");

Request request;

switch (method.toUpperCase()) {
Expand Down
8 changes: 8 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,11 @@ firebase deploy
```

6. run example app

## Driver tests

Run the current end to end test suite:

```
flutter drive --driver=test_driver/flutter_uploader_e2e_test.dart test_driver/flutter_uploader_test.dart
```
5 changes: 0 additions & 5 deletions example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,6 @@
"${BUILT_PRODUCTS_DIR}/e2e/e2e.framework",
"${BUILT_PRODUCTS_DIR}/file_picker/file_picker.framework",
"${BUILT_PRODUCTS_DIR}/flutter_local_notifications/flutter_local_notifications.framework",
"${BUILT_PRODUCTS_DIR}/flutter_plugin_android_lifecycle/flutter_plugin_android_lifecycle.framework",
"${BUILT_PRODUCTS_DIR}/flutter_uploader/flutter_uploader.framework",
"${BUILT_PRODUCTS_DIR}/image_picker/image_picker.framework",
"${BUILT_PRODUCTS_DIR}/path_provider/path_provider.framework",
Expand All @@ -313,7 +312,6 @@
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/e2e.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/file_picker.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/flutter_local_notifications.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/flutter_plugin_android_lifecycle.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/flutter_uploader.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/image_picker.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/path_provider.framework",
Expand Down Expand Up @@ -360,7 +358,6 @@
/* Begin XCBuildConfiguration section */
249021D3217E4FDB00AE95B9 /* Profile */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = 7AFA3C8E1D35360C0083082E /* Release.xcconfig */;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
Expand Down Expand Up @@ -441,7 +438,6 @@
};
97C147031CF9000F007C117D /* Debug */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = 9740EEB21CF90195004384FC /* Debug.xcconfig */;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
Expand Down Expand Up @@ -499,7 +495,6 @@
};
97C147041CF9000F007C117D /* Release */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = 7AFA3C8E1D35360C0083082E /* Release.xcconfig */;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
Expand Down
28 changes: 28 additions & 0 deletions example/test_driver/flutter_uploader_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,25 @@ void main() {

expect(json['message'], 'Successfully uploaded');
expect(res.statusCode, 200);
expect(json['request']['headers']['accept'], '*/*');
expect(res.status, UploadTaskStatus.complete);
});

testWidgets("can overwrite 'Accept' header", (WidgetTester tester) async {
var fileItem = FileItem(path: await _tmpFile(), field: "file");

final taskId = await uploader.enqueue(
url: url.toString(),
files: [fileItem],
headers: {'Accept': 'application/json, charset=utf-8'},
);
final res = await uploader.result.firstWhere(isCompleted(taskId));
final json = jsonDecode(res.response);

expect(json['request']['headers']['accept'],
'application/json, charset=utf-8');
});

testWidgets("multiple files", (WidgetTester tester) async {
final taskId = await uploader.enqueue(
MultipartFormDataUpload(url: url.toString(), files: [
Expand Down Expand Up @@ -139,9 +155,21 @@ void main() {

expect(json['message'], 'Successfully uploaded');
expect(res.statusCode, 200);
expect(json['headers']['accept'], '*/*');
expect(res.status, UploadTaskStatus.complete);
});

testWidgets("can overwrite 'Accept' header", (WidgetTester tester) async {
final taskId = await uploader.enqueueBinary(
url: url.toString(),
path: await _tmpFile(),
headers: {'Accept': 'application/json, charset=utf-8'},
);
final res = await uploader.result.firstWhere(isCompleted(taskId));
final json = jsonDecode(res.response);

expect(json['headers']['accept'], 'application/json, charset=utf-8');
});
testWidgets("fowards errors", (WidgetTester tester) async {
final taskId = await uploader.enqueue(
RawUpload(
Expand Down
2 changes: 1 addition & 1 deletion lib/flutter_uploader.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
library flutter_uploader;

import 'dart:async';
import 'dart:ui';
import 'dart:ui' show PluginUtilities;

import 'package:equatable/equatable.dart';
import 'package:flutter/foundation.dart';
Expand Down

0 comments on commit 19ac599

Please sign in to comment.