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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « test/loader_file_test.dart ('k') | test/resource_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 import "dart:async";
6 import "dart:convert";
7 import "dart:io";
8
9 import "package:resource/resource.dart";
10 import "package:test/test.dart";
11
12 const content = "Rødgrød med fløde";
13
14 main() {
15 var server;
16 var uri;
17 setUp(() async {
18 server = await HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, 0);
19 int port = server.port;
20 uri = Uri.parse("http://localhost:$port/default.html");
21 server.forEach((HttpRequest request) {
22 var accept = request.headers[HttpHeaders.ACCEPT];
23 var encodings = request.headers[HttpHeaders.ACCEPT_CHARSET];
24 var encoding = parseAcceptCharset(encodings);
25 request.response.headers.contentType =
26 new ContentType("text", "plain", charset: encoding.name);
27 request.response..write(content)
28 ..close();
29 }).catchError(print);
30 });
31
32 test("Default encoding", () async {
33 var loader = ResourceLoader.defaultLoader;
34 String string = await loader.readAsString(uri);
35 expect(string, content);
36 });
37
38 test("Latin-1 encoding", () async {
39 var loader = ResourceLoader.defaultLoader;
40 String string = await loader.readAsString(uri, encoding: LATIN1);
41 expect(string, content);
42 });
43
44 test("UTF-8 encoding", () async {
45 var loader = ResourceLoader.defaultLoader;
46 String string = await loader.readAsString(uri, encoding: UTF8);
47 expect(string, content);
48 });
49
50 test("bytes", () async {
51 var loader = ResourceLoader.defaultLoader;
52 List<int> bytes = await loader.readAsBytes(uri); // Sender uses Latin-1.
53 expect(bytes, content.codeUnits);
54 });
55
56 test("byte stream", () async {
57 var loader = ResourceLoader.defaultLoader;
58 Stream<int> bytes = loader.openRead(uri); // Sender uses Latin-1.
59 var buffer = [];
60 await bytes.forEach(buffer.addAll);
61 expect(buffer, content.codeUnits);
62 });
63
64 tearDown(() {
65 server.close();
66 server = null;
67 });
68 }
69
70
71 Encoding parseAcceptCharset(List<String> headers) {
72 var encoding = LATIN1;
73 if (headers != null) {
74 var weight = 0.0;
75 var pattern = new RegExp(r"([\w-]+)(;\s*q=[\d.]+)?");
76 for (var acceptCharset in headers) {
77 for (var match in pattern.allMatches(acceptCharset)) {
78 var e = Encoding.getByName(match[1]);
79 if (e == null) continue;
80 var w = 1.0;
81 if (match[2] != null) {
82 w = double.parse(match[2]);
83 }
84 if (w > weight) {
85 weight = w;
86 encoding = e;
87 }
88 }
89 }
90 }
91 return encoding;
92 }
OLDNEW
« 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