Index: trunk/phase3/maintenance/tests/MediaWikiAPITest.php |
— | — | @@ -1,74 +0,0 @@ |
2 | | -<?php |
3 | | - |
4 | | -require_once( "MediaWikiAPI_Setup.php" ); |
5 | | - |
6 | | -class MediaWikiAPITest extends MediaWikiAPI_Setup { |
7 | | - |
8 | | - function setup() { |
9 | | - parent::setup(); |
10 | | - } |
11 | | - |
12 | | - function testApi() { |
13 | | - /* Haven't thought about test ordering yet -- but this depends on HttpTest.php */ |
14 | | - $resp = Http::get( self::$apiUrl . "?format=xml" ); |
15 | | - |
16 | | - libxml_use_internal_errors( true ); |
17 | | - $sxe = simplexml_load_string( $resp ); |
18 | | - $this->assertNotType( "bool", $sxe ); |
19 | | - $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) ); |
20 | | - } |
21 | | - |
22 | | - function testApiLoginNoName() { |
23 | | - $resp = Http::post( self::$apiUrl . "?action=login&format=xml", |
24 | | - array( "postData" => array( |
25 | | - "lgname" => "", |
26 | | - "lgpassword" => self::$passWord ) ) ); |
27 | | - libxml_use_internal_errors( true ); |
28 | | - $sxe = simplexml_load_string( $resp ); |
29 | | - $this->assertNotType( "bool", $sxe ); |
30 | | - $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) ); |
31 | | - $a = $sxe->login[0]->attributes()->result; |
32 | | - $this->assertEquals( ' result="NoName"', $a->asXML() ); |
33 | | - } |
34 | | - |
35 | | - function testApiLoginBadPass() { |
36 | | - $resp = Http::post( self::$apiUrl . "?action=login&format=xml", |
37 | | - array( "postData" => array( |
38 | | - "lgname" => self::$userName, |
39 | | - "lgpassword" => "bad" ) ) ); |
40 | | - libxml_use_internal_errors( true ); |
41 | | - $sxe = simplexml_load_string( $resp ); |
42 | | - $this->assertNotType( "bool", $sxe ); |
43 | | - $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) ); |
44 | | - $a = $sxe->login[0]->attributes()->result; |
45 | | - $this->assertEquals( ' result="WrongPass"', $a->asXML() ); |
46 | | - } |
47 | | - |
48 | | - function testApiLoginGoodPass() { |
49 | | - $resp = Http::post( self::$apiUrl . "?action=login&format=xml", |
50 | | - array( "postData" => array( |
51 | | - "lgname" => self::$userName, |
52 | | - "lgpassword" => self::$passWord ) ) ); |
53 | | - libxml_use_internal_errors( true ); |
54 | | - $sxe = simplexml_load_string( $resp ); |
55 | | - $this->assertNotType( "bool", $sxe ); |
56 | | - $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) ); |
57 | | - $a = $sxe->login[0]->attributes()->result; |
58 | | - $this->assertEquals( ' result="Success"', $a->asXML() ); |
59 | | - } |
60 | | - |
61 | | - function testApiGotCookie() { |
62 | | - global $wgScriptPath, $wgServerName; |
63 | | - |
64 | | - $req = HttpRequest::factory( self::$apiUrl . "?action=login&format=xml", |
65 | | - array( "method" => "POST", |
66 | | - "postData" => array( |
67 | | - "lgname" => self::$userName, |
68 | | - "lgpassword" => self::$passWord ) ) ); |
69 | | - $req->execute(); |
70 | | - $cj = $req->getCookieJar(); |
71 | | - |
72 | | - $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . self::$userName . '; .*Token=/', |
73 | | - $cj->serializeToHttpRequest( $wgScriptPath, $wgServerName ) ); |
74 | | - } |
75 | | -} |
Index: trunk/phase3/maintenance/tests/MediaWikiAPI_Setup.php |
— | — | @@ -1,42 +0,0 @@ |
2 | | -<?php |
3 | | - |
4 | | -abstract class MediaWikiAPI_Setup extends PHPUnit_Framework_TestCase { |
5 | | - protected static $userName; |
6 | | - protected static $passWord; |
7 | | - protected static $user; |
8 | | - protected static $apiUrl; |
9 | | - |
10 | | - function setup() { |
11 | | - global $wgServerName, $wgServer, $wgContLang, $wgAuth, $wgScriptPath, |
12 | | - $wgScriptExtension, $wgMemc; |
13 | | - |
14 | | - if($wgServerName == "localhost" || $wgServer == "http://localhost") { |
15 | | - $this->markTestIncomplete('This test needs $wgServerName and $wgServer to '. |
16 | | - 'be set in LocalSettings.php'); |
17 | | - } |
18 | | - self::$apiUrl = $wgServer.$wgScriptPath."/api".$wgScriptExtension; |
19 | | - |
20 | | - $wgMemc = new FakeMemCachedClient; |
21 | | - $wgContLang = Language::factory( 'en' ); |
22 | | - $wgAuth = new StubObject( 'wgAuth', 'AuthPlugin' ); |
23 | | - self::setupUser(); |
24 | | - } |
25 | | - |
26 | | - static function setupUser() { |
27 | | - if ( self::$user == NULL ) { |
28 | | - self::$userName = "Useruser"; |
29 | | - self::$passWord = User::randomPassword(); |
30 | | - |
31 | | - self::$user = User::newFromName(self::$userName); |
32 | | - if ( !self::$user->getID() ) { |
33 | | - self::$user = User::createNew(self::$userName, array( |
34 | | - "password" => self::$passWord, |
35 | | - "email" => "test@example.com", |
36 | | - "real_name" => "Test User")); |
37 | | - } else { |
38 | | - self::$user->setPassword(self::$passWord); |
39 | | - } |
40 | | - self::$user->saveSettings(); |
41 | | - } |
42 | | - } |
43 | | -} |
Index: trunk/phase3/maintenance/tests/ApiSetup.php |
— | — | @@ -0,0 +1,42 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +abstract class ApiSetup extends PHPUnit_Framework_TestCase { |
| 5 | + protected static $userName; |
| 6 | + protected static $passWord; |
| 7 | + protected static $user; |
| 8 | + protected static $apiUrl; |
| 9 | + |
| 10 | + function setup() { |
| 11 | + global $wgServerName, $wgServer, $wgContLang, $wgAuth, $wgScriptPath, |
| 12 | + $wgScriptExtension, $wgMemc; |
| 13 | + |
| 14 | + if($wgServerName == "localhost" || $wgServer == "http://localhost") { |
| 15 | + $this->markTestIncomplete('This test needs $wgServerName and $wgServer to '. |
| 16 | + 'be set in LocalSettings.php'); |
| 17 | + } |
| 18 | + self::$apiUrl = $wgServer.$wgScriptPath."/api".$wgScriptExtension; |
| 19 | + |
| 20 | + $wgMemc = new FakeMemCachedClient; |
| 21 | + $wgContLang = Language::factory( 'en' ); |
| 22 | + $wgAuth = new StubObject( 'wgAuth', 'AuthPlugin' ); |
| 23 | + self::setupUser(); |
| 24 | + } |
| 25 | + |
| 26 | + static function setupUser() { |
| 27 | + if ( self::$user == NULL ) { |
| 28 | + self::$userName = "Useruser"; |
| 29 | + self::$passWord = User::randomPassword(); |
| 30 | + |
| 31 | + self::$user = User::newFromName(self::$userName); |
| 32 | + if ( !self::$user->getID() ) { |
| 33 | + self::$user = User::createNew(self::$userName, array( |
| 34 | + "password" => self::$passWord, |
| 35 | + "email" => "test@example.com", |
| 36 | + "real_name" => "Test User")); |
| 37 | + } else { |
| 38 | + self::$user->setPassword(self::$passWord); |
| 39 | + } |
| 40 | + self::$user->saveSettings(); |
| 41 | + } |
| 42 | + } |
| 43 | +} |
Property changes on: trunk/phase3/maintenance/tests/ApiSetup.php |
___________________________________________________________________ |
Name: svn:eol-style |
1 | 44 | + native |
Name: svn:eol-syle |
2 | 45 | + native |
Index: trunk/phase3/maintenance/tests/ApiTest.php |
— | — | @@ -0,0 +1,74 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +require_once( "ApiSetup.php" ); |
| 5 | + |
| 6 | +class ApiTest extends ApiSetup { |
| 7 | + |
| 8 | + function setup() { |
| 9 | + parent::setup(); |
| 10 | + } |
| 11 | + |
| 12 | + function testApi() { |
| 13 | + /* Haven't thought about test ordering yet -- but this depends on HttpTest.php */ |
| 14 | + $resp = Http::get( self::$apiUrl . "?format=xml" ); |
| 15 | + |
| 16 | + libxml_use_internal_errors( true ); |
| 17 | + $sxe = simplexml_load_string( $resp ); |
| 18 | + $this->assertNotType( "bool", $sxe ); |
| 19 | + $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) ); |
| 20 | + } |
| 21 | + |
| 22 | + function testApiLoginNoName() { |
| 23 | + $resp = Http::post( self::$apiUrl . "?action=login&format=xml", |
| 24 | + array( "postData" => array( |
| 25 | + "lgname" => "", |
| 26 | + "lgpassword" => self::$passWord ) ) ); |
| 27 | + libxml_use_internal_errors( true ); |
| 28 | + $sxe = simplexml_load_string( $resp ); |
| 29 | + $this->assertNotType( "bool", $sxe ); |
| 30 | + $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) ); |
| 31 | + $a = $sxe->login[0]->attributes()->result; |
| 32 | + $this->assertEquals( ' result="NoName"', $a->asXML() ); |
| 33 | + } |
| 34 | + |
| 35 | + function testApiLoginBadPass() { |
| 36 | + $resp = Http::post( self::$apiUrl . "?action=login&format=xml", |
| 37 | + array( "postData" => array( |
| 38 | + "lgname" => self::$userName, |
| 39 | + "lgpassword" => "bad" ) ) ); |
| 40 | + libxml_use_internal_errors( true ); |
| 41 | + $sxe = simplexml_load_string( $resp ); |
| 42 | + $this->assertNotType( "bool", $sxe ); |
| 43 | + $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) ); |
| 44 | + $a = $sxe->login[0]->attributes()->result; |
| 45 | + $this->assertEquals( ' result="WrongPass"', $a->asXML() ); |
| 46 | + } |
| 47 | + |
| 48 | + function testApiLoginGoodPass() { |
| 49 | + $resp = Http::post( self::$apiUrl . "?action=login&format=xml", |
| 50 | + array( "postData" => array( |
| 51 | + "lgname" => self::$userName, |
| 52 | + "lgpassword" => self::$passWord ) ) ); |
| 53 | + libxml_use_internal_errors( true ); |
| 54 | + $sxe = simplexml_load_string( $resp ); |
| 55 | + $this->assertNotType( "bool", $sxe ); |
| 56 | + $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) ); |
| 57 | + $a = $sxe->login[0]->attributes()->result; |
| 58 | + $this->assertEquals( ' result="Success"', $a->asXML() ); |
| 59 | + } |
| 60 | + |
| 61 | + function testApiGotCookie() { |
| 62 | + global $wgScriptPath, $wgServerName; |
| 63 | + |
| 64 | + $req = HttpRequest::factory( self::$apiUrl . "?action=login&format=xml", |
| 65 | + array( "method" => "POST", |
| 66 | + "postData" => array( |
| 67 | + "lgname" => self::$userName, |
| 68 | + "lgpassword" => self::$passWord ) ) ); |
| 69 | + $req->execute(); |
| 70 | + $cj = $req->getCookieJar(); |
| 71 | + $this->markTestIncomplete("Need to make sure cookie/domain handling is correct"); |
| 72 | + $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . self::$userName . '; .*Token=/', |
| 73 | + $cj->serializeToHttpRequest( $wgScriptPath, $wgServerName ) ); |
| 74 | + } |
| 75 | +} |
Property changes on: trunk/phase3/maintenance/tests/ApiTest.php |
___________________________________________________________________ |
Name: svn:eol-style |
1 | 76 | + native |
Name: svn:eol-syle |
2 | 77 | + native |
Index: trunk/phase3/maintenance/tests/HttpTest.php |
— | — | @@ -353,7 +353,7 @@ |
354 | 354 | "path" => "/path/", |
355 | 355 | ) ); |
356 | 356 | |
357 | | - $this->assertFalse($c->canServeDomain("example.com")); |
| 357 | + $this->assertTrue($c->canServeDomain("example.com")); |
358 | 358 | $this->assertFalse($c->canServeDomain("www.example.net")); |
359 | 359 | $this->assertTrue($c->canServeDomain("www.example.com")); |
360 | 360 | |
— | — | @@ -372,7 +372,7 @@ |
373 | 373 | array( |
374 | 374 | "domain" => ".example.com", |
375 | 375 | "path" => "/path/", |
376 | | - "expires" => "January 1, 1990", |
| 376 | + "expires" => "-1 day", |
377 | 377 | ) ); |
378 | 378 | $this->assertFalse($c->isUnExpired()); |
379 | 379 | $this->assertEquals("", $c->serializeToHttpRequest("/path/", "www.example.com")); |
— | — | @@ -381,12 +381,10 @@ |
382 | 382 | array( |
383 | 383 | "domain" => ".example.com", |
384 | 384 | "path" => "/path/", |
385 | | - "expires" => "January 1, 2999", |
| 385 | + "expires" => "+1 day", |
386 | 386 | ) ); |
387 | 387 | $this->assertTrue($c->isUnExpired()); |
388 | 388 | $this->assertEquals("name=value", $c->serializeToHttpRequest("/path/", "www.example.com")); |
389 | | - |
390 | | - |
391 | 389 | } |
392 | 390 | |
393 | 391 | function testCookieJarSetCookie() { |
— | — | @@ -415,7 +413,7 @@ |
416 | 414 | array( |
417 | 415 | "domain" => ".example.net", |
418 | 416 | "path" => "/path/", |
419 | | - "expires" => "January 1, 1999", |
| 417 | + "expires" => "-1 day", |
420 | 418 | ) ); |
421 | 419 | |
422 | 420 | $this->assertEquals("name4=value", $cj->serializeToHttpRequest("/path/", "www.example.net")); |
— | — | @@ -426,7 +424,7 @@ |
427 | 425 | array( |
428 | 426 | "domain" => ".example.net", |
429 | 427 | "path" => "/path/", |
430 | | - "expires" => "January 1, 2999", |
| 428 | + "expires" => "+1 day", |
431 | 429 | ) ); |
432 | 430 | $this->assertEquals("name4=value; name5=value", $cj->serializeToHttpRequest("/path/", "www.example.net")); |
433 | 431 | |
— | — | @@ -434,7 +432,7 @@ |
435 | 433 | array( |
436 | 434 | "domain" => ".example.net", |
437 | 435 | "path" => "/path/", |
438 | | - "expires" => "January 1, 1999", |
| 436 | + "expires" => "-1 day", |
439 | 437 | ) ); |
440 | 438 | $this->assertEquals("name5=value", $cj->serializeToHttpRequest("/path/", "www.example.net")); |
441 | 439 | } |
— | — | @@ -442,20 +440,20 @@ |
443 | 441 | function testParseResponseHeader() { |
444 | 442 | $cj = new CookieJar; |
445 | 443 | |
446 | | - $h[] = "Set-Cookie: name4=value; domain=.example.com; path=/; expires=Mon, 09-Dec-2999 13:46:00 GMT"; |
| 444 | + $h[] = "Set-Cookie: name4=value; domain=.example.com; path=/; expires=Mon, 09-Dec-2029 13:46:00 GMT"; |
447 | 445 | $cj->parseCookieResponseHeader( $h[0], "www.example.com" ); |
448 | 446 | $this->assertEquals("name4=value", $cj->serializeToHttpRequest("/", "www.example.com")); |
449 | 447 | |
450 | | - $h[] = "name4=value2; domain=.example.com; path=/path/; expires=Mon, 09-Dec-2999 13:46:00 GMT"; |
| 448 | + $h[] = "name4=value2; domain=.example.com; path=/path/; expires=Mon, 09-Dec-2029 13:46:00 GMT"; |
451 | 449 | $cj->parseCookieResponseHeader( $h[1], "www.example.com" ); |
452 | 450 | $this->assertEquals("", $cj->serializeToHttpRequest("/", "www.example.com")); |
453 | 451 | $this->assertEquals("name4=value2", $cj->serializeToHttpRequest("/path/", "www.example.com")); |
454 | 452 | |
455 | | - $h[] = "name5=value3; domain=.example.com; path=/path/; expires=Mon, 09-Dec-2999 13:46:00 GMT"; |
| 453 | + $h[] = "name5=value3; domain=.example.com; path=/path/; expires=Mon, 09-Dec-2029 13:46:00 GMT"; |
456 | 454 | $cj->parseCookieResponseHeader( $h[2], "www.example.com" ); |
457 | 455 | $this->assertEquals("name4=value2; name5=value3", $cj->serializeToHttpRequest("/path/", "www.example.com")); |
458 | 456 | |
459 | | - $h[] = "name6=value3; domain=.example.net; path=/path/; expires=Mon, 09-Dec-2999 13:46:00 GMT"; |
| 457 | + $h[] = "name6=value3; domain=.example.net; path=/path/; expires=Mon, 09-Dec-2029 13:46:00 GMT"; |
460 | 458 | $cj->parseCookieResponseHeader( $h[3], "www.example.com" ); |
461 | 459 | $this->assertEquals("", $cj->serializeToHttpRequest("/path/", "www.example.net")); |
462 | 460 | |
— | — | @@ -463,7 +461,7 @@ |
464 | 462 | $cj->parseCookieResponseHeader( $h[4], "www.example.net" ); |
465 | 463 | $this->assertEquals("", $cj->serializeToHttpRequest("/path/", "www.example.net")); |
466 | 464 | |
467 | | - $h[] = "name6=value4; domain=.example.net; path=/path/; expires=Mon, 09-Dec-2999 13:46:00 GMT"; |
| 465 | + $h[] = "name6=value4; domain=.example.net; path=/path/; expires=Mon, 09-Dec-2029 13:46:00 GMT"; |
468 | 466 | $cj->parseCookieResponseHeader( $h[5], "www.example.net" ); |
469 | 467 | $this->assertEquals("name6=value4", $cj->serializeToHttpRequest("/path/", "www.example.net")); |
470 | 468 | } |
Index: trunk/phase3/includes/HttpFunctions.php |
— | — | @@ -386,10 +386,10 @@ |
387 | 387 | * Parse the cookies in the response headers and store them in the cookie jar. |
388 | 388 | */ |
389 | 389 | protected function parseCookies() { |
| 390 | + if( !$this->cookieJar ) { |
| 391 | + $this->cookieJar = new CookieJar; |
| 392 | + } |
390 | 393 | if( isset( $this->respHeaders['set-cookie'] ) ) { |
391 | | - if( !$this->cookieJar ) { |
392 | | - $this->cookieJar = new CookieJar; |
393 | | - } |
394 | 394 | $url = parse_url( $this->getFinalUrl() ); |
395 | 395 | foreach( $this->respHeaders['set-cookie'] as $cookie ) { |
396 | 396 | $this->cookieJar->parseCookieResponseHeader( $cookie, $url['host'] ); |
— | — | @@ -453,12 +453,25 @@ |
454 | 454 | $this->path = "/"; |
455 | 455 | } |
456 | 456 | if( isset( $attr['domain'] ) ) { |
457 | | - $this->domain = $attr['domain']; |
| 457 | + $this->domain = self::parseCookieDomain( $attr['domain'] ); |
458 | 458 | } else { |
459 | 459 | throw new MWException("You must specify a domain."); |
460 | 460 | } |
461 | 461 | } |
462 | 462 | |
| 463 | + public static function parseCookieDomain( $domain ) { |
| 464 | + /* If domain is given, it has to contain at least two dots */ |
| 465 | + if ( strrpos( $domain, '.' ) === false |
| 466 | + || strrpos( $domain, '.' ) === strpos( $domain, '.' ) ) { |
| 467 | + return; |
| 468 | + } |
| 469 | + if ( substr( $domain, 0, 1 ) === '.' ) { |
| 470 | + $domain = substr( $domain, 1 ); |
| 471 | + } |
| 472 | + |
| 473 | + return $domain; |
| 474 | + } |
| 475 | + |
463 | 476 | /** |
464 | 477 | * Serialize the cookie jar into a format useful for HTTP Request headers. |
465 | 478 | * @param $path string the path that will be used. Required. |
— | — | @@ -560,14 +573,6 @@ |
561 | 574 | if( !isset( $attr['domain'] ) ) { |
562 | 575 | $attr['domain'] = $domain; |
563 | 576 | } else { |
564 | | - /* If domain is given, it has to contain at least two dots */ |
565 | | - if ( strrpos( $attr['domain'], '.' ) === false |
566 | | - || strrpos( $attr['domain'], '.' ) === strpos( $attr['domain'], '.' ) ) { |
567 | | - return; |
568 | | - } |
569 | | - if ( substr( $attr['domain'], 0, 1 ) === '.' ) { |
570 | | - $attr['domain'] = substr( $attr['domain'], 1 ); |
571 | | - } |
572 | 577 | if ( strlen( $attr['domain'] ) < strlen( $domain ) |
573 | 578 | && substr_compare( $domain, $attr['domain'], -strlen( $attr['domain'] ), |
574 | 579 | strlen( $attr['domain'] ), TRUE ) != 0 ) { |