| 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 curl_client_test; | 5 library curl_client_test; |
| 6 | 6 |
| 7 import 'dart:io'; | 7 import 'dart:io'; |
| 8 import 'dart:isolate'; | 8 import 'dart:isolate'; |
| 9 import 'dart:json'; | 9 import 'dart:json'; |
| 10 import 'dart:uri'; | 10 import 'dart:uri'; |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 item is RedirectLimitExceededException; | 208 item is RedirectLimitExceededException; |
| 209 } | 209 } |
| 210 | 210 |
| 211 // ---------------------------------------------------------------------------- | 211 // ---------------------------------------------------------------------------- |
| 212 | 212 |
| 213 void main() { | 213 void main() { |
| 214 setUp(startServer); | 214 setUp(startServer); |
| 215 tearDown(stopServer); | 215 tearDown(stopServer); |
| 216 | 216 |
| 217 test('head', () { | 217 test('head', () { |
| 218 expect(new CurlClient().head(serverUrl).transform((response) { | 218 expect(new CurlClient().head(serverUrl).then((response) { |
| 219 expect(response.statusCode, equals(200)); | 219 expect(response.statusCode, equals(200)); |
| 220 expect(response.body, equals('')); | 220 expect(response.body, equals('')); |
| 221 }), completes); | 221 }), completes); |
| 222 }); | 222 }); |
| 223 | 223 |
| 224 test('get', () { | 224 test('get', () { |
| 225 expect(new CurlClient().get(serverUrl, headers: { | 225 expect(new CurlClient().get(serverUrl, headers: { |
| 226 'X-Random-Header': 'Value', | 226 'X-Random-Header': 'Value', |
| 227 'X-Other-Header': 'Other Value' | 227 'X-Other-Header': 'Other Value' |
| 228 }).transform((response) { | 228 }).then((response) { |
| 229 expect(response.statusCode, equals(200)); | 229 expect(response.statusCode, equals(200)); |
| 230 expect(response.body, parse(equals({ | 230 expect(response.body, parse(equals({ |
| 231 'method': 'GET', | 231 'method': 'GET', |
| 232 'path': '/', | 232 'path': '/', |
| 233 'headers': { | 233 'headers': { |
| 234 'x-random-header': ['Value'], | 234 'x-random-header': ['Value'], |
| 235 'x-other-header': ['Other Value'] | 235 'x-other-header': ['Other Value'] |
| 236 }, | 236 }, |
| 237 }))); | 237 }))); |
| 238 }), completes); | 238 }), completes); |
| 239 }); | 239 }); |
| 240 | 240 |
| 241 test('post', () { | 241 test('post', () { |
| 242 expect(new CurlClient().post(serverUrl, headers: { | 242 expect(new CurlClient().post(serverUrl, headers: { |
| 243 'X-Random-Header': 'Value', | 243 'X-Random-Header': 'Value', |
| 244 'X-Other-Header': 'Other Value' | 244 'X-Other-Header': 'Other Value' |
| 245 }, fields: { | 245 }, fields: { |
| 246 'some-field': 'value', | 246 'some-field': 'value', |
| 247 'other-field': 'other value' | 247 'other-field': 'other value' |
| 248 }).transform((response) { | 248 }).then((response) { |
| 249 expect(response.statusCode, equals(200)); | 249 expect(response.statusCode, equals(200)); |
| 250 expect(response.body, parse(equals({ | 250 expect(response.body, parse(equals({ |
| 251 'method': 'POST', | 251 'method': 'POST', |
| 252 'path': '/', | 252 'path': '/', |
| 253 'headers': { | 253 'headers': { |
| 254 'content-type': [ | 254 'content-type': [ |
| 255 'application/x-www-form-urlencoded; charset=UTF-8' | 255 'application/x-www-form-urlencoded; charset=UTF-8' |
| 256 ], | 256 ], |
| 257 'content-length': ['40'], | 257 'content-length': ['40'], |
| 258 'x-random-header': ['Value'], | 258 'x-random-header': ['Value'], |
| 259 'x-other-header': ['Other Value'] | 259 'x-other-header': ['Other Value'] |
| 260 }, | 260 }, |
| 261 'body': 'some-field=value&other-field=other+value' | 261 'body': 'some-field=value&other-field=other+value' |
| 262 }))); | 262 }))); |
| 263 }), completes); | 263 }), completes); |
| 264 }); | 264 }); |
| 265 | 265 |
| 266 test('post without fields', () { | 266 test('post without fields', () { |
| 267 expect(new CurlClient().post(serverUrl, headers: { | 267 expect(new CurlClient().post(serverUrl, headers: { |
| 268 'X-Random-Header': 'Value', | 268 'X-Random-Header': 'Value', |
| 269 'X-Other-Header': 'Other Value', | 269 'X-Other-Header': 'Other Value', |
| 270 'Content-Type': 'text/plain' | 270 'Content-Type': 'text/plain' |
| 271 }).transform((response) { | 271 }).then((response) { |
| 272 expect(response.statusCode, equals(200)); | 272 expect(response.statusCode, equals(200)); |
| 273 expect(response.body, parse(equals({ | 273 expect(response.body, parse(equals({ |
| 274 'method': 'POST', | 274 'method': 'POST', |
| 275 'path': '/', | 275 'path': '/', |
| 276 'headers': { | 276 'headers': { |
| 277 'content-type': ['text/plain'], | 277 'content-type': ['text/plain'], |
| 278 'x-random-header': ['Value'], | 278 'x-random-header': ['Value'], |
| 279 'x-other-header': ['Other Value'] | 279 'x-other-header': ['Other Value'] |
| 280 } | 280 } |
| 281 }))); | 281 }))); |
| 282 }), completes); | 282 }), completes); |
| 283 }); | 283 }); |
| 284 | 284 |
| 285 test('put', () { | 285 test('put', () { |
| 286 expect(new CurlClient().put(serverUrl, headers: { | 286 expect(new CurlClient().put(serverUrl, headers: { |
| 287 'X-Random-Header': 'Value', | 287 'X-Random-Header': 'Value', |
| 288 'X-Other-Header': 'Other Value' | 288 'X-Other-Header': 'Other Value' |
| 289 }, fields: { | 289 }, fields: { |
| 290 'some-field': 'value', | 290 'some-field': 'value', |
| 291 'other-field': 'other value' | 291 'other-field': 'other value' |
| 292 }).transform((response) { | 292 }).then((response) { |
| 293 expect(response.statusCode, equals(200)); | 293 expect(response.statusCode, equals(200)); |
| 294 expect(response.body, parse(equals({ | 294 expect(response.body, parse(equals({ |
| 295 'method': 'PUT', | 295 'method': 'PUT', |
| 296 'path': '/', | 296 'path': '/', |
| 297 'headers': { | 297 'headers': { |
| 298 'content-type': [ | 298 'content-type': [ |
| 299 'application/x-www-form-urlencoded; charset=UTF-8' | 299 'application/x-www-form-urlencoded; charset=UTF-8' |
| 300 ], | 300 ], |
| 301 'content-length': ['40'], | 301 'content-length': ['40'], |
| 302 'x-random-header': ['Value'], | 302 'x-random-header': ['Value'], |
| 303 'x-other-header': ['Other Value'] | 303 'x-other-header': ['Other Value'] |
| 304 }, | 304 }, |
| 305 'body': 'some-field=value&other-field=other+value' | 305 'body': 'some-field=value&other-field=other+value' |
| 306 }))); | 306 }))); |
| 307 }), completes); | 307 }), completes); |
| 308 }); | 308 }); |
| 309 | 309 |
| 310 test('put without fields', () { | 310 test('put without fields', () { |
| 311 expect(new CurlClient().put(serverUrl, headers: { | 311 expect(new CurlClient().put(serverUrl, headers: { |
| 312 'X-Random-Header': 'Value', | 312 'X-Random-Header': 'Value', |
| 313 'X-Other-Header': 'Other Value', | 313 'X-Other-Header': 'Other Value', |
| 314 'Content-Type': 'text/plain' | 314 'Content-Type': 'text/plain' |
| 315 }).transform((response) { | 315 }).then((response) { |
| 316 expect(response.statusCode, equals(200)); | 316 expect(response.statusCode, equals(200)); |
| 317 expect(response.body, parse(equals({ | 317 expect(response.body, parse(equals({ |
| 318 'method': 'PUT', | 318 'method': 'PUT', |
| 319 'path': '/', | 319 'path': '/', |
| 320 'headers': { | 320 'headers': { |
| 321 'content-type': ['text/plain'], | 321 'content-type': ['text/plain'], |
| 322 'x-random-header': ['Value'], | 322 'x-random-header': ['Value'], |
| 323 'x-other-header': ['Other Value'] | 323 'x-other-header': ['Other Value'] |
| 324 } | 324 } |
| 325 }))); | 325 }))); |
| 326 }), completes); | 326 }), completes); |
| 327 }); | 327 }); |
| 328 | 328 |
| 329 test('delete', () { | 329 test('delete', () { |
| 330 expect(new CurlClient().delete(serverUrl, headers: { | 330 expect(new CurlClient().delete(serverUrl, headers: { |
| 331 'X-Random-Header': 'Value', | 331 'X-Random-Header': 'Value', |
| 332 'X-Other-Header': 'Other Value' | 332 'X-Other-Header': 'Other Value' |
| 333 }).transform((response) { | 333 }).then((response) { |
| 334 expect(response.statusCode, equals(200)); | 334 expect(response.statusCode, equals(200)); |
| 335 expect(response.body, parse(equals({ | 335 expect(response.body, parse(equals({ |
| 336 'method': 'DELETE', | 336 'method': 'DELETE', |
| 337 'path': '/', | 337 'path': '/', |
| 338 'headers': { | 338 'headers': { |
| 339 'x-random-header': ['Value'], | 339 'x-random-header': ['Value'], |
| 340 'x-other-header': ['Other Value'] | 340 'x-other-header': ['Other Value'] |
| 341 } | 341 } |
| 342 }))); | 342 }))); |
| 343 }), completes); | 343 }), completes); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 359 | 359 |
| 360 test('read throws an error for a 4** status code', () { | 360 test('read throws an error for a 4** status code', () { |
| 361 expect(new CurlClient().read(serverUrl.resolve('/error')), | 361 expect(new CurlClient().read(serverUrl.resolve('/error')), |
| 362 throwsHttpException); | 362 throwsHttpException); |
| 363 }); | 363 }); |
| 364 | 364 |
| 365 test('readBytes', () { | 365 test('readBytes', () { |
| 366 var future = new CurlClient().readBytes(serverUrl, headers: { | 366 var future = new CurlClient().readBytes(serverUrl, headers: { |
| 367 'X-Random-Header': 'Value', | 367 'X-Random-Header': 'Value', |
| 368 'X-Other-Header': 'Other Value' | 368 'X-Other-Header': 'Other Value' |
| 369 }).transform((bytes) => new String.fromCharCodes(bytes)); | 369 }).then((bytes) => new String.fromCharCodes(bytes)); |
| 370 | 370 |
| 371 expect(future, completion(parse(equals({ | 371 expect(future, completion(parse(equals({ |
| 372 'method': 'GET', | 372 'method': 'GET', |
| 373 'path': '/', | 373 'path': '/', |
| 374 'headers': { | 374 'headers': { |
| 375 'x-random-header': ['Value'], | 375 'x-random-header': ['Value'], |
| 376 'x-other-header': ['Other Value'] | 376 'x-other-header': ['Other Value'] |
| 377 }, | 377 }, |
| 378 })))); | 378 })))); |
| 379 }); | 379 }); |
| 380 | 380 |
| 381 test('readBytes throws an error for a 4** status code', () { | 381 test('readBytes throws an error for a 4** status code', () { |
| 382 expect(new CurlClient().readBytes(serverUrl.resolve('/error')), | 382 expect(new CurlClient().readBytes(serverUrl.resolve('/error')), |
| 383 throwsHttpException); | 383 throwsHttpException); |
| 384 }); | 384 }); |
| 385 | 385 |
| 386 test('#send a StreamedRequest', () { | 386 test('#send a StreamedRequest', () { |
| 387 var client = new CurlClient(); | 387 var client = new CurlClient(); |
| 388 var request = new http.StreamedRequest("POST", serverUrl); | 388 var request = new http.StreamedRequest("POST", serverUrl); |
| 389 request.headers[HttpHeaders.CONTENT_TYPE] = | 389 request.headers[HttpHeaders.CONTENT_TYPE] = |
| 390 'application/json; charset=utf-8'; | 390 'application/json; charset=utf-8'; |
| 391 | 391 |
| 392 var future = client.send(request).chain((response) { | 392 var future = client.send(request).then((response) { |
| 393 expect(response.statusCode, equals(200)); | 393 expect(response.statusCode, equals(200)); |
| 394 return consumeInputStream(response.stream); | 394 return consumeInputStream(response.stream); |
| 395 }).transform((bytes) => new String.fromCharCodes(bytes)); | 395 }).then((bytes) => new String.fromCharCodes(bytes)); |
| 396 future.onComplete((_) => client.close()); | 396 future.catchError((_) {}).then((_) => client.close()); |
| 397 | 397 |
| 398 expect(future, completion(parse(equals({ | 398 expect(future, completion(parse(equals({ |
| 399 'method': 'POST', | 399 'method': 'POST', |
| 400 'path': '/', | 400 'path': '/', |
| 401 'headers': { | 401 'headers': { |
| 402 'content-type': ['application/json; charset=utf-8'], | 402 'content-type': ['application/json; charset=utf-8'], |
| 403 'transfer-encoding': ['chunked'] | 403 'transfer-encoding': ['chunked'] |
| 404 }, | 404 }, |
| 405 'body': '{"hello": "world"}' | 405 'body': '{"hello": "world"}' |
| 406 })))); | 406 })))); |
| 407 | 407 |
| 408 request.stream.writeString('{"hello": "world"}'); | 408 request.stream.writeString('{"hello": "world"}'); |
| 409 request.stream.close(); | 409 request.stream.close(); |
| 410 }); | 410 }); |
| 411 | 411 |
| 412 test('with one redirect', () { | 412 test('with one redirect', () { |
| 413 var url = serverUrl.resolve('/redirect'); | 413 var url = serverUrl.resolve('/redirect'); |
| 414 expect(new CurlClient().get(url).transform((response) { | 414 expect(new CurlClient().get(url).then((response) { |
| 415 expect(response.statusCode, equals(200)); | 415 expect(response.statusCode, equals(200)); |
| 416 expect(response.body, parse(equals({ | 416 expect(response.body, parse(equals({ |
| 417 'method': 'GET', | 417 'method': 'GET', |
| 418 'path': '/', | 418 'path': '/', |
| 419 'headers': {} | 419 'headers': {} |
| 420 }))); | 420 }))); |
| 421 }), completes); | 421 }), completes); |
| 422 }); | 422 }); |
| 423 | 423 |
| 424 test('with too many redirects', () { | 424 test('with too many redirects', () { |
| 425 expect(new CurlClient().get(serverUrl.resolve('/loop?1')), | 425 expect(new CurlClient().get(serverUrl.resolve('/loop?1')), |
| 426 throwsRedirectLimitExceededException); | 426 throwsRedirectLimitExceededException); |
| 427 }); | 427 }); |
| 428 | 428 |
| 429 test('with a generic failure', () { | 429 test('with a generic failure', () { |
| 430 expect(new CurlClient().get('url fail'), | 430 expect(new CurlClient().get('url fail'), |
| 431 throwsHttpException); | 431 throwsHttpException); |
| 432 }); | 432 }); |
| 433 | 433 |
| 434 test('with one redirect via HEAD', () { | 434 test('with one redirect via HEAD', () { |
| 435 var url = serverUrl.resolve('/redirect'); | 435 var url = serverUrl.resolve('/redirect'); |
| 436 expect(new CurlClient().head(url).transform((response) { | 436 expect(new CurlClient().head(url).then((response) { |
| 437 expect(response.statusCode, equals(200)); | 437 expect(response.statusCode, equals(200)); |
| 438 }), completes); | 438 }), completes); |
| 439 }); | 439 }); |
| 440 | 440 |
| 441 test('with too many redirects via HEAD', () { | 441 test('with too many redirects via HEAD', () { |
| 442 expect(new CurlClient().head(serverUrl.resolve('/loop?1')), | 442 expect(new CurlClient().head(serverUrl.resolve('/loop?1')), |
| 443 throwsRedirectLimitExceededException); | 443 throwsRedirectLimitExceededException); |
| 444 }); | 444 }); |
| 445 | 445 |
| 446 test('with a generic failure via HEAD', () { | 446 test('with a generic failure via HEAD', () { |
| 447 expect(new CurlClient().head('url fail'), | 447 expect(new CurlClient().head('url fail'), |
| 448 throwsHttpException); | 448 throwsHttpException); |
| 449 }); | 449 }); |
| 450 | 450 |
| 451 test('without following redirects', () { | 451 test('without following redirects', () { |
| 452 var request = new http.Request('GET', serverUrl.resolve('/redirect')); | 452 var request = new http.Request('GET', serverUrl.resolve('/redirect')); |
| 453 request.followRedirects = false; | 453 request.followRedirects = false; |
| 454 expect(new CurlClient().send(request).chain(http.Response.fromStream) | 454 expect(new CurlClient().send(request).then(http.Response.fromStream) |
| 455 .transform((response) { | 455 .then((response) { |
| 456 expect(response.statusCode, equals(302)); | 456 expect(response.statusCode, equals(302)); |
| 457 expect(response.isRedirect, true); | 457 expect(response.isRedirect, true); |
| 458 }), completes); | 458 }), completes); |
| 459 }); | 459 }); |
| 460 } | 460 } |
| OLD | NEW |