gamajo / codeception-redirects
Codeception Redirects 模块
Requires
- php: ^7.0
- codeception/codeception: ^2.5 || ^3.0
- roave/security-advisories: dev-master
Requires (Dev)
- dealerdirect/phpcodesniffer-composer-installer: ^0.4
- squizlabs/php_codesniffer: ^3.1
- wimg/php-compatibility: dev-master
README
使用REST模块检查URL的Location
响应头和301
HTTP状态响应代码,或协议之间的重定向。
方便检查.htaccess
或其他网页重定向方法是否正确设置。
如果测试失败,则将最后显示的页面存储在output
目录中。
状态
- 维护者:GaryJones
- 稳定性:alpha
- 联系方式:https://gamajo.com
请审查不稳定模块的代码,并在有问题时提供补丁。
配置
无,但请确保REST模块已启用且已设置url
。
示例(acceptance.suite.yml
)
modules: enabled: - Redirects - REST: url: 'http://localhost'
示例用法
301 永久重定向
以下是一个Cest,它检查301重定向。我们关闭了Symfony Browserkit客户端的重定向自动跟踪,因此它不会直接跟踪到最终目的地。
<?php use Codeception\Example; class RedirectsCest { /** * @var AcceptanceTester */ protected $I; public function _before( AcceptanceTester $I ) { $this->I = $I; } /** * @example(old="content/abou", new="about-us") * @example(old="content/abou/over.php", new="about-us/company-overview") * @example(old="content/abou/miss.php", new="about-us/top-third-mission") * @example(old="content/abou/exec.php", new="about-us/executive-team") * @example(old="content/abou/team.php", new="about-us/risk-management-specialists") * * @group redirects * @group redirectsabout */ public function redirectOldAboutUrlsToAboutUsPages( AcceptanceTester $I, Example $example ) { $this->testIfOldRedirectsToNew($example['old'], $example['new']); } /** * @example(old="content/myac/index.php", new="my-account") * @example(old="content/myac/stat.php", new="my-account/account-statements-explained") * @example(old="content/myac/depo.php", new="my-account/deposits-withdrawals") * @example(old="content/myac/wire.php", new="wire-instructions-r-j-obrien") * * @group redirects * @group redirectsmyaccount */ public function redirectOldMyAccountUrlsToNewMyAccountPages( AcceptanceTester $I, Example $example ) { $this->testIfOldRedirectsToNew( $example['old'], $example['new'] ); } private function testIfOldRedirectsToNew($old, $new, $checkDestination = true) { $this->I->seePermanentRedirectBetween($old, $new); if ($checkDestinationExists) { $this->I->urlDoesNotRedirect($new); } // Check old URL with trailing slash also redirects. if ( '/' !== substr($old, -1) && false === strpos( strrev($old), strrev('.php')) && false === strpos( strrev($old), strrev('.pdf')) && false === strpos( $old, '?') ) { $old .= '/'; $this->testIfOldRedirectsToNew($old, $new, $checkDestinationExists); } } }
将这些测试分组意味着您可以更轻松地运行单个测试组
vendor/bin/codecept run --env=staging --group=redirectsmyaccount
307 临时重定向
上述示例代码也适用于检查307临时重定向,只是使用seeTemporaryRedirectBetween()
方法而不是seePermanentRedirectBetween()
方法。
协议重定向
以下是一个Cest,它检查协议重定向,以查看URL是否被强制以http://
或https://
提供服务。
<?php use Page\ProfileCalendar; use Page\ProfileContactInformation; use Page\ProfileMyProducts; use Step\Acceptance\Login; class ProtocolRedirectsCest { /** * @group protocolRedirects */ public function forceHttp(Login $I) { $I->wantTo('check forced redirects to HTTP are working.'); $I->seeHttpProtocolAlwaysUsedFor(ProfileCalendar::$URL); } /** * @group protocolRedirects */ public function forceHttps(Login $I) { $I->wantTo('check forced redirects to HTTPS are working.'); $I->seeHttpsProtocolAlwaysUsedFor(ProfileContactInformation::$URL); $I->seeHttpsProtocolAlwaysUsedFor(ProfileMyProducts::$URL); } }
公共API
期望在Cepts和Cests中使用的方法。
followRedirects
设置是否自动跟踪重定向。
$I->followRedirects(false);
-
param bool
$followRedirects
是否跟踪自动重定向。默认行为为true,因此大多数时候您需要传递false进行301重定向测试。此包中的其他方法已根据需要调用此方法。
seeRedirectBetween
检查是否返回指定的HTTP状态和正确的Location URL。如果任一缺少或Location头值不匹配$url
,则失败。自动避免跟踪重定向。
$I->seeRedirectBetween('company/financial-strength-and-security.cfm', 'company/financial-security', 302);
-
param
$oldUrl
应重定向的相对或绝对URL。
-
param
$newUrl
重定向目的地的相对或绝对URL。
-
param
$statusCode
要检查的HTTP状态码。
seePermanentRedirectBetween
一个便利方法,用于检查返回301 HTTP状态和正确的Location URL。如果任一缺少或Location头值不匹配$url
,则失败。自动避免跟踪重定向。
$I->seePermanentRedirectBetween('company/financial-strength-and-security.cfm', 'company/financial-security');
-
param
$oldUrl
应重定向的相对或绝对URL。
-
param
$newUrl
重定向目的地的相对或绝对URL。
seeTemporaryRedirectBetween
一个便利方法,用于检查返回307 HTTP状态和正确的Location URL。如果任一缺少或Location头值不匹配$url
,则失败。自动避免跟踪重定向。
$I->seeTemporaryRedirectBetween('company/financial-strength-and-security.cfm', 'company/financial-security');
-
param
$oldUrl
应重定向的相对或绝对URL。
-
param
$newUrl
重定向目的地的相对或绝对URL。
urlDoesNotRedirect
检查是否返回200 HTTP状态且URL没有重定向。允许跟踪重定向的可能性。
$I->urlDoesNotRedirect('company/financial-security');
-
param
$url
绝对或相对(相对于REST配置
url
)URL。
seeHttpProtocolAlwaysUsedFor
检查最终返回200 HTTP状态并使用HTTP协议。自动跟踪重定向。
$I->seePermanentRedirectToHttpFor('insecure-page');
-
param
$url
绝对或相对(相对于REST配置
url
)URL。
seePermanentRedirectToHttpsFor
检查最终返回200 HTTP状态并使用HTTPS协议。自动跟踪重定向。
$I->seePermanentRedirectToHttpsFor('contact-us');
-
param
$url
绝对或相对(相对于REST配置
url
)URL。