Skip to content

Commit

Permalink
Merge pull request #1472 from jim-parry/testing6/http
Browse files Browse the repository at this point in the history
HTTP\Response cookie testing & missing functionality
  • Loading branch information
jim-parry authored Nov 12, 2018
2 parents b92bdc7 + c03fd42 commit a7d36b5
Show file tree
Hide file tree
Showing 4 changed files with 335 additions and 7 deletions.
26 changes: 23 additions & 3 deletions system/HTTP/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@ public function hasCookie(string $name, $value = null, string $prefix = '')

foreach ($this->cookies as $cookie)
{
if ($cookie['name'] !== $prefix . $name)
if ($cookie['name'] !== $name)
{
continue;
}
Expand All @@ -894,8 +894,14 @@ public function hasCookie(string $name, $value = null, string $prefix = '')
*
* @return mixed
*/
public function getCookie(string $name, string $prefix = '')
public function getCookie(string $name = null, string $prefix = '')
{
// if no name given, return them all
if (empty($name))
{
return $this->cookies;
}

if ($prefix === '' && $this->cookiePrefix !== '')
{
$prefix = $this->cookiePrefix;
Expand All @@ -910,6 +916,7 @@ public function getCookie(string $name, string $prefix = '')
return $cookie;
}
}
return null;
}

