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

Side by Side Diff: pkg/http_server/test/utils.dart

Issue 124833003: pkg/http_server: return future for VirtualDirectory serveRequest (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: tweaks Created 6 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 library utils; 5 library utils;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 import 'dart:convert'; 8 import 'dart:convert';
9 import 'dart:io'; 9 import 'dart:io';
10 import "package:unittest/unittest.dart"; 10 import "package:unittest/unittest.dart";
11 11
12 import 'package:http_server/http_server.dart'; 12 import 'package:http_server/http_server.dart';
13 13
14 import 'http_mock.dart';
15
16 final _testLogicRequestExpando = new Expando('foo');
17
14 void testVirtualDir(String name, Future func(Directory dir)) { 18 void testVirtualDir(String name, Future func(Directory dir)) {
15 test(name, () { 19 test(name, () {
20 _testLogicRequestExpando[currentTestCase] = false;
21
16 var dir = Directory.systemTemp.createTempSync('http_server_virtual_'); 22 var dir = Directory.systemTemp.createTempSync('http_server_virtual_');
17 23
18 return func(dir) 24 return func(dir)
25 .whenComplete(() {
26 return dir.delete(recursive: true);
27 });
28 });
29
30 test('$name - logical', () {
31 _testLogicRequestExpando[currentTestCase] = true;
Anders Johnsen 2014/01/09 06:12:23 This stuff really needs some documentation. What i
kevmoo 2014/01/10 18:12:31 Done.
32
33 var dir = Directory.systemTemp.createTempSync('http_server_virtual_');
34
35 return func(dir)
19 .whenComplete(() { 36 .whenComplete(() {
20 return dir.delete(recursive: true); 37 return dir.delete(recursive: true);
21 }); 38 });
22 }); 39 });
23 } 40 }
24 41
42 Future<MockHttpResponse> _serveRequest(VirtualDirectory virDir, MockHttpRequest request) =>
43 virDir.serveRequest(request).then((value) {
44 expect(value, isNull);
45 expect(request.response.mockDone, isTrue);
46 return request.response;
47 });
48
25 Future<int> getStatusCodeForVirtDir(VirtualDirectory virtualDir, 49 Future<int> getStatusCodeForVirtDir(VirtualDirectory virtualDir,
26 String path, 50 String path,
27 {String host, 51 {String host,
28 bool secure: false, 52 bool secure: false,
29 DateTime ifModifiedSince, 53 DateTime ifModifiedSince,
30 bool rawPath: false}) { 54 bool rawPath: false}) {
31 return _withServer((server) { 55 if(_testLogicRequestExpando[currentTestCase] == true) {
56 var uri = _getUri(0, path, secure: secure, rawPath: rawPath);
32 57
33 virtualDir.serve(server); 58 var request = new MockHttpRequest(uri, ifModifiedSince: ifModifiedSince);
34 return getStatusCode(server.port, path, host: host, secure: secure, 59
60 return _serveRequest(virtualDir, request)
61 .then((response) {
62 return response.statusCode;
63 });
64 };
65
66 assert(_testLogicRequestExpando[currentTestCase] == false);
67
68 return _withServer(virtualDir, (port) {
69 return getStatusCode(port, path, host: host, secure: secure,
35 ifModifiedSince: ifModifiedSince, rawPath: rawPath); 70 ifModifiedSince: ifModifiedSince, rawPath: rawPath);
36 }); 71 });
37 } 72 }
38 73
39 Future<int> getStatusCode(int port, 74 Future<int> getStatusCode(int port,
40 String path, 75 String path,
41 {String host, 76 {String host,
42 bool secure: false, 77 bool secure: false,
43 DateTime ifModifiedSince, 78 DateTime ifModifiedSince,
44 bool rawPath: false}) { 79 bool rawPath: false}) {
45 Uri uri; 80
46 if (rawPath) { 81 var uri = _getUri(port, path, secure: secure, rawPath: rawPath);
47 uri = new Uri(scheme: secure ? 'https' : 'http',
48 host: 'localhost',
49 port: port,
50 path: path);
51 } else {
52 uri = (secure ?
53 new Uri.https('localhost:$port', path) :
54 new Uri.http('localhost:$port', path));
55 }
56 82
57 return new HttpClient().getUrl(uri) 83 return new HttpClient().getUrl(uri)
58 .then((request) { 84 .then((request) {
59 if (host != null) request.headers.host = host; 85 if (host != null) request.headers.host = host;
60 if (ifModifiedSince != null) { 86 if (ifModifiedSince != null) {
61 request.headers.ifModifiedSince = ifModifiedSince; 87 request.headers.ifModifiedSince = ifModifiedSince;
62 } 88 }
63 return request.close(); 89 return request.close();
64 }) 90 })
65 .then((response) => response.drain().then( 91 .then((response) => response.drain().then(
66 (_) => response.statusCode)); 92 (_) => response.statusCode));
67 } 93 }
68 94
69 Future<HttpHeaders> getHeaders(VirtualDirectory virDir, String path) { 95 Future<HttpHeaders> getHeaders(VirtualDirectory virDir, String path) {
70 return _withServer((server) { 96 if(_testLogicRequestExpando[currentTestCase] == true) {
Anders Johnsen 2014/01/09 06:12:23 remove '== true'
kevmoo 2014/01/10 05:02:00 Done.
71 virDir.serve(server); 97 var uri = _getUri(0, path);
72 98
73 return new HttpClient() 99 var request = new MockHttpRequest(uri);
74 .get('localhost', server.port, path) 100
75 .then((request) => request.close()) 101 return _serveRequest(virDir, request)
76 .then((response) => response.drain().then((_) => response.headers)); 102 .then((response) {
103 return response.headers;
104 });
105 };
106
107 assert(_testLogicRequestExpando[currentTestCase] == false);
108
109 return _withServer(virDir, (port) {
110 return _getHeaders(port, path);
77 }); 111 });
78 } 112 }
79 113
80 Future<String> getAsString(VirtualDirectory virtualDir, String path) { 114 Future<String> getAsString(VirtualDirectory virtualDir, String path) {
81 return _withServer((server) { 115 if(_testLogicRequestExpando[currentTestCase] == true) {
Anders Johnsen 2014/01/09 06:12:23 remove '== true'.
kevmoo 2014/01/10 05:02:00 Done.
82 virtualDir.serve(server); 116 var uri = _getUri(0, path);
83 117
84 return new HttpClient() 118 var request = new MockHttpRequest(uri);
85 .get('localhost', server.port, path) 119
86 .then((request) => request.close()) 120 return _serveRequest(virtualDir, request)
87 .then((response) => UTF8.decodeStream(response)); 121 .then((response) {
122 return response.mockContent;
123 });
124 };
125
126 assert(_testLogicRequestExpando[currentTestCase] == false);
127
128 return _withServer(virtualDir, (int port) {
129 return _getAsString(port, path);
88 }); 130 });
89 } 131 }
90 132
91 Future _withServer(Future func(HttpServer server)) { 133 Future _withServer(VirtualDirectory virDir, Future func(int port)) {
92 HttpServer server; 134 HttpServer server;
93 return HttpServer.bind('localhost', 0) 135 return HttpServer.bind('localhost', 0)
94 .then((value) { 136 .then((value) {
95 server = value; 137 server = value;
96 return func(server); 138 virDir.serve(server);
139 return func(server.port);
97 }) 140 })
98 .whenComplete(() => server.close()); 141 .whenComplete(() => server.close());
99 } 142 }
100 143
144 Future<HttpHeaders> _getHeaders(int port, String path) =>
145 new HttpClient()
146 .get('localhost', port, path)
147 .then((request) => request.close())
148 .then((response) => response.drain().then(
149 (_) => response.headers));
150
151 Future<String> _getAsString(int port, String path) =>
152 new HttpClient()
153 .get('localhost', port, path)
154 .then((request) => request.close())
155 .then((response) => UTF8.decodeStream(response));
156
157 Uri _getUri(int port,
158 String path,
159 {bool secure: false,
160 bool rawPath: false}) {
161 if (rawPath) {
162 return new Uri(scheme: secure ? 'https' : 'http',
163 host: 'localhost',
164 port: port,
165 path: path);
166 } else {
167 return (secure ?
168 new Uri.https('localhost:$port', path) :
169 new Uri.http('localhost:$port', path));
170 }
171 }
101 172
102 const CERTIFICATE = "localhost_cert"; 173 const CERTIFICATE = "localhost_cert";
103 174
104 175
105 void setupSecure() { 176 void setupSecure() {
106 String certificateDatabase = Platform.script.resolve('pkcert').toFilePath(); 177 String certificateDatabase = Platform.script.resolve('pkcert').toFilePath();
107 SecureSocket.initialize(database: certificateDatabase, 178 SecureSocket.initialize(database: certificateDatabase,
108 password: 'dartdart'); 179 password: 'dartdart');
109 } 180 }
OLDNEW
« pkg/http_server/test/http_mock.dart ('K') | « pkg/http_server/test/http_mock.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698