Skip to content

Instantly share code, notes, and snippets.

@supaweb
Forked from GaryJones/RedirectsCept.php
Created May 20, 2019 15:21
Show Gist options
  • Save supaweb/ce2427d8758805dbd6dbd0dd392ad574 to your computer and use it in GitHub Desktop.
Save supaweb/ce2427d8758805dbd6dbd0dd392ad574 to your computer and use it in GitHub Desktop.
Codeception 301 Redirection Tests
# Codeception Test Suite Configuration
# suite for redirection tests.
# perform tests in browser using the REST module with redirections NOT followed.
class_name: RedirectsTester
modules:
enabled:
- PhpBrowser
- REST
- RedirectsHelper
config:
PhpBrowser:
url: 'http://www.example.com/'
REST:
url: 'http://www.example.com/'
<?php
$I = new RedirectsTester($scenario);
$I->wantTo('check 301 redirects are working');
// First, test /login/ page gets rewritten to https://
$I->seePermanentRedirectToHttpsFor('login/');
$I->seePermanentRedirectToHttpsFor('login/?redirect_to=foo');
// For all other redirects, a Location header is sent, so we stop the redirect
// and just check that header instead.
$I->followRedirects(false);
// External
$I->sendHead('facebook');
$I->seePermanentRedirectTo('https://www.facebook.com/DanielsTrading');
// Internal link
$I->sendHead('don-debartolo/access');
$I->seePermanentRedirectTo('ddebartolo/#account');
<?php
namespace Helper;
// here you can define custom actions
// all public methods declared in helper class will be available in $I
class Redirects extends \Codeception\Module
{
/**
* Check that a 301 HTTP Status is returned with the correct Location URL.
*
* @since 1.0.0
*
* @param string $url Relative or absolute URL of redirect destination.
*/
public function seePermanentRedirectTo($url)
{
// Allow relative URLs.
$testDomain = \Codeception\Configuration::suiteSettings('redirects', \Codeception\Configuration::config())['modules']['enabled'][1]['REST']['url'];
$url = (strpos($url, '://') === false ? $testDomain : '') . $url;
$response = $this->getModule('PhpBrowser')->client->getInternalResponse();
$responseCode = $response->getStatus();
$locationHeader = $response->getHeaders()['Location'][0];
$this->assertEquals(301, $responseCode);
$this->assertEquals($url, $locationHeader);
}
/**
* Check that a 301 HTTP Status is returned with the correct Location URL.
*
* @since 1.0.0
*
* @param string $url Relative or absolute URL of redirect destination.
*/
public function seePermanentRedirectToHttpsFor($url)
{
$this->getModule('REST')->sendHead($url);
// Allow relative URLs.
$testDomain = \Codeception\Configuration::suiteSettings('redirects', \Codeception\Configuration::config())['modules']['enabled'][1]['REST']['url'];
$testDomainHttps = str_replace( 'http://', 'https://', $testDomain );
$url = (strpos($url, '://') === false ? $testDomainHttps : '') . $url;
$client = $this->getModule('PhpBrowser')->client;
$responseCode = $client->getInternalResponse()->getStatus();
$responseUri = $client->getHistory()->current()->getUri();
$this->assertEquals(200, $responseCode);
$this->assertEquals($url, $responseUri);
}
/**
* Toggle redirections on and off.
*
* By default, BrowserKit will follow redirections, so to check for 30*
* HTTP status codes and Location headers, they have to be turned off.
*
* @since 1.0.0
*
* @param bool $followRedirects Optional. Whether to follow redirects or not.
* Default is true.
*/
function followRedirects( $followRedirects = true ) {
$this->getModule('PhpBrowser')->client->followRedirects($followRedirects);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment