Merge "registration: Only allow one extension to set a specific config setting"
[lhc/web/wiklou.git] / tests / integration / includes / http / MWHttpRequestTestCase.php
index fb52de9..81473df 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 
+use Wikimedia\TestingAccessWrapper;
+
 class MWHttpRequestTestCase extends PHPUnit_Framework_TestCase {
        protected static $httpEngine;
        protected $oldHttpEngine;
@@ -175,8 +177,33 @@ class MWHttpRequestTestCase extends PHPUnit_Framework_TestCase {
                $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() );
+       }
+
        // --------------------
 
+       /**
+        * 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 );
@@ -190,6 +217,12 @@ class MWHttpRequestTestCase extends PHPUnit_Framework_TestCase {
                $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 );
@@ -199,10 +232,14 @@ class MWHttpRequestTestCase extends PHPUnit_Framework_TestCase {
                $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 ) );
        }
 }
-