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 response.outputStream.close(); | 32 closeResponse(request, response); |
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 response.outputStream.close(); | 42 closeResponse(request, response); |
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 response.outputStream.close(); | 50 closeResponse(request, response); |
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; | |
Bob Nystrom
2012/11/28 22:41:44
Will it cause any problems that we no longer synch
nweiz
2012/11/28 22:42:41
God, I hope not.
In seriousness, it was all being
| |
114 } | |
115 | |
107 /// A matcher that matches JSON that parses to a value that matches the inner | 116 /// A matcher that matches JSON that parses to a value that matches the inner |
108 /// matcher. | 117 /// matcher. |
109 Matcher parse(matcher) => new _Parse(matcher); | 118 Matcher parse(matcher) => new _Parse(matcher); |
110 | 119 |
111 class _Parse extends BaseMatcher { | 120 class _Parse extends BaseMatcher { |
112 final Matcher _matcher; | 121 final Matcher _matcher; |
113 | 122 |
114 _Parse(this._matcher); | 123 _Parse(this._matcher); |
115 | 124 |
116 bool matches(item, MatchState matchState) { | 125 bool matches(item, MatchState matchState) { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
165 const Matcher throwsRedirectLimitExceededException = | 174 const Matcher throwsRedirectLimitExceededException = |
166 const Throws(isRedirectLimitExceededException); | 175 const Throws(isRedirectLimitExceededException); |
167 | 176 |
168 class _RedirectLimitExceededException extends TypeMatcher { | 177 class _RedirectLimitExceededException extends TypeMatcher { |
169 const _RedirectLimitExceededException() : | 178 const _RedirectLimitExceededException() : |
170 super("RedirectLimitExceededException"); | 179 super("RedirectLimitExceededException"); |
171 | 180 |
172 bool matches(item, MatchState matchState) => | 181 bool matches(item, MatchState matchState) => |
173 item is RedirectLimitExceededException; | 182 item is RedirectLimitExceededException; |
174 } | 183 } |
OLD | NEW |