Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 52 additions & 57 deletions tests/system/HTTP/IncomingRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
use PHPUnit\Framework\Attributes\PreserveGlobalState;
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
use PHPUnit\Framework\Attributes\WithoutErrorHandler;
use TypeError;

/**
* @internal
Expand All @@ -38,7 +37,7 @@
#[Group('SeparateProcess')]
final class IncomingRequestTest extends CIUnitTestCase
{
private Request $request;
private IncomingRequest $request;

#[WithoutErrorHandler]
protected function setUp(): void
Expand All @@ -59,7 +58,7 @@ protected function setUp(): void
$this->request = $this->createRequest($config);
}

private function createRequest(?App $config = null, $body = null, ?string $path = null): IncomingRequest
private function createRequest(?App $config = null, false|string|null $body = null, ?string $path = null): IncomingRequest
{
$config ??= new App();
$path ??= '';
Expand Down Expand Up @@ -635,6 +634,9 @@ public function testCanGrabGetRawInputVar($rawstring, $var, $expected, $filter,
$this->assertSame($expected, $request->getRawInputVar($var, $filter, $flag));
}

/**
* @return iterable<array{string, mixed, mixed, mixed, mixed}>
*/
public static function provideCanGrabGetRawInputVar(): iterable
{
return [
Expand Down Expand Up @@ -729,6 +731,9 @@ public function testIsHTTPMethodLowerCase(string $value): void
$this->assertTrue($request->is(strtolower($value)));
}

/**
* @return iterable<array{string}>
*/
public static function provideIsHTTPMethods(): iterable
{
yield from [
Expand Down Expand Up @@ -756,21 +761,22 @@ public function testIsInvalidValue(): void
$this->expectExceptionMessage('Unknown type: invalid');

$request = $this->request->withMethod('GET');

$request->is('invalid');
}

public function testIsJson(): void
{
$request = $this->request->setHeader('Content-Type', 'application/json');

$this->assertInstanceOf(IncomingRequest::class, $request);
$this->assertTrue($request->is('json'));
}

public function testIsWithAjax(): void
{
$request = $this->request->setHeader('X-Requested-With', 'XMLHttpRequest');

$this->assertInstanceOf(IncomingRequest::class, $request);
$this->assertTrue($request->is('ajax'));
}

Expand Down Expand Up @@ -950,6 +956,7 @@ public function testGetFile(): void
]);

$gotit = $this->request->getFile('userfile');
$this->assertInstanceOf(UploadedFile::class, $gotit);
$this->assertSame(124, $gotit->getSize());
}

Expand Down Expand Up @@ -1047,12 +1054,12 @@ public function testGetIPAddressNormal(): void
$expected = '123.123.123.123';
service('superglobals')->setServer('REMOTE_ADDR', $expected);

$this->request = new Request(new App());
$this->request->populateHeaders();
$request = new Request(new App());
$request->populateHeaders();

$this->assertSame($expected, $this->request->getIPAddress());
$this->assertSame($expected, $request->getIPAddress());
// call a second time to exercise the initial conditional block in getIPAddress()
$this->assertSame($expected, $this->request->getIPAddress());
$this->assertSame($expected, $request->getIPAddress());
}

public function testGetIPAddressThruProxy(): void
Expand All @@ -1068,11 +1075,11 @@ public function testGetIPAddressThruProxy(): void
'192.168.5.0/24' => 'X-Forwarded-For',
];
Factories::injectMock('config', App::class, $config);
$this->request = new Request();
$this->request->populateHeaders();
$request = new Request();
$request->populateHeaders();

// we should see the original forwarded address
$this->assertSame($expected, $this->request->getIPAddress());
$this->assertSame($expected, $request->getIPAddress());
}

public function testGetIPAddressThruProxyIPv6(): void
Expand All @@ -1087,11 +1094,11 @@ public function testGetIPAddressThruProxyIPv6(): void
'2001:db8::2:1' => 'X-Forwarded-For',
];
Factories::injectMock('config', App::class, $config);
$this->request = new Request();
$this->request->populateHeaders();
$request = new Request();
$request->populateHeaders();

