Skip to content

Commit

Permalink
Remove hacky code.
Browse files Browse the repository at this point in the history
People only really use json/xml so we can support a limited set of types and do away with fancy things.
  • Loading branch information
markstory committed Dec 29, 2012
1 parent f53e5b3 commit 6f33811
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
22 changes: 11 additions & 11 deletions lib/Cake/Network/Http/Client.php
Expand Up @@ -13,6 +13,7 @@
*/
namespace Cake\Network\Http;

use Cake\Error;
use Cake\Network\Http\Request;
use Cake\Network\Http\Response;
use Cake\Utility\Hash;
Expand Down Expand Up @@ -314,18 +315,17 @@ protected function _typeHeaders($type) {
'Content-Type' => $type
];
}
// Hacky as hell but I'll clean it up I promise
$reflection = new \ReflectionClass('Cake\Network\Response');
$properties = $reflection->getDefaultProperties();
if (isset($properties['_mimeTypes'][$type])) {
$mimeTypes = $properties['_mimeTypes'][$type];
$mimeType = is_array($mimeTypes) ? current($mimeTypes) : $mimeTypes;
return [
'Accept' => $mimeType,
'Content-Type' => $mimeType,
];
$typeMap = [
'json' => 'application/json',
'xml' => 'application/xml',
];
if (!isset($typeMap[$type])) {
throw new Error\Exception(__d('cake_dev', 'Unknown type alias.'));
}
return [];
return [
'Accept' => $typeMap[$type],
'Content-Type' => $typeMap[$type],
];
}

}
18 changes: 18 additions & 0 deletions lib/Cake/Test/TestCase/Network/Http/ClientTest.php
Expand Up @@ -323,4 +323,22 @@ public function testPostWithTypeKey($type, $mime) {
]);
$http->post('/projects/add', $data, ['type' => $type]);
}

/**
* Test that exceptions are raised on invalid types.
*
* @expectedException Cake\Error\Exception
* @return void
*/
public function testExceptionOnUnknownType() {
$mock = $this->getMock('Cake\Network\Http\Adapter\Stream', ['send']);
$mock->expects($this->never())
->method('send');

$http = new Client([
'host' => 'cakephp.org',
'adapter' => $mock
]);
$http->post('/projects/add', 'it works', ['type' => 'invalid']);
}
}

0 comments on commit 6f33811

Please sign in to comment.