diff --git a/src/Guzzle/Service/Command/LocationVisitor/Request/XmlVisitor.php b/src/Guzzle/Service/Command/LocationVisitor/Request/XmlVisitor.php index 3c652c25..f71ca72e 100644 --- a/src/Guzzle/Service/Command/LocationVisitor/Request/XmlVisitor.php +++ b/src/Guzzle/Service/Command/LocationVisitor/Request/XmlVisitor.php @@ -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); diff --git a/tests/Guzzle/Tests/Service/Command/LocationVisitor/Request/XmlVisitorTest.php b/tests/Guzzle/Tests/Service/Command/LocationVisitor/Request/XmlVisitorTest.php index 9e466b96..6f4ac76b 100644 --- a/tests/Guzzle/Tests/Service/Command/LocationVisitor/Request/XmlVisitorTest.php +++ b/tests/Guzzle/Tests/Service/Command/LocationVisitor/Request/XmlVisitorTest.php @@ -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( + '' . "\n" + . '' . "\n", + (string) $request->getBody() + ); + } }