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

Side by Side Diff: tests/corelib/http_resource_test.dart

Issue 1276263002: Add resource tests. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Created 5 years, 4 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
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:io";
6
7 const sampleText = "Sample text file";
8
9 main() async {
10 var server = await startServer();
11 var uriText = "http://localhost:${server.port}/sample.txt";
12 var resource = new Resource(uriText);
13
14 if (resource.uri != Uri.parse(uriText)) {
15 throw "Incorrect URI: ${resource.uri}";
16 }
17
18 var text = await resource.readAsString();
19 if (text != sampleText) {
20 throw "Incorrect reading of text file: $text";
21 }
22
23 var bytes = await resource.readAsBytes();
24 if (!compareBytes(bytes, sampleText.codeUnits)) {
25 throw "Incorrect reading of bytes: $bytes";
26 }
27
28 var streamBytes = [];
29 await for (var byteSlice in resource.openRead()) {
30 streamBytes.addAll(byteSlice);
31 }
32 if (!compareBytes(streamBytes, sampleText.codeUnits)) {
33 throw "Incorrect reading of bytes: $bytes";
34 }
35 if (!compareBytes(streamBytes, bytes)) {
Søren Gjesse 2015/08/07 13:51:16 ditto.
36 throw "Inconsistent reading of bytes: $bytes / $streamBytes";
37 }
38
39 await server.close();
40 }
41
42 /// Checks that [bytes] and [expectedBytes] have the same contents.
43 bool compareBytes(bytes, expectedBytes) {
44 if (bytes.length != expectedBytes.length) return false;
45 for (int i = 0; i < expectedBytes.length; i++) {
46 if (bytes[i] != expectedBytes[i]) return false;
47 }
48 return true;
49 }
50
51 startServer() async {
52 var server = await HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, 0);
53 server.forEach((request) async {
54 await request.drain();
55 var response = request.response;
Ivan Posva 2015/08/10 05:39:56 Don't you want to verify that the request is made
Lasse Reichstein Nielsen 2015/08/10 10:40:09 Good idea.
56 response.write(sampleText);
57 response.close();
58 });
59 return server;
60 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698