mirror of
https://github.com/guzzle/guzzle.git
synced 2025-02-25 02:22:57 +01:00
Merge pull request #414 from checat/adds-json-header-fail
Json and XML visitors do not set correct Content-type when path has known extension
This commit is contained in:
commit
589e8ae9b0
@ -61,7 +61,7 @@ class EntityEnclosingRequest extends Request implements EntityEnclosingRequestIn
|
||||
|
||||
// Auto detect the Content-Type from the path of the request if possible
|
||||
if ($contentType === null && !$this->hasHeader('Content-Type')) {
|
||||
$contentType = $this->body->getContentType() ?: Mimetypes::getInstance()->fromFilename($this->getPath());
|
||||
$contentType = $this->body->getContentType();
|
||||
}
|
||||
|
||||
if ($contentType) {
|
||||
|
@ -51,12 +51,13 @@ class JsonVisitor extends AbstractRequestVisitor
|
||||
public function after(CommandInterface $command, RequestInterface $request)
|
||||
{
|
||||
if (isset($this->data[$command])) {
|
||||
$request->setBody(json_encode($this->data[$command]));
|
||||
unset($this->data[$command]);
|
||||
// Don't overwrite the Content-Type if one is set
|
||||
if ($this->jsonContentType && !$request->hasHeader('Content-Type')) {
|
||||
$request->setHeader('Content-Type', $this->jsonContentType);
|
||||
}
|
||||
|
||||
$request->setBody(json_encode($this->data[$command]));
|
||||
unset($this->data[$command]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -68,11 +68,11 @@ class XmlVisitor extends AbstractRequestVisitor
|
||||
}
|
||||
|
||||
if ($xml) {
|
||||
$request->setBody($xml);
|
||||
// Don't overwrite the Content-Type if one is set
|
||||
if ($this->contentType && !$request->hasHeader('Content-Type')) {
|
||||
$request->setHeader('Content-Type', $this->contentType);
|
||||
}
|
||||
$request->setBody($xml);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -416,13 +416,6 @@ class EntityEnclosingRequestTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
$this->assertTrue($request->getParams()->get(RedirectPlugin::DISABLE));
|
||||
}
|
||||
|
||||
public function testSetsContentTypeWhenSettingBodyByGuessingFromPath()
|
||||
{
|
||||
$request = new EntityEnclosingRequest('PUT', 'http://test.com/foo.json');
|
||||
$request->setBody('{"a":"b"}');
|
||||
$this->assertEquals('application/json', (string) $request->getHeader('Content-Type'));
|
||||
}
|
||||
|
||||
public function testSetsContentTypeWhenSettingBodyByGuessingFromEntityBody()
|
||||
{
|
||||
$request = new EntityEnclosingRequest('PUT', 'http://test.com/foo');
|
||||
|
@ -20,7 +20,7 @@ abstract class AbstractVisitorTestCase extends \Guzzle\Tests\GuzzleTestCase
|
||||
public function setUp()
|
||||
{
|
||||
$this->command = new MockCommand();
|
||||
$this->request = new EntityEnclosingRequest('POST', 'http://www.test.com');
|
||||
$this->request = new EntityEnclosingRequest('POST', 'http://www.test.com/some/path.php');
|
||||
$this->validator = new SchemaValidator();
|
||||
}
|
||||
|
||||
|
@ -305,7 +305,7 @@ class XmlVisitorTest extends AbstractVisitorTestCase
|
||||
$command = $this->getMockBuilder('Guzzle\Service\Command\OperationCommand')
|
||||
->setConstructorArgs(array($input, $operation))
|
||||
->getMockForAbstractClass();
|
||||
$command->setClient(new Client());
|
||||
$command->setClient(new Client('http://www.test.com/some/path.php'));
|
||||
$request = $command->prepare();
|
||||
if (!empty($input)) {
|
||||
$this->assertEquals('application/xml', (string) $request->getHeader('Content-Type'));
|
||||
|
Loading…
x
Reference in New Issue
Block a user