public function send($method, $path = null, $data = array(), $options = array()) { $defaults = array('return' => 'body', 'type' => 'form'); $options += $defaults; $request = $this->_request($method, $path, $data, $options); $response = new Response(); $response->body = json_encode(array('ok' => true, 'id' => '12345', 'rev' => '1-2', 'body' => 'something')); $this->last = (object) compact('request', 'response'); return $options['return'] == 'body' ? $response->body() : $response; }
/** * Sets/Gets the content type. If `'type'` is null, the method will attempt to determine the * type from the params, then from the environment setting * * @param string $type a full content type i.e. `'application/json'` or simple name `'json'` * @return string A simple content type name, i.e. `'html'`, `'xml'`, `'json'`, etc., depending * on the content type of the request. */ public function type($type = null) { if ($type === null && $this->_type === null) { $type = 'html'; } return parent::type($type); }
protected function _init() { parent::_init(); if (!empty($this->_config['request']) && is_object($this->_config['request'])) { $this->type = $this->_config['request']->type(); } }
/** * This tests that setting custom paths and disabling layout * via `\lithium\net\http\Media::type()` is handled properly * by the default `\lithium\template\View` class and `File` * rendered adapter. */ public function testMediaTypeViewRender() { Media::type('view-integration-test', 'lithium/viewtest', array('view' => 'lithium\\template\\View', 'paths' => array('layout' => false, 'template' => array('{:library}/tests/mocks/template/view/adapters/{:template}.{:type}.php', '{:library}/tests/mocks/template/view/adapters/{:template}.html.php')))); // testing no layout with a custom type template $response = new Response(); $response->type('view-integration-test'); Media::render($response, array(), array('layout' => true, 'library' => 'lithium', 'template' => 'testTypeFile')); $this->assertEqual('This is a type test.', $response->body()); // testing the template falls back to the html template $response = new Response(); $response->type('view-integration-test'); Media::render($response, array(), array('layout' => true, 'library' => 'lithium', 'template' => 'testFile')); $this->assertEqual('This is a test.', $response->body()); // testing with a layout Media::type('view-integration-test', 'lithium/viewtest', array('view' => 'lithium\\template\\View', 'paths' => array('layout' => '{:library}/tests/mocks/template/view/adapters/testLayoutFile.html.php', 'template' => array('{:library}/tests/mocks/template/view/adapters/{:template}.{:type}.php', '{:library}/tests/mocks/template/view/adapters/{:template}.html.php')))); $response = new Response(); $response->type('view-integration-test'); Media::render($response, array(), array('layout' => true, 'library' => 'lithium', 'template' => 'testTypeFile')); $this->assertEqual("Layout top.\nThis is a type test.Layout bottom.", $response->body()); }
protected function _init() { parent::_init(); $config = $this->_config; $this->status($config['status']); unset($this->_config['status']); if ($config['location']) { $classes = $this->_classes; $location = $classes['router']::match($config['location'], $config['request']); $this->headers('location', $location); } }
protected function _init() { $this->status($this->_config['status']); unset($this->_config['status']); if ($this->_config['request'] && is_object($this->_config['request'])) { $this->_type = $this->_config['request']->accepts(); } if ($this->_config['location']) { $router = $this->_classes['router']; $location = $router::match($this->_config['location'], $this->_config['request']); $this->headers('location', $location); } parent::_init(); }
function testTransferEncodingChunkedDecode() { $headers = join("\r\n", array('HTTP/1.1 200 OK', 'Server: CouchDB/0.10.0 (Erlang OTP/R13B)', 'Etag: "DWGTHR79JLSOGACPLVIZBJUBP"', 'Date: Wed, 11 Nov 2009 19:49:41 GMT', 'Content-Type: text/plain;charset=utf-8', 'Cache-Control: must-revalidate', 'Transfer-Encoding: chunked', 'Connection: Keep-alive', '', '')); $message = $headers . join("\r\n", array('b7', '{"total_rows":1,"offset":0,"rows":[', '{"id":"88989cafcd81b09f81078eb523832e8e","key":"gwoo","value":' . '{"author":"gwoo","language":"php","preview":"test","created":"2009-10-27 12:14:12"}}', '4', '', ']}', '1', '', '', '0', '', '')); $response = new Response(compact('message')); $expected = join("\r\n", array('{"total_rows":1,"offset":0,"rows":[', '{"id":"88989cafcd81b09f81078eb523832e8e","key":"gwoo","value":' . '{"author":"gwoo","language":"php","preview":"test","created":"2009-10-27 12:14:12"}}', ']}')); $result = $response->body(); $this->assertEqual($expected, $result); $message = $headers . join("\r\n", array('body')); $expected = 'body'; $response = new Response(compact('message')); $result = $response->body(); $this->assertEqual($expected, $result); $message = $headers . join("\r\n", array('[part one];', '[part two]')); $expected = '[part two]'; $response = new Response(compact('message')); $result = $response->body(); $this->assertEqual($expected, $result); $message = join("\r\n", array('HTTP/1.1 200 OK', 'Header: Value', 'Connection: close', 'Content-Type: text/html;charset=UTF-8', 'Transfer-Encoding: text', '', 'Test!')); $expected = 'Test!'; $response = new Response(compact('message')); $result = $response->body(); $this->assertEqual($expected, $result); }
/** * Expands on `\net\http\Message::headers()` with some magic conversions for shorthand headers. * * @deprecated This method will be removed in a future version. Note that the parent `header()` * wil continue to exist. * @param string|array $key * @param mixed $value * @param boolean $replace * @return mixed */ public function headers($key = null, $value = null, $replace = true) { if ($key === 'download' || $key === 'Download') { $message = "Shorthand header `Download` with `<FILENAME>` has been deprecated "; $message .= "because it's too magic. Please use `Content-Disposition` "; $message .= "with `attachment; filename=\"<FILENAME>\"` instead."; trigger_error($message, E_USER_DEPRECATED); $key = 'Content-Disposition'; $value = 'attachment; filename="' . $value . '"'; } return parent::headers($key, $value, $replace); }
public function testMalformedStatus() { $expected = "HTTP/1.1 304 Not Modified"; $message = join("\r\n", array('HTTP/1.1 304', 'Header: Value', 'Connection: close', 'Content-Type: application/json;charset=iso-8859-1', '', 'Test!')); $response = new Response(compact('message')); $result = $response->status(); $this->assertEqual($expected, $result); $expected = "HTTP/1.1 500 Internal Server Error"; $message = join("\r\n", array('HTTP/1.1 500', 'Header: Value', 'Connection: close', 'Content-Type: application/json;charset=iso-8859-1', '', 'Test!')); $response = new Response(compact('message')); $result = $response->status(); $this->assertEqual($expected, $result); }
public function testDigestParsing() { $auth = 'Digest realm="app",'; $auth .= 'qop="auth",nonce="4ee1617b8756e",opaque="dd7bcee161192cb8fba765eb595eba87"'; $headers = array("WWW-Authenticate" => $auth); $response = new Response(compact('headers')); $expected = array("WWW-Authenticate" => $auth); $result = $response->headers; $this->assertEqual($expected, $result); $expected = array('realm' => 'app', 'qop' => 'auth', 'nonce' => '4ee1617b8756e', 'opaque' => 'dd7bcee161192cb8fba765eb595eba87'); $result = array_filter($response->digest()); $this->assertEqual($expected, $result); }
public function testMessageWithNoHeaders() { $body = "\n<html>...</html>\n"; $message = "\r\n\r\n{$body}"; $response = new Response(compact('message')); $this->assertFalse($response->headers()); $this->assertEqual(trim($body), $response->body()); }