ChunkedEncoding.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. class RequestsTest_ChunkedDecoding extends PHPUnit_Framework_TestCase {
  3. public static function chunkedProvider() {
  4. return array(
  5. array(
  6. "25\r\nThis is the data in the first chunk\r\n\r\n1A\r\nand this is the second one\r\n0\r\n",
  7. "This is the data in the first chunk\r\nand this is the second one"
  8. ),
  9. array(
  10. "02\r\nab\r\n04\r\nra\nc\r\n06\r\nadabra\r\n0\r\nnothing\n",
  11. "abra\ncadabra"
  12. ),
  13. array(
  14. "02\r\nab\r\n04\r\nra\nc\r\n06\r\nadabra\r\n0c\r\n\nall we got\n",
  15. "abra\ncadabra\nall we got\n"
  16. ),
  17. );
  18. }
  19. /**
  20. * @dataProvider chunkedProvider
  21. */
  22. public function testChunked($body, $expected){
  23. $transport = new MockTransport();
  24. $transport->body = $body;
  25. $transport->chunked = true;
  26. $options = array(
  27. 'transport' => $transport
  28. );
  29. $response = Requests::get('http://example.com/', array(), $options);
  30. $this->assertEquals($expected, $response->body);
  31. }
  32. /**
  33. * Response says it's chunked, but actually isn't
  34. */
  35. public function testNotActuallyChunked() {
  36. $transport = new MockTransport();
  37. $transport->body = 'Hello! This is a non-chunked response!';
  38. $transport->chunked = true;
  39. $options = array(
  40. 'transport' => $transport
  41. );
  42. $response = Requests::get('http://example.com/', array(), $options);
  43. $this->assertEquals($transport->body, $response->body);
  44. }
  45. /**
  46. * Response says it's chunked and starts looking like it is, but turns out
  47. * that they're lying to us
  48. */
  49. public function testMixedChunkiness() {
  50. $transport = new MockTransport();
  51. $transport->body = "02\r\nab\r\nNot actually chunked!";
  52. $transport->chunked = true;
  53. $options = array(
  54. 'transport' => $transport
  55. );
  56. $response = Requests::get('http://example.com/', array(), $options);
  57. $this->assertEquals($transport->body, $response->body);
  58. }
  59. }