-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
143 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Packaged\Http\Cookies; | ||
|
||
abstract class AbstractCookieHandler implements CookieHandler | ||
{ | ||
public function canHandle(string $name, string $value = null): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function decodeName(string $name): string | ||
{ | ||
return $name; | ||
} | ||
|
||
public function encodeName(string $name): string | ||
{ | ||
return $name; | ||
} | ||
|
||
public function decodeValue(string $value): string | ||
{ | ||
return $value; | ||
} | ||
|
||
public function encodeValue(string $value): string | ||
{ | ||
return $value; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Packaged\Http\Cookies; | ||
|
||
class LowercaseCookieHandler extends AbstractCookieHandler | ||
{ | ||
public function decodeName(string $name): string | ||
{ | ||
return parent::decodeName(strtolower($name)); | ||
} | ||
|
||
public function encodeName(string $name): string | ||
{ | ||
return parent::encodeName(strtolower($name)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace Packaged\Tests\Http; | ||
|
||
use Packaged\Http\Cookies\CookieJar; | ||
use Packaged\Http\Cookies\LowercaseCookieHandler; | ||
use Packaged\Http\Request; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class CookieJarTest extends TestCase | ||
{ | ||
public function testCrud() | ||
{ | ||
$req = new Request([], [], [], ['ABC' => 'def']); | ||
$jar = new CookieJar(); | ||
$jar->hydrate($req); | ||
|
||
self::assertEquals('def', $jar->read('ABC')); | ||
self::assertNull($jar->read('xyz')); | ||
self::assertNull($jar->read('xyz', true)); | ||
self::assertFalse($jar->has('xyz')); | ||
self::assertFalse($jar->has('xyz', true)); | ||
|
||
$jar->store('xyz', '123', 10); | ||
self::assertNull($jar->read('xyz')); | ||
self::assertEquals('123', $jar->read('xyz', true)); | ||
self::assertFalse($jar->has('xyz')); | ||
self::assertTrue($jar->has('xyz', true)); | ||
|
||
//Check for an updated value | ||
$jar->store('xyz', '456'); | ||
self::assertEquals('456', $jar->read('xyz', true)); | ||
|
||
$jar->store('ABC', 'ghi'); | ||
self::assertEquals('def', $jar->read('ABC', false)); | ||
self::assertEquals('ghi', $jar->read('ABC', true)); | ||
|
||
//Check delete | ||
$jar->delete('xyz'); | ||
self::assertNull($jar->read('xyz')); | ||
self::assertNull($jar->read('xyz', true)); | ||
} | ||
|
||
public function testHandlers() | ||
{ | ||
$req = new Request([], [], [], ['ABC' => 'def']); | ||
|
||
//Check removing all handlers will fallback to the default handler | ||
$jar = new CookieJar(); | ||
self::assertTrue($jar->removeHandler(1000)); | ||
self::assertFalse($jar->removeHandler(1000)); | ||
$jar->hydrate($req); | ||
self::assertEquals('def', $jar->read('ABC')); | ||
self::assertNull($jar->read('abc')); | ||
|
||
$jar = new CookieJar(); | ||
$jar->addHandler(new LowercaseCookieHandler(), 10, true); | ||
$jar->hydrate($req); | ||
self::assertNull($jar->read('ABC')); | ||
self::assertEquals('def', $jar->read('abc')); | ||
|
||
$this->expectExceptionMessage("A cookie handler already exists with priority 10"); | ||
$jar->addHandler(new LowercaseCookieHandler(), 10); | ||
} | ||
} |