UtilsTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace GuzzleHttp\Test;
  3. use GuzzleHttp;
  4. use GuzzleHttp\Utils;
  5. use PHPUnit\Framework\TestCase;
  6. class UtilsTest extends TestCase
  7. {
  8. public function noBodyProvider()
  9. {
  10. return [['get'], ['head'], ['delete']];
  11. }
  12. public function typeProvider()
  13. {
  14. return [
  15. ['foo', 'string(3) "foo"'],
  16. [true, 'bool(true)'],
  17. [false, 'bool(false)'],
  18. [10, 'int(10)'],
  19. [1.0, 'float(1)'],
  20. [new StrClass(), 'object(GuzzleHttp\Test\StrClass)'],
  21. [['foo'], 'array(1)'],
  22. ];
  23. }
  24. /**
  25. * @dataProvider typeProvider
  26. */
  27. public function testDescribesType($input, $output)
  28. {
  29. /**
  30. * Output may not match if Xdebug is loaded and overloading var_dump().
  31. *
  32. * @see https://xdebug.org/docs/display#overload_var_dump
  33. */
  34. if (extension_loaded('xdebug')) {
  35. $originalOverload = ini_get('xdebug.overload_var_dump');
  36. ini_set('xdebug.overload_var_dump', 0);
  37. }
  38. try {
  39. self::assertSame($output, Utils::describeType($input));
  40. self::assertSame($output, GuzzleHttp\describe_type($input));
  41. } finally {
  42. if (extension_loaded('xdebug')) {
  43. ini_set('xdebug.overload_var_dump', $originalOverload);
  44. }
  45. }
  46. }
  47. public function testParsesHeadersFromLines()
  48. {
  49. $lines = [
  50. 'Foo: bar',
  51. 'Foo: baz',
  52. 'Abc: 123',
  53. 'Def: a, b',
  54. ];
  55. $expected = [
  56. 'Foo' => ['bar', 'baz'],
  57. 'Abc' => ['123'],
  58. 'Def' => ['a, b'],
  59. ];
  60. self::assertSame($expected, Utils::headersFromLines($lines));
  61. self::assertSame($expected, GuzzleHttp\headers_from_lines($lines));
  62. }
  63. public function testParsesHeadersFromLinesWithMultipleLines()
  64. {
  65. $lines = ['Foo: bar', 'Foo: baz', 'Foo: 123'];
  66. $expected = ['Foo' => ['bar', 'baz', '123']];
  67. self::assertSame($expected, Utils::headersFromLines($lines));
  68. self::assertSame($expected, GuzzleHttp\headers_from_lines($lines));
  69. }
  70. public function testChooseHandler()
  71. {
  72. self::assertIsCallable(Utils::chooseHandler());
  73. self::assertIsCallable(GuzzleHttp\choose_handler());
  74. }
  75. public function testDefaultUserAgent()
  76. {
  77. self::assertIsString(Utils::defaultUserAgent());
  78. self::assertIsString(GuzzleHttp\default_user_agent());
  79. }
  80. public function testReturnsDebugResource()
  81. {
  82. self::assertIsResource(Utils::debugResource());
  83. self::assertIsResource(GuzzleHttp\debug_resource());
  84. }
  85. public function testProvidesDefaultCaBundler()
  86. {
  87. self::assertFileExists(Utils::defaultCaBundle());
  88. self::assertFileExists(GuzzleHttp\default_ca_bundle());
  89. }
  90. public function testNormalizeHeaderKeys()
  91. {
  92. $input = ['HelLo' => 'foo', 'WORld' => 'bar'];
  93. $expected = ['hello' => 'HelLo', 'world' => 'WORld'];
  94. self::assertSame($expected, Utils::normalizeHeaderKeys($input));
  95. self::assertSame($expected, GuzzleHttp\normalize_header_keys($input));
  96. }
  97. public function noProxyProvider()
  98. {
  99. return [
  100. ['mit.edu', ['.mit.edu'], false],
  101. ['foo.mit.edu', ['.mit.edu'], true],
  102. ['foo.mit.edu:123', ['.mit.edu'], true],
  103. ['mit.edu', ['mit.edu'], true],
  104. ['mit.edu', ['baz', 'mit.edu'], true],
  105. ['mit.edu', ['', '', 'mit.edu'], true],
  106. ['mit.edu', ['baz', '*'], true],
  107. ];
  108. }
  109. /**
  110. * @dataProvider noproxyProvider
  111. */
  112. public function testChecksNoProxyList($host, $list, $result)
  113. {
  114. self::assertSame($result, Utils::isHostInNoProxy($host, $list));
  115. self::assertSame($result, \GuzzleHttp\is_host_in_noproxy($host, $list));
  116. }
  117. public function testEnsuresNoProxyCheckHostIsSet()
  118. {
  119. $this->expectException(\InvalidArgumentException::class);
  120. Utils::isHostInNoProxy('', []);
  121. }
  122. public function testEnsuresNoProxyCheckHostIsSetLegacy()
  123. {
  124. $this->expectException(\InvalidArgumentException::class);
  125. \GuzzleHttp\is_host_in_noproxy('', []);
  126. }
  127. public function testEncodesJson()
  128. {
  129. self::assertSame('true', Utils::jsonEncode(true));
  130. self::assertSame('true', \GuzzleHttp\json_encode(true));
  131. }
  132. public function testEncodesJsonAndThrowsOnError()
  133. {
  134. $this->expectException(\InvalidArgumentException::class);
  135. Utils::jsonEncode("\x99");
  136. }
  137. public function testEncodesJsonAndThrowsOnErrorLegacy()
  138. {
  139. $this->expectException(\InvalidArgumentException::class);
  140. \GuzzleHttp\json_encode("\x99");
  141. }
  142. public function testDecodesJson()
  143. {
  144. self::assertTrue(Utils::jsonDecode('true'));
  145. self::assertTrue(\GuzzleHttp\json_decode('true'));
  146. }
  147. public function testDecodesJsonAndThrowsOnError()
  148. {
  149. $this->expectException(\InvalidArgumentException::class);
  150. Utils::jsonDecode('{{]]');
  151. }
  152. public function testDecodesJsonAndThrowsOnErrorLegacy()
  153. {
  154. $this->expectException(\InvalidArgumentException::class);
  155. \GuzzleHttp\json_decode('{{]]');
  156. }
  157. }
  158. final class StrClass
  159. {
  160. public function __toString()
  161. {
  162. return 'foo';
  163. }
  164. }