From 5d0a9697d390f1c8af2f1a411372c596a9a35a6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josef=20K=C5=99=C3=AD=C5=BE?= Date: Mon, 12 Dec 2016 12:07:59 +0100 Subject: [PATCH] Fix request headers --- src/Driver/GuzzleSoapClientDriver.php | 9 ++-- .../Driver/GuzzleSoapClientDriverTest.php | 49 +++++++++++++++++++ 2 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 tests/SlevomatEET/Driver/GuzzleSoapClientDriverTest.php diff --git a/src/Driver/GuzzleSoapClientDriver.php b/src/Driver/GuzzleSoapClientDriver.php index 3f5f6e9..a1a0a19 100644 --- a/src/Driver/GuzzleSoapClientDriver.php +++ b/src/Driver/GuzzleSoapClientDriver.php @@ -6,6 +6,7 @@ class GuzzleSoapClientDriver implements SoapClientDriver { const DEFAULT_TIMEOUT = 2.5; + const HEADER_USER_AGENT = 'PHP'; /** @var \GuzzleHttp\Client */ private $httpClient; @@ -26,10 +27,10 @@ public function __construct(\GuzzleHttp\Client $httpClient, float $connectionTim public function send(string $request, string $location, string $action, int $soapVersion): string { $headers = [ - 'User-Agent: PHP', - sprintf('Content-Type: %s; charset=utf-8', $soapVersion === 2 ? 'application/soap+xml' : 'text/xml'), - sprintf('SOAPAction: %s', $action), - sprintf('Content-Length: %s', strlen($request)), + 'User-Agent' => self::HEADER_USER_AGENT, + 'Content-Type' => sprintf('%s; charset=utf-8', $soapVersion === 2 ? 'application/soap+xml' : 'text/xml'), + 'SOAPAction' => $action, + 'Content-Length' => strlen($request), ]; $request = new \GuzzleHttp\Psr7\Request('POST', $location, $headers, $request); diff --git a/tests/SlevomatEET/Driver/GuzzleSoapClientDriverTest.php b/tests/SlevomatEET/Driver/GuzzleSoapClientDriverTest.php new file mode 100644 index 0000000..f03b28a --- /dev/null +++ b/tests/SlevomatEET/Driver/GuzzleSoapClientDriverTest.php @@ -0,0 +1,49 @@ +createMock(\GuzzleHttp\Client::class); + $guzzleHttpClient + ->expects(self::once()) + ->method('send') + ->with(self::callback(function (\GuzzleHttp\Psr7\Request $request) use ($requestData, $location, $soapAction) { + $this->assertEquals([ + 'Host' => [ + 'pg.eet.cz', + ], + 'User-Agent' => [ + GuzzleSoapClientDriver::HEADER_USER_AGENT, + ], + 'Content-Type' => [ + 'text/xml; charset=utf-8', + ], + 'SOAPAction' => [ + $soapAction, + ], + 'Content-Length' => [ + strlen($requestData), + ], + ], $request->getHeaders()); + $this->assertEquals($location, (string) $request->getUri()); + + return true; + })) + ->willReturn(new \GuzzleHttp\Psr7\Response(200, [], $responseData)); + + $guzzleSoapClientDriver = new GuzzleSoapClientDriver($guzzleHttpClient); + $response = $guzzleSoapClientDriver->send($requestData, $location, $soapAction, SOAP_1_1); + + $this->assertSame($responseData, $response); + } + +}