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

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

Issue 810223002: Remove the http package from the repo. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years 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
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:convert';
8
9 import 'package:http/http.dart' as http;
10 import 'package:unittest/unittest.dart';
11
12 import 'utils.dart';
13
14 void main() {
15 group('#contentLength', () {
16 test('is computed from bodyBytes', () {
17 var request = new http.Request('POST', dummyUrl);
18 request.bodyBytes = [1, 2, 3, 4, 5];
19 expect(request.contentLength, equals(5));
20 request.bodyBytes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
21 expect(request.contentLength, equals(10));
22 });
23
24 test('is computed from body', () {
25 var request = new http.Request('POST', dummyUrl);
26 request.body = "hello";
27 expect(request.contentLength, equals(5));
28 request.body = "hello, world";
29 expect(request.contentLength, equals(12));
30 });
31
32 test('is not directly mutable', () {
33 var request = new http.Request('POST', dummyUrl);
34 expect(() => request.contentLength = 50, throwsUnsupportedError);
35 });
36 });
37
38 group('#encoding', () {
39 test('defaults to utf-8', () {
40 var request = new http.Request('POST', dummyUrl);
41 expect(request.encoding.name, equals(UTF8.name));
42 });
43
44 test('can be set', () {
45 var request = new http.Request('POST', dummyUrl);
46 request.encoding = LATIN1;
47 expect(request.encoding.name, equals(LATIN1.name));
48 });
49
50 test('is based on the content-type charset if it exists', () {
51 var request = new http.Request('POST', dummyUrl);
52 request.headers['Content-Type'] = 'text/plain; charset=iso-8859-1';
53 expect(request.encoding.name, equals(LATIN1.name));
54 });
55
56 test('remains the default if the content-type charset is set and unset',
57 () {
58 var request = new http.Request('POST', dummyUrl);
59 request.encoding = LATIN1;
60 request.headers['Content-Type'] = 'text/plain; charset=utf-8';
61 expect(request.encoding.name, equals(UTF8.name));
62
63 request.headers.remove('Content-Type');
64 expect(request.encoding.name, equals(LATIN1.name));
65 });
66
67 test('throws an error if the content-type charset is unknown', () {
68 var request = new http.Request('POST', dummyUrl);
69 request.headers['Content-Type'] =
70 'text/plain; charset=not-a-real-charset';
71 expect(() => request.encoding, throwsFormatException);
72 });
73 });
74
75 group('#bodyBytes', () {
76 test('defaults to empty', () {
77 var request = new http.Request('POST', dummyUrl);
78 expect(request.bodyBytes, isEmpty);
79 });
80
81 test('can be set', () {
82 var request = new http.Request('POST', dummyUrl);
83 request.bodyBytes = [104, 101, 108, 108, 111];
84 expect(request.bodyBytes, equals([104, 101, 108, 108, 111]));
85 });
86
87 test('changes when body changes', () {
88 var request = new http.Request('POST', dummyUrl);
89 request.body = "hello";
90 expect(request.bodyBytes, equals([104, 101, 108, 108, 111]));
91 });
92 });
93
94 group('#body', () {
95 test('defaults to empty', () {
96 var request = new http.Request('POST', dummyUrl);
97 expect(request.body, isEmpty);
98 });
99
100 test('can be set', () {
101 var request = new http.Request('POST', dummyUrl);
102 request.body = "hello";
103 expect(request.body, equals("hello"));
104 });
105
106 test('changes when bodyBytes changes', () {
107 var request = new http.Request('POST', dummyUrl);
108 request.bodyBytes = [104, 101, 108, 108, 111];
109 expect(request.body, equals("hello"));
110 });
111
112 test('is encoded according to the given encoding', () {
113 var request = new http.Request('POST', dummyUrl);
114 request.encoding = LATIN1;
115 request.body = "föøbãr";
116 expect(request.bodyBytes, equals([102, 246, 248, 98, 227, 114]));
117 });
118
119 test('is decoded according to the given encoding', () {
120 var request = new http.Request('POST', dummyUrl);
121 request.encoding = LATIN1;
122 request.bodyBytes = [102, 246, 248, 98, 227, 114];
123 expect(request.body, equals("föøbãr"));
124 });
125 });
126
127 group('#bodyFields', () {
128 test("can't be read without setting the content-type", () {
129 var request = new http.Request('POST', dummyUrl);
130 expect(() => request.bodyFields, throwsStateError);
131 });
132
133 test("can't be read with the wrong content-type", () {
134 var request = new http.Request('POST', dummyUrl);
135 request.headers['Content-Type'] = 'text/plain';
136 expect(() => request.bodyFields, throwsStateError);
137 });
138
139 test("can't be set with the wrong content-type", () {
140 var request = new http.Request('POST', dummyUrl);
141 request.headers['Content-Type'] = 'text/plain';
142 expect(() => request.bodyFields = {}, throwsStateError);
143 });
144
145 test('defaults to empty', () {
146 var request = new http.Request('POST', dummyUrl);
147 request.headers['Content-Type'] =
148 'application/x-www-form-urlencoded';
149 expect(request.bodyFields, isEmpty);
150 });
151
152 test('can be set with no content-type', () {
153 var request = new http.Request('POST', dummyUrl);
154 request.bodyFields = {'hello': 'world'};
155 expect(request.bodyFields, equals({'hello': 'world'}));
156 });
157
158 test('changes when body changes', () {
159 var request = new http.Request('POST', dummyUrl);
160 request.headers['Content-Type'] =
161 'application/x-www-form-urlencoded';
162 request.body = 'key%201=value&key+2=other%2bvalue';
163 expect(request.bodyFields,
164 equals({'key 1': 'value', 'key 2': 'other+value'}));
165 });
166
167 test('is encoded according to the given encoding', () {
168 var request = new http.Request('POST', dummyUrl);
169 request.headers['Content-Type'] =
170 'application/x-www-form-urlencoded';
171 request.encoding = LATIN1;
172 request.bodyFields = {"föø": "bãr"};
173 expect(request.body, equals('f%F6%F8=b%E3r'));
174 });
175
176 test('is decoded according to the given encoding', () {
177 var request = new http.Request('POST', dummyUrl);
178 request.headers['Content-Type'] =
179 'application/x-www-form-urlencoded';
180 request.encoding = LATIN1;
181 request.body = 'f%F6%F8=b%E3r';
182 expect(request.bodyFields, equals({"föø": "bãr"}));
183 });
184 });
185
186 group('content-type header', () {
187 test('defaults to empty', () {
188 var request = new http.Request('POST', dummyUrl);
189 expect(request.headers['Content-Type'], isNull);
190 });
191
192 test('defaults to empty if only encoding is set', () {
193 var request = new http.Request('POST', dummyUrl);
194 request.encoding = LATIN1;
195 expect(request.headers['Content-Type'], isNull);
196 });
197
198 test('name is case insensitive', () {
199 var request = new http.Request('POST', dummyUrl);
200 request.headers['CoNtEnT-tYpE'] = 'application/json';
201 expect(request.headers,
202 containsPair('content-type', 'application/json'));
203 });
204
205 test('is set to application/x-www-form-urlencoded with charset utf-8 if '
206 'bodyFields is set', () {
207 var request = new http.Request('POST', dummyUrl);
208 request.bodyFields = {'hello': 'world'};
209 expect(request.headers['Content-Type'],
210 equals('application/x-www-form-urlencoded; charset=utf-8'));
211 });
212
213 test('is set to application/x-www-form-urlencoded with the given charset '
214 'if bodyFields and encoding are set', () {
215 var request = new http.Request('POST', dummyUrl);
216 request.encoding = LATIN1;
217 request.bodyFields = {'hello': 'world'};
218 expect(request.headers['Content-Type'],
219 equals('application/x-www-form-urlencoded; charset=iso-8859-1'));
220 });
221
222 test('is set to text/plain and the given encoding if body and encoding are '
223 'both set', () {
224 var request = new http.Request('POST', dummyUrl);
225 request.encoding = LATIN1;
226 request.body = 'hello, world';
227 expect(request.headers['Content-Type'],
228 equals('text/plain; charset=iso-8859-1'));
229 });
230
231 test('is modified to include utf-8 if body is set', () {
232 var request = new http.Request('POST', dummyUrl);
233 request.headers['Content-Type'] = 'application/json';
234 request.body = '{"hello": "world"}';
235 expect(request.headers['Content-Type'],
236 equals('application/json; charset=utf-8'));
237 });
238
239 test('is modified to include the given encoding if encoding is set', () {
240 var request = new http.Request('POST', dummyUrl);
241 request.headers['Content-Type'] = 'application/json';
242 request.encoding = LATIN1;
243 expect(request.headers['Content-Type'],
244 equals('application/json; charset=iso-8859-1'));
245 });
246
247 test('has its charset overridden by an explicit encoding', () {
248 var request = new http.Request('POST', dummyUrl);
249 request.headers['Content-Type'] =
250 'application/json; charset=utf-8';
251 request.encoding = LATIN1;
252 expect(request.headers['Content-Type'],
253 equals('application/json; charset=iso-8859-1'));
254 });
255
256 test("doen't have its charset overridden by setting bodyFields", () {
257 var request = new http.Request('POST', dummyUrl);
258 request.headers['Content-Type'] =
259 'application/x-www-form-urlencoded; charset=iso-8859-1';
260 request.bodyFields = {'hello': 'world'};
261 expect(request.headers['Content-Type'],
262 equals('application/x-www-form-urlencoded; charset=iso-8859-1'));
263 });
264
265 test("doen't have its charset overridden by setting body", () {
266 var request = new http.Request('POST', dummyUrl);
267 request.headers['Content-Type'] =
268 'application/json; charset=iso-8859-1';
269 request.body = '{"hello": "world"}';
270 expect(request.headers['Content-Type'],
271 equals('application/json; charset=iso-8859-1'));
272 });
273 });
274
275 group('#finalize', () {
276 test('returns a stream that emits the request body', () {
277 var request = new http.Request('POST', dummyUrl);
278 request.body = "Hello, world!";
279 expect(request.finalize().bytesToString(),
280 completion(equals("Hello, world!")));
281 });
282
283 test('freezes #persistentConnection', () {
284 var request = new http.Request('POST', dummyUrl);
285 request.finalize();
286
287 expect(request.persistentConnection, isTrue);
288 expect(() => request.persistentConnection = false, throwsStateError);
289 });
290
291 test('freezes #followRedirects', () {
292 var request = new http.Request('POST', dummyUrl);
293 request.finalize();
294
295 expect(request.followRedirects, isTrue);
296 expect(() => request.followRedirects = false, throwsStateError);
297 });
298
299 test('freezes #maxRedirects', () {
300 var request = new http.Request('POST', dummyUrl);
301 request.finalize();
302
303 expect(request.maxRedirects, equals(5));
304 expect(() => request.maxRedirects = 10, throwsStateError);
305 });
306
307 test('freezes #encoding', () {
308 var request = new http.Request('POST', dummyUrl);
309 request.finalize();
310
311 expect(request.encoding.name, equals(UTF8.name));
312 expect(() => request.encoding = ASCII, throwsStateError);
313 });
314
315 test('freezes #bodyBytes', () {
316 var request = new http.Request('POST', dummyUrl);
317 request.bodyBytes = [1, 2, 3];
318 request.finalize();
319
320 expect(request.bodyBytes, equals([1, 2, 3]));
321 expect(() => request.bodyBytes = [4, 5, 6], throwsStateError);
322 });
323
324 test('freezes #body', () {
325 var request = new http.Request('POST', dummyUrl);
326 request.body = "hello";
327 request.finalize();
328
329 expect(request.body, equals("hello"));
330 expect(() => request.body = "goodbye", throwsStateError);
331 });
332
333 test('freezes #bodyFields', () {
334 var request = new http.Request('POST', dummyUrl);
335 request.bodyFields = {"hello": "world"};
336 request.finalize();
337
338 expect(request.bodyFields, equals({"hello": "world"}));
339 expect(() => request.bodyFields = {}, throwsStateError);
340 });
341
342 test("can't be called twice", () {
343 var request = new http.Request('POST', dummyUrl);
344 request.finalize();
345 expect(request.finalize, throwsStateError);
346 });
347 });
348
349 group('#toString()', () {
350 test('includes the method and URL', () {
351 var request = new http.Request('POST', dummyUrl);
352 expect(request.toString(), 'POST $dummyUrl');
353 });
354 });
355 }
356
OLDNEW
« no previous file with comments | « pkg/http/test/multipart_test.dart ('k') | pkg/http/test/response_test.dart » ('j') | pkg/pkg.gyp » ('J')

Powered by Google App Engine
This is Rietveld 408576698