encoding_test.php 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. // $Id: encoding_test.php 1788 2008-04-27 11:01:59Z pp11 $
  3. require_once(dirname(__FILE__) . '/../autorun.php');
  4. require_once(dirname(__FILE__) . '/../url.php');
  5. require_once(dirname(__FILE__) . '/../socket.php');
  6. Mock::generate('SimpleSocket');
  7. class TestOfEncodedParts extends UnitTestCase {
  8. function testFormEncodedAsKeyEqualsValue() {
  9. $pair = new SimpleEncodedPair('a', 'A');
  10. $this->assertEqual($pair->asRequest(), 'a=A');
  11. }
  12. function testMimeEncodedAsHeadersAndContent() {
  13. $pair = new SimpleEncodedPair('a', 'A');
  14. $this->assertEqual(
  15. $pair->asMime(),
  16. "Content-Disposition: form-data; name=\"a\"\r\n\r\nA");
  17. }
  18. function testAttachmentEncodedAsHeadersWithDispositionAndContent() {
  19. $part = new SimpleAttachment('a', 'A', 'aaa.txt');
  20. $this->assertEqual(
  21. $part->asMime(),
  22. "Content-Disposition: form-data; name=\"a\"; filename=\"aaa.txt\"\r\n" .
  23. "Content-Type: text/plain\r\n\r\nA");
  24. }
  25. }
  26. class TestOfEncoding extends UnitTestCase {
  27. private $content_so_far;
  28. function write($content) {
  29. $this->content_so_far .= $content;
  30. }
  31. function clear() {
  32. $this->content_so_far = '';
  33. }
  34. function assertWritten($encoding, $content, $message = '%s') {
  35. $this->clear();
  36. $encoding->writeTo($this);
  37. $this->assertIdentical($this->content_so_far, $content, $message);
  38. }
  39. function testGetEmpty() {
  40. $encoding = new SimpleGetEncoding();
  41. $this->assertIdentical($encoding->getValue('a'), false);
  42. $this->assertIdentical($encoding->asUrlRequest(), '');
  43. }
  44. function testPostEmpty() {
  45. $encoding = new SimplePostEncoding();
  46. $this->assertIdentical($encoding->getValue('a'), false);
  47. $this->assertWritten($encoding, '');
  48. }
  49. function testPrefilled() {
  50. $encoding = new SimplePostEncoding(array('a' => 'aaa'));
  51. $this->assertIdentical($encoding->getValue('a'), 'aaa');
  52. $this->assertWritten($encoding, 'a=aaa');
  53. }
  54. function testPrefilledWithTwoLevels() {
  55. $query = array('a' => array('aa' => 'aaa'));
  56. $encoding = new SimplePostEncoding($query);
  57. $this->assertTrue($encoding->hasMoreThanOneLevel($query));
  58. $this->assertEqual($encoding->rewriteArrayWithMultipleLevels($query), array('a[aa]' => 'aaa'));
  59. $this->assertIdentical($encoding->getValue('a[aa]'), 'aaa');
  60. $this->assertWritten($encoding, 'a%5Baa%5D=aaa');
  61. }
  62. function testPrefilledWithThreeLevels() {
  63. $query = array('a' => array('aa' => array('aaa' => 'aaaa')));
  64. $encoding = new SimplePostEncoding($query);
  65. $this->assertTrue($encoding->hasMoreThanOneLevel($query));
  66. $this->assertEqual($encoding->rewriteArrayWithMultipleLevels($query), array('a[aa][aaa]' => 'aaaa'));
  67. $this->assertIdentical($encoding->getValue('a[aa][aaa]'), 'aaaa');
  68. $this->assertWritten($encoding, 'a%5Baa%5D%5Baaa%5D=aaaa');
  69. }
  70. function testPrefilledWithObject() {
  71. $encoding = new SimplePostEncoding(new SimpleEncoding(array('a' => 'aaa')));
  72. $this->assertIdentical($encoding->getValue('a'), 'aaa');
  73. $this->assertWritten($encoding, 'a=aaa');
  74. }
  75. function testMultiplePrefilled() {
  76. $query = array('a' => array('a1', 'a2'));
  77. $encoding = new SimplePostEncoding($query);
  78. $this->assertTrue($encoding->hasMoreThanOneLevel($query));
  79. $this->assertEqual($encoding->rewriteArrayWithMultipleLevels($query), array('a[0]' => 'a1', 'a[1]' => 'a2'));
  80. $this->assertIdentical($encoding->getValue('a[0]'), 'a1');
  81. $this->assertIdentical($encoding->getValue('a[1]'), 'a2');
  82. $this->assertWritten($encoding, 'a%5B0%5D=a1&a%5B1%5D=a2');
  83. }
  84. function testSingleParameter() {
  85. $encoding = new SimplePostEncoding();
  86. $encoding->add('a', 'Hello');
  87. $this->assertEqual($encoding->getValue('a'), 'Hello');
  88. $this->assertWritten($encoding, 'a=Hello');
  89. }
  90. function testFalseParameter() {
  91. $encoding = new SimplePostEncoding();
  92. $encoding->add('a', false);
  93. $this->assertEqual($encoding->getValue('a'), false);
  94. $this->assertWritten($encoding, '');
  95. }
  96. function testUrlEncoding() {
  97. $encoding = new SimplePostEncoding();
  98. $encoding->add('a', 'Hello there!');
  99. $this->assertWritten($encoding, 'a=Hello+there%21');
  100. }
  101. function testUrlEncodingOfKey() {
  102. $encoding = new SimplePostEncoding();
  103. $encoding->add('a!', 'Hello');
  104. $this->assertWritten($encoding, 'a%21=Hello');
  105. }
  106. function testMultipleParameter() {
  107. $encoding = new SimplePostEncoding();
  108. $encoding->add('a', 'Hello');
  109. $encoding->add('b', 'Goodbye');
  110. $this->assertWritten($encoding, 'a=Hello&b=Goodbye');
  111. }
  112. function testEmptyParameters() {
  113. $encoding = new SimplePostEncoding();
  114. $encoding->add('a', '');
  115. $encoding->add('b', '');
  116. $this->assertWritten($encoding, 'a=&b=');
  117. }
  118. function testRepeatedParameter() {
  119. $encoding = new SimplePostEncoding();
  120. $encoding->add('a', 'Hello');
  121. $encoding->add('a', 'Goodbye');
  122. $this->assertIdentical($encoding->getValue('a'), array('Hello', 'Goodbye'));
  123. $this->assertWritten($encoding, 'a=Hello&a=Goodbye');
  124. }
  125. function testAddingLists() {
  126. $encoding = new SimplePostEncoding();
  127. $encoding->add('a', array('Hello', 'Goodbye'));
  128. $this->assertIdentical($encoding->getValue('a'), array('Hello', 'Goodbye'));
  129. $this->assertWritten($encoding, 'a=Hello&a=Goodbye');
  130. }
  131. function testMergeInHash() {
  132. $encoding = new SimpleGetEncoding(array('a' => 'A1', 'b' => 'B'));
  133. $encoding->merge(array('a' => 'A2'));
  134. $this->assertIdentical($encoding->getValue('a'), array('A1', 'A2'));
  135. $this->assertIdentical($encoding->getValue('b'), 'B');
  136. }
  137. function testMergeInObject() {
  138. $encoding = new SimpleGetEncoding(array('a' => 'A1', 'b' => 'B'));
  139. $encoding->merge(new SimpleEncoding(array('a' => 'A2')));
  140. $this->assertIdentical($encoding->getValue('a'), array('A1', 'A2'));
  141. $this->assertIdentical($encoding->getValue('b'), 'B');
  142. }
  143. function testPrefilledMultipart() {
  144. $encoding = new SimpleMultipartEncoding(array('a' => 'aaa'), 'boundary');
  145. $this->assertIdentical($encoding->getValue('a'), 'aaa');
  146. $this->assertwritten($encoding,
  147. "--boundary\r\n" .
  148. "Content-Disposition: form-data; name=\"a\"\r\n" .
  149. "\r\n" .
  150. "aaa\r\n" .
  151. "--boundary--\r\n");
  152. }
  153. function testAttachment() {
  154. $encoding = new SimpleMultipartEncoding(array(), 'boundary');
  155. $encoding->attach('a', 'aaa', 'aaa.txt');
  156. $this->assertIdentical($encoding->getValue('a'), 'aaa.txt');
  157. $this->assertwritten($encoding,
  158. "--boundary\r\n" .
  159. "Content-Disposition: form-data; name=\"a\"; filename=\"aaa.txt\"\r\n" .
  160. "Content-Type: text/plain\r\n" .
  161. "\r\n" .
  162. "aaa\r\n" .
  163. "--boundary--\r\n");
  164. }
  165. }
  166. class TestOfFormHeaders extends UnitTestCase {
  167. function testEmptyEncodingWritesZeroContentLength() {
  168. $socket = new MockSimpleSocket();
  169. $socket->expectAt(0, 'write', array("Content-Length: 0\r\n"));
  170. $socket->expectAt(1, 'write', array("Content-Type: application/x-www-form-urlencoded\r\n"));
  171. $encoding = new SimplePostEncoding();
  172. $encoding->writeHeadersTo($socket);
  173. }
  174. function testEmptyMultipartEncodingWritesEndBoundaryContentLength() {
  175. $socket = new MockSimpleSocket();
  176. $socket->expectAt(0, 'write', array("Content-Length: 14\r\n"));
  177. $socket->expectAt(1, 'write', array("Content-Type: multipart/form-data, boundary=boundary\r\n"));
  178. $encoding = new SimpleMultipartEncoding(array(), 'boundary');
  179. $encoding->writeHeadersTo($socket);
  180. }
  181. }
  182. ?>