OLD | NEW |
1 // Copyright (c) 2012, 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 test_utils; | 5 library test_utils; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 import 'dart:json' as json; | 9 import 'dart:json' as json; |
10 import 'dart:uri'; | 10 import 'dart:uri'; |
11 | 11 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 | 50 |
51 if (path == '/redirect') { | 51 if (path == '/redirect') { |
52 response.statusCode = 302; | 52 response.statusCode = 302; |
53 response.headers.set('location', serverUrl.resolve('/').toString()); | 53 response.headers.set('location', serverUrl.resolve('/').toString()); |
54 response.contentLength = 0; | 54 response.contentLength = 0; |
55 response.close(); | 55 response.close(); |
56 return; | 56 return; |
57 } | 57 } |
58 | 58 |
59 new ByteStream(request).toBytes().then((requestBodyBytes) { | 59 new ByteStream(request).toBytes().then((requestBodyBytes) { |
60 response.statusCode = 200; | 60 var outputEncoding; |
61 response.headers.contentType = new ContentType("application", "json"); | 61 var encodingName = request.queryParameters['response-encoding']; |
62 response.headers.set('single', 'value'); | 62 if (encodingName != null) { |
| 63 outputEncoding = requiredEncodingForCharset(encodingName); |
| 64 } else { |
| 65 outputEncoding = Encoding.ASCII; |
| 66 } |
| 67 |
| 68 response.headers.contentType = |
| 69 new ContentType( |
| 70 "application", "json", charset: outputEncoding.name); |
| 71 response.headers.set('single', 'value'); |
63 | 72 |
64 var requestBody; | 73 var requestBody; |
65 if (requestBodyBytes.isEmpty) { | 74 if (requestBodyBytes.isEmpty) { |
66 requestBody = null; | 75 requestBody = null; |
67 } else if (request.headers.contentType.charset != null) { | 76 } else if (request.headers.contentType.charset != null) { |
68 var encoding = requiredEncodingForCharset( | 77 var encoding = requiredEncodingForCharset( |
69 request.headers.contentType.charset); | 78 request.headers.contentType.charset); |
70 requestBody = decodeString(requestBodyBytes, encoding); | 79 requestBody = decodeString(requestBodyBytes, encoding); |
71 } else { | 80 } else { |
72 requestBody = requestBodyBytes; | 81 requestBody = requestBodyBytes; |
73 } | 82 } |
74 | 83 |
75 var content = { | 84 var content = { |
76 'method': request.method, | 85 'method': request.method, |
77 'path': request.uri.path, | 86 'path': request.uri.path, |
78 'headers': {} | 87 'headers': {} |
79 }; | 88 }; |
80 if (requestBody != null) content['body'] = requestBody; | 89 if (requestBody != null) content['body'] = requestBody; |
81 request.headers.forEach((name, values) { | 90 request.headers.forEach((name, values) { |
82 // These headers are automatically generated by dart:io, so we don't | 91 // These headers are automatically generated by dart:io, so we don't |
83 // want to test them here. | 92 // want to test them here. |
84 if (name == 'cookie' || name == 'host') return; | 93 if (name == 'cookie' || name == 'host') return; |
85 | 94 |
86 content['headers'][name] = values; | 95 content['headers'][name] = values; |
87 }); | 96 }); |
88 | 97 |
89 var outputEncoding; | |
90 var encodingName = request.queryParameters['response-encoding']; | |
91 if (encodingName != null) { | |
92 outputEncoding = requiredEncodingForCharset(encodingName); | |
93 } else { | |
94 outputEncoding = Encoding.ASCII; | |
95 } | |
96 | |
97 var body = json.stringify(content); | 98 var body = json.stringify(content); |
98 response.contentLength = body.length; | 99 response.contentLength = body.length; |
99 response.addString(body, outputEncoding); | 100 response.write(body); |
100 response.close(); | 101 response.close(); |
101 }); | 102 }); |
102 }); | 103 }); |
103 }); | 104 }); |
104 } | 105 } |
105 | 106 |
106 /// Stops the current HTTP server. | 107 /// Stops the current HTTP server. |
107 void stopServer() { | 108 void stopServer() { |
108 _server.close(); | 109 _server.close(); |
109 _server = null; | 110 _server = null; |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 const isSocketIOException = const _SocketIOException(); | 197 const isSocketIOException = const _SocketIOException(); |
197 | 198 |
198 /// A matcher for functions that throw SocketIOException. | 199 /// A matcher for functions that throw SocketIOException. |
199 const Matcher throwsSocketIOException = | 200 const Matcher throwsSocketIOException = |
200 const Throws(isSocketIOException); | 201 const Throws(isSocketIOException); |
201 | 202 |
202 class _SocketIOException extends TypeMatcher { | 203 class _SocketIOException extends TypeMatcher { |
203 const _SocketIOException() : super("SocketIOException"); | 204 const _SocketIOException() : super("SocketIOException"); |
204 bool matches(item, MatchState matchState) => item is SocketIOException; | 205 bool matches(item, MatchState matchState) => item is SocketIOException; |
205 } | 206 } |
OLD | NEW |