OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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:io'; | 7 import 'dart:io'; |
8 import 'dart:json'; | 8 import 'dart:json'; |
9 import 'dart:uri'; | 9 import 'dart:uri'; |
10 | 10 |
(...skipping 11 matching lines...) Expand all Loading... |
22 Uri get dummyUrl => new Uri.fromString('http://dartlang.org/'); | 22 Uri get dummyUrl => new Uri.fromString('http://dartlang.org/'); |
23 | 23 |
24 /// Starts a new HTTP server. | 24 /// Starts a new HTTP server. |
25 void startServer() { | 25 void startServer() { |
26 _server = new HttpServer(); | 26 _server = new HttpServer(); |
27 | 27 |
28 _server.addRequestHandler((request) => request.path == '/error', | 28 _server.addRequestHandler((request) => request.path == '/error', |
29 (request, response) { | 29 (request, response) { |
30 response.statusCode = 400; | 30 response.statusCode = 400; |
31 response.contentLength = 0; | 31 response.contentLength = 0; |
32 closeResponse(request, response); | 32 response.outputStream.close(); |
33 }); | 33 }); |
34 | 34 |
35 _server.addRequestHandler((request) => request.path == '/loop', | 35 _server.addRequestHandler((request) => request.path == '/loop', |
36 (request, response) { | 36 (request, response) { |
37 var n = int.parse(new Uri.fromString(request.uri).query); | 37 var n = int.parse(new Uri.fromString(request.uri).query); |
38 response.statusCode = 302; | 38 response.statusCode = 302; |
39 response.headers.set('location', | 39 response.headers.set('location', |
40 serverUrl.resolve('/loop?${n + 1}').toString()); | 40 serverUrl.resolve('/loop?${n + 1}').toString()); |
41 response.contentLength = 0; | 41 response.contentLength = 0; |
42 closeResponse(request, response); | 42 response.outputStream.close(); |
43 }); | 43 }); |
44 | 44 |
45 _server.addRequestHandler((request) => request.path == '/redirect', | 45 _server.addRequestHandler((request) => request.path == '/redirect', |
46 (request, response) { | 46 (request, response) { |
47 response.statusCode = 302; | 47 response.statusCode = 302; |
48 response.headers.set('location', serverUrl.resolve('/').toString()); | 48 response.headers.set('location', serverUrl.resolve('/').toString()); |
49 response.contentLength = 0; | 49 response.contentLength = 0; |
50 closeResponse(request, response); | 50 response.outputStream.close(); |
51 }); | 51 }); |
52 | 52 |
53 _server.defaultRequestHandler = (request, response) { | 53 _server.defaultRequestHandler = (request, response) { |
54 consumeInputStream(request.inputStream).then((requestBodyBytes) { | 54 consumeInputStream(request.inputStream).then((requestBodyBytes) { |
55 response.statusCode = 200; | 55 response.statusCode = 200; |
56 response.headers.contentType = new ContentType("application", "json"); | 56 response.headers.contentType = new ContentType("application", "json"); |
57 | 57 |
58 var requestBody; | 58 var requestBody; |
59 if (requestBodyBytes.isEmpty) { | 59 if (requestBodyBytes.isEmpty) { |
60 requestBody = null; | 60 requestBody = null; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 | 97 |
98 _server.listen("127.0.0.1", 0); | 98 _server.listen("127.0.0.1", 0); |
99 } | 99 } |
100 | 100 |
101 /// Stops the current HTTP server. | 101 /// Stops the current HTTP server. |
102 void stopServer() { | 102 void stopServer() { |
103 _server.close(); | 103 _server.close(); |
104 _server = null; | 104 _server = null; |
105 } | 105 } |
106 | 106 |
107 /// Closes [response] while ignoring the body of [request]. | |
108 /// | |
109 /// Due to issue 6984, it's necessary to drain the request body before closing | |
110 /// the response. | |
111 void closeResponse(HttpRequest request, HttpResponse response) { | |
112 request.inputStream.onData = request.inputStream.read; | |
113 request.inputStream.onClosed = response.outputStream.close; | |
114 } | |
115 | |
116 /// A matcher that matches JSON that parses to a value that matches the inner | 107 /// A matcher that matches JSON that parses to a value that matches the inner |
117 /// matcher. | 108 /// matcher. |
118 Matcher parse(matcher) => new _Parse(matcher); | 109 Matcher parse(matcher) => new _Parse(matcher); |
119 | 110 |
120 class _Parse extends BaseMatcher { | 111 class _Parse extends BaseMatcher { |
121 final Matcher _matcher; | 112 final Matcher _matcher; |
122 | 113 |
123 _Parse(this._matcher); | 114 _Parse(this._matcher); |
124 | 115 |
125 bool matches(item, MatchState matchState) { | 116 bool matches(item, MatchState matchState) { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 const Matcher throwsRedirectLimitExceededException = | 165 const Matcher throwsRedirectLimitExceededException = |
175 const Throws(isRedirectLimitExceededException); | 166 const Throws(isRedirectLimitExceededException); |
176 | 167 |
177 class _RedirectLimitExceededException extends TypeMatcher { | 168 class _RedirectLimitExceededException extends TypeMatcher { |
178 const _RedirectLimitExceededException() : | 169 const _RedirectLimitExceededException() : |
179 super("RedirectLimitExceededException"); | 170 super("RedirectLimitExceededException"); |
180 | 171 |
181 bool matches(item, MatchState matchState) => | 172 bool matches(item, MatchState matchState) => |
182 item is RedirectLimitExceededException; | 173 item is RedirectLimitExceededException; |
183 } | 174 } |
OLD | NEW |