1
0
mirror of https://github.com/guzzle/guzzle.git synced 2025-02-24 18:13:00 +01:00

Adding the ability to serialize and XML payload even if no XML parameters were set.

This commit is contained in:
Michael Dowling 2013-03-06 22:24:42 -08:00
parent e575c6b2d3
commit e03795e5a6
2 changed files with 33 additions and 1 deletions

View File

@ -61,9 +61,22 @@ class XmlVisitor extends AbstractRequestVisitor
*/
public function after(CommandInterface $command, RequestInterface $request)
{
$xml = null;
// If data was found that needs to be serialized, then do so
if (isset($this->data[$command])) {
$request->setBody($this->data[$command]->asXML());
$xml = $this->data[$command]->asXML();
unset($this->data[$command]);
} else {
// Check if XML should always be sent for the command
$operation = $command->getOperation();
if ($operation->getData('xmlAllowEmpty')) {
$xml = $this->createRootElement($operation)->asXML();
}
}
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);

View File

@ -503,4 +503,23 @@ class XmlVisitorTest extends AbstractVisitorTestCase
(string) $request->getBody()
);
}
public function testAllowsSendingXmlPayloadIfNoXmlParamsWereSet()
{
$operation = new Operation(array(
'httpMethod' => 'POST',
'data' => array('xmlAllowEmpty' => true),
'parameters' => array('Foo' => array('location' => 'xml'))
));
$command = $this->getMockBuilder('Guzzle\Service\Command\OperationCommand')
->setConstructorArgs(array(array(), $operation))
->getMockForAbstractClass();
$command->setClient(new Client('http://foo.com'));
$request = $command->prepare();
$this->assertEquals(
'<?xml version="1.0"?>' . "\n"
. '<Request/>' . "\n",
(string) $request->getBody()
);
}
}