From ad5775f0c5322d2297c7fbddac10c7acac3bc042 Mon Sep 17 00:00:00 2001 From: Jim Parry Date: Sun, 2 Dec 2018 21:54:35 -0800 Subject: [PATCH 1/3] DownloadResponse testing completed --- system/HTTP/DownloadResponse.php | 2 +- tests/system/HTTP/DownloadResponseTest.php | 54 +++++++++++++++++++++- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/system/HTTP/DownloadResponse.php b/system/HTTP/DownloadResponse.php index 3b8529115bf1..6d72601067c5 100644 --- a/system/HTTP/DownloadResponse.php +++ b/system/HTTP/DownloadResponse.php @@ -399,7 +399,7 @@ public function sendHeaders() // Per spec, MUST be sent with each request, if possible. // http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html - if (isset($this->headers['Date'])) + if (! isset($this->headers['Date'])) { $this->setDate(\DateTime::createFromFormat('U', time())); } diff --git a/tests/system/HTTP/DownloadResponseTest.php b/tests/system/HTTP/DownloadResponseTest.php index 78c616977810..92b00dbae495 100644 --- a/tests/system/HTTP/DownloadResponseTest.php +++ b/tests/system/HTTP/DownloadResponseTest.php @@ -1,4 +1,5 @@ -expectException(DownloadException::class); $response->sendBody(); } + + //-------------------------------------------------------------------- + public function testGetReason() + { + $response = new DownloadResponse('unit-test.php', false); + $this->assertEquals('OK', $response->getReason()); + } + + //-------------------------------------------------------------------- + public function testPretendOutput() + { + $response = new DownloadResponse('unit-test.php', false); + $response->pretend(true); + + $response->setFilePath(__FILE__); + + ob_start(); + $response->send(); + $actual = ob_get_contents(); + ob_end_clean(); + + $this->assertSame(file_get_contents(__FILE__), $actual); + } + + //-------------------------------------------------------------------- + /** + * @runInSeparateProcess + * @preserveGlobalState disabled + */ + public function testRealOutput() + { + $response = new DownloadResponse('unit-test.php', false); + $response->pretend(false); + $response->setFilePath(__FILE__); + + // send it + ob_start(); + $response->send(); + + $buffer = ob_clean(); + if (ob_get_level() > 0) + { + ob_end_clean(); + } + + // and what actually got sent? + $this->assertHeaderEmitted('Content-Length: ' . filesize(__FILE__)); + $this->assertHeaderEmitted('Date:'); + } + } From 4a454831f8f75d22b1dbe0a5b0576d5a9ee75e57 Mon Sep 17 00:00:00 2001 From: Jim Parry Date: Sun, 2 Dec 2018 21:59:39 -0800 Subject: [PATCH 2/3] Response testing completed --- tests/system/HTTP/ResponseTest.php | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/tests/system/HTTP/ResponseTest.php b/tests/system/HTTP/ResponseTest.php index 0b8e0521e229..12dc386d5deb 100644 --- a/tests/system/HTTP/ResponseTest.php +++ b/tests/system/HTTP/ResponseTest.php @@ -165,24 +165,21 @@ public function testSetLink() $response->setLink($pager); $this->assertEquals( - '; rel="first",; rel="prev",; rel="next",; rel="last"', - $response->getHeader('Link')->getValue() + '; rel="first",; rel="prev",; rel="next",; rel="last"', $response->getHeader('Link')->getValue() ); $pager->store('default', 1, 10, 200); $response->setLink($pager); $this->assertEquals( - '; rel="next",; rel="last"', - $response->getHeader('Link')->getValue() + '; rel="next",; rel="last"', $response->getHeader('Link')->getValue() ); $pager->store('default', 20, 10, 200); $response->setLink($pager); $this->assertEquals( - '; rel="first",; rel="prev"', - $response->getHeader('Link')->getValue() + '; rel="first",; rel="prev"', $response->getHeader('Link')->getValue() ); } @@ -529,4 +526,21 @@ public function testRedirectResponseCookies() $this->assertTrue($answer1->hasCookie('login_time')); } + //-------------------------------------------------------------------- + // Make sure we don't blow up if pretending to send headers + public function testPretendOutput() + { + $response = new Response(new App()); + $response->pretend(true); + + $response->setBody('Happy days'); + + ob_start(); + $response->send(); + $actual = ob_get_contents(); + ob_end_clean(); + + $this->assertEquals('Happy days', $actual); + } + } From fd9c820f8e64a5d60bb85750f3cfc05befd58ac1 Mon Sep 17 00:00:00 2001 From: Jim Parry Date: Sun, 2 Dec 2018 22:13:52 -0800 Subject: [PATCH 3/3] IncomingRequest testing completed --- .../HTTP/IncomingRequestDetectingTest.php | 9 ++++++ tests/system/HTTP/IncomingRequestTest.php | 32 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/tests/system/HTTP/IncomingRequestDetectingTest.php b/tests/system/HTTP/IncomingRequestDetectingTest.php index 9e7b40c71e1c..09c29260a0ef 100644 --- a/tests/system/HTTP/IncomingRequestDetectingTest.php +++ b/tests/system/HTTP/IncomingRequestDetectingTest.php @@ -35,6 +35,15 @@ public function testPathDefault() $this->assertEquals($expected, $this->request->detectPath()); } + public function testPathEmpty() + { + $this->request->uri = '/'; + $_SERVER['REQUEST_URI'] = '/'; + $_SERVER['SCRIPT_NAME'] = '/index.php'; + $expected = '/'; + $this->assertEquals($expected, $this->request->detectPath()); + } + public function testPathRequestURI() { $this->request->uri = '/index.php/woot?code=good#pos'; diff --git a/tests/system/HTTP/IncomingRequestTest.php b/tests/system/HTTP/IncomingRequestTest.php index 395593744038..9f061db11e7b 100644 --- a/tests/system/HTTP/IncomingRequestTest.php +++ b/tests/system/HTTP/IncomingRequestTest.php @@ -61,6 +61,11 @@ public function testCanGrabPostBeforeGet() //-------------------------------------------------------------------- + public function testNoOldInput() + { + $this->assertNull($this->request->getOldInput('name')); + } + public function testCanGetOldInput() { $_SESSION['_ci_old_input'] = [ @@ -83,6 +88,16 @@ public function testCanGetOldInputDotted() $this->assertEquals('two', $this->request->getOldInput('apple.name')); } + public function testMissingOldInput() + { + $_SESSION['_ci_old_input'] = [ + 'get' => ['apple' => ['name' => 'two']], + 'post' => ['banana' => ['name' => 'foo']], + ]; + + $this->assertNull($this->request->getOldInput('pineapple.name')); + } + // Reference: https://github.com/codeigniter4/CodeIgniter4/issues/1492 public function testCanGetOldInputArray() { @@ -349,4 +364,21 @@ public function testFileCollectionFactory() } //-------------------------------------------------------------------- + + public function testGetFile() + { + $_FILES = [ + 'userfile' => [ + 'name' => 'someFile.txt', + 'type' => 'text/plain', + 'size' => '124', + 'tmp_name' => '/tmp/myTempFile.txt', + 'error' => 0, + ], + ]; + + $gotit = $this->request->getFile('userfile'); + $this->assertEquals(124, $gotit->getSize()); + } + }