RequestExceptionTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. namespace GuzzleHttp\Tests\Exception;
  3. use GuzzleHttp\Exception\RequestException;
  4. use GuzzleHttp\Psr7\Request;
  5. use GuzzleHttp\Psr7\Response;
  6. use GuzzleHttp\Psr7\Stream;
  7. use PHPUnit\Framework\TestCase;
  8. /**
  9. * @covers \GuzzleHttp\Exception\RequestException
  10. */
  11. class RequestExceptionTest extends TestCase
  12. {
  13. public function testHasRequestAndResponse()
  14. {
  15. $req = new Request('GET', '/');
  16. $res = new Response(200);
  17. $e = new RequestException('foo', $req, $res);
  18. $this->assertSame($req, $e->getRequest());
  19. $this->assertSame($res, $e->getResponse());
  20. $this->assertTrue($e->hasResponse());
  21. $this->assertSame('foo', $e->getMessage());
  22. }
  23. public function testCreatesGenerateException()
  24. {
  25. $e = RequestException::create(new Request('GET', '/'));
  26. $this->assertSame('Error completing request', $e->getMessage());
  27. $this->assertInstanceOf('GuzzleHttp\Exception\RequestException', $e);
  28. }
  29. public function testCreatesClientErrorResponseException()
  30. {
  31. $e = RequestException::create(new Request('GET', '/'), new Response(400));
  32. $this->assertContains(
  33. 'GET /',
  34. $e->getMessage()
  35. );
  36. $this->assertContains(
  37. '400 Bad Request',
  38. $e->getMessage()
  39. );
  40. $this->assertInstanceOf('GuzzleHttp\Exception\ClientException', $e);
  41. }
  42. public function testCreatesServerErrorResponseException()
  43. {
  44. $e = RequestException::create(new Request('GET', '/'), new Response(500));
  45. $this->assertContains(
  46. 'GET /',
  47. $e->getMessage()
  48. );
  49. $this->assertContains(
  50. '500 Internal Server Error',
  51. $e->getMessage()
  52. );
  53. $this->assertInstanceOf('GuzzleHttp\Exception\ServerException', $e);
  54. }
  55. public function testCreatesGenericErrorResponseException()
  56. {
  57. $e = RequestException::create(new Request('GET', '/'), new Response(600));
  58. $this->assertContains(
  59. 'GET /',
  60. $e->getMessage()
  61. );
  62. $this->assertContains(
  63. '600 ',
  64. $e->getMessage()
  65. );
  66. $this->assertInstanceOf('GuzzleHttp\Exception\RequestException', $e);
  67. }
  68. public function dataPrintableResponses()
  69. {
  70. return [
  71. ['You broke the test!'],
  72. ['<h1>zlomený zkouška</h1>'],
  73. ['{"tester": "Philépe Gonzalez"}'],
  74. ["<xml>\n\t<text>Your friendly test</text>\n</xml>"],
  75. ['document.body.write("here comes a test");'],
  76. ["body:before {\n\tcontent: 'test style';\n}"],
  77. ];
  78. }
  79. /**
  80. * @dataProvider dataPrintableResponses
  81. */
  82. public function testCreatesExceptionWithPrintableBodySummary($content)
  83. {
  84. $response = new Response(
  85. 500,
  86. [],
  87. $content
  88. );
  89. $e = RequestException::create(new Request('GET', '/'), $response);
  90. $this->assertContains(
  91. $content,
  92. $e->getMessage()
  93. );
  94. $this->assertInstanceOf('GuzzleHttp\Exception\RequestException', $e);
  95. }
  96. public function testCreatesExceptionWithTruncatedSummary()
  97. {
  98. $content = str_repeat('+', 121);
  99. $response = new Response(500, [], $content);
  100. $e = RequestException::create(new Request('GET', '/'), $response);
  101. $expected = str_repeat('+', 120) . ' (truncated...)';
  102. $this->assertContains($expected, $e->getMessage());
  103. }
  104. public function testExceptionMessageIgnoresEmptyBody()
  105. {
  106. $e = RequestException::create(new Request('GET', '/'), new Response(500));
  107. $this->assertStringEndsWith('response', $e->getMessage());
  108. }
  109. public function testCreatesExceptionWithoutPrintableBody()
  110. {
  111. $response = new Response(
  112. 500,
  113. ['Content-Type' => 'image/gif'],
  114. $content = base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7') // 1x1 gif
  115. );
  116. $e = RequestException::create(new Request('GET', '/'), $response);
  117. $this->assertNotContains(
  118. $content,
  119. $e->getMessage()
  120. );
  121. $this->assertInstanceOf('GuzzleHttp\Exception\RequestException', $e);
  122. }
  123. public function testHasStatusCodeAsExceptionCode()
  124. {
  125. $e = RequestException::create(new Request('GET', '/'), new Response(442));
  126. $this->assertSame(442, $e->getCode());
  127. }
  128. public function testWrapsRequestExceptions()
  129. {
  130. $e = new \Exception('foo');
  131. $r = new Request('GET', 'http://www.oo.com');
  132. $ex = RequestException::wrapException($r, $e);
  133. $this->assertInstanceOf('GuzzleHttp\Exception\RequestException', $ex);
  134. $this->assertSame($e, $ex->getPrevious());
  135. }
  136. public function testDoesNotWrapExistingRequestExceptions()
  137. {
  138. $r = new Request('GET', 'http://www.oo.com');
  139. $e = new RequestException('foo', $r);
  140. $e2 = RequestException::wrapException($r, $e);
  141. $this->assertSame($e, $e2);
  142. }
  143. public function testCanProvideHandlerContext()
  144. {
  145. $r = new Request('GET', 'http://www.oo.com');
  146. $e = new RequestException('foo', $r, null, null, ['bar' => 'baz']);
  147. $this->assertSame(['bar' => 'baz'], $e->getHandlerContext());
  148. }
  149. public function testObfuscateUrlWithUsername()
  150. {
  151. $r = new Request('GET', 'http://username@www.oo.com');
  152. $e = RequestException::create($r, new Response(500));
  153. $this->assertContains('http://username@www.oo.com', $e->getMessage());
  154. }
  155. public function testObfuscateUrlWithUsernameAndPassword()
  156. {
  157. $r = new Request('GET', 'http://user:password@www.oo.com');
  158. $e = RequestException::create($r, new Response(500));
  159. $this->assertContains('http://user:***@www.oo.com', $e->getMessage());
  160. }
  161. public function testGetResponseBodySummaryOfNonReadableStream()
  162. {
  163. $this->assertNull(RequestException::getResponseBodySummary(new Response(500, [], new ReadSeekOnlyStream())));
  164. }
  165. }
  166. final class ReadSeekOnlyStream extends Stream
  167. {
  168. public function __construct()
  169. {
  170. parent::__construct(fopen('php://memory', 'wb'));
  171. }
  172. public function isSeekable()
  173. {
  174. return true;
  175. }
  176. public function isReadable()
  177. {
  178. return false;
  179. }
  180. }