Skip to content

Commit 8a10ea4

Browse files
authored
Merge pull request #57 from KarimGeiger/patch-1
Add `withHeaders()` method to add custom SOAP headers
2 parents e802f94 + 29c074a commit 8a10ea4

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ This project provides a *simple* API for invoking *async* RPCs to remote web ser
4242
* [getTypes()](#gettypes)
4343
* [getLocation()](#getlocation)
4444
* [withLocation()](#withlocation)
45+
* [withHeaders()](#withheaders)
4546
* [Proxy](#proxy)
4647
* [Functions](#functions)
4748
* [Promises](#promises)
@@ -307,6 +308,17 @@ assert('http://example.com/soap' === $client->getLocation('echo'));
307308
As an alternative to this method, you can also set the `location` option
308309
in the `Client` constructor (such as when in non-WSDL mode).
309310

311+
#### withHeaders()
312+
313+
The `withHeaders(array $headers): self` method can be used to
314+
return a new `Client` with the updated headers for all functions.
315+
This allows to set specific headers required by some SOAP endpoints, like
316+
for authentication, etc.
317+
318+
```php
319+
$client = $client->withHeaders([new SoapHeader(...)]);
320+
```
321+
310322
### Proxy
311323

312324
The `Proxy` class wraps an existing [`Client`](#client) instance in order to ease calling

src/Client.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,4 +322,19 @@ public function withLocation(string $location): self
322322

323323
return $client;
324324
}
325+
326+
/**
327+
* Returns a new `Client` with the given headers for all functions.
328+
*
329+
* @param array $headers
330+
* @return self
331+
*/
332+
public function withHeaders(array $headers): self
333+
{
334+
$client = clone $this;
335+
$client->encoder = clone $this->encoder;
336+
$client->encoder->__setSoapHeaders($headers);
337+
338+
return $client;
339+
}
325340
}

tests/ClientTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,29 @@ public function testNonWsdlClientSendsPostRequestToGivenLocationForAnySoapCall()
5959

6060
$client->soapCall('ping', array());
6161
}
62+
63+
public function testWithHeaderProperlySetsHeadersInRequestBody()
64+
{
65+
$browser = $this->getMockBuilder('React\Http\Browser')->disableOriginalConstructor()->getMock();
66+
$browser->expects($this->once())->method('withRejectErrorResponse')->willReturnSelf();
67+
$browser->expects($this->once())->method('withFollowRedirects')->willReturnSelf();
68+
$browser->expects($this->once())->method('request')->with('POST', 'http://example.com')
69+
->willReturnCallback(function ($method, $url, $headers, $body) {
70+
// We expect the SOAP header to be present on the request body
71+
$this->assertStringContainsString(
72+
'<SOAP-ENV:Header><ns2:echoMeStringRequest>hello world</ns2:echoMeStringRequest></SOAP-ENV:Header>',
73+
$body
74+
);
75+
return new Promise(function () { });
76+
});
77+
78+
$client = new Client($browser, null, ['location' => 'http://example.com', 'uri' => 'http://example.com/uri']);
79+
$client = $client->withHeaders([new \SoapHeader(
80+
'http://soapinterop.org/echoheader/',
81+
'echoMeStringRequest',
82+
'hello world'
83+
)]);
84+
85+
$client->soapCall('ping', []);
86+
}
6287
}

0 commit comments

Comments
 (0)