Skip to content

Commit

Permalink
test: update tests to use SiteURI
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Nov 12, 2023
1 parent a1f8ae6 commit 2751f48
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 96 deletions.
15 changes: 8 additions & 7 deletions tests/system/API/ResponseTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use CodeIgniter\Format\FormatterInterface;
use CodeIgniter\Format\JSONFormatter;
use CodeIgniter\Format\XMLFormatter;
use CodeIgniter\HTTP\URI;
use CodeIgniter\HTTP\SiteURI;
use CodeIgniter\HTTP\UserAgent;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Mock\MockIncomingRequest;
Expand All @@ -42,7 +42,7 @@ protected function setUp(): void
$this->formatter = new JSONFormatter();
}

protected function makeController(array $userConfig = [], string $uri = 'http://example.com', array $userHeaders = [])
protected function makeController(array $userConfig = [], string $routePath = '', array $userHeaders = [])
{
$config = new App();

Expand Down Expand Up @@ -73,7 +73,7 @@ protected function makeController(array $userConfig = [], string $uri = 'http://
Factories::injectMock('config', 'Cookie', $cookie);

if ($this->request === null) {
$this->request = new MockIncomingRequest($config, new URI($uri), null, new UserAgent());
$this->request = new MockIncomingRequest($config, new SiteURI($config, $routePath), null, new UserAgent());
$this->response = new MockResponse($config);
}

Expand Down Expand Up @@ -115,7 +115,7 @@ public function resetFormatter(): void
public function testNoFormatterJSON(): void
{
$this->formatter = null;
$controller = $this->makeController([], 'http://codeigniter.com', ['Accept' => 'application/json']);
$controller = $this->makeController([], '', ['Accept' => 'application/json']);

$this->invoke($controller, 'respondCreated', [['id' => 3], 'A Custom Reason']);

Expand All @@ -133,7 +133,7 @@ public function testNoFormatterJSON(): void
public function testNoFormatter(): void
{
$this->formatter = null;
$controller = $this->makeController([], 'http://codeigniter.com', ['Accept' => 'application/json']);
$controller = $this->makeController([], '', ['Accept' => 'application/json']);

$this->invoke($controller, 'respondCreated', ['A Custom Reason']);

Expand Down Expand Up @@ -484,8 +484,9 @@ private function tryValidContentType($mimeType, $contentType): void
$original = $_SERVER;
$_SERVER['CONTENT_TYPE'] = $mimeType;

$this->makeController([], 'http://codeigniter.com', ['Accept' => $mimeType]);
$this->makeController([], '', ['Accept' => $mimeType]);
$this->assertSame($mimeType, $this->request->getHeaderLine('Accept'), 'Request header...');

$this->response->setContentType($contentType);
$this->assertSame($contentType, $this->response->getHeaderLine('Content-Type'), 'Response header pre-response...');

Expand Down Expand Up @@ -554,7 +555,7 @@ public function testFormatByRequestNegotiateIfFormatIsNotJsonOrXML(): void
}
Factories::injectMock('config', 'Cookie', $cookie);

$request = new MockIncomingRequest($config, new URI($config->baseURL), null, new UserAgent());
$request = new MockIncomingRequest($config, new SiteURI($config), null, new UserAgent());
$response = new MockResponse($config);

$controller = new class ($request, $response) {
Expand Down
3 changes: 2 additions & 1 deletion tests/system/Cache/ResponseCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\Response;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\HTTP\SiteURI;
use CodeIgniter\HTTP\URI;
use CodeIgniter\HTTP\UserAgent;
use CodeIgniter\Test\CIUnitTestCase;
Expand Down Expand Up @@ -53,7 +54,7 @@ private function createIncomingRequest(

$appConfig ??= $this->appConfig;

$siteUri = new URI($appConfig->baseURL . $uri);
$siteUri = new SiteURI($appConfig, $uri);
if ($query !== []) {
$_GET = $_REQUEST = $query;
$siteUri->setQueryArray($query);
Expand Down
4 changes: 2 additions & 2 deletions tests/system/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\Request;
use CodeIgniter\HTTP\Response;
use CodeIgniter\HTTP\URI;
use CodeIgniter\HTTP\SiteURI;
use CodeIgniter\HTTP\UserAgent;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Validation\Exceptions\ValidationException;
Expand Down Expand Up @@ -58,7 +58,7 @@ protected function setUp(): void
parent::setUp();

$this->config = new App();
$this->request = new IncomingRequest($this->config, new URI('https://somwhere.com'), null, new UserAgent());
$this->request = new IncomingRequest($this->config, new SiteURI($this->config), null, new UserAgent());
$this->response = new Response($this->config);
$this->logger = Services::logger();
}
Expand Down
4 changes: 2 additions & 2 deletions tests/system/Filters/InvalidCharsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use CodeIgniter\HTTP\CLIRequest;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\URI;
use CodeIgniter\HTTP\SiteURI;
use CodeIgniter\HTTP\UserAgent;
use CodeIgniter\Security\Exceptions\SecurityException;
use CodeIgniter\Test\CIUnitTestCase;
Expand Down Expand Up @@ -53,7 +53,7 @@ protected function tearDown(): void
private function createRequest(): IncomingRequest
{
$config = new MockAppConfig();
$uri = new URI();
$uri = new SiteURI($config);
$userAgent = new UserAgent();
$request = $this->getMockBuilder(IncomingRequest::class)
->setConstructorArgs([$config, $uri, null, $userAgent])
Expand Down
3 changes: 1 addition & 2 deletions tests/system/HTTP/IncomingRequestDetectingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ protected function setUp(): void
$_POST = $_GET = $_SERVER = $_REQUEST = $_ENV = $_COOKIE = $_SESSION = [];

// The URI object is not used in detectPath().
$origin = 'http://www.example.com/index.php/woot?code=good#pos';
$this->request = new IncomingRequest(new App(), new URI($origin), null, new UserAgent());
$this->request = new IncomingRequest(new App(), new SiteURI(new App(), 'woot?code=good#pos'), null, new UserAgent());
}

public function testPathDefault(): void
Expand Down
4 changes: 2 additions & 2 deletions tests/system/Helpers/CookieHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use CodeIgniter\Cookie\Exceptions\CookieException;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\Response;
use CodeIgniter\HTTP\URI;
use CodeIgniter\HTTP\SiteURI;
use CodeIgniter\HTTP\UserAgent;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Mock\MockResponse;
Expand Down Expand Up @@ -49,7 +49,7 @@ protected function setUp(): void

Services::injectMock('response', new MockResponse(new App()));
$this->response = Services::response();
$this->request = new IncomingRequest(new App(), new URI(), null, new UserAgent());
$this->request = new IncomingRequest(new App(), new SiteURI(new App()), null, new UserAgent());
Services::injectMock('request', $this->request);

helper('cookie');
Expand Down
7 changes: 4 additions & 3 deletions tests/system/Pager/PagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use CodeIgniter\Config\Factories;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\SiteURI;
use CodeIgniter\HTTP\URI;
use CodeIgniter\HTTP\UserAgent;
use CodeIgniter\Pager\Exceptions\PagerException;
Expand Down Expand Up @@ -53,7 +54,7 @@ private function createPager(string $requestUri): void

$request = new IncomingRequest(
$config,
new URI($config->baseURL . ltrim($requestUri, '/')),
new SiteURI($config, ltrim($requestUri, '/')),
'php://input',
new UserAgent()
);
Expand All @@ -70,7 +71,7 @@ public function testSetPathRemembersPath(): void

$details = $this->pager->getDetails();

$this->assertSame('foo/bar', $details['uri']->getPath());
$this->assertSame('foo/bar', $details['uri']->getRoutePath());
}

public function testGetDetailsRecognizesPageQueryVar(): void
Expand Down Expand Up @@ -474,7 +475,7 @@ public function testBasedURI(): void

$request = new IncomingRequest(
$config,
new URI(),
new SiteURI($config, 'x/y'),
'php://input',
new UserAgent()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use CodeIgniter\Config\Factories;
use CodeIgniter\Cookie\Cookie;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\URI;
use CodeIgniter\HTTP\SiteURI;
use CodeIgniter\HTTP\UserAgent;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Mock\MockAppConfig;
Expand Down Expand Up @@ -74,7 +74,8 @@ public function testCSRFVerifySetNewCookie(): void
$_POST['foo'] = 'bar';
$_POST['csrf_test_name'] = $this->randomizedToken;

$request = new IncomingRequest(new MockAppConfig(), new URI('http://badurl.com'), null, new UserAgent());
$config = new MockAppConfig();
$request = new IncomingRequest($config, new SiteURI($config), null, new UserAgent());

$security = new Security($this->config);

Expand Down
91 changes: 24 additions & 67 deletions tests/system/Security/SecurityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use CodeIgniter\Config\Factories;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\Request;
use CodeIgniter\HTTP\URI;
use CodeIgniter\HTTP\SiteURI;
use CodeIgniter\HTTP\UserAgent;
use CodeIgniter\Security\Exceptions\SecurityException;
use CodeIgniter\Test\CIUnitTestCase;
Expand Down Expand Up @@ -101,17 +101,24 @@ public function testCSRFVerifyPostThrowsExceptionOnNoMatch(): void
$_COOKIE['csrf_cookie_name'] = '8b9218a55906f9dcc1dc263dce7f005b';

$security = $this->createMockSecurity();
$request = new IncomingRequest(
new MockAppConfig(),
new URI('http://badurl.com'),
null,
new UserAgent()
);
$request = $this->createIncomingRequest();

$this->expectException(SecurityException::class);
$security->verify($request);
}

private function createIncomingRequest(): IncomingRequest
{
$config = new MockAppConfig();

return new IncomingRequest(
$config,
new SiteURI($config),
null,
new UserAgent()
);
}

public function testCSRFVerifyPostReturnsSelfOnMatch(): void
{
$_SERVER['REQUEST_METHOD'] = 'POST';
Expand All @@ -120,12 +127,7 @@ public function testCSRFVerifyPostReturnsSelfOnMatch(): void
$_COOKIE['csrf_cookie_name'] = '8b9218a55906f9dcc1dc263dce7f005a';

$security = $this->createMockSecurity();
$request = new IncomingRequest(
new MockAppConfig(),
new URI('http://badurl.com'),
null,
new UserAgent()
);
$request = $this->createIncomingRequest();

$this->assertInstanceOf(Security::class, $security->verify($request));
$this->assertLogged('info', 'CSRF token verified.');
Expand All @@ -139,12 +141,7 @@ public function testCSRFVerifyHeaderThrowsExceptionOnNoMatch(): void
$_COOKIE['csrf_cookie_name'] = '8b9218a55906f9dcc1dc263dce7f005b';

$security = $this->createMockSecurity();
$request = new IncomingRequest(
new MockAppConfig(),
new URI('http://badurl.com'),
null,
new UserAgent()
);
$request = $this->createIncomingRequest();

$request->setHeader('X-CSRF-TOKEN', '8b9218a55906f9dcc1dc263dce7f005a');

Expand All @@ -159,12 +156,7 @@ public function testCSRFVerifyHeaderReturnsSelfOnMatch(): void
$_COOKIE['csrf_cookie_name'] = '8b9218a55906f9dcc1dc263dce7f005a';

$security = $this->createMockSecurity();
$request = new IncomingRequest(
new MockAppConfig(),
new URI('http://badurl.com'),
null,
new UserAgent()
);
$request = $this->createIncomingRequest();

$request->setHeader('X-CSRF-TOKEN', '8b9218a55906f9dcc1dc263dce7f005a');

Expand All @@ -180,12 +172,7 @@ public function testCSRFVerifyJsonThrowsExceptionOnNoMatch(): void
$_COOKIE['csrf_cookie_name'] = '8b9218a55906f9dcc1dc263dce7f005b';

$security = $this->createMockSecurity();
$request = new IncomingRequest(
new MockAppConfig(),
new URI('http://badurl.com'),
null,
new UserAgent()
);
$request = $this->createIncomingRequest();

$request->setBody(
'{"csrf_test_name":"8b9218a55906f9dcc1dc263dce7f005a"}'
Expand All @@ -201,12 +188,7 @@ public function testCSRFVerifyJsonReturnsSelfOnMatch(): void
$_COOKIE['csrf_cookie_name'] = '8b9218a55906f9dcc1dc263dce7f005a';

$security = $this->createMockSecurity();
$request = new IncomingRequest(
new MockAppConfig(),
new URI('http://badurl.com'),
null,
new UserAgent()
);
$request = $this->createIncomingRequest();

$request->setBody(
'{"csrf_test_name":"8b9218a55906f9dcc1dc263dce7f005a","foo":"bar"}'
Expand All @@ -224,12 +206,7 @@ public function testCSRFVerifyPutBodyThrowsExceptionOnNoMatch(): void
$_COOKIE['csrf_cookie_name'] = '8b9218a55906f9dcc1dc263dce7f005b';

$security = $this->createMockSecurity();
$request = new IncomingRequest(
new MockAppConfig(),
new URI('http://badurl.com'),
null,
new UserAgent()
);
$request = $this->createIncomingRequest();

$request->setBody(
'csrf_test_name=8b9218a55906f9dcc1dc263dce7f005a'
Expand All @@ -245,12 +222,7 @@ public function testCSRFVerifyPutBodyReturnsSelfOnMatch(): void
$_COOKIE['csrf_cookie_name'] = '8b9218a55906f9dcc1dc263dce7f005a';

$security = $this->createMockSecurity();
$request = new IncomingRequest(
new MockAppConfig(),
new URI('http://badurl.com'),
null,
new UserAgent()
);
$request = $this->createIncomingRequest();

$request->setBody(
'csrf_test_name=8b9218a55906f9dcc1dc263dce7f005a&foo=bar'
Expand Down Expand Up @@ -282,12 +254,7 @@ public function testRegenerateWithFalseSecurityRegenerateProperty(): void
Factories::injectMock('config', 'Security', $config);

$security = new MockSecurity($config);
$request = new IncomingRequest(
new MockAppConfig(),
new URI('http://badurl.com'),
null,
new UserAgent()
);
$request = $this->createIncomingRequest();

$oldHash = $security->getHash();
$security->verify($request);
Expand All @@ -307,12 +274,7 @@ public function testRegenerateWithFalseSecurityRegeneratePropertyManually(): voi
Factories::injectMock('config', 'Security', $config);

$security = $this->createMockSecurity($config);
$request = new IncomingRequest(
new MockAppConfig(),
new URI('http://badurl.com'),
null,
new UserAgent()
);
$request = $this->createIncomingRequest();

$oldHash = $security->getHash();
$security->verify($request);
Expand All @@ -333,12 +295,7 @@ public function testRegenerateWithTrueSecurityRegenerateProperty(): void
Factories::injectMock('config', 'Security', $config);

$security = $this->createMockSecurity($config);
$request = new IncomingRequest(
new MockAppConfig(),
new URI('http://badurl.com'),
null,
new UserAgent()
);
$request = $this->createIncomingRequest();

$oldHash = $security->getHash();
$security->verify($request);
Expand Down
Loading

0 comments on commit 2751f48

Please sign in to comment.