“make tap” spawns a new php instance for each test file.
Found some problems with date/time parsing on older 32bit php. I
forgot the year 2999 isn't a valid epoch date.
* Parse the cookies in the response headers and store them in the cookie jar.
*/
protected function parseCookies() {
+ if( !$this->cookieJar ) {
+ $this->cookieJar = new CookieJar;
+ }
if( isset( $this->respHeaders['set-cookie'] ) ) {
- if( !$this->cookieJar ) {
- $this->cookieJar = new CookieJar;
- }
$url = parse_url( $this->getFinalUrl() );
foreach( $this->respHeaders['set-cookie'] as $cookie ) {
$this->cookieJar->parseCookieResponseHeader( $cookie, $url['host'] );
$this->path = "/";
}
if( isset( $attr['domain'] ) ) {
- $this->domain = $attr['domain'];
+ $this->domain = self::parseCookieDomain( $attr['domain'] );
} else {
throw new MWException("You must specify a domain.");
}
}
+ public static function parseCookieDomain( $domain ) {
+ /* If domain is given, it has to contain at least two dots */
+ if ( strrpos( $domain, '.' ) === false
+ || strrpos( $domain, '.' ) === strpos( $domain, '.' ) ) {
+ return;
+ }
+ if ( substr( $domain, 0, 1 ) === '.' ) {
+ $domain = substr( $domain, 1 );
+ }
+
+ return $domain;
+ }
+
/**
* Serialize the cookie jar into a format useful for HTTP Request headers.
* @param $path string the path that will be used. Required.
if( !isset( $attr['domain'] ) ) {
$attr['domain'] = $domain;
} else {
- /* If domain is given, it has to contain at least two dots */
- if ( strrpos( $attr['domain'], '.' ) === false
- || strrpos( $attr['domain'], '.' ) === strpos( $attr['domain'], '.' ) ) {
- return;
- }
- if ( substr( $attr['domain'], 0, 1 ) === '.' ) {
- $attr['domain'] = substr( $attr['domain'], 1 );
- }
if ( strlen( $attr['domain'] ) < strlen( $domain )
&& substr_compare( $domain, $attr['domain'], -strlen( $attr['domain'] ),
strlen( $attr['domain'] ), TRUE ) != 0 ) {
--- /dev/null
+<?php
+
+abstract class ApiSetup extends PHPUnit_Framework_TestCase {
+ protected static $userName;
+ protected static $passWord;
+ protected static $user;
+ protected static $apiUrl;
+
+ function setup() {
+ global $wgServerName, $wgServer, $wgContLang, $wgAuth, $wgScriptPath,
+ $wgScriptExtension, $wgMemc;
+
+ if($wgServerName == "localhost" || $wgServer == "http://localhost") {
+ $this->markTestIncomplete('This test needs $wgServerName and $wgServer to '.
+ 'be set in LocalSettings.php');
+ }
+ self::$apiUrl = $wgServer.$wgScriptPath."/api".$wgScriptExtension;
+
+ $wgMemc = new FakeMemCachedClient;
+ $wgContLang = Language::factory( 'en' );
+ $wgAuth = new StubObject( 'wgAuth', 'AuthPlugin' );
+ self::setupUser();
+ }
+
+ static function setupUser() {
+ if ( self::$user == NULL ) {
+ self::$userName = "Useruser";
+ self::$passWord = User::randomPassword();
+
+ self::$user = User::newFromName(self::$userName);
+ if ( !self::$user->getID() ) {
+ self::$user = User::createNew(self::$userName, array(
+ "password" => self::$passWord,
+ "email" => "test@example.com",
+ "real_name" => "Test User"));
+ } else {
+ self::$user->setPassword(self::$passWord);
+ }
+ self::$user->saveSettings();
+ }
+ }
+}
--- /dev/null
+<?php
+
+require_once( "ApiSetup.php" );
+
+class ApiTest extends ApiSetup {
+
+ function setup() {
+ parent::setup();
+ }
+
+ function testApi() {
+ /* Haven't thought about test ordering yet -- but this depends on HttpTest.php */
+ $resp = Http::get( self::$apiUrl . "?format=xml" );
+
+ libxml_use_internal_errors( true );
+ $sxe = simplexml_load_string( $resp );
+ $this->assertNotType( "bool", $sxe );
+ $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
+ }
+
+ function testApiLoginNoName() {
+ $resp = Http::post( self::$apiUrl . "?action=login&format=xml",
+ array( "postData" => array(
+ "lgname" => "",
+ "lgpassword" => self::$passWord ) ) );
+ libxml_use_internal_errors( true );
+ $sxe = simplexml_load_string( $resp );
+ $this->assertNotType( "bool", $sxe );
+ $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
+ $a = $sxe->login[0]->attributes()->result;
+ $this->assertEquals( ' result="NoName"', $a->asXML() );
+ }
+
+ function testApiLoginBadPass() {
+ $resp = Http::post( self::$apiUrl . "?action=login&format=xml",
+ array( "postData" => array(
+ "lgname" => self::$userName,
+ "lgpassword" => "bad" ) ) );
+ libxml_use_internal_errors( true );
+ $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() );
+ }
+
+ function testApiLoginGoodPass() {
+ $resp = Http::post( self::$apiUrl . "?action=login&format=xml",
+ array( "postData" => array(
+ "lgname" => self::$userName,
+ "lgpassword" => self::$passWord ) ) );
+ libxml_use_internal_errors( true );
+ $sxe = simplexml_load_string( $resp );
+ $this->assertNotType( "bool", $sxe );
+ $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
+ $a = $sxe->login[0]->attributes()->result;
+ $this->assertEquals( ' result="Success"', $a->asXML() );
+ }
+
+ function testApiGotCookie() {
+ global $wgScriptPath, $wgServerName;
+
+ $req = HttpRequest::factory( self::$apiUrl . "?action=login&format=xml",
+ array( "method" => "POST",
+ "postData" => array(
+ "lgname" => self::$userName,
+ "lgpassword" => self::$passWord ) ) );
+ $req->execute();
+ $cj = $req->getCookieJar();
+ $this->markTestIncomplete("Need to make sure cookie/domain handling is correct");
+ $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . self::$userName . '; .*Token=/',
+ $cj->serializeToHttpRequest( $wgScriptPath, $wgServerName ) );
+ }
+}
"path" => "/path/",
) );
- $this->assertFalse($c->canServeDomain("example.com"));
+ $this->assertTrue($c->canServeDomain("example.com"));
$this->assertFalse($c->canServeDomain("www.example.net"));
$this->assertTrue($c->canServeDomain("www.example.com"));
array(
"domain" => ".example.com",
"path" => "/path/",
- "expires" => "January 1, 1990",
+ "expires" => "-1 day",
) );
$this->assertFalse($c->isUnExpired());
$this->assertEquals("", $c->serializeToHttpRequest("/path/", "www.example.com"));
array(
"domain" => ".example.com",
"path" => "/path/",
- "expires" => "January 1, 2999",
+ "expires" => "+1 day",
) );
$this->assertTrue($c->isUnExpired());
$this->assertEquals("name=value", $c->serializeToHttpRequest("/path/", "www.example.com"));
-
-
}
function testCookieJarSetCookie() {
array(
"domain" => ".example.net",
"path" => "/path/",
- "expires" => "January 1, 1999",
+ "expires" => "-1 day",
) );
$this->assertEquals("name4=value", $cj->serializeToHttpRequest("/path/", "www.example.net"));
array(
"domain" => ".example.net",
"path" => "/path/",
- "expires" => "January 1, 2999",
+ "expires" => "+1 day",
) );
$this->assertEquals("name4=value; name5=value", $cj->serializeToHttpRequest("/path/", "www.example.net"));
array(
"domain" => ".example.net",
"path" => "/path/",
- "expires" => "January 1, 1999",
+ "expires" => "-1 day",
) );
$this->assertEquals("name5=value", $cj->serializeToHttpRequest("/path/", "www.example.net"));
}
function testParseResponseHeader() {
$cj = new CookieJar;
- $h[] = "Set-Cookie: name4=value; domain=.example.com; path=/; expires=Mon, 09-Dec-2999 13:46:00 GMT";
+ $h[] = "Set-Cookie: name4=value; domain=.example.com; path=/; expires=Mon, 09-Dec-2029 13:46:00 GMT";
$cj->parseCookieResponseHeader( $h[0], "www.example.com" );
$this->assertEquals("name4=value", $cj->serializeToHttpRequest("/", "www.example.com"));
- $h[] = "name4=value2; domain=.example.com; path=/path/; expires=Mon, 09-Dec-2999 13:46:00 GMT";
+ $h[] = "name4=value2; domain=.example.com; path=/path/; expires=Mon, 09-Dec-2029 13:46:00 GMT";
$cj->parseCookieResponseHeader( $h[1], "www.example.com" );
$this->assertEquals("", $cj->serializeToHttpRequest("/", "www.example.com"));
$this->assertEquals("name4=value2", $cj->serializeToHttpRequest("/path/", "www.example.com"));
- $h[] = "name5=value3; domain=.example.com; path=/path/; expires=Mon, 09-Dec-2999 13:46:00 GMT";
+ $h[] = "name5=value3; domain=.example.com; path=/path/; expires=Mon, 09-Dec-2029 13:46:00 GMT";
$cj->parseCookieResponseHeader( $h[2], "www.example.com" );
$this->assertEquals("name4=value2; name5=value3", $cj->serializeToHttpRequest("/path/", "www.example.com"));
- $h[] = "name6=value3; domain=.example.net; path=/path/; expires=Mon, 09-Dec-2999 13:46:00 GMT";
+ $h[] = "name6=value3; domain=.example.net; path=/path/; expires=Mon, 09-Dec-2029 13:46:00 GMT";
$cj->parseCookieResponseHeader( $h[3], "www.example.com" );
$this->assertEquals("", $cj->serializeToHttpRequest("/path/", "www.example.net"));
$cj->parseCookieResponseHeader( $h[4], "www.example.net" );
$this->assertEquals("", $cj->serializeToHttpRequest("/path/", "www.example.net"));
- $h[] = "name6=value4; domain=.example.net; path=/path/; expires=Mon, 09-Dec-2999 13:46:00 GMT";
+ $h[] = "name6=value4; domain=.example.net; path=/path/; expires=Mon, 09-Dec-2029 13:46:00 GMT";
$cj->parseCookieResponseHeader( $h[5], "www.example.net" );
$this->assertEquals("name6=value4", $cj->serializeToHttpRequest("/path/", "www.example.net"));
}
+++ /dev/null
-<?php
-
-require_once( "MediaWikiAPI_Setup.php" );
-
-class MediaWikiAPITest extends MediaWikiAPI_Setup {
-
- function setup() {
- parent::setup();
- }
-
- function testApi() {
- /* Haven't thought about test ordering yet -- but this depends on HttpTest.php */
- $resp = Http::get( self::$apiUrl . "?format=xml" );
-
- libxml_use_internal_errors( true );
- $sxe = simplexml_load_string( $resp );
- $this->assertNotType( "bool", $sxe );
- $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
- }
-
- function testApiLoginNoName() {
- $resp = Http::post( self::$apiUrl . "?action=login&format=xml",
- array( "postData" => array(
- "lgname" => "",
- "lgpassword" => self::$passWord ) ) );
- libxml_use_internal_errors( true );
- $sxe = simplexml_load_string( $resp );
- $this->assertNotType( "bool", $sxe );
- $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
- $a = $sxe->login[0]->attributes()->result;
- $this->assertEquals( ' result="NoName"', $a->asXML() );
- }
-
- function testApiLoginBadPass() {
- $resp = Http::post( self::$apiUrl . "?action=login&format=xml",
- array( "postData" => array(
- "lgname" => self::$userName,
- "lgpassword" => "bad" ) ) );
- libxml_use_internal_errors( true );
- $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() );
- }
-
- function testApiLoginGoodPass() {
- $resp = Http::post( self::$apiUrl . "?action=login&format=xml",
- array( "postData" => array(
- "lgname" => self::$userName,
- "lgpassword" => self::$passWord ) ) );
- libxml_use_internal_errors( true );
- $sxe = simplexml_load_string( $resp );
- $this->assertNotType( "bool", $sxe );
- $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
- $a = $sxe->login[0]->attributes()->result;
- $this->assertEquals( ' result="Success"', $a->asXML() );
- }
-
- function testApiGotCookie() {
- global $wgScriptPath, $wgServerName;
-
- $req = HttpRequest::factory( self::$apiUrl . "?action=login&format=xml",
- array( "method" => "POST",
- "postData" => array(
- "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 ) );
- }
-}
+++ /dev/null
-<?php
-
-abstract class MediaWikiAPI_Setup extends PHPUnit_Framework_TestCase {
- protected static $userName;
- protected static $passWord;
- protected static $user;
- protected static $apiUrl;
-
- function setup() {
- global $wgServerName, $wgServer, $wgContLang, $wgAuth, $wgScriptPath,
- $wgScriptExtension, $wgMemc;
-
- if($wgServerName == "localhost" || $wgServer == "http://localhost") {
- $this->markTestIncomplete('This test needs $wgServerName and $wgServer to '.
- 'be set in LocalSettings.php');
- }
- self::$apiUrl = $wgServer.$wgScriptPath."/api".$wgScriptExtension;
-
- $wgMemc = new FakeMemCachedClient;
- $wgContLang = Language::factory( 'en' );
- $wgAuth = new StubObject( 'wgAuth', 'AuthPlugin' );
- self::setupUser();
- }
-
- static function setupUser() {
- if ( self::$user == NULL ) {
- self::$userName = "Useruser";
- self::$passWord = User::randomPassword();
-
- self::$user = User::newFromName(self::$userName);
- if ( !self::$user->getID() ) {
- self::$user = User::createNew(self::$userName, array(
- "password" => self::$passWord,
- "email" => "test@example.com",
- "real_name" => "Test User"));
- } else {
- self::$user->setPassword(self::$passWord);
- }
- self::$user->saveSettings();
- }
- }
-}