blob: 2bb3b5563c76955d00c991eb3016cad5c38d4da1 [file] [log] [blame]
// Copyright 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:io' as io;
import 'package:deps/fetch.dart';
import 'package:deps/package.dart';
import 'package:file/memory.dart';
import 'package:file/src/interface/file_system_entity.dart';
import 'package:file/src/interface/directory.dart';
import 'package:test/test.dart';
class TestHttpPackage extends Package with HttpDownloader {
TestHttpPackage(Uri source) : super('test', source);
@override
Future<Directory> build(List<FileSystemEntity> inputs, Directory workingDir) {
throw UnimplementedError();
}
@override
Future<bool> check(Directory artifacts) {
throw UnimplementedError();
}
@override
Future<void> upload(Directory artifacts, bool provenance) {
throw UnimplementedError();
}
}
void main() {
late io.HttpServer server;
late String authority;
late Uri existingFile;
setUp(() async {
server = await io.HttpServer.bind('localhost', 0);
server.listen((request) {
if (request.uri.path == existingFile.path) {
request.response.write('Just testing');
} else {
request.response.statusCode = 404;
}
request.response.close();
});
authority = '${server.address.host}:${server.port}';
existingFile = Uri.http(authority, '/path/to/a_file.zip');
});
tearDown(() async {
await server.close(force: true);
});
test('fetch http package', () async {
final fileSystem = MemoryFileSystem.test();
final workingDir = await fileSystem.directory('/temp').create();
final target = workingDir.childFile('a_file.zip');
final package = TestHttpPackage(existingFile);
final result = await package.fetch(workingDir);
expect(result.map((e) => e.path), [target.path]);
expect(await target.readAsString(), 'Just testing');
});
test('fetch http package failed', () async {
final fileSystem = MemoryFileSystem.test();
final workingDir = await fileSystem.directory('/temp').create();
final package =
TestHttpPackage(Uri.http(authority, '/path/to/missing_file.zip'));
final future = package.fetch(workingDir);
expect(future, throwsA(isA<io.HttpException>()));
try {
await future;
} catch (e) {
expect(workingDir.listSync(), isEmpty);
}
});
}