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();