/**
Expand All @@ -920,8 +927,13 @@ public function getCookie(string $name, string $prefix = '')
* @param string $path
* @param string $prefix
*/
public function deleteCookie($name, string $domain = '', string $path = '/', string $prefix = '')
public function deleteCookie($name = '', string $domain = '', string $path = '/', string $prefix = '')
{
if (empty($name))
{
return;
}

if ($prefix === '' && $this->cookiePrefix !== '')
{
$prefix = $this->cookiePrefix;
Expand All @@ -933,6 +945,14 @@ public function deleteCookie($name, string $domain = '', string $path = '/', str
{
if ($cookie['name'] === $name)
{
if (! empty($domain) && $cookie['domain'] !== $domain)
{
continue;
}
if (! empty($path) && $cookie['path'] !== $path)
{
continue;
}
$cookie['value'] = '';
$cookie['expires'] = '';

Expand Down
240 changes: 240 additions & 0 deletions tests/system/HTTP/ResponseCookieTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
<?php
namespace CodeIgniter\HTTP;

use CodeIgniter\HTTP\Exceptions\HTTPException;
use Config\App;
use Tests\Support\HTTP\MockResponse;

class ResponseCookieTest extends \CIUnitTestCase
{

public function setUp()
{
parent::setUp();
$this->server = $_SERVER;
}

public function tearDown()
{
$_SERVER = $this->server;
}

public function testCookiePrefixed()
{
$config = new App();
$config->cookiePrefix = 'mine';
$response = new Response($config);
$response->setCookie('foo', 'bar');

$this->assertTrue(is_array($response->getCookie('foo')));
$this->assertTrue($response->hasCookie('foo'));
$this->assertTrue($response->hasCookie('foo', 'bar'));
$this->assertTrue($response->hasCookie('foo', 'bar', 'mine'));
$this->assertTrue($response->hasCookie('foo', null, 'mine'));
$this->assertFalse($response->hasCookie('foo', null, 'yours'));
}

public function testCookiesAll()
{
$config = new App();
$response = new Response($config);
$response->setCookie('foo', 'bar');
$response->setCookie('bee', 'bop');

$allCookies = $response->getCookie();
$this->assertEquals(2, count($allCookies));
$this->assertTrue($response->hasCookie('foo'));
$this->assertTrue($response->hasCookie('bee'));
}

public function testCookieGet()
{
$config = new App();
$response = new Response($config);
$response->setCookie('foo', 'bar');
$response->setCookie('bee', 'bop');

$allCookies = $response->getCookie();
$this->assertEquals(2, count($allCookies));
$this->assertEquals(null, $response->getCookie('bogus'));
}

public function testCookieDomain()
{
$config = new App();
$response = new Response($config);

$response->setCookie('foo', 'bar');
$cookie = $response->getCookie('foo');
$this->assertEquals('', $cookie['domain']);

$response->setCookie(['name' => 'bee', 'value' => 'bop', 'domain' => 'somewhere.com']);
$cookie = $response->getCookie('bee');
$this->assertEquals('somewhere.com', $cookie['domain']);

$config->cookieDomain = 'mine.com';
$response = new Response($config);
$response->setCookie('alu', 'la');
$cookie = $response->getCookie('alu');
$this->assertEquals('mine.com', $cookie['domain']);
}

public function testCookiePath()
{
$config = new App();
$response = new Response($config);

$response->setCookie('foo', 'bar');
$cookie = $response->getCookie('foo');
$this->assertEquals('/', $cookie['path']);

$response->setCookie(['name' => 'bee', 'value' => 'bop', 'path' => '/tmp/here']);
$cookie = $response->getCookie('bee');
$this->assertEquals('/tmp/here', $cookie['path']);

$config->cookiePath = '/tmp/there';
$response = new Response($config);
$response->setCookie('alu', 'la');
$cookie = $response->getCookie('alu');
$this->assertEquals('/tmp/there', $cookie['path']);
}

public function testCookieSecure()
{
$config = new App();
$response = new Response($config);

$response->setCookie('foo', 'bar');
$cookie = $response->getCookie('foo');
$this->assertEquals(false, $cookie['secure']);

$response->setCookie(['name' => 'bee', 'value' => 'bop', 'secure' => true]);
$cookie = $response->getCookie('bee');
$this->assertEquals(true, $cookie['secure']);

$config->cookieSecure = true;
$response = new Response($config);
$response->setCookie('alu', 'la');
$cookie = $response->getCookie('alu');
$this->assertEquals(true, $cookie['secure']);
}

public function testCookieHTTPOnly()
{
$config = new App();
$response = new Response($config);

$response->setCookie('foo', 'bar');
$cookie = $response->getCookie('foo');
$this->assertEquals(false, $cookie['httponly']);

$response->setCookie(['name' => 'bee', 'value' => 'bop', 'httponly' => true]);
$cookie = $response->getCookie('bee');
$this->assertEquals(true, $cookie['httponly']);

$config->cookieHTTPOnly = true;
$response = new Response($config);
$response->setCookie('alu', 'la');
$cookie = $response->getCookie('alu');
$this->assertEquals(true, $cookie['httponly']);
}

public function testCookieExpiry()
{
$config = new App();
$response = new Response($config);

$response->setCookie('foo', 'bar');
$cookie = $response->getCookie('foo');
$this->assertTrue($cookie['expires'] < time());

$response = new Response($config);
$response->setCookie(['name' => 'bee', 'value' => 'bop', 'expire' => 1000]);
$cookie = $response->getCookie('bee');
$this->assertFalse($cookie['expires'] < time());

$response = new Response($config);
$response->setCookie(['name' => 'bee', 'value' => 'bop', 'expire' => 'oops']);
$cookie = $response->getCookie('bee');
$this->assertTrue($cookie['expires'] < time());

$response = new Response($config);
$response->setCookie(['name' => 'bee', 'value' => 'bop', 'expire' => -1000]);
$cookie = $response->getCookie('bee');
$this->assertEquals(0, $cookie['expires']);
}

public function testCookieDelete()
{
$config = new App();
$response = new Response($config);

// foo is already expired, bee will stick around
$response->setCookie('foo', 'bar');
$response->setCookie(['name' => 'bee', 'value' => 'bop', 'expire' => 1000]);
$cookie = $response->getCookie('bee');
$this->assertFalse($cookie['expires'] <= time());

// delete cookie manually
$response = new Response($config);
$response->setCookie(['name' => 'bee', 'value' => 'bop', 'expire' => '']);
$cookie = $response->getCookie('bee');
$this->assertTrue($cookie['expires'] <= time(), $cookie['expires'] . ' should be less than ' . time());

// delete with no effect
$response = new Response($config);
$response->setCookie(['name' => 'bee', 'value' => 'bop', 'expire' => 1000]);
$response->deleteCookie();
$cookie = $response->getCookie('bee');
$this->assertFalse($cookie['expires'] < time());

// delete cookie for real
$response = new Response($config);
$response->setCookie(['name' => 'bee', 'value' => 'bop', 'expire' => 1000]);
$response->deleteCookie('bee');
$cookie = $response->getCookie('bee');
$this->assertTrue($cookie['expires'] <= time(), $cookie['expires'] . ' should be less than ' . time());

// delete cookie for real, with prefix
$config->cookiePrefix = 'mine';
$response = new Response($config);
$response->setCookie(['name' => 'bee', 'value' => 'bop', 'expire' => 1000]);
$response->deleteCookie('bee');
$cookie = $response->getCookie('bee');
$this->assertTrue($cookie['expires'] <= time(), $cookie['expires'] . ' should be less than ' . time());

// delete cookie with wrong prefix?
$config->cookiePrefix = 'mine';
$response = new Response($config);
$response->setCookie(['name' => 'bee', 'value' => 'bop', 'expire' => 1000]);
$response->deleteCookie('bee', '', '', 'wrong');
$cookie = $response->getCookie('bee');
$this->assertFalse($cookie['expires'] <= time(), $cookie['expires'] . ' should be less than ' . time());
$response->deleteCookie('bee', '', '', 'mine');
$cookie = $response->getCookie('bee');
$this->assertTrue($cookie['expires'] <= time(), $cookie['expires'] . ' should be less than ' . time());

// delete cookie with wrong domain?
$config->cookieDomain = '.mine.com';
$response = new Response($config);
$response->setCookie(['name' => 'bee', 'value' => 'bop', 'expire' => 1000]);
$response->deleteCookie('bee', 'wrong', '', '');
$cookie = $response->getCookie('bee');
$this->assertFalse($cookie['expires'] <= time(), $cookie['expires'] . ' should be less than ' . time());
$response->deleteCookie('bee', '.mine.com', '', '');
$cookie = $response->getCookie('bee');
$this->assertTrue($cookie['expires'] <= time(), $cookie['expires'] . ' should be less than ' . time());

// delete cookie with wrong path?
$config->cookiePath = '/whoknowswhere';
$response = new Response($config);
$response->setCookie(['name' => 'bee', 'value' => 'bop', 'expire' => 1000]);
$response->deleteCookie('bee', '', '/wrong', '');
$cookie = $response->getCookie('bee');
$this->assertFalse($cookie['expires'] <= time(), $cookie['expires'] . ' should be less than ' . time());
$response->deleteCookie('bee', '', '/whoknowswhere', '');
$cookie = $response->getCookie('bee');
$this->assertTrue($cookie['expires'] <= time(), $cookie['expires'] . ' should be less than ' . time());
}

}
3 changes: 2 additions & 1 deletion tests/system/HTTP/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,8 @@ public function testSetCookieSuccessOnPrefix()
$response = new Response(new App());
$response->setCookie('foo', 'bar', '', '', '', 'ack');

$this->assertFalse($response->hasCookie('foo', null, 'ack'));
$this->assertTrue($response->hasCookie('foo', null, 'ack'));
$this->assertFalse($response->hasCookie('foo', null, 'nak'));
}

//--------------------------------------------------------------------
Expand Down
Loading

0 comments on commit a7d36b5

Please sign in to comment.