From bd3faaeb15cd3af6eaed06c46cd4182dce702413 Mon Sep 17 00:00:00 2001 From: "Mark A. Hershberger" Date: Fri, 9 Apr 2010 05:25:40 +0000 Subject: [PATCH] * Fix a bug to keep consecutive HTTP requests from sharing results * Update Login API --- includes/HttpFunctions.php | 11 +++++ includes/WebRequest.php | 2 +- maintenance/tests/ApiTest.php | 76 +++++++++++++++++++++++++++++------ 3 files changed, 76 insertions(+), 13 deletions(-) diff --git a/includes/HttpFunctions.php b/includes/HttpFunctions.php index 8355ec188a..5bf7050a16 100644 --- a/includes/HttpFunctions.php +++ b/includes/HttpFunctions.php @@ -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; } diff --git a/includes/WebRequest.php b/includes/WebRequest.php index 903ea28f25..834f2b1e77 100644 --- a/includes/WebRequest.php +++ b/includes/WebRequest.php @@ -774,7 +774,7 @@ class FauxRequest extends WebRequest { } public function setSessionData( $key, $data ) { - $this->notImplemented( __METHOD__ ); + $this->session[$key] = $data; } public function isPathInfoBad() { diff --git a/maintenance/tests/ApiTest.php b/maintenance/tests/ApiTest.php index 4b838ce73d..bb8e37c1ce 100644 --- a/maintenance/tests/ApiTest.php +++ b/maintenance/tests/ApiTest.php @@ -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 ) ); -- 2.20.1