From 14fa21c969d737fb43554062e074a72dad1925ef Mon Sep 17 00:00:00 2001 From: Felix Angelov Date: Fri, 18 Oct 2024 12:43:44 -0500 Subject: [PATCH] feat(code_push_client): add `getGCPDownloadSpeedTestUrl` (#2555) --- .../lib/src/code_push_client.dart | 14 ++++++ .../test/src/code_push_client_test.dart | 44 +++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/packages/shorebird_code_push_client/lib/src/code_push_client.dart b/packages/shorebird_code_push_client/lib/src/code_push_client.dart index 272f2419..520559a4 100644 --- a/packages/shorebird_code_push_client/lib/src/code_push_client.dart +++ b/packages/shorebird_code_push_client/lib/src/code_push_client.dart @@ -515,6 +515,20 @@ class CodePushClient { return Uri.parse(jsonBody['upload_url'] as String); } + /// Returns a GCP download link for measuring download speed. + Future 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; + return Uri.parse(jsonBody['download_url'] as String); + } + /// Closes the client. void close() => _httpClient.close(); diff --git a/packages/shorebird_code_push_client/test/src/code_push_client_test.dart b/packages/shorebird_code_push_client/test/src/code_push_client_test.dart index a385c8f8..9f6ad6d1 100644 --- a/packages/shorebird_code_push_client/test/src/code_push_client_test.dart +++ b/packages/shorebird_code_push_client/test/src/code_push_client_test.dart @@ -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().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();