/api/vendor/guzzle/guzzle/tests/Guzzle/Tests/Http/EntityBodyTest.php

https://gitlab.com/x33n/respond · PHP · 182 lines · 136 code · 26 blank · 20 comment · 3 complexity · 97d01d63991035eb65db1e64fc92c48b MD5 · raw file

  1. <?php
  2. namespace Guzzle\Tests\Http;
  3. use Guzzle\Http\EntityBody;
  4. use Guzzle\Http\QueryString;
  5. /**
  6. * @group server
  7. * @covers Guzzle\Http\EntityBody
  8. */
  9. class EntityBodyTest extends \Guzzle\Tests\GuzzleTestCase
  10. {
  11. /**
  12. * @expectedException \Guzzle\Common\Exception\InvalidArgumentException
  13. */
  14. public function testFactoryThrowsException()
  15. {
  16. $body = EntityBody::factory(false);
  17. }
  18. public function testFactory()
  19. {
  20. $body = EntityBody::factory('data');
  21. $this->assertEquals('data', (string) $body);
  22. $this->assertEquals(4, $body->getContentLength());
  23. $this->assertEquals('PHP', $body->getWrapper());
  24. $this->assertEquals('TEMP', $body->getStreamType());
  25. $handle = fopen(__DIR__ . '/../../../../phpunit.xml.dist', 'r');
  26. if (!$handle) {
  27. $this->fail('Could not open test file');
  28. }
  29. $body = EntityBody::factory($handle);
  30. $this->assertEquals(__DIR__ . '/../../../../phpunit.xml.dist', $body->getUri());
  31. $this->assertTrue($body->isLocal());
  32. $this->assertEquals(__DIR__ . '/../../../../phpunit.xml.dist', $body->getUri());
  33. $this->assertEquals(filesize(__DIR__ . '/../../../../phpunit.xml.dist'), $body->getContentLength());
  34. // make sure that a body will return as the same object
  35. $this->assertTrue($body === EntityBody::factory($body));
  36. }
  37. public function testFactoryCreatesTempStreamByDefault()
  38. {
  39. $body = EntityBody::factory('');
  40. $this->assertEquals('PHP', $body->getWrapper());
  41. $this->assertEquals('TEMP', $body->getStreamType());
  42. $body = EntityBody::factory();
  43. $this->assertEquals('PHP', $body->getWrapper());
  44. $this->assertEquals('TEMP', $body->getStreamType());
  45. }
  46. public function testFactoryCanCreateFromObject()
  47. {
  48. $body = EntityBody::factory(new QueryString(array('foo' => 'bar')));
  49. $this->assertEquals('foo=bar', (string) $body);
  50. }
  51. /**
  52. * @expectedException \Guzzle\Common\Exception\InvalidArgumentException
  53. */
  54. public function testFactoryEnsuresObjectsHaveToStringMethod()
  55. {
  56. EntityBody::factory(new \stdClass('a'));
  57. }
  58. public function testHandlesCompression()
  59. {
  60. $body = EntityBody::factory('testing 123...testing 123');
  61. $this->assertFalse($body->getContentEncoding(), '-> getContentEncoding() must initially return FALSE');
  62. $size = $body->getContentLength();
  63. $body->compress();
  64. $this->assertEquals('gzip', $body->getContentEncoding(), '-> getContentEncoding() must return the correct encoding after compressing');
  65. $this->assertEquals(gzdeflate('testing 123...testing 123'), (string) $body);
  66. $this->assertTrue($body->getContentLength() < $size);
  67. $this->assertTrue($body->uncompress());
  68. $this->assertEquals('testing 123...testing 123', (string) $body);
  69. $this->assertFalse($body->getContentEncoding(), '-> getContentEncoding() must reset to FALSE');
  70. if (in_array('bzip2.*', stream_get_filters())) {
  71. $this->assertTrue($body->compress('bzip2.compress'));
  72. $this->assertEquals('compress', $body->getContentEncoding(), '-> compress() must set \'compress\' as the Content-Encoding');
  73. }
  74. $this->assertFalse($body->compress('non-existent'), '-> compress() must return false when a non-existent stream filter is used');
  75. // Release the body
  76. unset($body);
  77. // Use gzip compression on the initial content. This will include a
  78. // gzip header which will need to be stripped when deflating the stream
  79. $body = EntityBody::factory(gzencode('test'));
  80. $this->assertSame($body, $body->setStreamFilterContentEncoding('zlib.deflate'));
  81. $this->assertTrue($body->uncompress('zlib.inflate'));
  82. $this->assertEquals('test', (string) $body);
  83. unset($body);
  84. // Test using a very long string
  85. $largeString = '';
  86. for ($i = 0; $i < 25000; $i++) {
  87. $largeString .= chr(rand(33, 126));
  88. }
  89. $body = EntityBody::factory($largeString);
  90. $this->assertEquals($largeString, (string) $body);
  91. $this->assertTrue($body->compress());
  92. $this->assertNotEquals($largeString, (string) $body);
  93. $compressed = (string) $body;
  94. $this->assertTrue($body->uncompress());
  95. $this->assertEquals($largeString, (string) $body);
  96. $this->assertEquals($compressed, gzdeflate($largeString));
  97. $body = EntityBody::factory(fopen(__DIR__ . '/../TestData/compress_test', 'w'));
  98. $this->assertFalse($body->compress());
  99. unset($body);
  100. unlink(__DIR__ . '/../TestData/compress_test');
  101. }
  102. public function testDeterminesContentType()
  103. {
  104. // Test using a string/temp stream
  105. $body = EntityBody::factory('testing 123...testing 123');
  106. $this->assertNull($body->getContentType());
  107. // Use a local file
  108. $body = EntityBody::factory(fopen(__FILE__, 'r'));
  109. $this->assertContains('text/x-', $body->getContentType());
  110. }
  111. public function testCreatesMd5Checksum()
  112. {
  113. $body = EntityBody::factory('testing 123...testing 123');
  114. $this->assertEquals(md5('testing 123...testing 123'), $body->getContentMd5());
  115. $server = $this->getServer()->enqueue(
  116. "HTTP/1.1 200 OK" . "\r\n" .
  117. "Content-Length: 3" . "\r\n\r\n" .
  118. "abc"
  119. );
  120. $body = EntityBody::factory(fopen($this->getServer()->getUrl(), 'r'));
  121. $this->assertFalse($body->getContentMd5());
  122. }
  123. public function testSeeksToOriginalPosAfterMd5()
  124. {
  125. $body = EntityBody::factory('testing 123');
  126. $body->seek(4);
  127. $this->assertEquals(md5('testing 123'), $body->getContentMd5());
  128. $this->assertEquals(4, $body->ftell());
  129. $this->assertEquals('ing 123', $body->read(1000));
  130. }
  131. public function testGetTypeFormBodyFactoring()
  132. {
  133. $body = EntityBody::factory(array('key1' => 'val1', 'key2' => 'val2'));
  134. $this->assertEquals('key1=val1&key2=val2', (string) $body);
  135. }
  136. public function testAllowsCustomRewind()
  137. {
  138. $body = EntityBody::factory('foo');
  139. $rewound = false;
  140. $body->setRewindFunction(function ($body) use (&$rewound) {
  141. $rewound = true;
  142. return $body->seek(0);
  143. });
  144. $body->seek(2);
  145. $this->assertTrue($body->rewind());
  146. $this->assertTrue($rewound);
  147. }
  148. /**
  149. * @expectedException \Guzzle\Common\Exception\InvalidArgumentException
  150. */
  151. public function testCustomRewindFunctionMustBeCallable()
  152. {
  153. $body = EntityBody::factory();
  154. $body->setRewindFunction('foo');
  155. }
  156. }