Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(371)

Side by Side Diff: pkg/http/test/request_test.dart

Issue 11363063: Add an HTTP library that wraps dart:io. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/http/test/http_test.dart ('k') | pkg/http/test/response_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 library request_test;
6
7 import 'dart:io';
8
9 import '../../unittest/lib/unittest.dart';
10 import '../lib/http.dart' as http;
11 import '../lib/src/utils.dart';
12 import 'utils.dart';
13
14 void main() {
15 test('.send', () {
16 startServer();
17
18 var request = new http.Request('POST', serverUrl);
19 request.body = "hello";
20 var future = request.send().chain((response) {
21 expect(response.statusCode, equals(200));
22 return consumeInputStream(response.stream);
23 }).transform((bytes) => new String.fromCharCodes(bytes));
24 future.onComplete((_) => stopServer());
25
26 expect(future, completion(parse(equals({
27 'method': 'POST',
28 'path': '/',
29 'headers': {
30 'content-type': ['text/plain; charset=UTF-8'],
31 'content-length': ['5']
32 },
33 'body': 'hello'
34 }))));
35 });
36
37 group('#contentLength', () {
38 test('is computed from bodyBytes', () {
39 var request = new http.Request('POST', dummyUrl);
40 request.bodyBytes = [1, 2, 3, 4, 5];
41 expect(request.contentLength, equals(5));
42 request.bodyBytes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
43 expect(request.contentLength, equals(10));
44 });
45
46 test('is computed from body', () {
47 var request = new http.Request('POST', dummyUrl);
48 request.body = "hello";
49 expect(request.contentLength, equals(5));
50 request.body = "hello, world";
51 expect(request.contentLength, equals(12));
52 });
53
54 test('is not directly mutable', () {
55 var request = new http.Request('POST', dummyUrl);
56 expect(() => request.contentLength = 50, throwsUnsupportedError);
57 });
58 });
59
60 group('#encoding', () {
61 test('defaults to utf-8', () {
62 var request = new http.Request('POST', dummyUrl);
63 expect(request.encoding.name, equals(Encoding.UTF_8.name));
64 });
65
66 test('can be set', () {
67 var request = new http.Request('POST', dummyUrl);
68 request.encoding = Encoding.ISO_8859_1;
69 expect(request.encoding.name, equals(Encoding.ISO_8859_1.name));
70 });
71
72 test('is based on the content-type charset if it exists', () {
73 var request = new http.Request('POST', dummyUrl);
74 request.headers[HttpHeaders.CONTENT_TYPE] =
75 'text/plain; charset=iso-8859-1';
76 expect(request.encoding.name, equals(Encoding.ISO_8859_1.name));
77 });
78
79 test('remains the default if the content-type charset is set and unset',
80 () {
81 var request = new http.Request('POST', dummyUrl);
82 request.encoding = Encoding.ISO_8859_1;
83 request.headers[HttpHeaders.CONTENT_TYPE] =
84 'text/plain; charset=utf-8';
85 expect(request.encoding.name, equals(Encoding.UTF_8.name));
86
87 request.headers.remove(HttpHeaders.CONTENT_TYPE);
88 expect(request.encoding.name, equals(Encoding.ISO_8859_1.name));
89 });
90
91 test('throws an error if the content-type charset is unknown', () {
92 var request = new http.Request('POST', dummyUrl);
93 request.headers[HttpHeaders.CONTENT_TYPE] =
94 'text/plain; charset=not-a-real-charset';
95 expect(() => request.encoding, throwsFormatException);
96 });
97 });
98
99 group('#bodyBytes', () {
100 test('defaults to empty', () {
101 var request = new http.Request('POST', dummyUrl);
102 expect(request.bodyBytes, isEmpty);
103 });
104
105 test('can be set', () {
106 var request = new http.Request('POST', dummyUrl);
107 request.bodyBytes = [104, 101, 108, 108, 111];
108 expect(request.bodyBytes, equals([104, 101, 108, 108, 111]));
109 });
110
111 test('changes when body changes', () {
112 var request = new http.Request('POST', dummyUrl);
113 request.body = "hello";
114 expect(request.bodyBytes, equals([104, 101, 108, 108, 111]));
115 });
116 });
117
118 group('#body', () {
119 test('defaults to empty', () {
120 var request = new http.Request('POST', dummyUrl);
121 expect(request.body, isEmpty);
122 });
123
124 test('can be set', () {
125 var request = new http.Request('POST', dummyUrl);
126 request.body = "hello";
127 expect(request.body, equals("hello"));
128 });
129
130 test('changes when bodyBytes changes', () {
131 var request = new http.Request('POST', dummyUrl);
132 request.bodyBytes = [104, 101, 108, 108, 111];
133 expect(request.body, equals("hello"));
134 });
135
136 // TODO(nweiz): test that both the getter and the setter respect #encoding
137 // when issue 6284 is fixed.
138 });
139
140 group('#bodyFields', () {
141 test("can't be read without setting the content-type", () {
142 var request = new http.Request('POST', dummyUrl);
143 expect(() => request.bodyFields, throwsStateError);
144 });
145
146 test("can't be read with the wrong content-type", () {
147 var request = new http.Request('POST', dummyUrl);
148 request.headers[HttpHeaders.CONTENT_TYPE] = 'text/plain';
149 expect(() => request.bodyFields, throwsStateError);
150 });
151
152 test("can't be set with the wrong content-type", () {
153 var request = new http.Request('POST', dummyUrl);
154 request.headers[HttpHeaders.CONTENT_TYPE] = 'text/plain';
155 expect(() => request.bodyFields = {}, throwsStateError);
156 });
157
158 test('defaults to empty', () {
159 var request = new http.Request('POST', dummyUrl);
160 request.headers[HttpHeaders.CONTENT_TYPE] =
161 'application/x-www-form-urlencoded';
162 expect(request.bodyFields, isEmpty);
163 });
164
165 test('can be set with no content-type', () {
166 var request = new http.Request('POST', dummyUrl);
167 request.bodyFields = {'hello': 'world'};
168 expect(request.bodyFields, equals({'hello': 'world'}));
169 });
170
171 test('changes when body changes', () {
172 var request = new http.Request('POST', dummyUrl);
173 request.headers[HttpHeaders.CONTENT_TYPE] =
174 'application/x-www-form-urlencoded';
175 request.body = 'key%201=value&key+2=other%2bvalue';
176 expect(request.bodyFields,
177 equals({'key 1': 'value', 'key 2': 'other+value'}));
178 });
179
180 // TODO(nweiz): test that both the getter and the setter respect #encoding
181 // when issue 6284 is fixed.
182 });
183
184 group('content-type header', () {
185 test('defaults to empty', () {
186 var request = new http.Request('POST', dummyUrl);
187 expect(request.headers[HttpHeaders.CONTENT_TYPE], isNull);
188 });
189
190 test('defaults to empty if only encoding is set', () {
191 var request = new http.Request('POST', dummyUrl);
192 request.encoding = Encoding.ISO_8859_1;
193 expect(request.headers[HttpHeaders.CONTENT_TYPE], isNull);
194 });
195
196 test('is set to application/x-www-form-urlencoded with charset utf-8 if '
197 'bodyFields is set', () {
198 var request = new http.Request('POST', dummyUrl);
199 request.bodyFields = {'hello': 'world'};
200 expect(request.headers[HttpHeaders.CONTENT_TYPE],
201 equals('application/x-www-form-urlencoded; charset=UTF-8'));
202 });
203
204 test('is set to application/x-www-form-urlencoded with the given charset '
205 'if bodyFields and encoding are set', () {
206 var request = new http.Request('POST', dummyUrl);
207 request.encoding = Encoding.ISO_8859_1;
208 request.bodyFields = {'hello': 'world'};
209 expect(request.headers[HttpHeaders.CONTENT_TYPE],
210 equals('application/x-www-form-urlencoded; charset=ISO-8859-1'));
211 });
212
213 test('is set to text/plain and the given encoding if body and encoding are '
214 'both set', () {
215 var request = new http.Request('POST', dummyUrl);
216 request.encoding = Encoding.ISO_8859_1;
217 request.body = 'hello, world';
218 expect(request.headers[HttpHeaders.CONTENT_TYPE],
219 equals('text/plain; charset=ISO-8859-1'));
220 });
221
222 test('is modified to include utf-8 if body is set', () {
223 var request = new http.Request('POST', dummyUrl);
224 request.headers[HttpHeaders.CONTENT_TYPE] = 'application/json';
225 request.body = '{"hello": "world"}';
226 expect(request.headers[HttpHeaders.CONTENT_TYPE],
227 equals('application/json; charset=UTF-8'));
228 });
229
230 test('is modified to include the given encoding if encoding is set', () {
231 var request = new http.Request('POST', dummyUrl);
232 request.headers[HttpHeaders.CONTENT_TYPE] = 'application/json';
233 request.encoding = Encoding.ISO_8859_1;
234 expect(request.headers[HttpHeaders.CONTENT_TYPE],
235 equals('application/json; charset=ISO-8859-1'));
236 });
237
238 test('has its charset overridden by an explicit encoding', () {
239 var request = new http.Request('POST', dummyUrl);
240 request.headers[HttpHeaders.CONTENT_TYPE] =
241 'application/json; charset=utf-8';
242 request.encoding = Encoding.ISO_8859_1;
243 expect(request.headers[HttpHeaders.CONTENT_TYPE],
244 equals('application/json; charset=ISO-8859-1'));
245 });
246
247 test("doen't have its charset overridden by setting bodyFields", () {
248 var request = new http.Request('POST', dummyUrl);
249 request.headers[HttpHeaders.CONTENT_TYPE] =
250 'application/x-www-form-urlencoded; charset=iso-8859-1';
251 request.bodyFields = {'hello': 'world'};
252 expect(request.headers[HttpHeaders.CONTENT_TYPE],
253 equals('application/x-www-form-urlencoded; charset=iso-8859-1'));
254 });
255
256 test("doen't have its charset overridden by setting body", () {
257 var request = new http.Request('POST', dummyUrl);
258 request.headers[HttpHeaders.CONTENT_TYPE] =
259 'application/json; charset=iso-8859-1';
260 request.body = '{"hello": "world"}';
261 expect(request.headers[HttpHeaders.CONTENT_TYPE],
262 equals('application/json; charset=iso-8859-1'));
263 });
264 });
265
266 group('#finalize', () {
267 test('returns a stream that emits the request body', () {
268 var request = new http.Request('POST', dummyUrl);
269 request.body = "Hello, world!";
270 expect(
271 consumeInputStream(request.finalize())
272 .transform((bytes) => new String.fromCharCodes(bytes)),
273 completion(equals("Hello, world!")));
274 });
275
276 test('freezes #persistentConnection', () {
277 var request = new http.Request('POST', dummyUrl);
278 request.finalize();
279
280 expect(request.persistentConnection, isTrue);
281 expect(() => request.persistentConnection = false, throwsStateError);
282 });
283
284 test('freezes #followRedirects', () {
285 var request = new http.Request('POST', dummyUrl);
286 request.finalize();
287
288 expect(request.followRedirects, isTrue);
289 expect(() => request.followRedirects = false, throwsStateError);
290 });
291
292 test('freezes #maxRedirects', () {
293 var request = new http.Request('POST', dummyUrl);
294 request.finalize();
295
296 expect(request.maxRedirects, equals(5));
297 expect(() => request.maxRedirects = 10, throwsStateError);
298 });
299
300 test('freezes #encoding', () {
301 var request = new http.Request('POST', dummyUrl);
302 request.finalize();
303
304 expect(request.encoding.name, equals(Encoding.UTF_8.name));
305 expect(() => request.encoding = Encoding.ASCII, throwsStateError);
306 });
307
308 test('freezes #bodyBytes', () {
309 var request = new http.Request('POST', dummyUrl);
310 request.bodyBytes = [1, 2, 3];
311 request.finalize();
312
313 expect(request.bodyBytes, equals([1, 2, 3]));
314 expect(() => request.bodyBytes = [4, 5, 6], throwsStateError);
315 });
316
317 test('freezes #body', () {
318 var request = new http.Request('POST', dummyUrl);
319 request.body = "hello";
320 request.finalize();
321
322 expect(request.body, equals("hello"));
323 expect(() => request.body = "goodbye", throwsStateError);
324 });
325
326 test('freezes #bodyFields', () {
327 var request = new http.Request('POST', dummyUrl);
328 request.bodyFields = {"hello": "world"};
329 request.finalize();
330
331 expect(request.bodyFields, equals({"hello": "world"}));
332 expect(() => request.bodyFields = {}, throwsStateError);
333 });
334
335 test("can't be called twice", () {
336 var request = new http.Request('POST', dummyUrl);
337 request.finalize();
338 expect(request.finalize, throwsStateError);
339 });
340 });
341 }
342
OLDNEW
« no previous file with comments | « pkg/http/test/http_test.dart ('k') | pkg/http/test/response_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698