3 class MWHttpRequestTestCase
extends PHPUnit_Framework_TestCase
{
4 protected static $httpEngine;
5 protected $oldHttpEngine;
7 public function setUp() {
9 $this->oldHttpEngine
= Http
::$httpEngine;
10 Http
::$httpEngine = static::$httpEngine;
13 $request = MWHttpRequest
::factory( 'null:' );
14 } catch ( DomainException
$e ) {
15 $this->markTestSkipped( static::$httpEngine . ' engine not supported' );
18 if ( static::$httpEngine === 'php' ) {
19 $this->assertInstanceOf( PhpHttpRequest
::class, $request );
21 $this->assertInstanceOf( CurlHttpRequest
::class, $request );
25 public function tearDown() {
27 Http
::$httpEngine = $this->oldHttpEngine
;
30 // --------------------
32 public function testIsRedirect() {
33 $request = MWHttpRequest
::factory( 'http://httpbin.org/get' );
34 $status = $request->execute();
35 $this->assertTrue( $status->isGood() );
36 $this->assertFalse( $request->isRedirect() );
38 $request = MWHttpRequest
::factory( 'http://httpbin.org/redirect/1' );
39 $status = $request->execute();
40 $this->assertTrue( $status->isGood() );
41 $this->assertTrue( $request->isRedirect() );
44 public function testgetFinalUrl() {
45 $request = MWHttpRequest
::factory( 'http://httpbin.org/redirect/3' );
46 if ( !$request->canFollowRedirects() ) {
47 $this->markTestSkipped( 'cannot follow redirects' );
49 $status = $request->execute();
50 $this->assertTrue( $status->isGood() );
51 $this->assertNotSame( 'http://httpbin.org/get', $request->getFinalUrl() );
53 $request = MWHttpRequest
::factory( 'http://httpbin.org/redirect/3', [ 'followRedirects'
55 $status = $request->execute();
56 $this->assertTrue( $status->isGood() );
57 $this->assertSame( 'http://httpbin.org/get', $request->getFinalUrl() );
58 $this->assertResponseFieldValue( 'url', 'http://httpbin.org/get', $request );
60 $request = MWHttpRequest
::factory( 'http://httpbin.org/redirect/3', [ 'followRedirects'
62 $status = $request->execute();
63 $this->assertTrue( $status->isGood() );
64 $this->assertSame( 'http://httpbin.org/get', $request->getFinalUrl() );
65 $this->assertResponseFieldValue( 'url', 'http://httpbin.org/get', $request );
67 if ( static::$httpEngine === 'curl' ) {
68 $this->markTestIncomplete( 'maxRedirects seems to be ignored by CurlHttpRequest' );
72 $request = MWHttpRequest
::factory( 'http://httpbin.org/redirect/3', [ 'followRedirects'
73 => true, 'maxRedirects' => 1 ] );
74 $status = $request->execute();
75 $this->assertTrue( $status->isGood() );
76 $this->assertNotSame( 'http://httpbin.org/get', $request->getFinalUrl() );
79 public function testSetCookie() {
80 $request = MWHttpRequest
::factory( 'http://httpbin.org/cookies' );
81 $request->setCookie( 'foo', 'bar' );
82 $request->setCookie( 'foo2', 'bar2', [ 'domain' => 'example.com' ] );
83 $status = $request->execute();
84 $this->assertTrue( $status->isGood() );
85 $this->assertResponseFieldValue( 'cookies', [ 'foo' => 'bar' ], $request );
88 public function testSetCookieJar() {
89 $request = MWHttpRequest
::factory( 'http://httpbin.org/cookies' );
90 $cookieJar = new CookieJar();
91 $cookieJar->setCookie( 'foo', 'bar', [ 'domain' => 'httpbin.org' ] );
92 $cookieJar->setCookie( 'foo2', 'bar2', [ 'domain' => 'example.com' ] );
93 $request->setCookieJar( $cookieJar );
94 $status = $request->execute();
95 $this->assertTrue( $status->isGood() );
96 $this->assertResponseFieldValue( 'cookies', [ 'foo' => 'bar' ], $request );
98 $request = MWHttpRequest
::factory( 'http://httpbin.org/cookies/set?foo=bar' );
99 $cookieJar = new CookieJar();
100 $request->setCookieJar( $cookieJar );
101 $status = $request->execute();
102 $this->assertTrue( $status->isGood() );
103 $this->assertHasCookie( 'foo', 'bar', $request->getCookieJar() );
105 $this->markTestIncomplete( 'CookieJar does not handle deletion' );
108 $request = MWHttpRequest
::factory( 'http://httpbin.org/cookies/delete?foo' );
109 $cookieJar = new CookieJar();
110 $cookieJar->setCookie( 'foo', 'bar', [ 'domain' => 'httpbin.org' ] );
111 $cookieJar->setCookie( 'foo2', 'bar2', [ 'domain' => 'httpbin.org' ] );
112 $request->setCookieJar( $cookieJar );
113 $status = $request->execute();
114 $this->assertTrue( $status->isGood() );
115 $this->assertNotHasCookie( 'foo', $request->getCookieJar() );
116 $this->assertHasCookie( 'foo2', 'bar2', $request->getCookieJar() );
119 public function testGetResponseHeaders() {
120 $request = MWHttpRequest
::factory( 'http://httpbin.org/response-headers?Foo=bar' );
121 $status = $request->execute();
122 $this->assertTrue( $status->isGood() );
123 $headers = array_change_key_case( $request->getResponseHeaders(), CASE_LOWER
);
124 $this->assertArrayHasKey( 'foo', $headers );
125 $this->assertSame( $request->getResponseHeader( 'Foo' ), 'bar' );
128 public function testSetHeader() {
129 $request = MWHttpRequest
::factory( 'http://httpbin.org/headers' );
130 $request->setHeader( 'Foo', 'bar' );
131 $status = $request->execute();
132 $this->assertTrue( $status->isGood() );
133 $this->assertResponseFieldValue( [ 'headers', 'Foo' ], 'bar', $request );
136 public function testGetStatus() {
137 $request = MWHttpRequest
::factory( 'http://httpbin.org/status/418' );
138 $status = $request->execute();
139 $this->assertFalse( $status->isOK() );
140 $this->assertSame( $request->getStatus(), 418 );
143 public function testSetUserAgent() {
144 $request = MWHttpRequest
::factory( 'http://httpbin.org/user-agent' );
145 $request->setUserAgent( 'foo' );
146 $status = $request->execute();
147 $this->assertTrue( $status->isGood() );
148 $this->assertResponseFieldValue( 'user-agent', 'foo', $request );
151 public function testSetData() {
152 $request = MWHttpRequest
::factory( 'http://httpbin.org/post', [ 'method' => 'POST' ] );
153 $request->setData( [ 'foo' => 'bar', 'foo2' => 'bar2' ] );
154 $status = $request->execute();
155 $this->assertTrue( $status->isGood() );
156 $this->assertResponseFieldValue( 'form', [ 'foo' => 'bar', 'foo2' => 'bar2' ], $request );
159 public function testSetCallback() {
160 if ( static::$httpEngine === 'php' ) {
161 $this->markTestIncomplete( 'PhpHttpRequest does not use setCallback()' );
165 $request = MWHttpRequest
::factory( 'http://httpbin.org/ip' );
167 $request->setCallback( function ( $fh, $content ) use ( &$data ) {
169 return strlen( $content );
171 $status = $request->execute();
172 $this->assertTrue( $status->isGood() );
173 $data = json_decode( $data, true );
174 $this->assertInternalType( 'array', $data );
175 $this->assertArrayHasKey( 'origin', $data );
178 // --------------------
180 protected function assertResponseFieldValue( $key, $expectedValue, MWHttpRequest
$response ) {
181 $this->assertSame( 200, $response->getStatus(), 'response status is not 200' );
182 $data = json_decode( $response->getContent(), true );
183 $this->assertInternalType( 'array', $data, 'response is not JSON' );
185 foreach ( (array)$key as $keySegment ) {
186 $keyPath .= ( $keyPath ?
'.' : '' ) . $keySegment;
187 $this->assertArrayHasKey( $keySegment, $data, $keyPath . ' not found' );
188 $data = $data[$keySegment];
190 $this->assertSame( $expectedValue, $data );
193 protected function assertHasCookie( $expectedName, $expectedValue, CookieJar
$cookieJar ) {
194 $cookieJar = TestingAccessWrapper
::newFromObject( $cookieJar );
195 $cookies = array_change_key_case( $cookieJar->cookie
, CASE_LOWER
);
196 $this->assertArrayHasKey( strtolower( $expectedName ), $cookies );
197 $cookie = TestingAccessWrapper
::newFromObject(
198 $cookies[strtolower( $expectedName )] );
199 $this->assertSame( $expectedValue, $cookie->value
);
202 protected function assertNotHasCookie( $name, CookieJar
$cookieJar ) {
203 $cookieJar = TestingAccessWrapper
::newFromObject( $cookieJar );
204 $this->assertArrayNotHasKey( strtolower( $name ),
205 array_change_key_case( $cookieJar->cookie
, CASE_LOWER
) );