Encoding.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. class RequestsTests_Encoding extends PHPUnit_Framework_TestCase {
  3. protected static function mapData($type, $data) {
  4. $real_data = array();
  5. foreach ($data as $value) {
  6. $key = $type . ': ' . $value[0];
  7. $real_data[$key] = $value;
  8. }
  9. return $real_data;
  10. }
  11. public static function gzipData() {
  12. return array(
  13. array(
  14. 'foobar',
  15. "\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\x4b\xcb\xcf\x4f\x4a"
  16. . "\x2c\x02\x00\x95\x1f\xf6\x9e\x06\x00\x00\x00",
  17. ),
  18. array(
  19. 'Requests for PHP',
  20. "\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\x0b\x4a\x2d\x2c\x4d"
  21. . "\x2d\x2e\x29\x56\x48\xcb\x2f\x52\x08\xf0\x08\x00\x00\x58\x35"
  22. . "\x18\x17\x10\x00\x00\x00",
  23. ),
  24. );
  25. }
  26. public static function deflateData() {
  27. return array(
  28. array(
  29. 'foobar',
  30. "\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\x78\x9c\x4b\xcb\xcf"
  31. . "\x4f\x4a\x2c\x02\x00\x08\xab\x02\x7a"
  32. ),
  33. array(
  34. 'Requests for PHP',
  35. "\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\x78\x9c\x0b\x4a\x2d"
  36. . "\x2c\x4d\x2d\x2e\x29\x56\x48\xcb\x2f\x52\x08\xf0\x08\x00\x00"
  37. . "\x34\x68\x05\xcc"
  38. )
  39. );
  40. }
  41. public static function deflateWithoutHeadersData() {
  42. return array(
  43. array(
  44. 'foobar',
  45. "\x78\x9c\x4b\xcb\xcf\x4f\x4a\x2c\x02\x00\x08\xab\x02\x7a"
  46. ),
  47. array(
  48. 'Requests for PHP',
  49. "\x78\x9c\x0b\x4a\x2d\x2c\x4d\x2d\x2e\x29\x56\x48\xcb\x2f\x52"
  50. . "\x08\xf0\x08\x00\x00\x34\x68\x05\xcc"
  51. )
  52. );
  53. }
  54. public static function encodedData() {
  55. $datasets = array();
  56. $datasets['gzip'] = self::gzipData();
  57. $datasets['deflate'] = self::deflateData();
  58. $datasets['deflate without zlib headers'] = self::deflateWithoutHeadersData();
  59. $data = array();
  60. foreach ($datasets as $key => $set) {
  61. $real_set = self::mapData($key, $set);
  62. $data = array_merge($data, $real_set);
  63. }
  64. return $data;
  65. }
  66. /**
  67. * @dataProvider encodedData
  68. */
  69. public function testDecompress($original, $encoded) {
  70. $decoded = Requests::decompress($encoded);
  71. $this->assertEquals($original, $decoded);
  72. }
  73. /**
  74. * @dataProvider encodedData
  75. */
  76. public function testCompatibleInflate($original, $encoded) {
  77. $decoded = Requests::compatible_gzinflate($encoded);
  78. $this->assertEquals($original, $decoded);
  79. }
  80. protected function bin2hex($field) {
  81. $field = bin2hex($field);
  82. $field = chunk_split($field,2,"\\x");
  83. $field = "\\x" . substr($field,0,-2);
  84. return $field;
  85. }
  86. }