* Fix a bug to keep consecutive HTTP requests from sharing results
authorMark A. Hershberger <mah@users.mediawiki.org>
Fri, 9 Apr 2010 05:25:40 +0000 (05:25 +0000)
committerMark A. Hershberger <mah@users.mediawiki.org>
Fri, 9 Apr 2010 05:25:40 +0000 (05:25 +0000)
* Update Login API

includes/HttpFunctions.php
includes/WebRequest.php
maintenance/tests/ApiTest.php

index 8355ec1..5bf7050 100644 (file)
@@ -211,6 +211,15 @@ class HttpRequest {
                return $this->content;
        }
 
+       /**
+        * Set the parameters of the request
+        * @param $params array
+        * @todo overload the args param
+        */
+       public function setData($args) {
+               $this->postData = $args;
+       }
+
        /**
         * Take care of setting up the proxy
         * (override in subclass)
@@ -296,6 +305,8 @@ class HttpRequest {
        public function execute() {
                global $wgTitle;
 
+               $this->content = "";
+
                if( strtoupper($this->method) == "HEAD" ) {
                        $this->headersOnly = true;
                }
index 903ea28..834f2b1 100644 (file)
@@ -774,7 +774,7 @@ class FauxRequest extends WebRequest {
        }
 
        public function setSessionData( $key, $data ) {
-               $this->notImplemented( __METHOD__ );
+               $this->session[$key] = $data;
        }
 
        public function isPathInfoBad() {
index 4b838ce..bb8e37c 100644 (file)
@@ -112,8 +112,24 @@ class ApiTest extends ApiSetup {
                $sxe = simplexml_load_string( $resp );
                $this->assertNotType( "bool", $sxe );
                $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
-               $a = $sxe->login[0]->attributes()->result;
-               $this->assertEquals( ' result="WrongPass"', $a->asXML() );
+               $a = $sxe->login[0]->attributes()->result[0];
+               $this->assertEquals( ' result="NeedToken"', $a->asXML() );
+
+               $token = (string)$sxe->login[0]->attributes()->token;
+
+               $resp = Http::post( self::$apiUrl . "?action=login&format=xml",
+                                                  array( "postData" => array(
+                                                                       "lgtoken" => $token,
+                                                                       "lgname" => self::$userName,
+                                                                       "lgpassword" => "bad" ) ) );
+
+
+               $sxe = simplexml_load_string( $resp );
+               $this->assertNotType( "bool", $sxe );
+               $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
+               $a = $sxe->login[0]->attributes()->result[0];
+
+               $this->assertEquals( ' result="NeedToken"', $a->asXML() );
        }
 
        function testApiLoginGoodPass() {
@@ -125,15 +141,34 @@ class ApiTest extends ApiSetup {
                        $this->markTestIncomplete('This test needs $wgServerName and $wgServer to '.
                                                                          'be set in LocalSettings.php');
                }
-               $resp = Http::post( self::$apiUrl . "?action=login&format=xml",
-                                                  array( "postData" => array(
-                                                                        "lgname" => self::$userName,
-                                                                        "lgpassword" => self::$passWord ) ) );
+               $req = HttpRequest::factory(self::$apiUrl . "?action=login&format=xml",
+                       array( "method" => "POST",
+                               "postData" => array(
+                               "lgname" => self::$userName,
+                               "lgpassword" => self::$passWord ) ) );
+               $req->execute();
+
                libxml_use_internal_errors( true );
-               $sxe = simplexml_load_string( $resp );
+               $sxe = simplexml_load_string( $req->getContent() );
                $this->assertNotType( "bool", $sxe );
                $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
-               $a = $sxe->login[0]->attributes()->result;
+
+               $a = $sxe->login[0]->attributes()->result[0];
+               $this->assertEquals( ' result="NeedToken"', $a->asXML() );
+               $token = (string)$sxe->login[0]->attributes()->token;
+
+               $req->setData(array(
+                       "lgtoken" => $token,
+                       "lgname" => self::$userName,
+                       "lgpassword" => self::$passWord ) );
+               $req->execute();
+
+               $sxe = simplexml_load_string( $req->getContent() );
+
+               $this->assertNotType( "bool", $sxe );
+               $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
+               $a = $sxe->login[0]->attributes()->result[0];
+
                $this->assertEquals( ' result="Success"', $a->asXML() );
        }
 
@@ -146,11 +181,28 @@ class ApiTest extends ApiSetup {
                        $this->markTestIncomplete('This test needs $wgServerName and $wgServer to '.
                                                                          'be set in LocalSettings.php');
                }
-               $req = HttpRequest::factory( self::$apiUrl . "?action=login&format=xml",
-                                                                        array( "method" => "POST",
-                                                                                       "postData" => array( "lgname" => self::$userName,
-                                                                                                                                "lgpassword" => self::$passWord ) ) );
+               $req = HttpRequest::factory(self::$apiUrl . "?action=login&format=xml",
+                       array( "method" => "POST",
+                               "postData" => array(
+                               "lgname" => self::$userName,
+                               "lgpassword" => self::$passWord ) ) );
                $req->execute();
+
+               libxml_use_internal_errors( true );
+               $sxe = simplexml_load_string( $req->getContent() );
+               $this->assertNotType( "bool", $sxe );
+               $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
+
+               $a = $sxe->login[0]->attributes()->result[0];
+               $this->assertEquals( ' result="NeedToken"', $a->asXML() );
+               $token = (string)$sxe->login[0]->attributes()->token;
+
+               $req->setData(array(
+                       "lgtoken" => $token,
+                       "lgname" => self::$userName,
+                       "lgpassword" => self::$passWord ) );
+               $req->execute();
+
                $cj = $req->getCookieJar();
                $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . self::$userName . '; .*Token=/',
                                                         $cj->serializeToHttpRequest( $wgScriptPath, $wgServerName ) );