feat: run GCP upload speed test as part of shorebird doctor -v (#2540)

Co-authored-by: Felix Angelov <felix@shorebird.dev>
This commit is contained in:
Bryan Oltman
2024-10-16 11:54:47 -04:00
committed by GitHub
parent 3d57505335
commit 2a40ec035c
10 changed files with 332 additions and 4 deletions
@@ -501,6 +501,20 @@ class CodePushClient {
).organizations;
}
/// Returns a GCP upload link for measuring upload speed.
Future<Uri> getGCPSpeedTestUrl() async {
final response = await _httpClient.get(
Uri.parse('$_v1/diagnostics/gcp_upload'),
);
if (!response.isSuccess) {
throw _parseErrorResponse(response.statusCode, response.body);
}
final jsonBody = json.decode(response.body) as Map<String, dynamic>;
return Uri.parse(jsonBody['upload_url'] as String);
}
/// Closes the client.
void close() => _httpClient.close();
@@ -1978,6 +1978,50 @@ void main() {
});
});
group('getGCPSpeedTestUrl', () {
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.getGCPSpeedTestUrl(),
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('{"upload_url": "https://example.com"}'),
),
HttpStatus.ok,
),
);
});
test('returns upload_url as parsed Uri', () async {
final url = await codePushClient.getGCPSpeedTestUrl();
expect(url, equals(Uri.parse('https://example.com')));
});
});
});
group('close', () {
test('closes the underlying client', () {
codePushClient.close();