refactor(shorebird_code_push_api): stream engine bytes (#19)

This commit is contained in:
Felix Angelov
2023-03-06 13:45:04 -06:00
committed by GitHub
parent 196e4704ac
commit f7501ce1f3
2 changed files with 24 additions and 11 deletions
@@ -10,17 +10,18 @@ final _engineUrl = Uri.parse(
Future<Response> downloadEngineHandler(Request request, String revision) async {
final httpClient = await request.lookup<Future<http.Client>>();
final response = await httpClient.get(
_engineUrl,
headers: {
final req = http.Request('GET', _engineUrl);
req.headers.addAll(
{
'Content-Type': 'application/octet-stream',
'Connection': 'close'
'Connection': 'close',
},
);
final response = await httpClient.send(req);
if (response.statusCode != HttpStatus.ok) {
return Response(response.statusCode, body: response.body);
return Response(response.statusCode, body: response.stream);
}
return Response.ok(response.bodyBytes);
return Response.ok(response.stream);
}
@@ -9,13 +9,15 @@ import 'package:test/test.dart';
class _MockHttpClient extends Mock implements http.Client {}
class _FakeBaseRequest extends Fake implements http.BaseRequest {}
void main() {
group('downloadEngineHandler', () {
final uri = Uri.parse('http://localhost/');
late http.Client httpClient;
setUpAll(() {
registerFallbackValue(Uri());
registerFallbackValue(_FakeBaseRequest());
});
setUp(() {
@@ -24,8 +26,13 @@ void main() {
test('returns error on failure', () async {
when(
() => httpClient.get(any(), headers: any(named: 'headers')),
).thenAnswer((_) async => http.Response('oops', HttpStatus.unauthorized));
() => httpClient.send(any()),
).thenAnswer((_) async {
return http.StreamedResponse(
const Stream.empty(),
HttpStatus.unauthorized,
);
});
final request = Request('GET', uri).provide(() async => httpClient);
final response = await downloadEngineHandler(request, 'revision');
@@ -34,8 +41,13 @@ void main() {
test('returns bytes on success', () async {
when(
() => httpClient.get(any(), headers: any(named: 'headers')),
).thenAnswer((_) async => http.Response('OK', HttpStatus.ok));
() => httpClient.send(any()),
).thenAnswer((_) async {
return http.StreamedResponse(
const Stream.empty(),
HttpStatus.ok,
);
});
final request = Request('GET', uri).provide(() async => httpClient);
final response = await downloadEngineHandler(request, 'revision');