Skip to content

Commit e0f3fac

Browse files
committed
Clarify logic with 418 enabled and raw disabled
1 parent fa06972 commit e0f3fac

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

includes/ResponseFactory.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,27 @@ public function __construct( ServiceOptions $options ) {
6363
/**
6464
* Deny access using the configured strategy.
6565
*
66+
* When CrawlerProtectionRawDenial is enabled, a raw HTTP response is
67+
* sent. If CrawlerProtectionUse418 is *also* enabled the response
68+
* uses the 418 "I'm a teapot" status; otherwise the configured
69+
* header / body are used.
70+
*
71+
* When CrawlerProtectionRawDenial is disabled, a pretty 403 page is
72+
* rendered through OutputPage regardless of the Use418 setting.
73+
*
6674
* @param OutputPage $output Used only for the "pretty" strategy
6775
* @return void
6876
*/
6977
public function denyAccess( $output ): void {
70-
if ( $this->options->get( 'CrawlerProtectionUse418' ) ) {
71-
$this->denyAccessWith418();
72-
} elseif ( $this->options->get( 'CrawlerProtectionRawDenial' ) ) {
73-
$this->denyAccessRaw(
74-
$this->options->get( 'CrawlerProtectionRawDenialHeader' ),
75-
$this->options->get( 'CrawlerProtectionRawDenialText' )
76-
);
78+
if ( $this->options->get( 'CrawlerProtectionRawDenial' ) ) {
79+
if ( $this->options->get( 'CrawlerProtectionUse418' ) ) {
80+
$this->denyAccessWith418();
81+
} else {
82+
$this->denyAccessRaw(
83+
$this->options->get( 'CrawlerProtectionRawDenialHeader' ),
84+
$this->options->get( 'CrawlerProtectionRawDenialText' )
85+
);
86+
}
7787
} else {
7888
$this->denyAccessPretty( $output );
7989
}

tests/phpunit/unit/ResponseFactoryTest.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@ public function testDenyAccessPrettySetStatusCode() {
6767
/**
6868
* @covers ::denyAccess
6969
*/
70-
public function testDenyAccessChooses418WhenConfigured() {
70+
public function testDenyAccessChooses418WhenBothRawAndUse418() {
7171
$factory = $this->getMockBuilder( ResponseFactory::class )
7272
->setConstructorArgs( [
7373
new ServiceOptions( ResponseFactory::CONSTRUCTOR_OPTIONS, [
7474
'CrawlerProtectionUse418' => true,
75-
'CrawlerProtectionRawDenial' => false,
75+
'CrawlerProtectionRawDenial' => true,
7676
'CrawlerProtectionRawDenialHeader' => '',
7777
'CrawlerProtectionRawDenialText' => '',
7878
] )
@@ -86,6 +86,35 @@ public function testDenyAccessChooses418WhenConfigured() {
8686
$factory->denyAccess( $output );
8787
}
8888

89+
/**
90+
* When RawDenial is false, Use418 should be a no-op and the factory
91+
* must fall through to the pretty 403 page.
92+
*
93+
* @covers ::denyAccess
94+
*/
95+
public function testDenyAccessIgnoresUse418WhenRawDenialDisabled() {
96+
$factory = $this->getMockBuilder( ResponseFactory::class )
97+
->setConstructorArgs( [
98+
new ServiceOptions( ResponseFactory::CONSTRUCTOR_OPTIONS, [
99+
'CrawlerProtectionUse418' => true,
100+
'CrawlerProtectionRawDenial' => false,
101+
'CrawlerProtectionRawDenialHeader' => '',
102+
'CrawlerProtectionRawDenialText' => '',
103+
] )
104+
] )
105+
->onlyMethods( [ 'denyAccessPretty', 'denyAccessWith418' ] )
106+
->getMock();
107+
108+
$factory->expects( $this->never() )->method( 'denyAccessWith418' );
109+
110+
$output = $this->createMock( self::$outputPageClassName );
111+
$factory->expects( $this->once() )
112+
->method( 'denyAccessPretty' )
113+
->with( $output );
114+
115+
$factory->denyAccess( $output );
116+
}
117+
89118
/**
90119
* @covers ::denyAccess
91120
*/

0 commit comments

Comments
 (0)