Skip to content

Commit

Permalink
Refactor: Remove dependency on scriptlet library
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Apr 5, 2020
1 parent e03cd7f commit b774394
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/main/php/unittest/web/Cookie.class.php
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; }
}
31 changes: 31 additions & 0 deletions src/test/php/unittest/web/tests/CookiesTest.class.php
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()
);
}
}

0 comments on commit b774394

Please sign in to comment.