|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Arachne\Codeception\Http; |
| 4 | + |
| 5 | +use Nette\Http\IResponse; |
| 6 | +use Nette\Http\Response as HttpResponse; |
| 7 | +use Nette\Object; |
| 8 | +use Nette\Utils\DateTime; |
| 9 | + |
| 10 | +/** |
| 11 | + * HttpResponse class for tests. |
| 12 | + * |
| 13 | + * @author Jáchym Toušek |
| 14 | + */ |
| 15 | +class Response extends Object implements IResponse |
| 16 | +{ |
| 17 | + |
| 18 | + /** @var int */ |
| 19 | + private $code = self::S200_OK; |
| 20 | + |
| 21 | + /** @var array */ |
| 22 | + private $headers = []; |
| 23 | + |
| 24 | + /** |
| 25 | + * @param int |
| 26 | + * @return self |
| 27 | + */ |
| 28 | + public function setCode($code) |
| 29 | + { |
| 30 | + $this->code = $code; |
| 31 | + return $this; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @return int |
| 36 | + */ |
| 37 | + public function getCode() |
| 38 | + { |
| 39 | + return $this->code; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @param string $name |
| 44 | + * @param string $value |
| 45 | + * @return self |
| 46 | + */ |
| 47 | + public function setHeader($name, $value) |
| 48 | + { |
| 49 | + $this->headers[$name] = $value; |
| 50 | + return $this; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @param string $name |
| 55 | + * @param string $value |
| 56 | + * @return self |
| 57 | + */ |
| 58 | + public function addHeader($name, $value) |
| 59 | + { |
| 60 | + $this->headers[$name] = $value; |
| 61 | + return $this; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @param string $type |
| 66 | + * @param string $charset |
| 67 | + * @return self |
| 68 | + */ |
| 69 | + public function setContentType($type, $charset = NULL) |
| 70 | + { |
| 71 | + $this->setHeader('Content-Type', $type . ($charset ? '; charset=' . $charset : '')); |
| 72 | + return $this; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @param string $url |
| 77 | + * @param int $code |
| 78 | + */ |
| 79 | + public function redirect($url, $code = self::S302_FOUND) |
| 80 | + { |
| 81 | + $this->setCode($code); |
| 82 | + $this->setHeader('Location', $url); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @param string|int|DateTime $time |
| 87 | + * @return self |
| 88 | + */ |
| 89 | + public function setExpiration($time) |
| 90 | + { |
| 91 | + if (!$time) { |
| 92 | + $this->setHeader('Cache-Control', 's-maxage=0, max-age=0, must-revalidate'); |
| 93 | + $this->setHeader('Expires', 'Mon, 23 Jan 1978 10:00:00 GMT'); |
| 94 | + return $this; |
| 95 | + } |
| 96 | + |
| 97 | + $time = DateTime::from($time); |
| 98 | + $this->setHeader('Cache-Control', 'max-age=' . ($time->format('U') - time())); |
| 99 | + $this->setHeader('Expires', HttpResponse::date($time)); |
| 100 | + return $this; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @return bool |
| 105 | + */ |
| 106 | + public function isSent() |
| 107 | + { |
| 108 | + return FALSE; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * @param string $name |
| 113 | + * @param mixed $default |
| 114 | + * @return mixed |
| 115 | + */ |
| 116 | + public function getHeader($name, $default = NULL) |
| 117 | + { |
| 118 | + return isset($this->headers[$name]) ? $this->headers[$name] : $default; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @return array |
| 123 | + */ |
| 124 | + public function getHeaders() |
| 125 | + { |
| 126 | + return $this->headers; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * @param string $name |
| 131 | + * @param string $value |
| 132 | + * @param string|int|DateTime $time |
| 133 | + * @param string $path |
| 134 | + * @param string $domain |
| 135 | + * @param bool $secure |
| 136 | + * @param bool $httpOnly |
| 137 | + * @return self |
| 138 | + */ |
| 139 | + public function setCookie($name, $value, $time, $path = NULL, $domain = NULL, $secure = NULL, $httpOnly = NULL) |
| 140 | + { |
| 141 | + return $this; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * @param string $name |
| 146 | + * @param string $path |
| 147 | + * @param string $domain |
| 148 | + * @param bool $secure |
| 149 | + */ |
| 150 | + public function deleteCookie($name, $path = NULL, $domain = NULL, $secure = NULL) |
| 151 | + { |
| 152 | + return $this; |
| 153 | + } |
| 154 | + |
| 155 | +} |
0 commit comments