Hashar has uploaded a new change for review. (
https://gerrit.wikimedia.org/r/328182 )
Change subject: rpc: raise exception instead of die
......................................................................
rpc: raise exception instead of die
Change-Id: Ib01c7f9fa43cdbd15e0c71042870692c283db22a
---
M rpc/RunJobs.php
A tests/rpc/RunJobsTest.php
2 files changed, 37 insertions(+), 2 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/operations/mediawiki-config
refs/changes/82/328182/1
diff --git a/rpc/RunJobs.php b/rpc/RunJobs.php
index ccc0e6d..bbeb189 100755
--- a/rpc/RunJobs.php
+++ b/rpc/RunJobs.php
@@ -19,9 +19,9 @@
* @author Aaron Schulz
*/
if ( !in_array( $_SERVER['REMOTE_ADDR'], array( '127.0.0.1',
'0:0:0:0:0:0:0:1', '::1' ), true ) ) {
- die( "Only loopback requests are allowed.\n" );
+ throw new Exception( "Only loopback requests are allowed.\n", 1 );
} elseif ( $_SERVER['REQUEST_METHOD'] !== 'POST' ) {
- die( "Request must use POST.\n" );
+ throw new Exception( "Request must use POST.\n", 2 );
}
define( 'MEDIAWIKI_JOB_RUNNER', 1 );
diff --git a/tests/rpc/RunJobsTest.php b/tests/rpc/RunJobsTest.php
new file mode 100644
index 0000000..48c3d15
--- /dev/null
+++ b/tests/rpc/RunJobsTest.php
@@ -0,0 +1,35 @@
+<?php
+/**
+ * @backupGlobals enabled
+ */
+class RunJobsTest extends \PHPUnit_Framework_TestCase {
+
+ /**
+ * @expectedException Exception
+ * @expectedExceptionCode 1
+ * @expectedExceptionMessage Only loopback requests are allowed
+ */
+ function testRaiseAnExceptionFromNonLocalhost() {
+ $_SERVER['REMOTE_ADDR'] = '192.0.2.42';
+ require __DIR__ . '/../../rpc/RunJobs.php';
+ }
+
+ /**
+ * @dataProvider provideLocalhostIps
+ *
+ * @expectedException PHPUnit_Framework_Error_Notice
+ * @expectedExceptionMessage Undefined index: REQUEST_METHOD
+ */
+ function testAcceptLocalhostRequest( $address ) {
+ $_SERVER['REMOTE_ADDR'] = $address;
+ require __DIR__ . '/../../rpc/RunJobs.php';
+ }
+
+ public function provideLocalhostIps() {
+ return [
+ 'IPv4 loopback' => [ '127.0.0.1' ],
+ 'IPv6 loopback' => [ '0:0:0:0:0:0:0:1' ],
+ 'IPv6 loopback (short)' => [ '::1' ],
+ ];
+ }
+}
--
To view, visit https://gerrit.wikimedia.org/r/328182
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib01c7f9fa43cdbd15e0c71042870692c283db22a
Gerrit-PatchSet: 1
Gerrit-Project: operations/mediawiki-config
Gerrit-Branch: master
Gerrit-Owner: Hashar <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits