3 use Wikimedia\TestingAccessWrapper;
16 }
catch ( DomainException
$e ) {
17 $this->markTestSkipped( static::$httpEngine .
' engine not supported' );
20 if ( static::$httpEngine ===
'php' ) {
37 $this->assertTrue(
$status->isGood() );
38 $this->assertFalse(
$request->isRedirect() );
42 $this->assertTrue(
$status->isGood() );
43 $this->assertTrue(
$request->isRedirect() );
48 if ( !
$request->canFollowRedirects() ) {
49 $this->markTestSkipped(
'cannot follow redirects' );
52 $this->assertTrue(
$status->isGood() );
53 $this->assertNotSame(
'http://httpbin.org/get',
$request->getFinalUrl() );
58 $this->assertTrue(
$status->isGood() );
59 $this->assertSame(
'http://httpbin.org/get',
$request->getFinalUrl() );
65 $this->assertTrue(
$status->isGood() );
66 $this->assertSame(
'http://httpbin.org/get',
$request->getFinalUrl() );
69 if ( static::$httpEngine ===
'curl' ) {
70 $this->markTestIncomplete(
'maxRedirects seems to be ignored by CurlHttpRequest' );
75 =>
true,
'maxRedirects' => 1 ] );
77 $this->assertTrue(
$status->isGood() );
78 $this->assertNotSame(
'http://httpbin.org/get',
$request->getFinalUrl() );
84 $request->setCookie(
'foo2',
'bar2', [
'domain' =>
'example.com' ] );
86 $this->assertTrue(
$status->isGood() );
93 $cookieJar->setCookie(
'foo',
'bar', [
'domain' =>
'httpbin.org' ] );
94 $cookieJar->setCookie(
'foo2',
'bar2', [
'domain' =>
'example.com' ] );
95 $request->setCookieJar( $cookieJar );
97 $this->assertTrue(
$status->isGood() );
102 $request->setCookieJar( $cookieJar );
104 $this->assertTrue(
$status->isGood() );
107 $this->markTestIncomplete(
'CookieJar does not handle deletion' );
112 $cookieJar->setCookie(
'foo',
'bar', [
'domain' =>
'httpbin.org' ] );
113 $cookieJar->setCookie(
'foo2',
'bar2', [
'domain' =>
'httpbin.org' ] );
114 $request->setCookieJar( $cookieJar );
116 $this->assertTrue(
$status->isGood() );
124 $this->assertTrue(
$status->isGood() );
125 $headers = array_change_key_case(
$request->getResponseHeaders(), CASE_LOWER );
126 $this->assertArrayHasKey(
'foo', $headers );
127 $this->assertSame(
$request->getResponseHeader(
'Foo' ),
'bar' );
132 $request->setHeader(
'Foo',
'bar' );
134 $this->assertTrue(
$status->isGood() );
141 $this->assertFalse(
$status->isOK() );
142 $this->assertSame(
$request->getStatus(), 418 );
149 $this->assertTrue(
$status->isGood() );
155 $request->setData( [
'foo' =>
'bar',
'foo2' =>
'bar2' ] );
157 $this->assertTrue(
$status->isGood() );
162 if ( static::$httpEngine ===
'php' ) {
163 $this->markTestIncomplete(
'PhpHttpRequest does not use setCallback()' );
174 $this->assertTrue(
$status->isGood() );
175 $data = json_decode( $data,
true );
176 $this->assertInternalType(
'array', $data );
177 $this->assertArrayHasKey(
'origin', $data );
182 'username' =>
'user',
183 'password' =>
'pass',
186 $this->assertTrue(
$status->isGood() );
190 'username' =>
'user',
191 'password' =>
'wrongpass',
194 $this->assertFalse(
$status->isOK() );
195 $this->assertSame( 401,
$request->getStatus() );
208 $this->assertSame( 200,
$response->getStatus(),
'response status is not 200' );
210 $this->assertInternalType(
'array', $data,
'response is not JSON' );
212 foreach ( (
array)$key
as $keySegment ) {
213 $keyPath .= ( $keyPath ?
'.' :
'' ) . $keySegment;
214 $this->assertArrayHasKey( $keySegment, $data, $keyPath .
' not found' );
215 $data = $data[$keySegment];
217 $this->assertSame( $expectedValue, $data );
227 $cookieJar = TestingAccessWrapper::newFromObject( $cookieJar );
228 $cookies = array_change_key_case( $cookieJar->cookie, CASE_LOWER );
229 $this->assertArrayHasKey( strtolower( $expectedName ), $cookies );
230 $cookie = TestingAccessWrapper::newFromObject(
231 $cookies[strtolower( $expectedName )] );
232 $this->assertSame( $expectedValue, $cookie->value );
241 $cookieJar = TestingAccessWrapper::newFromObject( $cookieJar );
242 $this->assertArrayNotHasKey( strtolower(
$name ),
243 array_change_key_case( $cookieJar->cookie, CASE_LOWER ) );