-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: Remove dependency on scriptlet library
- Loading branch information
Showing
2 changed files
with
84 additions
and
0 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,53 @@ | ||
<?php namespace unittest\web; | ||
|
||
/** | ||
* Represents a cookie | ||
* | ||
* @see xp://unittest.web.WebTestCase#getCookies | ||
* @see xp://unittest.web.WebTestCase#getCookie | ||
*/ | ||
class Cookie { | ||
private $name, $value, $attributes; | ||
|
||
public function __construct($name, $value, $attributes= []) { | ||
$this->name= $name; | ||
$this->value= $value; | ||
$this->attributes= $attributes; | ||
} | ||
|
||
public static function parse($header) { | ||
sscanf($header, '%[^=]=%[^;]', $name, $value); | ||
if (false === ($offset= strpos($header, ';'))) { | ||
return new self($name, $value); | ||
} | ||
|
||
$offset++; | ||
while (false !== ($p= strpos($header, '=', $offset))) { | ||
$key= ltrim(substr($header, $offset, $p - $offset), '; '); | ||
if ('"' === $header[$p + 1]) { | ||
$offset= $p + 2; | ||
do { | ||
if (false === ($offset= strpos($header, '"', $offset))) { | ||
throw new FormatException('Unclosed string in parameter "'.$name.'"'); | ||
} | ||
} while ('\\' === $header[$offset++ - 1]); | ||
$attribute= strtr(substr($header, $p + 2, $offset - $p - 3), ['\"' => '"']); | ||
} else { | ||
$attribute= substr($header, $p + 1, strcspn($header, ';', $p) - 1); | ||
$offset= $p + strlen($attribute) + 1; | ||
} | ||
|
||
$attributes[$key]= $attribute; | ||
} | ||
return new self($name, $value, $attributes); | ||
} | ||
|
||
/** @return string */ | ||
public function name() { return $this->name; } | ||
|
||
/** @return string */ | ||
public function value() { return $this->value; } | ||
|
||
/** @return [:var] */ | ||
public function attributes() { return $this->attributes; } | ||
} |
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,31 @@ | ||
<?php namespace unittest\web\tests; | ||
|
||
use peer\http\HttpConstants; | ||
use unittest\web\Cookie; | ||
|
||
class CookiesTest extends WebTestCaseTest { | ||
|
||
#[@test] | ||
public function no_cookies_present() { | ||
$this->fixture->respondWith(HttpConstants::STATUS_OK, [], ''); | ||
$this->fixture->beginAt('/'); | ||
$this->assertEquals([], $this->fixture->getCookies()); | ||
} | ||
|
||
#[@test] | ||
public function single_cookie_present() { | ||
$this->fixture->respondWith(HttpConstants::STATUS_OK, ['Set-Cookie: uid=6100'], ''); | ||
$this->fixture->beginAt('/'); | ||
$this->assertEquals(['uid' => new Cookie('uid', '6100')], $this->fixture->getCookies()); | ||
} | ||
|
||
#[@test] | ||
public function multiple_cookies_present() { | ||
$this->fixture->respondWith(HttpConstants::STATUS_OK, ['Set-Cookie: uid=6100', 'Set-Cookie: session=abcd'], ''); | ||
$this->fixture->beginAt('/'); | ||
$this->assertEquals( | ||
['uid' => new Cookie('uid', '6100'), 'session' => new Cookie('session', 'abcd')], | ||
$this->fixture->getCookies() | ||
); | ||
} | ||
} |