Skip to content

Commit

Permalink
Closes #2 fixes missing Tus-Resumable header
Browse files Browse the repository at this point in the history
  • Loading branch information
jjmutumi committed Sep 4, 2020
1 parent f9cea6c commit 809364c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
8 changes: 7 additions & 1 deletion lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ class TusClient {
while (!_pauseUpload && _offset < totalBytes) {
final uploadHeaders = Map<String, String>.from(headers ?? {})
..addAll({
"Tus-Resumable": tusVersion,
"Upload-Offset": "$_offset",
"Content-Type": "application/offset+octet-stream"
});
Expand Down Expand Up @@ -213,7 +214,12 @@ class TusClient {
/// Get offset from server throwing [ProtocolException] on error
Future<int> _getOffset() async {
final client = getHttpClient();
final response = await client.head(_uploadUrl, headers: headers);

final offsetHeaders = Map<String, String>.from(headers ?? {})
..addAll({
"Tus-Resumable": tusVersion,
});
final response = await client.head(_uploadUrl, headers: offsetHeaders);

if (!(response.statusCode >= 200 && response.statusCode < 300)) {
throw ProtocolException(
Expand Down
37 changes: 29 additions & 8 deletions test/src/client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ main() {
await client.create();

expect(client.uploadUrl.toString(), equals(uploadLocation));
expect(
verify(client.httpClient.post(url, headers: captureAnyNamed('headers')))
.captured
.first,
contains('Tus-Resumable'));
});

test('client_test.TusClient.create().failure.empty.location', () async {
Expand Down Expand Up @@ -139,7 +144,7 @@ main() {
when(client.httpClient.post(url, headers: anyNamed('headers'))).thenAnswer(
(_) async =>
http.Response("", 201, headers: {"location": uploadLocation}));
when(client.httpClient.head(any)).thenAnswer(
when(client.httpClient.head(any, headers: anyNamed('headers'))).thenAnswer(
(_) async => http.Response("", 200, headers: {"upload-offset": "0"}));
when(client.httpClient.patch(
any,
Expand All @@ -155,14 +160,30 @@ main() {

expect(success, isTrue);
expect(progress, equals(100));
expect(
verify(client.httpClient.post(any, headers: captureAnyNamed('headers')))
.captured
.first,
contains('Tus-Resumable'));
expect(
verify(client.httpClient.head(any, headers: captureAnyNamed('headers')))
.captured
.first,
contains('Tus-Resumable'));
expect(
verify(client.httpClient.patch(any,
headers: captureAnyNamed('headers'), body: anyNamed('body')))
.captured
.first,
contains('Tus-Resumable'));
});

test('client_test.TusClient.upload().pause', () async {
final client = MockTusClient(url, file, maxChunkSize: 50);
when(client.httpClient.post(url, headers: anyNamed('headers'))).thenAnswer(
(_) async =>
http.Response("", 201, headers: {"location": uploadLocation}));
when(client.httpClient.head(any)).thenAnswer(
when(client.httpClient.head(any, headers: anyNamed('headers'))).thenAnswer(
(_) async => http.Response("", 200, headers: {"upload-offset": "0"}));
when(client.httpClient.patch(
any,
Expand All @@ -187,7 +208,7 @@ main() {
when(client.httpClient.post(url, headers: anyNamed('headers'))).thenAnswer(
(_) async =>
http.Response("", 201, headers: {"location": uploadLocation}));
when(client.httpClient.head(any)).thenAnswer(
when(client.httpClient.head(any, headers: anyNamed('headers'))).thenAnswer(
(_) async => http.Response("", 200, headers: {"upload-offset": "0"}));
int i = 0;
when(client.httpClient.patch(
Expand All @@ -213,7 +234,7 @@ main() {
when(client.httpClient.post(url, headers: anyNamed('headers'))).thenAnswer(
(_) async =>
http.Response("", 201, headers: {"location": uploadLocation}));
when(client.httpClient.head(any))
when(client.httpClient.head(any, headers: anyNamed('headers')))
.thenAnswer((_) async => http.Response("500 Server Error", 500));

expectLater(
Expand All @@ -229,7 +250,7 @@ main() {
when(client.httpClient.post(url, headers: anyNamed('headers'))).thenAnswer(
(_) async =>
http.Response("", 201, headers: {"location": uploadLocation}));
when(client.httpClient.head(any)).thenAnswer(
when(client.httpClient.head(any, headers: anyNamed('headers'))).thenAnswer(
(_) async => http.Response("", 204, headers: {"upload-offset": ""}));

expectLater(
Expand All @@ -245,7 +266,7 @@ main() {
when(client.httpClient.post(url, headers: anyNamed('headers'))).thenAnswer(
(_) async =>
http.Response("", 201, headers: {"location": uploadLocation}));
when(client.httpClient.head(any)).thenAnswer(
when(client.httpClient.head(any, headers: anyNamed('headers'))).thenAnswer(
(_) async => http.Response("", 204, headers: {"upload-offset": "0"}));
when(client.httpClient.patch(
any,
Expand All @@ -266,7 +287,7 @@ main() {
when(client.httpClient.post(url, headers: anyNamed('headers'))).thenAnswer(
(_) async =>
http.Response("", 201, headers: {"location": uploadLocation}));
when(client.httpClient.head(any)).thenAnswer(
when(client.httpClient.head(any, headers: anyNamed('headers'))).thenAnswer(
(_) async => http.Response("", 204, headers: {"upload-offset": "0"}));
when(client.httpClient.patch(
any,
Expand All @@ -287,7 +308,7 @@ main() {
when(client.httpClient.post(url, headers: anyNamed('headers'))).thenAnswer(
(_) async =>
http.Response("", 201, headers: {"location": uploadLocation}));
when(client.httpClient.head(any)).thenAnswer(
when(client.httpClient.head(any, headers: anyNamed('headers'))).thenAnswer(
(_) async => http.Response("", 204, headers: {"upload-offset": "0"}));
when(client.httpClient.patch(
any,
Expand Down

0 comments on commit 809364c

Please sign in to comment.