Skip to content

Commit

Permalink
Add getCookies()
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Apr 5, 2020
1 parent b774394 commit d5147f6
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/main/php/unittest/web/WebTestCase.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use lang\IllegalArgumentException;
use peer\http\HttpConnection;
use peer\http\HttpConstants;
use scriptlet\Cookie;
use text\regex\Pattern;
use unittest\TestCase;
use xml\XPath;
Expand Down Expand Up @@ -130,9 +129,9 @@ protected function doRequest($method, $params) {
if (isset($this->cookies[$host]) && 0 < sizeof($this->cookies[$host])) {
$cookies= '';
foreach ($this->cookies[$host] as $cookie) {
$cookies.= $cookie->getHeadervalue().'; ';
$cookies.= '; '.$cookie->name().'='.$cookie->value();
}
$request->setHeader('Cookie', substr($cookies, 0, -2));
$request->setHeader('Cookie', substr($cookies, 2));
}
return $this->conn->send($request);
}
Expand All @@ -155,7 +154,7 @@ public function beginAt($relative, $params= null, $method= HttpConstants::GET) {
// would be creating new sessions with every request otherwise!
foreach ((array)$this->response->header('Set-Cookie') as $str) {
$cookie= Cookie::parse($str);
$this->cookies[$this->conn->getUrl()->getHost()][$cookie->getName()]= $cookie;
$this->cookies[$this->conn->getUrl()->getHost()][$cookie->name()]= $cookie;
}
} catch (\lang\XPException $e) {
$this->response= null;
Expand Down Expand Up @@ -524,7 +523,7 @@ public function assertTitleEquals($title, $message= 'not_equals') {
* @param string name
* @throws unittest.AssertionFailedError
*/
protected function assertCookiePresent($name) {
public function assertCookiePresent($name) {
$domain= $this->conn->getUrl()->getHost();
$this->assertTrue(isset($this->cookies[$domain][$name]), \xp::stringOf($this->cookies));
}
Expand All @@ -535,11 +534,21 @@ protected function assertCookiePresent($name) {
* @param string name
* @return scriptlet.Cookie
*/
protected function getCookie($name) {
public function getCookie($name) {
$domain= $this->conn->getUrl()->getHost();
if (!isset($this->cookies[$domain][$name])) {
$this->fail('Failed to locate a cookie named "'.$name.'"', null, '[cookie]');
}
return $this->cookies[$domain][$name];
}
}

/**
* Gets cookies
*
* @return [:scriptlet.Cookie]
*/
public function getCookies() {
$domain= $this->conn->getUrl()->getHost();
return isset($this->cookies[$domain]) ? $this->cookies[$domain] : [];
}
}

0 comments on commit d5147f6

Please sign in to comment.