Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(480)

Unified Diff: test/loader_http_test.dart

Issue 1387163002: Add Resource package. (Closed) Base URL: https://github.com/dart-lang/resource.git@master
Patch Set: Address comments. Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « test/loader_file_test.dart ('k') | test/resource_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/loader_http_test.dart
diff --git a/test/loader_http_test.dart b/test/loader_http_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..c47fa270ebf39690dcfe0b2222d72562b0868e67
--- /dev/null
+++ b/test/loader_http_test.dart
@@ -0,0 +1,92 @@
+// 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";
+import "dart:convert";
+import "dart:io";
+
+import "package:resource/resource.dart";
+import "package:test/test.dart";
+
+const content = "Rødgrød med fløde";
+
+main() {
+ var server;
+ var uri;
+ setUp(() async {
+ server = await HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, 0);
+ int port = server.port;
+ uri = Uri.parse("http://localhost:$port/default.html");
+ server.forEach((HttpRequest request) {
+ var accept = request.headers[HttpHeaders.ACCEPT];
+ var encodings = request.headers[HttpHeaders.ACCEPT_CHARSET];
+ var encoding = parseAcceptCharset(encodings);
+ request.response.headers.contentType =
+ new ContentType("text", "plain", charset: encoding.name);
+ request.response..write(content)
+ ..close();
+ }).catchError(print);
+ });
+
+ test("Default encoding", () async {
+ var loader = ResourceLoader.defaultLoader;
+ String string = await loader.readAsString(uri);
+ expect(string, content);
+ });
+
+ test("Latin-1 encoding", () async {
+ var loader = ResourceLoader.defaultLoader;
+ String string = await loader.readAsString(uri, encoding: LATIN1);
+ expect(string, content);
+ });
+
+ test("UTF-8 encoding", () async {
+ var loader = ResourceLoader.defaultLoader;
+ String string = await loader.readAsString(uri, encoding: UTF8);
+ expect(string, content);
+ });
+
+ test("bytes", () async {
+ var loader = ResourceLoader.defaultLoader;
+ List<int> bytes = await loader.readAsBytes(uri); // Sender uses Latin-1.
+ expect(bytes, content.codeUnits);
+ });
+
+ test("byte stream", () async {
+ var loader = ResourceLoader.defaultLoader;
+ Stream<int> bytes = loader.openRead(uri); // Sender uses Latin-1.
+ var buffer = [];
+ await bytes.forEach(buffer.addAll);
+ expect(buffer, content.codeUnits);
+ });
+
+ tearDown(() {
+ server.close();
+ server = null;
+ });
+}
+
+
+Encoding parseAcceptCharset(List<String> headers) {
+ var encoding = LATIN1;
+ if (headers != null) {
+ var weight = 0.0;
+ var pattern = new RegExp(r"([\w-]+)(;\s*q=[\d.]+)?");
+ for (var acceptCharset in headers) {
+ for (var match in pattern.allMatches(acceptCharset)) {
+ var e = Encoding.getByName(match[1]);
+ if (e == null) continue;
+ var w = 1.0;
+ if (match[2] != null) {
+ w = double.parse(match[2]);
+ }
+ if (w > weight) {
+ weight = w;
+ encoding = e;
+ }
+ }
+ }
+ }
+ return encoding;
+}
« no previous file with comments | « test/loader_file_test.dart ('k') | test/resource_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698