OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 shelf_io_test; | 5 library shelf_io_test; |
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 | 10 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 | 87 |
88 expect(request.url.path, '/foo/bar'); | 88 expect(request.url.path, '/foo/bar'); |
89 expect(request.url.pathSegments, ['foo', 'bar']); | 89 expect(request.url.pathSegments, ['foo', 'bar']); |
90 expect(request.protocolVersion, '1.1'); | 90 expect(request.protocolVersion, '1.1'); |
91 expect(request.url.query, 'qs=value'); | 91 expect(request.url.query, 'qs=value'); |
92 expect(request.scriptName, ''); | 92 expect(request.scriptName, ''); |
93 | 93 |
94 return syncHandler(request); | 94 return syncHandler(request); |
95 }); | 95 }); |
96 | 96 |
97 return schedule(() => http.get('http://localhost:$_serverPort$path')) | 97 return schedule(() => http.get('http://localhost:$_serverPort$path')).then( |
98 .then((response) { | 98 (response) { |
99 expect(response.statusCode, HttpStatus.OK); | 99 expect(response.statusCode, HttpStatus.OK); |
100 expect(response.body, 'Hello from /foo/bar'); | 100 expect(response.body, 'Hello from /foo/bar'); |
101 }); | 101 }); |
102 }); | 102 }); |
103 | 103 |
104 test('custom response headers are received by the client', () { | 104 test('custom response headers are received by the client', () { |
105 _scheduleServer((request) { | 105 _scheduleServer((request) { |
106 return new Response.ok('Hello from /', headers: { | 106 return new Response.ok('Hello from /', |
107 'test-header': 'test-value', | 107 headers: {'test-header': 'test-value', 'test-list': 'a, b, c'}); |
108 'test-list': 'a, b, c' | |
109 }); | |
110 }); | 108 }); |
111 | 109 |
112 return _scheduleGet().then((response) { | 110 return _scheduleGet().then((response) { |
113 expect(response.statusCode, HttpStatus.OK); | 111 expect(response.statusCode, HttpStatus.OK); |
114 expect(response.headers['test-header'], 'test-value'); | 112 expect(response.headers['test-header'], 'test-value'); |
115 expect(response.body, 'Hello from /'); | 113 expect(response.body, 'Hello from /'); |
116 }); | 114 }); |
117 }); | 115 }); |
118 | 116 |
119 test('custom status code is received by the client', () { | 117 test('custom status code is received by the client', () { |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 }); | 184 }); |
187 }); | 185 }); |
188 | 186 |
189 test('supports request hijacking', () { | 187 test('supports request hijacking', () { |
190 _scheduleServer((request) { | 188 _scheduleServer((request) { |
191 expect(request.method, 'POST'); | 189 expect(request.method, 'POST'); |
192 | 190 |
193 request.hijack(expectAsync((stream, sink) { | 191 request.hijack(expectAsync((stream, sink) { |
194 expect(stream.first, completion(equals("Hello".codeUnits))); | 192 expect(stream.first, completion(equals("Hello".codeUnits))); |
195 | 193 |
196 sink.add(( | 194 sink.add(("HTTP/1.1 404 Not Found\r\n" |
197 "HTTP/1.1 404 Not Found\r\n" | |
198 "Date: Mon, 23 May 2005 22:38:34 GMT\r\n" | 195 "Date: Mon, 23 May 2005 22:38:34 GMT\r\n" |
199 "Content-Length: 13\r\n" | 196 "Content-Length: 13\r\n" |
200 "\r\n" | 197 "\r\n" |
201 "Hello, world!").codeUnits); | 198 "Hello, world!").codeUnits); |
202 sink.close(); | 199 sink.close(); |
203 })); | 200 })); |
204 }); | 201 }); |
205 | 202 |
206 return _schedulePost(body: "Hello").then((response) { | 203 return _schedulePost(body: "Hello").then((response) { |
207 expect(response.statusCode, HttpStatus.NOT_FOUND); | 204 expect(response.statusCode, HttpStatus.NOT_FOUND); |
208 expect(response.headers["date"], "Mon, 23 May 2005 22:38:34 GMT"); | 205 expect(response.headers["date"], "Mon, 23 May 2005 22:38:34 GMT"); |
209 expect(response.stream.bytesToString(), | 206 expect( |
210 completion(equals("Hello, world!"))); | 207 response.stream.bytesToString(), completion(equals("Hello, world!"))); |
211 }); | 208 }); |
212 }); | 209 }); |
213 | 210 |
214 test('reports an error if a HijackException is thrown without hijacking', () { | 211 test('reports an error if a HijackException is thrown without hijacking', () { |
215 _scheduleServer((request) => throw const HijackException()); | 212 _scheduleServer((request) => throw const HijackException()); |
216 | 213 |
217 return _scheduleGet().then((response) { | 214 return _scheduleGet().then((response) { |
218 expect(response.statusCode, HttpStatus.INTERNAL_SERVER_ERROR); | 215 expect(response.statusCode, HttpStatus.INTERNAL_SERVER_ERROR); |
219 }); | 216 }); |
220 }); | 217 }); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 var responseDate = parser.parseHttpDate(response.headers['date']); | 291 var responseDate = parser.parseHttpDate(response.headers['date']); |
295 | 292 |
296 expect(responseDate.isAfter(beforeRequest), isTrue); | 293 expect(responseDate.isAfter(beforeRequest), isTrue); |
297 expect(responseDate.isBefore(new DateTime.now()), isTrue); | 294 expect(responseDate.isBefore(new DateTime.now()), isTrue); |
298 }); | 295 }); |
299 }); | 296 }); |
300 | 297 |
301 test('defers to header in response', () { | 298 test('defers to header in response', () { |
302 var date = new DateTime.utc(1981, 6, 5); | 299 var date = new DateTime.utc(1981, 6, 5); |
303 _scheduleServer((request) { | 300 _scheduleServer((request) { |
304 return new Response.ok('test', headers: { | 301 return new Response.ok('test', |
305 HttpHeaders.DATE: parser.formatHttpDate(date) | 302 headers: {HttpHeaders.DATE: parser.formatHttpDate(date)}); |
306 }); | |
307 }); | 303 }); |
308 | 304 |
309 return _scheduleGet().then((response) { | 305 return _scheduleGet().then((response) { |
310 expect(response.headers, contains('date')); | 306 expect(response.headers, contains('date')); |
311 var responseDate = parser.parseHttpDate(response.headers['date']); | 307 var responseDate = parser.parseHttpDate(response.headers['date']); |
312 expect(responseDate, date); | 308 expect(responseDate, date); |
313 }); | 309 }); |
314 }); | 310 }); |
315 }); | 311 }); |
316 | 312 |
(...skipping 29 matching lines...) Expand all Loading... |
346 return server.close(force: true); | 342 return server.close(force: true); |
347 }); | 343 }); |
348 | 344 |
349 _serverPort = server.port; | 345 _serverPort = server.port; |
350 })); | 346 })); |
351 } | 347 } |
352 | 348 |
353 Future<http.Response> _scheduleGet({Map<String, String> headers}) { | 349 Future<http.Response> _scheduleGet({Map<String, String> headers}) { |
354 if (headers == null) headers = {}; | 350 if (headers == null) headers = {}; |
355 | 351 |
356 return schedule(() => | 352 return schedule( |
357 http.get('http://localhost:$_serverPort/', headers: headers)); | 353 () => http.get('http://localhost:$_serverPort/', headers: headers)); |
358 } | 354 } |
359 | 355 |
360 Future<http.StreamedResponse> _schedulePost({Map<String, String> headers, | 356 Future<http.StreamedResponse> _schedulePost( |
361 String body}) { | 357 {Map<String, String> headers, String body}) { |
362 | |
363 return schedule(() { | 358 return schedule(() { |
364 | 359 var request = |
365 var request = new http.Request('POST', | 360 new http.Request('POST', Uri.parse('http://localhost:$_serverPort/')); |
366 Uri.parse('http://localhost:$_serverPort/')); | |
367 | 361 |
368 if (headers != null) request.headers.addAll(headers); | 362 if (headers != null) request.headers.addAll(headers); |
369 if (body != null) request.body = body; | 363 if (body != null) request.body = body; |
370 | 364 |
371 return request.send(); | 365 return request.send(); |
372 }); | 366 }); |
373 } | 367 } |
OLD | NEW |