URLUtilTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\HTTP;
  4. class URLUtilTest extends \PHPUnit\Framework\TestCase
  5. {
  6. public function testEncodePath()
  7. {
  8. $str = '';
  9. for ($i = 0; $i < 128; ++$i) {
  10. $str .= chr($i);
  11. }
  12. $newStr = encodePath($str);
  13. $this->assertEquals(
  14. '%00%01%02%03%04%05%06%07%08%09%0a%0b%0c%0d%0e%0f'.
  15. '%10%11%12%13%14%15%16%17%18%19%1a%1b%1c%1d%1e%1f'.
  16. '%20%21%22%23%24%25%26%27()%2a%2b%2c-./'.
  17. '0123456789:%3b%3c%3d%3e%3f'.
  18. '@ABCDEFGHIJKLMNO'.
  19. 'PQRSTUVWXYZ%5b%5c%5d%5e_'.
  20. '%60abcdefghijklmno'.
  21. 'pqrstuvwxyz%7b%7c%7d~%7f',
  22. $newStr);
  23. $this->assertEquals($str, decodePath($newStr));
  24. }
  25. public function testEncodePathSegment()
  26. {
  27. $str = '';
  28. for ($i = 0; $i < 128; ++$i) {
  29. $str .= chr($i);
  30. }
  31. $newStr = encodePathSegment($str);
  32. // Note: almost exactly the same as the last test, with the
  33. // exception of the encoding of / (ascii code 2f)
  34. $this->assertEquals(
  35. '%00%01%02%03%04%05%06%07%08%09%0a%0b%0c%0d%0e%0f'.
  36. '%10%11%12%13%14%15%16%17%18%19%1a%1b%1c%1d%1e%1f'.
  37. '%20%21%22%23%24%25%26%27()%2a%2b%2c-.%2f'.
  38. '0123456789:%3b%3c%3d%3e%3f'.
  39. '@ABCDEFGHIJKLMNO'.
  40. 'PQRSTUVWXYZ%5b%5c%5d%5e_'.
  41. '%60abcdefghijklmno'.
  42. 'pqrstuvwxyz%7b%7c%7d~%7f',
  43. $newStr);
  44. $this->assertEquals($str, decodePathSegment($newStr));
  45. }
  46. public function testDecode()
  47. {
  48. $str = 'Hello%20Test+Test2.txt';
  49. $newStr = decodePath($str);
  50. $this->assertEquals('Hello Test+Test2.txt', $newStr);
  51. }
  52. /**
  53. * @depends testDecode
  54. */
  55. public function testDecodeUmlaut()
  56. {
  57. $str = 'Hello%C3%BC.txt';
  58. $newStr = decodePath($str);
  59. $this->assertEquals("Hello\xC3\xBC.txt", $newStr);
  60. }
  61. /**
  62. * @depends testDecode
  63. */
  64. public function testDecodeSlavicWords()
  65. {
  66. $words = [
  67. 'Ostroměr',
  68. 'Šventaragis',
  69. 'Świętopełk',
  70. 'Dušan',
  71. 'Živko',
  72. ];
  73. foreach ($words as $word) {
  74. $str = rawurlencode($word);
  75. $newStr = decodePath($str);
  76. $this->assertEquals($word, $newStr);
  77. }
  78. }
  79. /**
  80. * @depends testDecodeUmlaut
  81. */
  82. public function testDecodeUmlautLatin1()
  83. {
  84. $str = 'Hello%FC.txt';
  85. $newStr = decodePath($str);
  86. $this->assertEquals("Hello\xC3\xBC.txt", $newStr);
  87. }
  88. /**
  89. * This testcase was sent by a bug reporter.
  90. *
  91. * @depends testDecode
  92. */
  93. public function testDecodeAccentsWindows7()
  94. {
  95. $str = '/webdav/%C3%A0fo%C3%B3';
  96. $newStr = decodePath($str);
  97. $this->assertEquals(strtolower($str), encodePath($newStr));
  98. }
  99. }