// we should see the original forwarded address
$this->assertSame($expected, $this->request->getIPAddress());
$this->assertSame($expected, $request->getIPAddress());
}

public function testGetIPAddressThruProxyInvalidIPAddress(): void
Expand All @@ -1105,11 +1112,11 @@ public function testGetIPAddressThruProxyInvalidIPAddress(): void
'10.0.1.200' => 'X-Forwarded-For',
'192.168.5.0/24' => 'X-Forwarded-For',
];
$this->request = new Request($config);
$this->request->populateHeaders();
$request = new Request($config);
$request->populateHeaders();

// spoofed address invalid
$this->assertSame($expected, $this->request->getIPAddress());
$this->assertSame($expected, $request->getIPAddress());
}

public function testGetIPAddressThruProxyInvalidIPAddressIPv6(): void
Expand All @@ -1122,11 +1129,11 @@ public function testGetIPAddressThruProxyInvalidIPAddressIPv6(): void
$config->proxyIPs = [
'2001:db8::2:1' => 'X-Forwarded-For',
];
$this->request = new Request($config);
$this->request->populateHeaders();
$request = new Request($config);
$request->populateHeaders();

// spoofed address invalid
$this->assertSame($expected, $this->request->getIPAddress());
$this->assertSame($expected, $request->getIPAddress());
}

public function testGetIPAddressThruProxyNotWhitelisted(): void
Expand All @@ -1140,11 +1147,11 @@ public function testGetIPAddressThruProxyNotWhitelisted(): void
'10.0.1.200' => 'X-Forwarded-For',
'192.168.5.0/24' => 'X-Forwarded-For',
];
$this->request = new Request($config);
$this->request->populateHeaders();
$request = new Request($config);
$request->populateHeaders();

// spoofed address invalid
$this->assertSame($expected, $this->request->getIPAddress());
$this->assertSame($expected, $request->getIPAddress());
}

public function testGetIPAddressThruProxyNotWhitelistedIPv6(): void
Expand All @@ -1157,11 +1164,11 @@ public function testGetIPAddressThruProxyNotWhitelistedIPv6(): void
$config->proxyIPs = [
'2001:db8::2:1' => 'X-Forwarded-For',
];
$this->request = new Request($config);
$this->request->populateHeaders();
$request = new Request($config);
$request->populateHeaders();

// spoofed address invalid
$this->assertSame($expected, $this->request->getIPAddress());
$this->assertSame($expected, $request->getIPAddress());
}

public function testGetIPAddressThruProxySubnet(): void
Expand All @@ -1173,11 +1180,11 @@ public function testGetIPAddressThruProxySubnet(): void
$config = new App();
$config->proxyIPs = ['192.168.5.0/24' => 'X-Forwarded-For'];
Factories::injectMock('config', App::class, $config);
$this->request = new Request();
$this->request->populateHeaders();
$request = new Request();
$request->populateHeaders();

// we should see the original forwarded address
$this->assertSame($expected, $this->request->getIPAddress());
$this->assertSame($expected, $request->getIPAddress());
}

public function testGetIPAddressThruProxySubnetIPv6(): void
Expand All @@ -1189,11 +1196,11 @@ public function testGetIPAddressThruProxySubnetIPv6(): void
$config = new App();
$config->proxyIPs = ['2001:db8:1234::/48' => 'X-Forwarded-For'];
Factories::injectMock('config', App::class, $config);
$this->request = new Request();
$this->request->populateHeaders();
$request = new Request();
$request->populateHeaders();

// we should see the original forwarded address
$this->assertSame($expected, $this->request->getIPAddress());
$this->assertSame($expected, $request->getIPAddress());
}

