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

Side by Side Diff: test/loader_http_test.dart

Issue 2638403002: Make failing HTTP requests thrown when using dart:io. (Closed)
Patch Set: Fix typo in changelog Created 3 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 | « pubspec.yaml ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file 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 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 @TestOn("vm") 5 @TestOn("vm")
6 6
7 import "dart:convert"; 7 import "dart:convert";
8 import "dart:io"; 8 import "dart:io";
9 9
10 import "package:resource/resource.dart"; 10 import "package:resource/resource.dart";
11 import "package:test/test.dart"; 11 import "package:test/test.dart";
12 12
13 const content = "Rødgrød med fløde"; 13 const content = "Rødgrød med fløde";
14 14
15 main() { 15 main() {
16 var server; 16 var server;
17 var uri; 17 var uri;
18 setUp(() async { 18 setUp(() async {
19 server = await HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, 0); 19 server = await HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, 0);
20 int port = server.port; 20 int port = server.port;
21 uri = Uri.parse("http://localhost:$port/default.html"); 21 uri = Uri.parse("http://localhost:$port/default.html");
22 server.forEach((HttpRequest request) { 22 server.forEach((HttpRequest request) {
23 if (request.uri.path.endsWith(".not")) {
24 request.response
25 ..statusCode = HttpStatus.NOT_FOUND
26 ..close();
27 return;
28 }
23 var encodings = request.headers[HttpHeaders.ACCEPT_CHARSET]; 29 var encodings = request.headers[HttpHeaders.ACCEPT_CHARSET];
24 var encoding = parseAcceptCharset(encodings); 30 var encoding = parseAcceptCharset(encodings);
25 request.response.headers.contentType = 31 request.response.headers.contentType =
26 new ContentType("text", "plain", charset: encoding.name); 32 new ContentType("text", "plain", charset: encoding.name);
27 request.response..write(content) 33 request.response..write(content)
28 ..close(); 34 ..close();
29 }).catchError(print); 35 }).catchError(print);
30 }); 36 });
31 37
32 test("Default encoding", () async { 38 test("Default encoding", () async {
(...skipping 21 matching lines...) Expand all
54 }); 60 });
55 61
56 test("byte stream", () async { 62 test("byte stream", () async {
57 var loader = ResourceLoader.defaultLoader; 63 var loader = ResourceLoader.defaultLoader;
58 var bytes = loader.openRead(uri); // Sender uses Latin-1. 64 var bytes = loader.openRead(uri); // Sender uses Latin-1.
59 var buffer = []; 65 var buffer = [];
60 await bytes.forEach(buffer.addAll); 66 await bytes.forEach(buffer.addAll);
61 expect(buffer, content.codeUnits); 67 expect(buffer, content.codeUnits);
62 }); 68 });
63 69
70 test("not found - String", () async {
71 var loader = ResourceLoader.defaultLoader;
72 var badUri = uri.resolve("file.not"); // .not makes server fail.
73 expect(loader.readAsString(badUri), throws);
74 });
75
76 test("not found - bytes", () async {
77 var loader = ResourceLoader.defaultLoader;
78 var badUri = uri.resolve("file.not"); // .not makes server fail.
79 expect(loader.readAsBytes(badUri), throws);
80 });
81
82 test("not found - byte stream", () async {
83 var loader = ResourceLoader.defaultLoader;
84 var badUri = uri.resolve("file.not"); // .not makes server fail.
85 expect(loader.openRead(badUri).length, throws);
86 });
87
64 tearDown(() { 88 tearDown(() {
65 server.close(); 89 server.close();
66 server = null; 90 server = null;
67 }); 91 });
68 } 92 }
69 93
70 94
71 Encoding parseAcceptCharset(List<String> headers) { 95 Encoding parseAcceptCharset(List<String> headers) {
72 var encoding = LATIN1; 96 var encoding = LATIN1;
73 if (headers != null) { 97 if (headers != null) {
74 var weight = 0.0; 98 var weight = 0.0;
75 var pattern = new RegExp(r"([\w-]+)(;\s*q=[\d.]+)?"); 99 var pattern = new RegExp(r"([\w-]+)(;\s*q=[\d.]+)?");
76 for (var acceptCharset in headers) { 100 for (var acceptCharset in headers) {
77 for (var match in pattern.allMatches(acceptCharset)) { 101 for (var match in pattern.allMatches(acceptCharset)) {
78 var e = Encoding.getByName(match[1]); 102 var e = Encoding.getByName(match[1]);
79 if (e == null) continue; 103 if (e == null) continue;
80 var w = 1.0; 104 var w = 1.0;
81 if (match[2] != null) { 105 if (match[2] != null) {
82 w = double.parse(match[2]); 106 w = double.parse(match[2]);
83 } 107 }
84 if (w > weight) { 108 if (w > weight) {
85 weight = w; 109 weight = w;
86 encoding = e; 110 encoding = e;
87 } 111 }
88 } 112 }
89 } 113 }
90 } 114 }
91 return encoding; 115 return encoding;
92 } 116 }
OLDNEW
« no previous file with comments | « pubspec.yaml ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698