diff --git a/README.md b/README.md index eca4e0c..00d16e4 100644 --- a/README.md +++ b/README.md @@ -166,6 +166,11 @@ assert('http://example.com/soap/service' == $client->getLocation(0)); Passing a `$function` not defined in the WSDL file will throw a `SoapFault`. +#### withRequestTarget($requestTarget) + +This method allows you to change the destination of your SOAP calls. It returns a new Client instance +with the specified request-target. + ### Proxy The `Proxy` class wraps an existing [`Client`](#client) instance in order to ease calling diff --git a/src/Client.php b/src/Client.php index d112772..542b93c 100644 --- a/src/Client.php +++ b/src/Client.php @@ -102,4 +102,12 @@ public function getLocation($function) // encode request for given $function return (string)$this->encoder->encode($function, array())->getUri(); } + + public function withRequestTarget($requestTarget) + { + $copy = clone $this; + $copy->encoder = $this->encoder->withRequestTarget($requestTarget); + + return $copy; + } } diff --git a/src/Protocol/ClientEncoder.php b/src/Protocol/ClientEncoder.php index e84a0d2..65bb565 100644 --- a/src/Protocol/ClientEncoder.php +++ b/src/Protocol/ClientEncoder.php @@ -7,7 +7,8 @@ class ClientEncoder extends SoapClient { - private $request = null; + private $request = null; + private $requestTarget = null; public function encode($name, $args) { @@ -21,9 +22,12 @@ public function encode($name, $args) public function __doRequest($request, $location, $action, $version, $one_way = 0) { + + $requestTarget = $this->requestTarget !== null ? $this->requestTarget : $location; + $this->request = new Request( 'POST', - (string)$location, + (string)$requestTarget, array( 'SOAPAction' => (string)$action, 'Content-Type' => 'text/xml; charset=utf-8', @@ -35,4 +39,12 @@ public function __doRequest($request, $location, $action, $version, $one_way = 0 // do not actually block here, just pretend we're done... return ''; } + + public function withRequestTarget($requestTarget) + { + $copy = clone $this; + $copy->requestTarget = $requestTarget; + + return $copy; + } } diff --git a/tests/FunctionalTest.php b/tests/FunctionalTest.php index bd101eb..2d657f8 100644 --- a/tests/FunctionalTest.php +++ b/tests/FunctionalTest.php @@ -88,4 +88,27 @@ public function testGetLocationForUnknownFunctionNumberFails() { $this->assertEquals('http://www.thomas-bayer.com/axis2/services/BLZService', $this->client->getLocation(100)); } + + public function testWrongRequestTarget() + { + $api = new Proxy($this->client->withRequestTarget('nonsense.not.existing')); + + $promise = $api->getBank(array('blz' => '12070000')); + + $this->expectException('Exception'); + Block\await($promise, $this->loop); + } + + public function testCorrectRequestTarget() + { + $client = $this->client->withRequestTarget('nonsense.not.existing'); + $client = $client->withRequestTarget('http://www.thomas-bayer.com/axis2/services/BLZService'); + $api = new Proxy($client); + + $promise = $api->getBank(array('blz' => '12070000')); + + $result = Block\await($promise, $this->loop); + + $this->assertInternalType('object', $result); + } }