ChunkedEncoding.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. array(
  18. "02;foo=bar;hello=world\r\nab\r\n04;foo=baz\r\nra\nc\r\n06;justfoo\r\nadabra\r\n0c\r\n\nall we got\n",
  19. "abra\ncadabra\nall we got\n"
  20. ),
  21. array(
  22. "02;foo=\"quoted value\"\r\nab\r\n04\r\nra\nc\r\n06\r\nadabra\r\n0c\r\n\nall we got\n",
  23. "abra\ncadabra\nall we got\n"
  24. ),
  25. array(
  26. "02;foo-bar=baz\r\nab\r\n04\r\nra\nc\r\n06\r\nadabra\r\n0c\r\n\nall we got\n",
  27. "abra\ncadabra\nall we got\n"
  28. ),
  29. );
  30. }
  31. /**
  32. * @dataProvider chunkedProvider
  33. */
  34. public function testChunked($body, $expected){
  35. $transport = new MockTransport();
  36. $transport->body = $body;
  37. $transport->chunked = true;
  38. $options = array(
  39. 'transport' => $transport
  40. );
  41. $response = Requests::get('http://example.com/', array(), $options);
  42. $this->assertEquals($expected, $response->body);
  43. }
  44. public static function notChunkedProvider() {
  45. return array(
  46. 'invalid chunk size' => array( 'Hello! This is a non-chunked response!' ),
  47. 'invalid chunk extension' => array( '1BNot chunked\r\nLooks chunked but it is not\r\n' ),
  48. 'unquoted chunk-ext-val with space' => array( "02;foo=unquoted with space\r\nab\r\n04\r\nra\nc\r\n06\r\nadabra\r\n0c\r\n\nall we got\n" ),
  49. 'unquoted chunk-ext-val with forbidden character' => array( "02;foo={unquoted}\r\nab\r\n04\r\nra\nc\r\n06\r\nadabra\r\n0c\r\n\nall we got\n" ),
  50. 'invalid chunk-ext-name' => array( "02;{foo}=bar\r\nab\r\n04\r\nra\nc\r\n06\r\nadabra\r\n0c\r\n\nall we got\n" ),
  51. 'incomplete quote for chunk-ext-value' => array( "02;foo=\"no end quote\r\nab\r\n04\r\nra\nc\r\n06\r\nadabra\r\n0c\r\n\nall we got\n" ),
  52. );
  53. }
  54. /**
  55. * Response says it's chunked, but actually isn't
  56. * @dataProvider notChunkedProvider
  57. */
  58. public function testNotActuallyChunked($body) {
  59. $transport = new MockTransport();
  60. $transport->body = $body;
  61. $transport->chunked = true;
  62. $options = array(
  63. 'transport' => $transport
  64. );
  65. $response = Requests::get('http://example.com/', array(), $options);
  66. $this->assertEquals($transport->body, $response->body);
  67. }
  68. /**
  69. * Response says it's chunked and starts looking like it is, but turns out
  70. * that they're lying to us
  71. */
  72. public function testMixedChunkiness() {
  73. $transport = new MockTransport();
  74. $transport->body = "02\r\nab\r\nNot actually chunked!";
  75. $transport->chunked = true;
  76. $options = array(
  77. 'transport' => $transport
  78. );
  79. $response = Requests::get('http://example.com/', array(), $options);
  80. $this->assertEquals($transport->body, $response->body);
  81. }
  82. }