feat(code_push_client): add getGCPDownloadSpeedTestUrl (#2555)

This commit is contained in:
Felix Angelov
2024-10-18 12:43:44 -05:00
committed by GitHub
parent 207cf4ffbf
commit 14fa21c969
2 changed files with 58 additions and 0 deletions
@@ -515,6 +515,20 @@ class CodePushClient {
return Uri.parse(jsonBody['upload_url'] as String);
}
/// Returns a GCP download link for measuring download speed.
Future<Uri> getGCPDownloadSpeedTestUrl() async {
final response = await _httpClient.get(
Uri.parse('$_v1/diagnostics/gcp_download'),
);
if (!response.isSuccess) {
throw _parseErrorResponse(response.statusCode, response.body);
}
final jsonBody = json.decode(response.body) as Map<String, dynamic>;
return Uri.parse(jsonBody['download_url'] as String);
}
/// Closes the client.
void close() => _httpClient.close();
@@ -2022,6 +2022,50 @@ void main() {
});
});
group('getGCPDownloadSpeedTestUrl', () {
group('when request fails', () {
setUp(() {
when(() => httpClient.send(any())).thenAnswer(
(_) async => http.StreamedResponse(
const Stream.empty(),
HttpStatus.failedDependency,
),
);
});
test('throws exception', () async {
expect(
() async => codePushClient.getGCPDownloadSpeedTestUrl(),
throwsA(
isA<CodePushException>().having(
(e) => e.message,
'message',
CodePushClient.unknownErrorMessage,
),
),
);
});
});
group('when request succeeds', () {
setUp(() {
when(() => httpClient.send(any())).thenAnswer(
(_) async => http.StreamedResponse(
Stream.value(
utf8.encode('{"download_url": "https://example.com"}'),
),
HttpStatus.ok,
),
);
});
test('returns download_url as parsed Uri', () async {
final url = await codePushClient.getGCPDownloadSpeedTestUrl();
expect(url, equals(Uri.parse('https://example.com')));
});
});
});
group('close', () {
test('closes the underlying client', () {
codePushClient.close();