Skip to content

Commit

Permalink
Fixing server returns partial url & double header.
Browse files Browse the repository at this point in the history
  • Loading branch information
jjmutumi committed Feb 4, 2021
1 parent 41374fa commit 2913110
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 4 deletions.
19 changes: 15 additions & 4 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,8 @@ class TusClient {
throw ProtocolException(
"missing upload Uri in response for creating upload");
}
if (urlStr.indexOf("//") == 0) {
urlStr = "${url.scheme}:$urlStr";
}

_uploadUrl = Uri.parse(urlStr);
_uploadUrl = _parseUrl(urlStr);
store?.set(_fingerprint, _uploadUrl);
}

Expand Down Expand Up @@ -256,4 +253,18 @@ class TusClient {
}
return int.tryParse(offset ?? "");
}

Uri _parseUrl(String urlStr) {
if (urlStr?.contains(",") ?? false) {
urlStr = urlStr.substring(0, urlStr.indexOf(","));
}
Uri uploadUrl = Uri.parse(urlStr);
if (uploadUrl.host.isEmpty) {
uploadUrl = uploadUrl.replace(host: url.host);
}
if (uploadUrl.scheme.isEmpty) {
uploadUrl = uploadUrl.replace(scheme: url.scheme);
}
return uploadUrl;
}
}
90 changes: 90 additions & 0 deletions test/src/client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,57 @@ main() {
contains('Tus-Resumable'));
});

test('client_test.TusClient.create().no.scheme', () async {
final client = MockTusClient(url, file);
when(client.httpClient.post(url, headers: anyNamed('headers'))).thenAnswer(
(_) async => http.Response("", 201, headers: {
"location":
"//example.com/tus/1ae64b4f-bd7a-410b-893d-3614a4bd68a6"
}));

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().no.host', () async {
final client = MockTusClient(url, file);
when(client.httpClient.post(url, headers: anyNamed('headers'))).thenAnswer(
(_) async => http.Response("", 201, headers: {
"location": "/tus/1ae64b4f-bd7a-410b-893d-3614a4bd68a6"
}));

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().double.header', () async {
final client = MockTusClient(url, file);
when(client.httpClient.post(url, headers: anyNamed('headers'))).thenAnswer(
(_) async => http.Response("", 201,
headers: {"location": "$uploadLocation,$uploadLocation"}));

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 {
final client = MockTusClient(url, file);
when(client.httpClient.post(url, headers: anyNamed('headers'))).thenAnswer(
Expand Down Expand Up @@ -177,6 +228,45 @@ main() {
contains('Tus-Resumable'));
});

test('client_test.TusClient.upload().double.header', () async {
final client = MockTusClient(url, file);
when(client.httpClient.post(url, headers: anyNamed('headers'))).thenAnswer(
(_) async =>
http.Response("", 201, headers: {"location": uploadLocation}));
when(client.httpClient.head(any, headers: anyNamed('headers'))).thenAnswer(
(_) async => http.Response("", 200, headers: {"upload-offset": "0,0"}));
when(client.httpClient.patch(
any,
headers: anyNamed('headers'),
body: anyNamed('body'),
)).thenAnswer((_) async =>
http.Response("", 204, headers: {"upload-offset": "100,100"}));

bool success = false;
double progress;
await client.upload(
onComplete: () => success = true, onProgress: (p) => progress = p);

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(
Expand Down

0 comments on commit 2913110

Please sign in to comment.