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.request_test; | 5 library shelf.request_test; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'package:shelf/shelf.dart'; | 9 import 'package:shelf/shelf.dart'; |
10 import 'package:unittest/unittest.dart'; | 10 import 'package:unittest/unittest.dart'; |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
140 test("supports a null body", () { | 140 test("supports a null body", () { |
141 var request = _request(); | 141 var request = _request(); |
142 expect(request.readAsString(), completion(equals(""))); | 142 expect(request.readAsString(), completion(equals(""))); |
143 }); | 143 }); |
144 | 144 |
145 test("supports a Stream<List<int>> body", () { | 145 test("supports a Stream<List<int>> body", () { |
146 var controller = new StreamController(); | 146 var controller = new StreamController(); |
147 var request = _request({}, controller.stream); | 147 var request = _request({}, controller.stream); |
148 expect(request.readAsString(), completion(equals("hello, world"))); | 148 expect(request.readAsString(), completion(equals("hello, world"))); |
149 | 149 |
150 controller.add([104, 101, 108, 108, 111, 44]); | 150 controller.add(_HELLO_BYTES); |
151 return new Future(() { | 151 return new Future(() { |
152 controller | 152 controller |
153 ..add([32, 119, 111, 114, 108, 100]) | 153 ..add(_WORLD_BYTES) |
154 ..close(); | 154 ..close(); |
155 }); | 155 }); |
156 }); | 156 }); |
157 }); | 157 }); |
158 | 158 |
159 group("read", () { | 159 group("read", () { |
160 test("supports a null body", () { | 160 test("supports a null body", () { |
161 var request = _request(); | 161 var request = _request(); |
162 expect(request.read().toList(), completion(isEmpty)); | 162 expect(request.read().toList(), completion(isEmpty)); |
163 }); | 163 }); |
164 | 164 |
165 test("supports a Stream<List<int>> body", () { | 165 test("supports a Stream<List<int>> body", () { |
166 var controller = new StreamController(); | 166 var controller = new StreamController(); |
167 var request = _request({}, controller.stream); | 167 var request = _request({}, controller.stream); |
168 expect(request.read().toList(), completion(equals([ | 168 expect(request.read().toList(), completion(equals([ |
169 [104, 101, 108, 108, 111, 44], | 169 _HELLO_BYTES, |
170 [32, 119, 111, 114, 108, 100] | 170 _WORLD_BYTES |
171 ]))); | 171 ]))); |
172 | 172 |
173 controller.add([104, 101, 108, 108, 111, 44]); | 173 controller.add(_HELLO_BYTES); |
174 return new Future(() { | 174 return new Future(() { |
175 controller | 175 controller |
176 ..add([32, 119, 111, 114, 108, 100]) | 176 ..add(_WORLD_BYTES) |
177 ..close(); | 177 ..close(); |
178 }); | 178 }); |
179 }); | 179 }); |
180 }); | 180 }); |
181 | |
182 group('copy', () { | |
183 test('with no arguments returns identical instance', () { | |
184 var controller = new StreamController(); | |
185 | |
186 var uri = Uri.parse('https://test.example.com/static/file.html'); | |
187 | |
188 var request = new Request('GET', uri, | |
189 protocolVersion: '2.0', | |
190 headers: {'header1': 'header value 1'}, | |
191 url: Uri.parse('/file.html'), | |
192 scriptName: '/static', | |
193 body: controller.stream, | |
194 context: {'context1': 'context value 1'}); | |
195 | |
196 var copy = request.copy(); | |
197 | |
198 expect(copy.method, request.method); | |
199 expect(copy.requestedUri, request.requestedUri); | |
200 expect(copy.protocolVersion, request.protocolVersion); | |
201 expect(copy.headers, request.headers); | |
202 expect(copy.url, request.url); | |
203 expect(copy.scriptName, request.scriptName); | |
204 expect(copy.context, request.context); | |
205 expect(copy.readAsString(), completion('hello, world')); | |
206 | |
207 controller.add(_HELLO_BYTES); | |
208 return new Future(() { | |
209 controller | |
210 ..add(_WORLD_BYTES) | |
211 ..close(); | |
212 }); | |
213 }); | |
214 | |
215 test('with empty headers returns indentical instance', () { | |
nweiz
2014/04/28 22:13:33
"indentical" -> "identical"
This also doesn't tes
kevmoo
2014/04/29 10:41:54
Done.
| |
216 var request = new Request('GET', LOCALHOST_URI, | |
217 headers: {'header1': 'header value 1'}); | |
218 var copy = request.copy(headers: {}); | |
219 | |
220 expect(copy.headers, request.headers); | |
221 }); | |
222 | |
223 test('with empty context returns identical instance', () { | |
224 var request = new Request('GET', LOCALHOST_URI, | |
225 context: {'context1': 'context value 1'}); | |
226 var copy = request.copy(context: {}); | |
227 | |
228 expect(copy.context, request.context); | |
229 }); | |
230 | |
231 test('new header values are added', () { | |
232 var request = new Request('GET', LOCALHOST_URI, | |
233 headers: {'test':'test value'}); | |
234 | |
235 var copy = request.copy(headers: {'test2': 'test2 value'}); | |
236 | |
237 expect(copy.headers, {'test':'test value', 'test2':'test2 value'}); | |
238 }); | |
239 | |
240 test('existing header values are overwritten', () { | |
241 var request = new Request('GET', LOCALHOST_URI, | |
242 headers: {'test':'test value'}); | |
243 | |
244 var copy = request.copy(headers: {'test': 'new test value'}); | |
245 | |
246 expect(copy.headers, {'test':'new test value'}); | |
247 }); | |
248 | |
249 test('new context values are added', () { | |
250 var request = new Request('GET', LOCALHOST_URI, | |
251 context: {'test':'test value'}); | |
252 | |
253 var copy = request.copy(context: {'test2': 'test2 value'}); | |
254 | |
255 expect(copy.context, {'test':'test value', 'test2':'test2 value'}); | |
256 }); | |
257 | |
258 test('existing context values are overwritten', () { | |
259 var request = new Request('GET', LOCALHOST_URI, | |
260 context: {'test':'test value'}); | |
261 | |
262 var copy = request.copy(context: {'test': 'new test value'}); | |
263 | |
264 expect(copy.context, {'test':'new test value'}); | |
265 }); | |
266 }); | |
181 } | 267 } |
268 | |
269 // "hello," | |
270 const _HELLO_BYTES = const [104, 101, 108, 108, 111, 44]; | |
271 | |
272 // " world" | |
273 const _WORLD_BYTES = const [32, 119, 111, 114, 108, 100]; | |
nweiz
2014/04/28 22:13:33
Constants go at the top of the file.
kevmoo
2014/04/29 10:41:54
Done.
| |
OLD | NEW |