PageRenderTime 52ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/integration/includes/http/MWHttpRequestTestCase.php

https://bitbucket.org/andersus/querytalogo
PHP | 245 lines | 193 code | 32 blank | 20 comment | 5 complexity | 8b3a0e525c6d64c67b537d6801f0a180 MD5 | raw file
Possible License(s): LGPL-3.0, MPL-2.0-no-copyleft-exception, JSON, MIT, CC0-1.0, BSD-3-Clause, Apache-2.0, BSD-2-Clause, LGPL-2.1, GPL-2.0
  1. <?php
  2. use Wikimedia\TestingAccessWrapper;
  3. class MWHttpRequestTestCase extends PHPUnit_Framework_TestCase {
  4. protected static $httpEngine;
  5. protected $oldHttpEngine;
  6. public function setUp() {
  7. parent::setUp();
  8. $this->oldHttpEngine = Http::$httpEngine;
  9. Http::$httpEngine = static::$httpEngine;
  10. try {
  11. $request = MWHttpRequest::factory( 'null:' );
  12. } catch ( DomainException $e ) {
  13. $this->markTestSkipped( static::$httpEngine . ' engine not supported' );
  14. }
  15. if ( static::$httpEngine === 'php' ) {
  16. $this->assertInstanceOf( PhpHttpRequest::class, $request );
  17. } else {
  18. $this->assertInstanceOf( CurlHttpRequest::class, $request );
  19. }
  20. }
  21. public function tearDown() {
  22. parent::tearDown();
  23. Http::$httpEngine = $this->oldHttpEngine;
  24. }
  25. // --------------------
  26. public function testIsRedirect() {
  27. $request = MWHttpRequest::factory( 'http://httpbin.org/get' );
  28. $status = $request->execute();
  29. $this->assertTrue( $status->isGood() );
  30. $this->assertFalse( $request->isRedirect() );
  31. $request = MWHttpRequest::factory( 'http://httpbin.org/redirect/1' );
  32. $status = $request->execute();
  33. $this->assertTrue( $status->isGood() );
  34. $this->assertTrue( $request->isRedirect() );
  35. }
  36. public function testgetFinalUrl() {
  37. $request = MWHttpRequest::factory( 'http://httpbin.org/redirect/3' );
  38. if ( !$request->canFollowRedirects() ) {
  39. $this->markTestSkipped( 'cannot follow redirects' );
  40. }
  41. $status = $request->execute();
  42. $this->assertTrue( $status->isGood() );
  43. $this->assertNotSame( 'http://httpbin.org/get', $request->getFinalUrl() );
  44. $request = MWHttpRequest::factory( 'http://httpbin.org/redirect/3', [ 'followRedirects'
  45. => true ] );
  46. $status = $request->execute();
  47. $this->assertTrue( $status->isGood() );
  48. $this->assertSame( 'http://httpbin.org/get', $request->getFinalUrl() );
  49. $this->assertResponseFieldValue( 'url', 'http://httpbin.org/get', $request );
  50. $request = MWHttpRequest::factory( 'http://httpbin.org/redirect/3', [ 'followRedirects'
  51. => true ] );
  52. $status = $request->execute();
  53. $this->assertTrue( $status->isGood() );
  54. $this->assertSame( 'http://httpbin.org/get', $request->getFinalUrl() );
  55. $this->assertResponseFieldValue( 'url', 'http://httpbin.org/get', $request );
  56. if ( static::$httpEngine === 'curl' ) {
  57. $this->markTestIncomplete( 'maxRedirects seems to be ignored by CurlHttpRequest' );
  58. return;
  59. }
  60. $request = MWHttpRequest::factory( 'http://httpbin.org/redirect/3', [ 'followRedirects'
  61. => true, 'maxRedirects' => 1 ] );
  62. $status = $request->execute();
  63. $this->assertTrue( $status->isGood() );
  64. $this->assertNotSame( 'http://httpbin.org/get', $request->getFinalUrl() );
  65. }
  66. public function testSetCookie() {
  67. $request = MWHttpRequest::factory( 'http://httpbin.org/cookies' );
  68. $request->setCookie( 'foo', 'bar' );
  69. $request->setCookie( 'foo2', 'bar2', [ 'domain' => 'example.com' ] );
  70. $status = $request->execute();
  71. $this->assertTrue( $status->isGood() );
  72. $this->assertResponseFieldValue( 'cookies', [ 'foo' => 'bar' ], $request );
  73. }
  74. public function testSetCookieJar() {
  75. $request = MWHttpRequest::factory( 'http://httpbin.org/cookies' );
  76. $cookieJar = new CookieJar();
  77. $cookieJar->setCookie( 'foo', 'bar', [ 'domain' => 'httpbin.org' ] );
  78. $cookieJar->setCookie( 'foo2', 'bar2', [ 'domain' => 'example.com' ] );
  79. $request->setCookieJar( $cookieJar );
  80. $status = $request->execute();
  81. $this->assertTrue( $status->isGood() );
  82. $this->assertResponseFieldValue( 'cookies', [ 'foo' => 'bar' ], $request );
  83. $request = MWHttpRequest::factory( 'http://httpbin.org/cookies/set?foo=bar' );
  84. $cookieJar = new CookieJar();
  85. $request->setCookieJar( $cookieJar );
  86. $status = $request->execute();
  87. $this->assertTrue( $status->isGood() );
  88. $this->assertHasCookie( 'foo', 'bar', $request->getCookieJar() );
  89. $this->markTestIncomplete( 'CookieJar does not handle deletion' );
  90. return;
  91. $request = MWHttpRequest::factory( 'http://httpbin.org/cookies/delete?foo' );
  92. $cookieJar = new CookieJar();
  93. $cookieJar->setCookie( 'foo', 'bar', [ 'domain' => 'httpbin.org' ] );
  94. $cookieJar->setCookie( 'foo2', 'bar2', [ 'domain' => 'httpbin.org' ] );
  95. $request->setCookieJar( $cookieJar );
  96. $status = $request->execute();
  97. $this->assertTrue( $status->isGood() );
  98. $this->assertNotHasCookie( 'foo', $request->getCookieJar() );
  99. $this->assertHasCookie( 'foo2', 'bar2', $request->getCookieJar() );
  100. }
  101. public function testGetResponseHeaders() {
  102. $request = MWHttpRequest::factory( 'http://httpbin.org/response-headers?Foo=bar' );
  103. $status = $request->execute();
  104. $this->assertTrue( $status->isGood() );
  105. $headers = array_change_key_case( $request->getResponseHeaders(), CASE_LOWER );
  106. $this->assertArrayHasKey( 'foo', $headers );
  107. $this->assertSame( $request->getResponseHeader( 'Foo' ), 'bar' );
  108. }
  109. public function testSetHeader() {
  110. $request = MWHttpRequest::factory( 'http://httpbin.org/headers' );
  111. $request->setHeader( 'Foo', 'bar' );
  112. $status = $request->execute();
  113. $this->assertTrue( $status->isGood() );
  114. $this->assertResponseFieldValue( [ 'headers', 'Foo' ], 'bar', $request );
  115. }
  116. public function testGetStatus() {
  117. $request = MWHttpRequest::factory( 'http://httpbin.org/status/418' );
  118. $status = $request->execute();
  119. $this->assertFalse( $status->isOK() );
  120. $this->assertSame( $request->getStatus(), 418 );
  121. }
  122. public function testSetUserAgent() {
  123. $request = MWHttpRequest::factory( 'http://httpbin.org/user-agent' );
  124. $request->setUserAgent( 'foo' );
  125. $status = $request->execute();
  126. $this->assertTrue( $status->isGood() );
  127. $this->assertResponseFieldValue( 'user-agent', 'foo', $request );
  128. }
  129. public function testSetData() {
  130. $request = MWHttpRequest::factory( 'http://httpbin.org/post', [ 'method' => 'POST' ] );
  131. $request->setData( [ 'foo' => 'bar', 'foo2' => 'bar2' ] );
  132. $status = $request->execute();
  133. $this->assertTrue( $status->isGood() );
  134. $this->assertResponseFieldValue( 'form', [ 'foo' => 'bar', 'foo2' => 'bar2' ], $request );
  135. }
  136. public function testSetCallback() {
  137. if ( static::$httpEngine === 'php' ) {
  138. $this->markTestIncomplete( 'PhpHttpRequest does not use setCallback()' );
  139. return;
  140. }
  141. $request = MWHttpRequest::factory( 'http://httpbin.org/ip' );
  142. $data = '';
  143. $request->setCallback( function ( $fh, $content ) use ( &$data ) {
  144. $data .= $content;
  145. return strlen( $content );
  146. } );
  147. $status = $request->execute();
  148. $this->assertTrue( $status->isGood() );
  149. $data = json_decode( $data, true );
  150. $this->assertInternalType( 'array', $data );
  151. $this->assertArrayHasKey( 'origin', $data );
  152. }
  153. public function testBasicAuthentication() {
  154. $request = MWHttpRequest::factory( 'http://httpbin.org/basic-auth/user/pass', [
  155. 'username' => 'user',
  156. 'password' => 'pass',
  157. ] );
  158. $status = $request->execute();
  159. $this->assertTrue( $status->isGood() );
  160. $this->assertResponseFieldValue( 'authenticated', true, $request );
  161. $request = MWHttpRequest::factory( 'http://httpbin.org/basic-auth/user/pass', [
  162. 'username' => 'user',
  163. 'password' => 'wrongpass',
  164. ] );
  165. $status = $request->execute();
  166. $this->assertFalse( $status->isOK() );
  167. $this->assertSame( 401, $request->getStatus() );
  168. }
  169. // --------------------
  170. /**
  171. * Verifies that the request was successful, returned valid JSON and the given field of that
  172. * JSON data is as expected.
  173. * @param string|string[] $key Path to the data in the response object
  174. * @param mixed $expectedValue
  175. * @param MWHttpRequest $response
  176. */
  177. protected function assertResponseFieldValue( $key, $expectedValue, MWHttpRequest $response ) {
  178. $this->assertSame( 200, $response->getStatus(), 'response status is not 200' );
  179. $data = json_decode( $response->getContent(), true );
  180. $this->assertInternalType( 'array', $data, 'response is not JSON' );
  181. $keyPath = '';
  182. foreach ( (array)$key as $keySegment ) {
  183. $keyPath .= ( $keyPath ? '.' : '' ) . $keySegment;
  184. $this->assertArrayHasKey( $keySegment, $data, $keyPath . ' not found' );
  185. $data = $data[$keySegment];
  186. }
  187. $this->assertSame( $expectedValue, $data );
  188. }
  189. /**
  190. * Asserts that the cookie jar has the given cookie with the given value.
  191. * @param string $expectedName Cookie name
  192. * @param string $expectedValue Cookie value
  193. * @param CookieJar $cookieJar
  194. */
  195. protected function assertHasCookie( $expectedName, $expectedValue, CookieJar $cookieJar ) {
  196. $cookieJar = TestingAccessWrapper::newFromObject( $cookieJar );
  197. $cookies = array_change_key_case( $cookieJar->cookie, CASE_LOWER );
  198. $this->assertArrayHasKey( strtolower( $expectedName ), $cookies );
  199. $cookie = TestingAccessWrapper::newFromObject(
  200. $cookies[strtolower( $expectedName )] );
  201. $this->assertSame( $expectedValue, $cookie->value );
  202. }
  203. /**
  204. * Asserts that the cookie jar does not have the given cookie.
  205. * @param string $name Cookie name
  206. * @param CookieJar $cookieJar
  207. */
  208. protected function assertNotHasCookie( $name, CookieJar $cookieJar ) {
  209. $cookieJar = TestingAccessWrapper::newFromObject( $cookieJar );
  210. $this->assertArrayNotHasKey( strtolower( $name ),
  211. array_change_key_case( $cookieJar->cookie, CASE_LOWER ) );
  212. }
  213. }