Tim Starling has submitted this change and it was merged.

Change subject: Added a few tests for redirects.conf
......................................................................


Added a few tests for redirects.conf

* Committed the CLI testing system that I've been using for a while now
* Quickly wrote a script for automatic testing, using the same server
  configuration

Change-Id: I8215a84ce573ca318c2534bfd1157bc5f6eef749
---
A test/cli-redirect-test.php
A test/redirect-test-server.sh
A test/redirect-test.conf
A test/run-tests.php
4 files changed, 137 insertions(+), 0 deletions(-)

Approvals:
  Tim Starling: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/test/cli-redirect-test.php b/test/cli-redirect-test.php
new file mode 100644
index 0000000..ad2e555
--- /dev/null
+++ b/test/cli-redirect-test.php
@@ -0,0 +1,28 @@
+<?php
+
+while (( $line = readline( '> ' ) ) !== false ) {
+    $url = trim( $line );
+    if ( !preg_match( '/^http:\/\//', $url ) ) {
+        $url = 'http://' . $url;
+    }
+    $c = curl_init( $url );
+    curl_setopt_array( $c, array(
+        CURLOPT_HEADER => true,
+        CURLOPT_RETURNTRANSFER => true,
+        CURLOPT_PROXY => 'localhost:8999',
+        CURLOPT_USERAGENT => 'curl',
+    ) );
+    $result = curl_exec( $c );
+    $info = curl_getinfo( $c );
+    if ( $info['http_code'] == 301 || $info['http_code'] == 302 ) {
+        $m = false;
+        preg_match( '/Location: (.*)\\n/', $result, $m );
+        echo "-> {$m[1]}\n";
+    } else {
+        echo $info['http_code'] . "\n";
+        if ( $info['http_code'] == 403 ) {
+            echo $result;
+        }
+    }
+    readline_add_history( $line );
+}
diff --git a/test/redirect-test-server.sh b/test/redirect-test-server.sh
new file mode 100755
index 0000000..3e92ad8
--- /dev/null
+++ b/test/redirect-test-server.sh
@@ -0,0 +1,5 @@
+#!/bin/bash
+
+apache2 -d .. \
+    -f test/redirect-test.conf \
+    -X
diff --git a/test/redirect-test.conf b/test/redirect-test.conf
new file mode 100644
index 0000000..314adbb
--- /dev/null
+++ b/test/redirect-test.conf
@@ -0,0 +1,13 @@
+# vim: syn=apache
+LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so
+Listen 8999
+ErrorLog /dev/stderr
+PidFile test/apache2.pid
+NameVirtualHost *
+MaxRequestsPerChild 0
+<VirtualHost *>
+    ServerName default
+    DocumentRoot /nonexistent
+</VirtualHost>
+Include redirects.conf
+
diff --git a/test/run-tests.php b/test/run-tests.php
new file mode 100644
index 0000000..030e53d
--- /dev/null
+++ b/test/run-tests.php
@@ -0,0 +1,91 @@
+<?php
+
+class RunRedirectTests {
+       var $status;
+
+       var $redirectTests = array(
+               'http://mediawiki.org/' => 'http://www.mediawiki.org/',
+               'https://mediawiki.org/' => 'https://www.mediawiki.org/',
+               'http://wikibooks.org/' => 'http://en.wikibooks.org/',
+               'http://wikibooks.org/x' => 'http://en.wikibooks.org/x',
+               'http://wiktionary.org/' => 'http://www.wiktionary.org/',
+               'http://wiktionary.org/x' => 'http://www.wiktionary.org/x',
+               'https://wiktionary.org/x' => 'https://www.wiktionary.org/x',
+       );
+
+       function execute() {
+               $this->status = true;
+
+               $fd = popen( __DIR__ . '/redirect-test-server.sh', 'w' );
+               $t = microtime( true );
+               do {
+                       $f = @fsockopen( 'localhost', '8999', $errno, $errstr, 
1 );
+                       if ( !$f ) {
+                               usleep( 100000 );
+                       }
+               } while ( !$f && microtime( true ) - $t < 5 );
+               if ( !$f ) {
+                       print "Unable to start server\n";
+                       exit( 1 );
+               }
+
+               $pid = trim( file_get_contents( __DIR__ . '/apache2.pid' ) );
+
+               foreach ( $this->redirectTests as $source => $expectedDest ) {
+                       if ( preg_match( '/^https:/', $source ) ) {
+                               $protocol = 'https';
+                               $url = preg_replace( '/^https:/', 'http:', 
$source );
+                       } else {
+                               $url = $source;
+                               $protocol = 'http';
+                       }
+                       $c = curl_init( $url );
+                       curl_setopt_array( $c, array(
+                               CURLOPT_HEADER => true,
+                               CURLOPT_RETURNTRANSFER => true,
+                               CURLOPT_PROXY => 'localhost:8999',
+                               CURLOPT_USERAGENT => 'curl',
+                               CURLOPT_HTTPHEADER => array('X-Forwarded-Proto: 
' . $protocol)
+                       ) );
+                       $result = curl_exec( $c );
+                       $info = curl_getinfo( $c );
+                       if ( $info['http_code'] == 301 || $info['http_code'] == 
302 ) {
+                               if (preg_match( '/Location: (.*)\\r\\n/', 
$result, $m )) {
+                                       if ( $m[1] == $expectedDest ) {
+                                               $this->success();
+                                       } else {
+                                               $this->fail( $source, 
$expectedDest, $m[1] );
+                                       }
+                               } else {
+                                       $this->fail( $source, $expectedDest, 
"no location header" );
+                               }
+                       } else {
+                               $this->fail( $source, $expectedDest, 
"Unexpected HTTP code \"{$info['http_code']}\"" );
+                       }
+               }
+               print "\nKilling server $pid\n";
+               posix_kill( $pid, SIGTERM );
+               return $this->status;
+       }
+
+       function success() {
+               print ".";
+       }
+
+       function fail($source, $expected, $got) {
+               print "Failed redirect test $source:\n" .
+                       "Expected: $expected\n" .
+                       "Got: $got\n\n";
+               $this->status = false;
+       }
+}
+
+$r = new RunRedirectTests;
+$status = $r->execute();
+if ( $status ) {
+       print "All tests passed\n";
+       exit( 0 );
+} else {
+       print "There were failures\n";
+       exit( 1 );
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/133045
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I8215a84ce573ca318c2534bfd1157bc5f6eef749
Gerrit-PatchSet: 1
Gerrit-Project: operations/apache-config
Gerrit-Branch: master
Gerrit-Owner: Tim Starling <tstarl...@wikimedia.org>
Gerrit-Reviewer: Tim Starling <tstarl...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to