Skip to content

Instantly share code, notes, and snippets.

@typhonius
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save typhonius/059841ce53b8b935bb3a to your computer and use it in GitHub Desktop.
Save typhonius/059841ce53b8b935bb3a to your computer and use it in GitHub Desktop.
further tests for ape
diff --git a/ape.test b/ape.test
index 756cfeb..f34a7fe 100644
--- a/ape.test
+++ b/ape.test
@@ -31,6 +31,7 @@ class ApeTestHelper extends DrupalWebTestCase {
$modules[] = 'path';
$modules[] = 'user';
$modules[] = 'common_test';
+ $modules[] = 'ape_redirect_test';
parent::setUp($modules);
variable_set('clean_url', 1);
@@ -46,7 +47,7 @@ class ApeTestHelper extends DrupalWebTestCase {
/**
* Test cache-control headers after configuration of the module.
*/
- function testApeHeaders() {
+ public function testApeHeaders() {
// Check user registration page has global age.
$this->drupalGet('user/register');
@@ -66,7 +67,20 @@ class ApeTestHelper extends DrupalWebTestCase {
$this->drupalGet('user');
$this->assertFalse($this->drupalGetHeader('X-Drupal-Cache'), 'Caching was bypassed.');
$this->assertEqual($this->drupalGetHeader('Cache-Control'), 'no-cache, must-revalidate, post-check=0, pre-check=0', 'Cache-Control header was sent.');
+ $this->drupalLogout();
- // TODO: Create tests for 301 and 302 redirects.
+ // Check that 301 redirects work correctly
+ $this->drupalGet('ape_redirect_301');
+ $headers = $this->drupalGetHeaders(TRUE);
+ $this->assertEqual($headers[0]['cache-control'], 'public, max-age=1800', 'Alternative Cache-Control header set.');
+
+ // Check that 302 redirects work correctly
+ $this->drupalGet('ape_redirect_302');
+ $headers = $this->drupalGetHeaders(TRUE);
+ $this->assertEqual($headers[0]['cache-control'], 'public, max-age=600', 'Alternative Cache-Control header set.');
+
+ // Check that 404 redirects do not get cached.
+ $this->drupalGet('lolcats');
+ $this->assertEqual($this->drupalGetHeader('Cache-Control'), 'no-cache, must-revalidate, post-check=0, pre-check=0', 'Cache-Control header was sent.');
}
}
diff --git a/tests/ape_redirect_test/ape_redirect_test.info b/tests/ape_redirect_test/ape_redirect_test.info
new file mode 100644
index 0000000..748215a
--- /dev/null
+++ b/tests/ape_redirect_test/ape_redirect_test.info
@@ -0,0 +1,5 @@
+name = Ape redirect test
+description = 'Support module for ape tests.'
+package = Testing
+core = 7.x
+hidden = TRUE
diff --git a/tests/ape_redirect_test/ape_redirect_test.module b/tests/ape_redirect_test/ape_redirect_test.module
new file mode 100644
index 0000000..58aad80
--- /dev/null
+++ b/tests/ape_redirect_test/ape_redirect_test.module
@@ -0,0 +1,52 @@
+<?php
+
+/**
+ * Implements hook_menu().
+ */
+function ape_redirect_test_menu() {
+ $items = array();
+
+ $items['ape_redirect_301'] = array(
+ 'title' => 'ape redirect 301',
+ 'access callback' => TRUE,
+ 'page callback' => 'ape_redirect_test_callback',
+ 'page arguments' => array('301'),
+ 'type' => MENU_CALLBACK,
+ );
+
+ $items['ape_redirect_302'] = array(
+ 'title' => 'ape redirect 302',
+ 'access callback' => TRUE,
+ 'page callback' => 'ape_redirect_test_callback',
+ 'page arguments' => array('302'),
+ 'type' => MENU_CALLBACK,
+ );
+
+ $items['ape_redirect_landing'] = array(
+ 'title' => 'ape redirect landing',
+ 'access callback' => TRUE,
+ 'page callback' => 'ape_redirect_test_callback',
+ 'page arguments' => array('landing'),
+ 'type' => MENU_CALLBACK,
+);
+
+ return $items;
+}
+
+/**
+ * Page callback for ape_redirect_test_menu().
+ */
+function ape_redirect_test_callback($argument) {
+ switch($argument) {
+ case '301':
+ drupal_goto('ape_redirect_landing', array(), 301);
+ break;
+ case '302':
+ drupal_goto('ape_redirect_landing', array(), 302);
+ break;
+ case 'landing':
+ print 'bananas';
+ break;
+
+ }
+}
@dbailey-acquia
Copy link

I don't believe that approach will work. Looking at the common.test for drupal_goto, it appears you have to capture the headers from the first request, otherwise you are getting the headers from the your final destination.

function testDrupalGoto() {
$this->drupalGet('common-test/drupal_goto/redirect');
$headers = $this->drupalGetHeaders(TRUE);
list(, $status) = explode(' ', $headers[0][':status'], 3);
$this->assertEqual($status, 302, 'Expected response code was sent.');
$this->assertText('drupal_goto', 'Drupal goto redirect succeeded.');
$this->assertEqual($this->getUrl(), url('common-test/drupal_goto', array('absolute' => TRUE)), 'Drupal goto redirected to expected URL.');

@typhonius
Copy link
Author

You are right @dbailey-acquia - although it needed additional fungling to get through. That alone wasn't enough to pass tests, 301/302 redirects weren't having their cache headers set and not logging out during the test was also breaking the 301/302 specific tests. Along with the pointers placed in the issue queue I've updated the above diff which should round out the additional tests

@typhonius
Copy link
Author

The last test fails however because 404s are getting cached. I'll leave that to you to decide if you want to remove that test because it's desired behaviour or address that. Again, I've noted that in the issue queue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment