Skip to content

Commit

Permalink
Implement setting content for a request.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Dec 23, 2012
1 parent 80d1b32 commit a72e48c
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 7 deletions.
31 changes: 30 additions & 1 deletion lib/Cake/Network/Http/Adapter/Stream.php
Expand Up @@ -15,6 +15,7 @@

use Cake\Network\Http\Request;
use Cake\Network\Http\Response;
use Cake\Network\Http\FormData;

/**
* Implements sending Cake\Network\Http\Request
Expand All @@ -41,8 +42,8 @@ public function send(Request $request, $options) {
* @return void
*/
protected function _buildContext(Request $request, $options) {
$this->_buildHeaders($request, $options);
$this->_buildContent($request, $options);
$this->_buildHeaders($request, $options);
$this->_buildOptions($request, $options);

$url = $request->url();
Expand Down Expand Up @@ -73,7 +74,31 @@ protected function _buildHeaders(Request $request, $options) {
$this->_contextOptions['header'] = implode("\r\n", $headers);
}

/**
* Builds the request content based on the request object.
*
* If the $request->content() is a string, it will be used as is.
* Array data will be processed with Cake\Network\Http\FormData
*
* @param Request $request
* @param array $options
*/
protected function _buildContent($request, $options) {
$content = $request->content();
if (empty($content)) {
return;
}
if (is_string($content)) {
$this->_contextOptions['content'] = $content;
return;
}
if (is_array($content)) {
$formData = new FormData();
$formData->addMany($content);
$type = 'multipart/form-data; boundary="' . $formData->boundary() . '"';
$request->header('Content-Type', $type);
$this->_contextOptions['content'] = (string)$formData;
}
}

/**
Expand All @@ -85,10 +110,14 @@ protected function _buildContent($request, $options) {
protected function _buildOptions(Request $request, $options) {
$this->_contextOptions['method'] = $request->method();
$this->_contextOptions['protocol_version'] = $request->version();
$this->_contextOptions['ignore_errors'] = true;

if (isset($options['timeout'])) {
$this->_contextOptions['timeout'] = $options['timeout'];
}
if (!empty($options['redirect'])) {
$this->_contextOptions['max_redirects'] = $options['redirect'];
}
}

protected function _send() {
Expand Down
74 changes: 68 additions & 6 deletions lib/Cake/Test/TestCase/Network/Http/Adapter/StreamTest.php
Expand Up @@ -23,6 +23,14 @@
*/
class StreamTest extends TestCase {

public function setUp() {
parent::setUp();
$this->stream = $this->getMock(
'Cake\Network\Http\Adapter\Stream',
['_send']
);
}

/**
* Test the send method
*
Expand All @@ -45,10 +53,6 @@ public function testSend() {
* @return void
*/
public function testBuildingContextHeader() {
$stream = $this->getMock(
'Cake\Network\Http\Adapter\Stream',
['_send']
);
$request = new Request();
$request->url('http://localhost')
->header([
Expand All @@ -60,15 +64,73 @@ public function testBuildingContextHeader() {
'utm_src' => 'awesome',
]);

$stream->send($request, []);
$result = $stream->contextOptions();
$options = [
'redirect' => 20
];
$this->stream->send($request, $options);
$result = $this->stream->contextOptions();
$expected = [
'Connection: close',
'User-Agent: CakePHP TestSuite',
'Content-Type: application/json',
'Cookie: testing=value; utm_src=awesome',
];
$this->assertEquals(implode("\r\n", $expected), $result['header']);
$this->assertEquals($options['redirect'], $result['max_redirects']);
$this->assertTrue($result['ignore_errors']);
}

/**
* Test send() + context options with string content.
*
* @return void
*/
public function testSendContextContentString() {
$content = json_encode(['a' => 'b']);
$request = new Request();
$request->url('http://localhost')
->header([
'Content-Type' => 'application/json'
])
->content($content);

$options = [
'redirect' => 20
];
$this->stream->send($request, $options);
$result = $this->stream->contextOptions();
$expected = [
'Connection: close',
'User-Agent: CakePHP',
'Content-Type: application/json',
];
$this->assertEquals(implode("\r\n", $expected), $result['header']);
$this->assertEquals($content, $result['content']);
}

/**
* Test send() + context options with array content.
*
* @return void
*/
public function testSendContextContentArray() {
$request = new Request();
$request->url('http://localhost')
->header([
'Content-Type' => 'application/json'
])
->content(['a' => 'my value']);

$this->stream->send($request, []);
$result = $this->stream->contextOptions();
$expected = [
'Connection: close',
'User-Agent: CakePHP',
'Content-Type: multipart/form-data; boundary="',
];
$this->assertStringStartsWith(implode("\r\n", $expected), $result['header']);
$this->assertContains('Content-Disposition: form-data; name="a"', $result['content']);
$this->assertContains('my value', $result['content']);
}

}

0 comments on commit a72e48c

Please sign in to comment.