Index: test/resource_test.dart |
diff --git a/test/resource_test.dart b/test/resource_test.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..d057c11f4138191331344b4b495760538dc17021 |
--- /dev/null |
+++ b/test/resource_test.dart |
@@ -0,0 +1,67 @@ |
+// Copyright (c) 2015, 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:async" show Future, Stream; |
+import "package:resource/resource.dart"; |
+import "package:test/test.dart"; |
+import "dart:convert" show Encoding, ASCII; |
+ |
+main() { |
+ pkguri(path) => new Uri(scheme: "package", path: path); |
+ |
+ testLoading(String name, PackageResolver resolver) { |
+ group("$name resolver", () { |
+ testLoad(Uri uri) async { |
+ LogLoader loader = new LogLoader(); |
+ var resource = resolver.resource(uri, loader: loader); |
+ var res = await resource.openRead().toList(); |
+ expect(res, [[0, 0, 0]]); |
+ res = await resource.readAsBytes(); |
+ expect(res, [0, 0, 0]); |
+ res = await resource.readAsString(encoding: ASCII); |
+ expect(res, "\x00\x00\x00"); |
+ var resolved = resolver.resolve(uri); |
+ |
+ expect(loader.requests, [["Stream", resolved], |
+ ["Bytes", resolved], |
+ ["String", resolved, ASCII]]); |
+ } |
+ |
+ test("load package: URIs", () async { |
+ await testLoad(pkguri("foo/bar/baz")); |
+ await testLoad(pkguri("bar/foo/baz")); |
+ }); |
+ test("load non-pkgUri", () async { |
+ await testLoad(Uri.parse("file://localhost/something?x#y")); |
+ await testLoad(Uri.parse("http://auth/something?x#y")); |
+ await testLoad(Uri.parse("https://auth/something?x#y")); |
+ await testLoad(Uri.parse("unknown:/something")); |
+ }); |
+ }); |
+ } |
+ testLoading("root", |
+ new PackageResolver.fromRoot(Uri.parse("file:///path/root/"))); |
+ var fooRoot = Uri.parse("file:///files/foo/"); |
+ var barRoot = Uri.parse("http://example.com/files/bar/"); |
+ testLoading("map", |
+ new PackageResolver.fromMap({"foo": fooRoot, "bar": barRoot })); |
+} |
+ |
+ |
+class LogLoader implements ResourceLoader { |
+ final List requests = []; |
+ void reset() { requests.clear(); } |
+ Stream<List<int>> openRead(Uri uri) async* { |
+ requests.add(["Stream", uri]); |
+ yield [0x00, 0x00, 0x00]; |
+ } |
+ Future<List<int>> readAsBytes(Uri uri) async { |
+ requests.add(["Bytes", uri]); |
+ return [0x00, 0x00, 0x00]; |
+ } |
+ Future<String> readAsString(Uri uri, {Encoding encoding}) async { |
+ requests.add(["String", uri, encoding]); |
+ return "\x00\x00\x00"; |
+ } |
+} |