Skip to content

Commit

Permalink
changed Media::type() to cast a Content-Type string to array
Browse files Browse the repository at this point in the history
  • Loading branch information
cgarvis committed Feb 15, 2012
1 parent 0c94093 commit a2d40ab
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
12 changes: 6 additions & 6 deletions net/http/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public static function type($type, $content = null, array $options = array()) {
return compact('content') + array('options' => static::_handlers($type));
}
if ($content) {
static::$_types[$type] = $content;
static::$_types[$type] = (array)$content;
}
static::$_handlers[$type] = $options ? Set::merge($defaults, $options) : array();
}
Expand Down Expand Up @@ -756,12 +756,12 @@ protected static function _types($type = null) {
'html' => array('text/html', 'application/xhtml+xml', '*/*'),
'htm' => array('alias' => 'html'),
'form' => array('application/x-www-form-urlencoded', 'multipart/form-data'),
'json' => 'application/json',
'rss' => 'application/rss+xml',
'atom' => 'application/atom+xml',
'css' => 'text/css',
'json' => array('application/json'),
'rss' => array('application/rss+xml'),
'atom' => array('application/atom+xml'),
'css' => array('text/css'),
'js' => array('application/javascript', 'text/javascript'),
'text' => 'text/plain',
'text' => array('text/plain'),
'txt' => array('alias' => 'text'),
'xml' => array('application/xml', 'text/xml')
);
Expand Down
8 changes: 3 additions & 5 deletions net/http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,11 @@ public function to($format, array $options = array()) {
$this->headers('Authorization', "Basic {$auth}");
}
}

if(in_array($options['method'], array('POST', 'PUT'))) {
$media = $this->_classes['media'];
$contentType = $media::type($this->_type);
$contentType = is_array($contentType) ? reset($contentType) : $contentType;
$contentType = is_array($contentType) ? reset($contentType) : $contentType;
$this->headers('Content-Type', $contentType);
if($type = $media::type($this->_type)) {
$this->headers('Content-Type', $type['content'][0]);
}
}

$body = $this->body($options['body']);
Expand Down
8 changes: 4 additions & 4 deletions tests/cases/net/http/MediaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function testMediaTypes() {
$this->assertEqual($expected, Media::formats());

$result = Media::type('json');
$expected = 'application/json';
$expected = array('application/json');
$this->assertEqual($expected, $result['content']);

$expected = array(
Expand All @@ -57,7 +57,7 @@ public function testMediaTypes() {
$this->assertTrue(in_array('my', $result));

$result = Media::type('my');
$expected = 'text/x-my';
$expected = array('text/x-my');
$this->assertEqual($expected, $result['content']);

$expected = array(
Expand Down Expand Up @@ -90,7 +90,7 @@ public function testContentTypeDetection() {
$this->assertEqual('json', Media::type('application/json; charset=UTF-8'));

$result = Media::type('json');
$expected = array('content' => 'application/json', 'options' => array(
$expected = array('content' => array('application/json'), 'options' => array(
'cast' => true, 'encode' => 'json_encode', 'decode' => $result['options']['decode']
));
$this->assertEqual($expected, $result);
Expand Down Expand Up @@ -517,7 +517,7 @@ public function testEncodeRecordSet() {
*/
public function testTypeAliasResolution() {
$resolved = Media::type('text');
$this->assertEqual('text/plain', $resolved['content']);
$this->assertEqual(array('text/plain'), $resolved['content']);
unset($resolved['options']['encode']);

$result = Media::type('txt');
Expand Down

0 comments on commit a2d40ab

Please sign in to comment.