summaryrefslogtreecommitdiff
path: root/www/wiki/tests/integration/includes/http/MWHttpRequestTestCase.php
blob: 262eb35080282a7873c2acfc0766e75ca6e5c129 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php

use Wikimedia\TestingAccessWrapper;

abstract class MWHttpRequestTestCase extends PHPUnit\Framework\TestCase {
	protected static $httpEngine;
	protected $oldHttpEngine;

	public function setUp() {
		parent::setUp();
		$this->oldHttpEngine = Http::$httpEngine;
		Http::$httpEngine = static::$httpEngine;

		try {
			$request = MWHttpRequest::factory( 'null:' );
		} catch ( DomainException $e ) {
			$this->markTestSkipped( static::$httpEngine . ' engine not supported' );
		}

		if ( static::$httpEngine === 'php' ) {
			$this->assertInstanceOf( PhpHttpRequest::class, $request );
		} else {
			$this->assertInstanceOf( CurlHttpRequest::class, $request );
		}
	}

	public function tearDown() {
		parent::tearDown();
		Http::$httpEngine = $this->oldHttpEngine;
	}

	// --------------------

	public function testIsRedirect() {
		$request = MWHttpRequest::factory( 'http://httpbin.org/get' );
		$status = $request->execute();
		$this->assertTrue( $status->isGood() );
		$this->assertFalse( $request->isRedirect() );

		$request = MWHttpRequest::factory( 'http://httpbin.org/redirect/1' );
		$status = $request->execute();
		$this->assertTrue( $status->isGood() );
		$this->assertTrue( $request->isRedirect() );
	}

	public function testgetFinalUrl() {
		$request = MWHttpRequest::factory( 'http://httpbin.org/redirect/3' );
		if ( !$request->canFollowRedirects() ) {
			$this->markTestSkipped( 'cannot follow redirects' );
		}
		$status = $request->execute();
		$this->assertTrue( $status->isGood() );
		$this->assertNotSame( 'http://httpbin.org/get', $request->getFinalUrl() );

		$request = MWHttpRequest::factory( 'http://httpbin.org/redirect/3', [ 'followRedirects'
			=> true ] );
		$status = $request->execute();
		$this->assertTrue( $status->isGood() );
		$this->assertSame( 'http://httpbin.org/get', $request->getFinalUrl() );
		$this->assertResponseFieldValue( 'url', 'http://httpbin.org/get', $request );

		$request = MWHttpRequest::factory( 'http://httpbin.org/redirect/3', [ 'followRedirects'
		=> true ] );
		$status = $request->execute();
		$this->assertTrue( $status->isGood() );
		$this->assertSame( 'http://httpbin.org/get', $request->getFinalUrl() );
		$this->assertResponseFieldValue( 'url', 'http://httpbin.org/get', $request );

		if ( static::$httpEngine === 'curl' ) {
			$this->markTestIncomplete( 'maxRedirects seems to be ignored by CurlHttpRequest' );
			return;
		}

		$request = MWHttpRequest::factory( 'http://httpbin.org/redirect/3', [ 'followRedirects'
		=> true, 'maxRedirects' => 1 ] );
		$status = $request->execute();
		$this->assertTrue( $status->isGood() );
		$this->assertNotSame( 'http://httpbin.org/get', $request->getFinalUrl() );
	}

	public function testSetCookie() {
		$request = MWHttpRequest::factory( 'http://httpbin.org/cookies' );
		$request->setCookie( 'foo', 'bar' );
		$request->setCookie( 'foo2', 'bar2', [ 'domain' => 'example.com' ] );
		$status = $request->execute();
		$this->assertTrue( $status->isGood() );
		$this->assertResponseFieldValue( 'cookies', [ 'foo' => 'bar' ], $request );
	}

	public function testSetCookieJar() {
		$request = MWHttpRequest::factory( 'http://httpbin.org/cookies' );
		$cookieJar = new CookieJar();
		$cookieJar->setCookie( 'foo', 'bar', [ 'domain' => 'httpbin.org' ] );
		$cookieJar->setCookie( 'foo2', 'bar2', [ 'domain' => 'example.com' ] );
		$request->setCookieJar( $cookieJar );
		$status = $request->execute();
		$this->assertTrue( $status->isGood() );
		$this->assertResponseFieldValue( 'cookies', [ 'foo' => 'bar' ], $request );

		$request = MWHttpRequest::factory( 'http://httpbin.org/cookies/set?foo=bar' );
		$cookieJar = new CookieJar();
		$request->setCookieJar( $cookieJar );
		$status = $request->execute();
		$this->assertTrue( $status->isGood() );
		$this->assertHasCookie( 'foo', 'bar', $request->getCookieJar() );

		$this->markTestIncomplete( 'CookieJar does not handle deletion' );
		return;

		$request = MWHttpRequest::factory( 'http://httpbin.org/cookies/delete?foo' );
		$cookieJar = new CookieJar();
		$cookieJar->setCookie( 'foo', 'bar', [ 'domain' => 'httpbin.org' ] );
		$cookieJar->setCookie( 'foo2', 'bar2', [ 'domain' => 'httpbin.org' ] );
		$request->setCookieJar( $cookieJar );
		$status = $request->execute();
		$this->assertTrue( $status->isGood() );
		$this->assertNotHasCookie( 'foo', $request->getCookieJar() );
		$this->assertHasCookie( 'foo2', 'bar2', $request->getCookieJar() );
	}

