MultipartStreamTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace RingCentral\Tests;
  3. use RingCentral\Psr7;
  4. use RingCentral\Psr7\MultipartStream;
  5. class MultipartStreamTest extends \PHPUnit_Framework_TestCase
  6. {
  7. public function testCreatesDefaultBoundary()
  8. {
  9. $b = new MultipartStream();
  10. $this->assertNotEmpty($b->getBoundary());
  11. }
  12. public function testCanProvideBoundary()
  13. {
  14. $b = new MultipartStream(array(), 'foo');
  15. $this->assertEquals('foo', $b->getBoundary());
  16. }
  17. public function testIsNotWritable()
  18. {
  19. $b = new MultipartStream();
  20. $this->assertFalse($b->isWritable());
  21. }
  22. public function testCanCreateEmptyStream()
  23. {
  24. $b = new MultipartStream();
  25. $boundary = $b->getBoundary();
  26. $this->assertSame("--{$boundary}--\r\n", $b->getContents());
  27. $this->assertSame(strlen($boundary) + 6, $b->getSize());
  28. }
  29. /**
  30. * @expectedException \InvalidArgumentException
  31. */
  32. public function testValidatesFilesArrayElement()
  33. {
  34. new MultipartStream(array(array('foo' => 'bar')));
  35. }
  36. /**
  37. * @expectedException \InvalidArgumentException
  38. */
  39. public function testEnsuresFileHasName()
  40. {
  41. new MultipartStream(array(array('contents' => 'bar')));
  42. }
  43. public function testSerializesFields()
  44. {
  45. $b = new MultipartStream(array(
  46. array(
  47. 'name' => 'foo',
  48. 'contents' => 'bar'
  49. ),
  50. array(
  51. 'name' => 'baz',
  52. 'contents' => 'bam'
  53. )
  54. ), 'boundary');
  55. $this->assertEquals(
  56. "--boundary\r\nContent-Disposition: form-data; name=\"foo\"\r\nContent-Length: 3\r\n\r\n"
  57. . "bar\r\n--boundary\r\nContent-Disposition: form-data; name=\"baz\"\r\nContent-Length: 3"
  58. . "\r\n\r\nbam\r\n--boundary--\r\n", (string) $b);
  59. }
  60. public function testSerializesFiles()
  61. {
  62. $f1 = Psr7\FnStream::decorate(Psr7\stream_for('foo'), array(
  63. 'getMetadata' => function () {
  64. return '/foo/bar.txt';
  65. }
  66. ));
  67. $f2 = Psr7\FnStream::decorate(Psr7\stream_for('baz'), array(
  68. 'getMetadata' => function () {
  69. return '/foo/baz.jpg';
  70. }
  71. ));
  72. $f3 = Psr7\FnStream::decorate(Psr7\stream_for('bar'), array(
  73. 'getMetadata' => function () {
  74. return '/foo/bar.gif';
  75. }
  76. ));
  77. $b = new MultipartStream(array(
  78. array(
  79. 'name' => 'foo',
  80. 'contents' => $f1
  81. ),
  82. array(
  83. 'name' => 'qux',
  84. 'contents' => $f2
  85. ),
  86. array(
  87. 'name' => 'qux',
  88. 'contents' => $f3
  89. ),
  90. ), 'boundary');
  91. $expected = <<<EOT
  92. --boundary
  93. Content-Disposition: form-data; name="foo"; filename="bar.txt"
  94. Content-Length: 3
  95. Content-Type: text/plain
  96. foo
  97. --boundary
  98. Content-Disposition: form-data; name="qux"; filename="baz.jpg"
  99. Content-Length: 3
  100. Content-Type: image/jpeg
  101. baz
  102. --boundary
  103. Content-Disposition: form-data; name="qux"; filename="bar.gif"
  104. Content-Length: 3
  105. Content-Type: image/gif
  106. bar
  107. --boundary--
  108. EOT;
  109. $this->assertEquals($expected, str_replace("\r", '', $b));
  110. }
  111. public function testSerializesFilesWithCustomHeaders()
  112. {
  113. $f1 = Psr7\FnStream::decorate(Psr7\stream_for('foo'), array(
  114. 'getMetadata' => function () {
  115. return '/foo/bar.txt';
  116. }
  117. ));
  118. $b = new MultipartStream(array(
  119. array(
  120. 'name' => 'foo',
  121. 'contents' => $f1,
  122. 'headers' => array(
  123. 'x-foo' => 'bar',
  124. 'content-disposition' => 'custom'
  125. )
  126. )
  127. ), 'boundary');
  128. $expected = <<<EOT
  129. --boundary
  130. x-foo: bar
  131. content-disposition: custom
  132. Content-Length: 3
  133. Content-Type: text/plain
  134. foo
  135. --boundary--
  136. EOT;
  137. $this->assertEquals($expected, str_replace("\r", '', $b));
  138. }
  139. public function testSerializesFilesWithCustomHeadersAndMultipleValues()
  140. {
  141. $f1 = Psr7\FnStream::decorate(Psr7\stream_for('foo'), array(
  142. 'getMetadata' => function () {
  143. return '/foo/bar.txt';
  144. }
  145. ));
  146. $f2 = Psr7\FnStream::decorate(Psr7\stream_for('baz'), array(
  147. 'getMetadata' => function () {
  148. return '/foo/baz.jpg';
  149. }
  150. ));
  151. $b = new MultipartStream(array(
  152. array(
  153. 'name' => 'foo',
  154. 'contents' => $f1,
  155. 'headers' => array(
  156. 'x-foo' => 'bar',
  157. 'content-disposition' => 'custom'
  158. )
  159. ),
  160. array(
  161. 'name' => 'foo',
  162. 'contents' => $f2,
  163. 'headers' => array('cOntenT-Type' => 'custom'),
  164. )
  165. ), 'boundary');
  166. $expected = <<<EOT
  167. --boundary
  168. x-foo: bar
  169. content-disposition: custom
  170. Content-Length: 3
  171. Content-Type: text/plain
  172. foo
  173. --boundary
  174. cOntenT-Type: custom
  175. Content-Disposition: form-data; name="foo"; filename="baz.jpg"
  176. Content-Length: 3
  177. baz
  178. --boundary--
  179. EOT;
  180. $this->assertEquals($expected, str_replace("\r", '', $b));
  181. }
  182. }