IDNAEncoder.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. class RequestsTest_IDNAEncoder extends PHPUnit_Framework_TestCase {
  3. public static function specExamples() {
  4. return array(
  5. array(
  6. "\xe4\xbb\x96\xe4\xbb\xac\xe4\xb8\xba\xe4\xbb\x80\xe4\xb9\x88\xe4\xb8\x8d\xe8\xaf\xb4\xe4\xb8\xad\xe6\x96\x87",
  7. "xn--ihqwcrb4cv8a8dqg056pqjye"
  8. ),
  9. array(
  10. "\x33\xe5\xb9\xb4\x42\xe7\xb5\x84\xe9\x87\x91\xe5\x85\xab\xe5\x85\x88\xe7\x94\x9f",
  11. "xn--3B-ww4c5e180e575a65lsy2b",
  12. )
  13. );
  14. }
  15. /**
  16. * @dataProvider specExamples
  17. */
  18. public function testEncoding($data, $expected) {
  19. $result = Requests_IDNAEncoder::encode($data);
  20. $this->assertEquals($expected, $result);
  21. }
  22. /**
  23. * @expectedException Requests_Exception
  24. */
  25. public function testASCIITooLong() {
  26. $data = str_repeat("abcd", 20);
  27. $result = Requests_IDNAEncoder::encode($data);
  28. }
  29. /**
  30. * @expectedException Requests_Exception
  31. */
  32. public function testEncodedTooLong() {
  33. $data = str_repeat("\xe4\xbb\x96", 60);
  34. $result = Requests_IDNAEncoder::encode($data);
  35. }
  36. /**
  37. * @expectedException Requests_Exception
  38. */
  39. public function testAlreadyPrefixed() {
  40. $result = Requests_IDNAEncoder::encode("xn--\xe4\xbb\x96");
  41. }
  42. public function testASCIICharacter() {
  43. $result = Requests_IDNAEncoder::encode("a");
  44. $this->assertEquals('a', $result);
  45. }
  46. public function testTwoByteCharacter() {
  47. $result = Requests_IDNAEncoder::encode("\xc2\xb6"); // Pilcrow character
  48. $this->assertEquals('xn--tba', $result);
  49. }
  50. public function testThreeByteCharacter() {
  51. $result = Requests_IDNAEncoder::encode("\xe2\x82\xac"); // Euro symbol
  52. $this->assertEquals('xn--lzg', $result);
  53. }
  54. public function testFourByteCharacter() {
  55. $result = Requests_IDNAEncoder::encode("\xf0\xa4\xad\xa2"); // Chinese symbol?
  56. $this->assertEquals('xn--ww6j', $result);
  57. }
  58. /**
  59. * @expectedException Requests_Exception
  60. */
  61. public function testFiveByteCharacter() {
  62. $result = Requests_IDNAEncoder::encode("\xfb\xb6\xb6\xb6\xb6");
  63. }
  64. /**
  65. * @expectedException Requests_Exception
  66. */
  67. public function testSixByteCharacter() {
  68. $result = Requests_IDNAEncoder::encode("\xfd\xb6\xb6\xb6\xb6\xb6");
  69. }
  70. /**
  71. * @expectedException Requests_Exception
  72. */
  73. public function testInvalidASCIICharacterWithMultibyte() {
  74. $result = Requests_IDNAEncoder::encode("\0\xc2\xb6");
  75. }
  76. /**
  77. * @expectedException Requests_Exception
  78. */
  79. public function testUnfinishedMultibyte() {
  80. $result = Requests_IDNAEncoder::encode("\xc2");
  81. }
  82. /**
  83. * @expectedException Requests_Exception
  84. */
  85. public function testPartialMultibyte() {
  86. $result = Requests_IDNAEncoder::encode("\xc2\xc2\xb6");
  87. }
  88. }