	public function testGetResponseHeaders() {
		$request = MWHttpRequest::factory( 'http://httpbin.org/response-headers?Foo=bar' );
		$status = $request->execute();
		$this->assertTrue( $status->isGood() );
		$headers = array_change_key_case( $request->getResponseHeaders(), CASE_LOWER );
		$this->assertArrayHasKey( 'foo', $headers );
		$this->assertSame( $request->getResponseHeader( 'Foo' ), 'bar' );
	}

	public function testSetHeader() {
		$request = MWHttpRequest::factory( 'http://httpbin.org/headers' );
		$request->setHeader( 'Foo', 'bar' );
		$status = $request->execute();
		$this->assertTrue( $status->isGood() );
		$this->assertResponseFieldValue( [ 'headers', 'Foo' ], 'bar', $request );
	}

	public function testGetStatus() {
		$request = MWHttpRequest::factory( 'http://httpbin.org/status/418' );
		$status = $request->execute();
		$this->assertFalse( $status->isOK() );
		$this->assertSame( $request->getStatus(), 418 );
	}

	public function testSetUserAgent() {
		$request = MWHttpRequest::factory( 'http://httpbin.org/user-agent' );
		$request->setUserAgent( 'foo' );
		$status = $request->execute();
		$this->assertTrue( $status->isGood() );
		$this->assertResponseFieldValue( 'user-agent', 'foo', $request );
	}

	public function testSetData() {
		$request = MWHttpRequest::factory( 'http://httpbin.org/post', [ 'method' => 'POST' ] );
		$request->setData( [ 'foo' => 'bar', 'foo2' => 'bar2' ] );
		$status = $request->execute();
		$this->assertTrue( $status->isGood() );
		$this->assertResponseFieldValue( 'form', [ 'foo' => 'bar', 'foo2' => 'bar2' ], $request );
	}

	public function testSetCallback() {
		if ( static::$httpEngine === 'php' ) {
			$this->markTestIncomplete( 'PhpHttpRequest does not use setCallback()' );
			return;
		}

		$request = MWHttpRequest::factory( 'http://httpbin.org/ip' );
		$data = '';
		$request->setCallback( function ( $fh, $content ) use ( &$data ) {
			$data .= $content;
			return strlen( $content );
		} );
		$status = $request->execute();
		$this->assertTrue( $status->isGood() );
		$data = json_decode( $data, true );
		$this->assertInternalType( 'array', $data );
		$this->assertArrayHasKey( 'origin', $data );
	}

	public function testBasicAuthentication() {
		$request = MWHttpRequest::factory( 'http://httpbin.org/basic-auth/user/pass', [
			'username' => 'user',
			'password' => 'pass',
		] );
		$status = $request->execute();
		$this->assertTrue( $status->isGood() );
		$this->assertResponseFieldValue( 'authenticated', true, $request );

		$request = MWHttpRequest::factory( 'http://httpbin.org/basic-auth/user/pass', [
			'username' => 'user',
			'password' => 'wrongpass',
		] );
		$status = $request->execute();
		$this->assertFalse( $status->isOK() );
		$this->assertSame( 401, $request->getStatus() );
	}

	public function testFactoryDefaults() {
		$request = MWHttpRequest::factory( 'http://acme.test' );
		$this->assertInstanceOf( MWHttpRequest::class, $request );
	}

	// --------------------

	/**
	 * Verifies that the request was successful, returned valid JSON and the given field of that
	 * JSON data is as expected.
	 * @param string|string[] $key Path to the data in the response object
	 * @param mixed $expectedValue
	 * @param MWHttpRequest $response
	 */
	protected function assertResponseFieldValue( $key, $expectedValue, MWHttpRequest $response ) {
		$this->assertSame( 200, $response->getStatus(), 'response status is not 200' );
		$data = json_decode( $response->getContent(), true );
		$this->assertInternalType( 'array', $data, 'response is not JSON' );
		$keyPath = '';
		foreach ( (array)$key as $keySegment ) {
			$keyPath .= ( $keyPath ? '.' : '' ) . $keySegment;
			$this->assertArrayHasKey( $keySegment, $data, $keyPath . ' not found' );
			$data = $data[$keySegment];
		}
		$this->assertSame( $expectedValue, $data );
	}

	/**
	 * Asserts that the cookie jar has the given cookie with the given value.
	 * @param string $expectedName Cookie name
	 * @param string $expectedValue Cookie value
	 * @param CookieJar $cookieJar
	 */
	protected function assertHasCookie( $expectedName, $expectedValue, CookieJar $cookieJar ) {
		$cookieJar = TestingAccessWrapper::newFromObject( $cookieJar );
		$cookies = array_change_key_case( $cookieJar->cookie, CASE_LOWER );
		$this->assertArrayHasKey( strtolower( $expectedName ), $cookies );
		$cookie = TestingAccessWrapper::newFromObject(
			$cookies[strtolower( $expectedName )] );
		$this->assertSame( $expectedValue, $cookie->value );
	}

	/**
	 * Asserts that the cookie jar does not have the given cookie.
	 * @param string $name Cookie name
	 * @param CookieJar $cookieJar
	 */
	protected function assertNotHasCookie( $name, CookieJar $cookieJar ) {
		$cookieJar = TestingAccessWrapper::newFromObject( $cookieJar );
		$this->assertArrayNotHasKey( strtolower( $name ),
			array_change_key_case( $cookieJar->cookie, CASE_LOWER ) );
	}

}