public function testGetIPAddressThruProxyOutOfSubnet(): void
Expand All @@ -1204,11 +1211,11 @@ public function testGetIPAddressThruProxyOutOfSubnet(): void

$config = new App();
$config->proxyIPs = ['192.168.5.0/28' => 'X-Forwarded-For'];
$this->request = new Request($config);
$this->request->populateHeaders();
$request = new Request($config);
$request->populateHeaders();

// we should see the original forwarded address
$this->assertSame($expected, $this->request->getIPAddress());
$this->assertSame($expected, $request->getIPAddress());
}

public function testGetIPAddressThruProxyOutOfSubnetIPv6(): void
Expand All @@ -1219,11 +1226,11 @@ public function testGetIPAddressThruProxyOutOfSubnetIPv6(): void

$config = new App();
$config->proxyIPs = ['2001:db8:1234::/48' => 'X-Forwarded-For'];
$this->request = new Request($config);
$this->request->populateHeaders();
$request = new Request($config);
$request->populateHeaders();

// we should see the original forwarded address
$this->assertSame($expected, $this->request->getIPAddress());
$this->assertSame($expected, $request->getIPAddress());
}

public function testGetIPAddressThruProxyBothIPv4AndIPv6(): void
Expand All @@ -1237,23 +1244,11 @@ public function testGetIPAddressThruProxyBothIPv4AndIPv6(): void
'192.168.5.0/28' => 'X-Forwarded-For',
'2001:db8:1234::/48' => 'X-Forwarded-For',
];
$this->request = new Request($config);
$this->request->populateHeaders();
$request = new Request($config);
$request->populateHeaders();

// we should see the original forwarded address
$this->assertSame($expected, $this->request->getIPAddress());
}

public function testGetIPAddressThruProxyInvalidConfigString(): void
{
$this->expectException(TypeError::class);

$config = new App();
$config->proxyIPs = '192.168.5.0/28';
$this->request = new Request($config);
$this->request->populateHeaders();

$this->request->getIPAddress();
$this->assertSame($expected, $request->getIPAddress());
}

public function testGetIPAddressThruProxyInvalidConfigArray(): void
Expand All @@ -1264,12 +1259,12 @@ public function testGetIPAddressThruProxyInvalidConfigArray(): void
);

$config = new App();
$config->proxyIPs = ['192.168.5.0/28'];
$config->proxyIPs = ['192.168.5.0/28']; // @phpstan-ignore assign.propertyType (deliberately keyless, to assert the ConfigException)
Factories::injectMock('config', App::class, $config);
$this->request = new Request();
$this->request->populateHeaders();
$request = new Request();
$request->populateHeaders();

$this->request->getIPAddress();
$request->getIPAddress();
}

// @TODO getIPAddress should have more testing, to 100% code coverage
Expand Down
12 changes: 1 addition & 11 deletions utils/phpstan-baseline/assign.propertyType.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# total 28 errors
# total 26 errors

parameters:
ignoreErrors:
Expand Down Expand Up @@ -57,16 +57,6 @@ parameters:
count: 4
path: ../../tests/system/Filters/HoneypotTest.php

-
message: '#^Property Config\\App\:\:\$proxyIPs \(array\<string, string\>\) does not accept array\<int, string\>\.$#'
count: 1
path: ../../tests/system/HTTP/IncomingRequestTest.php

-
message: '#^Property Config\\App\:\:\$proxyIPs \(array\<string, string\>\) does not accept string\.$#'
count: 1
path: ../../tests/system/HTTP/IncomingRequestTest.php

-
message: '#^Property CodeIgniter\\Helpers\\CookieHelperTest\:\:\$response \(CodeIgniter\\HTTP\\Response\) does not accept CodeIgniter\\HTTP\\ResponseInterface\.$#'
count: 1
Expand Down
2 changes: 1 addition & 1 deletion utils/phpstan-baseline/loader.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# total 1434 errors
# total 1381 errors

includes:
- argument.type.neon
Expand Down
Loading
Loading