Skip to content

Commit

Permalink
Using the Content-Type of the EntityBody if one cannot be determined …
Browse files Browse the repository at this point in the history
…by the request path
  • Loading branch information
mtdowling committed Apr 7, 2013
1 parent 863998b commit 6f044c5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/Guzzle/Http/EntityBody.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ public function getContentLength()
*/ */
public function getContentType() public function getContentType()
{ {
return (!($this->isLocal() && $this->getWrapper() == 'plainfile' && file_exists($this->getUri()))) return ($this->isLocal() && $this->getWrapper() == 'plainfile' && file_exists($this->getUri()))
? null ? Mimetypes::getInstance()->fromFilename($this->getUri())
: Mimetypes::getInstance()->fromFilename($this->getUri()); : null;
} }


/** /**
Expand Down
2 changes: 1 addition & 1 deletion src/Guzzle/Http/Message/EntityEnclosingRequest.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function setBody($body, $contentType = null, $tryChunkedTransfer = false)


// Auto detect the Content-Type from the path of the request if possible // Auto detect the Content-Type from the path of the request if possible
if ($contentType === null && !$this->hasHeader('Content-Type')) { if ($contentType === null && !$this->hasHeader('Content-Type')) {
$contentType = Mimetypes::getInstance()->fromFilename($this->getPath()); $contentType = Mimetypes::getInstance()->fromFilename($this->getPath()) ?: $this->body->getContentType();
} }


if ($contentType) { if ($contentType) {
Expand Down
14 changes: 12 additions & 2 deletions tests/Guzzle/Tests/Http/Message/EntityEnclosingRequestTest.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -525,10 +525,20 @@ public function testCanDisableRedirects()
/** /**
* @covers Guzzle\Http\Message\EntityEnclosingRequest::setBody * @covers Guzzle\Http\Message\EntityEnclosingRequest::setBody
*/ */
public function testSetsContentTypeWhenSettingBodyByGuessing() public function testSetsContentTypeWhenSettingBodyByGuessingFromPath()
{ {
$request = new EntityEnclosingRequest('PUT', 'http://test.com/foo.json'); $request = new EntityEnclosingRequest('PUT', 'http://test.com/foo.json');
$request->setBody('{"a":"b"}'); $request->setBody('{"a":"b"}');
$this->assertEquals('application/json', $request->getHeader('Content-Type')); $this->assertEquals('application/json', (string) $request->getHeader('Content-Type'));
}

/**
* @covers Guzzle\Http\Message\EntityEnclosingRequest::setBody
*/
public function testSetsContentTypeWhenSettingBodyByGuessingFromEntityBody()
{
$request = new EntityEnclosingRequest('PUT', 'http://test.com/foo');
$request->setBody(EntityBody::factory(fopen(__FILE__, 'r')));
$this->assertEquals('text/x-php', (string) $request->getHeader('Content-Type'));
} }
} }

0 comments on commit 6f044c5

Please sign in to comment.