Skip to content

Commit

Permalink
Merge pull request #1576 from jim-parry/testing13/http
Browse files Browse the repository at this point in the history
Testing13/http
  • Loading branch information
jim-parry authored Dec 3, 2018
2 parents 4075f00 + fd9c820 commit 3b31bfd
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 8 deletions.
2 changes: 1 addition & 1 deletion system/HTTP/DownloadResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
Expand Down
54 changes: 53 additions & 1 deletion tests/system/HTTP/DownloadResponseTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php namespace CodeIgniter\HTTP;
<?php
namespace CodeIgniter\HTTP;

use CodeIgniter\Files\Exceptions\FileNotFoundException;
use DateTime;
Expand All @@ -7,6 +8,7 @@

class DownloadResponseTest extends \CIUnitTestCase
{

public function tearDown()
{
if (isset($_SERVER['HTTP_USER_AGENT']))
Expand Down Expand Up @@ -250,4 +252,54 @@ public function testThrowExceptionWhenNoSetDownloadSource()
$this->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:');
}

}
9 changes: 9 additions & 0 deletions tests/system/HTTP/IncomingRequestDetectingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
32 changes: 32 additions & 0 deletions tests/system/HTTP/IncomingRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public function testCanGrabPostBeforeGet()

//--------------------------------------------------------------------

public function testNoOldInput()
{
$this->assertNull($this->request->getOldInput('name'));
}

public function testCanGetOldInput()
{
$_SESSION['_ci_old_input'] = [
Expand All @@ -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()
{
Expand Down Expand Up @@ -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());
}

}
26 changes: 20 additions & 6 deletions tests/system/HTTP/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,24 +165,21 @@ public function testSetLink()
$response->setLink($pager);

$this->assertEquals(
'<http://example.com?page=1>; rel="first",<http://example.com?page=2>; rel="prev",<http://example.com?page=4>; rel="next",<http://example.com?page=20>; rel="last"',
$response->getHeader('Link')->getValue()
'<http://example.com?page=1>; rel="first",<http://example.com?page=2>; rel="prev",<http://example.com?page=4>; rel="next",<http://example.com?page=20>; rel="last"', $response->getHeader('Link')->getValue()
);

$pager->store('default', 1, 10, 200);
$response->setLink($pager);

$this->assertEquals(
'<http://example.com?page=2>; rel="next",<http://example.com?page=20>; rel="last"',
$response->getHeader('Link')->getValue()
'<http://example.com?page=2>; rel="next",<http://example.com?page=20>; rel="last"', $response->getHeader('Link')->getValue()
);

$pager->store('default', 20, 10, 200);
$response->setLink($pager);

$this->assertEquals(
'<http://example.com?page=1>; rel="first",<http://example.com?page=19>; rel="prev"',
$response->getHeader('Link')->getValue()
'<http://example.com?page=1>; rel="first",<http://example.com?page=19>; rel="prev"', $response->getHeader('Link')->getValue()
);
}

Expand Down Expand Up @@ -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);
}

}

0 comments on commit 3b31bfd

Please sign in to comment.