| 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 request_test; | 5 library request_test; |
| 6 | 6 |
| 7 import 'dart:io'; | 7 import 'dart:io'; |
| 8 | 8 |
| 9 import 'package:unittest/unittest.dart'; | 9 import 'package:unittest/unittest.dart'; |
| 10 import 'package:http/http.dart' as http; | 10 import 'package:http/http.dart' as http; |
| 11 import 'package:http/src/utils.dart'; | 11 import 'package:http/src/utils.dart'; |
| 12 import 'utils.dart'; | 12 import 'utils.dart'; |
| 13 | 13 |
| 14 void main() { | 14 void main() { |
| 15 test('.send', () { | 15 test('.send', () { |
| 16 startServer(); | 16 expect(startServer().then((_) { |
| 17 | 17 |
| 18 var request = new http.Request('POST', serverUrl); | 18 var request = new http.Request('POST', serverUrl); |
| 19 request.body = "hello"; | 19 request.body = "hello"; |
| 20 | 20 |
| 21 expect(request.send().then((response) { | 21 expect(request.send().then((response) { |
| 22 expect(response.statusCode, equals(200)); | 22 expect(response.statusCode, equals(200)); |
| 23 return response.stream.bytesToString(); | 23 return response.stream.bytesToString(); |
| 24 }).whenComplete(stopServer), completion(parse(equals({ | 24 }).whenComplete(stopServer), completion(parse(equals({ |
| 25 'method': 'POST', | 25 'method': 'POST', |
| 26 'path': '/', | 26 'path': '/', |
| 27 'headers': { | 27 'headers': { |
| 28 'content-type': ['text/plain; charset=UTF-8'], | 28 'content-type': ['text/plain; charset=UTF-8'], |
| 29 'content-length': ['5'] | 29 'content-length': ['5'] |
| 30 }, | 30 }, |
| 31 'body': 'hello' | 31 'body': 'hello' |
| 32 })))); | 32 })))); |
| 33 }), completes); |
| 33 }); | 34 }); |
| 34 | 35 |
| 35 group('#contentLength', () { | 36 group('#contentLength', () { |
| 36 test('is computed from bodyBytes', () { | 37 test('is computed from bodyBytes', () { |
| 37 var request = new http.Request('POST', dummyUrl); | 38 var request = new http.Request('POST', dummyUrl); |
| 38 request.bodyBytes = [1, 2, 3, 4, 5]; | 39 request.bodyBytes = [1, 2, 3, 4, 5]; |
| 39 expect(request.contentLength, equals(5)); | 40 expect(request.contentLength, equals(5)); |
| 40 request.bodyBytes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | 41 request.bodyBytes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
| 41 expect(request.contentLength, equals(10)); | 42 expect(request.contentLength, equals(10)); |
| 42 }); | 43 }); |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 }); | 177 }); |
| 177 | 178 |
| 178 // TODO(nweiz): test that both the getter and the setter respect #encoding | 179 // TODO(nweiz): test that both the getter and the setter respect #encoding |
| 179 // when issue 6284 is fixed. | 180 // when issue 6284 is fixed. |
| 180 }); | 181 }); |
| 181 | 182 |
| 182 test('#followRedirects', () { | 183 test('#followRedirects', () { |
| 183 print("This test is known to be flaky, please ignore " | 184 print("This test is known to be flaky, please ignore " |
| 184 "(debug prints below added by sgjesse@)"); | 185 "(debug prints below added by sgjesse@)"); |
| 185 print("#followRedirects test starting server..."); | 186 print("#followRedirects test starting server..."); |
| 186 startServer(); | 187 expect(startServer().then((_) { |
| 187 print("#followRedirects test server running"); | 188 print("#followRedirects test server running"); |
| 188 | 189 |
| 189 var request = new http.Request('POST', serverUrl.resolve('/redirect')) | 190 var request = new http.Request('POST', serverUrl.resolve('/redirect')) |
| 190 ..followRedirects = false; | 191 ..followRedirects = false; |
| 191 var future = request.send().then((response) { | 192 var future = request.send().then((response) { |
| 192 print("#followRedirects test response received"); | 193 print("#followRedirects test response received"); |
| 193 expect(response.statusCode, equals(302)); | 194 expect(response.statusCode, equals(302)); |
| 194 }); | 195 }); |
| 195 future.catchError((_) {}).then(expectAsync1((_) { | 196 expect(future.catchError((_) {}).then((_) { |
| 196 print("#followRedirects test stopping server..."); | 197 print("#followRedirects test stopping server..."); |
| 197 stopServer(); | 198 stopServer(); |
| 198 print("#followRedirects test server stopped"); | 199 print("#followRedirects test server stopped"); |
| 199 })); | 200 }), completes); |
| 200 | 201 |
| 201 expect(future, completes); | 202 expect(future, completes); |
| 202 print("#followRedirects test started"); | 203 print("#followRedirects test started"); |
| 204 }), completes); |
| 203 }); | 205 }); |
| 204 | 206 |
| 205 test('#maxRedirects', () { | 207 test('#maxRedirects', () { |
| 206 print("This test is known to be flaky, please ignore " | 208 print("This test is known to be flaky, please ignore " |
| 207 "(debug prints below added by sgjesse@)"); | 209 "(debug prints below added by sgjesse@)"); |
| 208 print("#maxRedirects test starting server..."); | 210 print("#maxRedirects test starting server..."); |
| 209 startServer(); | 211 expect(startServer().then((_) { |
| 210 print("#maxRedirects test server running"); | 212 print("#maxRedirects test server running"); |
| 211 | 213 |
| 212 var request = new http.Request('POST', serverUrl.resolve('/loop?1')) | 214 var request = new http.Request('POST', serverUrl.resolve('/loop?1')) |
| 213 ..maxRedirects = 2; | 215 ..maxRedirects = 2; |
| 214 var future = request.send().catchError((e) { | 216 var future = request.send().catchError((e) { |
| 215 print("#maxRedirects test exception received"); | 217 print("#maxRedirects test exception received"); |
| 216 expect(e.error, isRedirectLimitExceededException); | 218 expect(e.error, isRedirectLimitExceededException); |
| 217 expect(e.error.redirects.length, equals(2)); | 219 expect(e.error.redirects.length, equals(2)); |
| 218 }); | 220 }); |
| 219 future.catchError((_) {}).then(expectAsync1((_) { | 221 expect(future.catchError((_) {}).then((_) { |
| 220 print("#maxRedirects test stopping server..."); | 222 print("#maxRedirects test stopping server..."); |
| 221 stopServer(); | 223 stopServer(); |
| 222 print("#maxRedirects test server stopped"); | 224 print("#maxRedirects test server stopped"); |
| 223 })); | 225 }), completes); |
| 224 | 226 |
| 225 expect(future, completes); | 227 expect(future, completes); |
| 226 print("#maxRedirects test started"); | 228 print("#maxRedirects test started"); |
| 229 }), completes); |
| 227 }); | 230 }); |
| 228 | 231 |
| 229 group('content-type header', () { | 232 group('content-type header', () { |
| 230 test('defaults to empty', () { | 233 test('defaults to empty', () { |
| 231 var request = new http.Request('POST', dummyUrl); | 234 var request = new http.Request('POST', dummyUrl); |
| 232 expect(request.headers[HttpHeaders.CONTENT_TYPE], isNull); | 235 expect(request.headers[HttpHeaders.CONTENT_TYPE], isNull); |
| 233 }); | 236 }); |
| 234 | 237 |
| 235 test('defaults to empty if only encoding is set', () { | 238 test('defaults to empty if only encoding is set', () { |
| 236 var request = new http.Request('POST', dummyUrl); | 239 var request = new http.Request('POST', dummyUrl); |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 383 }); | 386 }); |
| 384 | 387 |
| 385 group('#toString()', () { | 388 group('#toString()', () { |
| 386 test('includes the method and URL', () { | 389 test('includes the method and URL', () { |
| 387 var request = new http.Request('POST', dummyUrl); | 390 var request = new http.Request('POST', dummyUrl); |
| 388 expect(request.toString(), 'POST $dummyUrl'); | 391 expect(request.toString(), 'POST $dummyUrl'); |
| 389 }); | 392 }); |
| 390 }); | 393 }); |
| 391 } | 394 } |
| 392 | 395 |
| OLD | NEW |