8 Commits
v4.3.2 ... v4.4

Author SHA1 Message Date
b650254d54 SoapClient::trace = SoapClientOptions::SOAP_CLIENT_TRACE_OFF fixed when SoapFault is thrown
Incompatible changes: 1) Default SoapClientOptionsBuilder method now sets tracing to ON and 2) SoapResponse now contains request in all calls so that SoapRequestFactory interface had to be changed.
2017-06-16 13:42:08 +02:00
668f2dd258 PhpUnit - code coverage whitelisted 2017-06-13 13:06:58 +02:00
a8bc834077 Mime/PartHeaders now handle both Content-ID and Content-id according to W3 specs 2017-06-12 15:14:28 +02:00
f74e4b08ce Mime Parser: throws Exception with MimePartMessage contents 2017-06-12 00:28:27 +02:00
dcd5ff5234 Travis now run only PHP 7.x builds, phpstan requirements will fail on 5.6.x or previous 2017-06-07 17:10:10 +02:00
7bd8481a4e Travis CI - test fixes
1. Travis has to run PHP build-in server separately, 2. SoapFault message expectations are now Couldn't/Could not
2017-06-07 17:05:03 +02:00
c82288d641 Travis deployment fixed 2017-06-07 16:23:47 +02:00
bb20882ade Readme updated 2017-06-07 16:18:46 +02:00
17 changed files with 389 additions and 148 deletions

View File

@ -1,16 +1,14 @@
language: php language: php
php: php:
- 5.3
- 5.4
- 5.5
- 5.6
- 7.0 - 7.0
- 7.1 - 7.1
before_script: before_script:
- composer self-update - composer self-update
- composer install - composer install
- cp phpunit.xml.dist phpunit.xml
- php -S localhost:8000 > /dev/null 2>&1 &
script: script:
- vendor/phing/phing/bin/phing -f build.xml - vendor/phing/phing/bin/phing -f build.xml

View File

@ -145,3 +145,11 @@ See a simplified implementation of ``dummyServiceMethod`` to get a clue:
``` ```
For further information and getting inspiration for your implementation, see the unit tests in ``tests`` dir. For further information and getting inspiration for your implementation, see the unit tests in ``tests`` dir.
# Contribute
[![Build Status](https://travis-ci.org/tuscanicz/BeSimpleSoap.svg?branch=master)](https://travis-ci.org/tuscanicz/BeSimpleSoap)
Feel free to contribute! Please, run the tests via Phing ``php phing -f build.xml``.
**Warning:** Unit tests may fail under Windows OS, tested under Linux, MacOS.

View File

@ -21,6 +21,16 @@
</testsuite> </testsuite>
</testsuites> </testsuites>
<filter>
<whitelist>
<directory>src</directory>
<exclude>
<directory>src/BeSimple/SoapBundle</directory>
<directory>src/BeSimple/SoapCommon/Type</directory>
</exclude>
</whitelist>
</filter>
<logging> <logging>
<log type="coverage-text" target="php://stdout" showUncoveredFiles="true" showOnlySummary="true"/> <log type="coverage-text" target="php://stdout" showUncoveredFiles="true" showOnlySummary="true"/>
<log type="coverage-clover" target="cache/clover.xml"/> <log type="coverage-clover" target="cache/clover.xml"/>

View File

@ -95,7 +95,7 @@ class SoapClient extends \SoapClient
} catch (SoapFault $soapFault) { } catch (SoapFault $soapFault) {
if (SoapFaultSourceGetter::isNativeSoapFault($soapFault)) { if (SoapFaultSourceGetter::isNativeSoapFault($soapFault)) {
$soapFault = $this->decorateNativeSoapFault($soapFault); $soapFault = $this->decorateNativeSoapFaultWithSoapResponseTracingData($soapFault);
} }
throw $soapFault; throw $soapFault;
@ -257,6 +257,23 @@ class SoapClient extends \SoapClient
return $loadedWsdlFilePath; return $loadedWsdlFilePath;
} }
private function getHttpHeadersBySoapVersion(SoapRequest $soapRequest)
{
if ($soapRequest->getVersion() === SOAP_1_1) {
return [
'Content-Type: ' . $soapRequest->getContentType(),
'SOAPAction: "' . $soapRequest->getAction() . '"',
'Connection: ' . ($this->soapOptions->isConnectionKeepAlive() ? 'Keep-Alive' : 'close'),
];
}
return [
'Content-Type: ' . $soapRequest->getContentType() . '; action="' . $soapRequest->getAction() . '"',
'Connection: ' . ($this->soapOptions->isConnectionKeepAlive() ? 'Keep-Alive' : 'close'),
];
}
private function getAttachmentFilters() private function getAttachmentFilters()
{ {
$filters = []; $filters = [];
@ -277,7 +294,6 @@ class SoapClient extends \SoapClient
array $soapAttachments = [] array $soapAttachments = []
) { ) {
if ($this->soapClientOptions->getTrace() === true) { if ($this->soapClientOptions->getTrace() === true) {
return SoapResponseFactory::createWithTracingData( return SoapResponseFactory::createWithTracingData(
$soapRequest, $soapRequest,
$curlResponse->getResponseBody(), $curlResponse->getResponseBody(),
@ -288,10 +304,8 @@ class SoapClient extends \SoapClient
} }
return SoapResponseFactory::create( return SoapResponseFactory::create(
$soapRequest,
$curlResponse->getResponseBody(), $curlResponse->getResponseBody(),
$soapRequest->getLocation(),
$soapRequest->getAction(),
$soapRequest->getVersion(),
$curlResponse->getHttpResponseContentType(), $curlResponse->getHttpResponseContentType(),
$soapAttachments $soapAttachments
); );
@ -320,68 +334,43 @@ class SoapClient extends \SoapClient
); );
} }
private function decorateNativeSoapFault(SoapFault $nativePhpSoapFault) private function decorateNativeSoapFaultWithSoapResponseTracingData(SoapFault $nativePhpSoapFault)
{ {
$soapResponse = $this->getSoapResponseFromStorage(); return $this->throwSoapFaultByTracing(
if ($soapResponse instanceof SoapResponse) {
$soapFault = $this->throwSoapFaultByTracing(
SoapFaultPrefixEnum::PREFIX_PHP . '-' . $nativePhpSoapFault->getCode(),
$nativePhpSoapFault->getMessage(),
$this->getSoapResponseTracingDataFromNativeSoapFault(
$nativePhpSoapFault,
new SoapResponseTracingData(
'Content-Type: '.$soapResponse->getRequest()->getContentType(),
$soapResponse->getRequest()->getContent(),
'Content-Type: '.$soapResponse->getContentType(),
$soapResponse->getResponseContent()
)
)
);
} else {
$soapFault = $this->throwSoapFaultByTracing(
$nativePhpSoapFault->faultcode, $nativePhpSoapFault->faultcode,
$nativePhpSoapFault->getMessage(), $nativePhpSoapFault->getMessage(),
$this->getSoapResponseTracingDataFromNativeSoapFault( $this->getSoapResponseTracingDataFromNativeSoapFaultOrStorage($nativePhpSoapFault)
$nativePhpSoapFault,
new SoapResponseTracingData(
null,
null,
null,
null
)
)
); );
} }
return $soapFault; private function getSoapResponseTracingDataFromNativeSoapFaultOrStorage(SoapFault $nativePhpSoapFault)
} {
private function getSoapResponseTracingDataFromNativeSoapFault(
SoapFault $nativePhpSoapFault,
SoapResponseTracingData $defaultSoapFaultTracingData
) {
if ($nativePhpSoapFault instanceof SoapFaultWithTracingData) { if ($nativePhpSoapFault instanceof SoapFaultWithTracingData) {
return $nativePhpSoapFault->getSoapResponseTracingData(); return $nativePhpSoapFault->getSoapResponseTracingData();
} }
return $defaultSoapFaultTracingData; return $this->getSoapResponseTracingDataFromRequestStorage();
} }
private function getHttpHeadersBySoapVersion(SoapRequest $soapRequest) private function getSoapResponseTracingDataFromRequestStorage()
{ {
if ($soapRequest->getVersion() === SOAP_1_1) { $lastResponseHeaders = $lastResponse = $lastRequestHeaders = $lastRequest = null;
$soapResponse = $this->getSoapResponseFromStorage();
if ($soapResponse instanceof SoapResponse) {
$lastResponseHeaders = 'Content-Type: ' . $soapResponse->getContentType();
$lastResponse = $soapResponse->getResponseContent();
return [ if ($soapResponse->hasRequest() === true) {
'Content-Type: ' . $soapRequest->getContentType(), $lastRequestHeaders = 'Content-Type: ' . $soapResponse->getRequest()->getContentType();
'SOAPAction: "' . $soapRequest->getAction() . '"', $lastRequest = $soapResponse->getRequest()->getContent();
'Connection: ' . ($this->soapOptions->isConnectionKeepAlive() ? 'Keep-Alive' : 'close'), }
];
} }
return [ return new SoapResponseTracingData(
'Content-Type: ' . $soapRequest->getContentType() . '; action="' . $soapRequest->getAction() . '"', $lastRequestHeaders,
'Connection: ' . ($this->soapOptions->isConnectionKeepAlive() ? 'Keep-Alive' : 'close'), $lastRequest,
]; $lastResponseHeaders,
$lastResponse
);
} }
} }

View File

@ -28,7 +28,7 @@ class SoapClientOptionsBuilder
public static function createWithDefaults() public static function createWithDefaults()
{ {
return new SoapClientOptions( return new SoapClientOptions(
SoapClientOptions::SOAP_CLIENT_TRACE_OFF, SoapClientOptions::SOAP_CLIENT_TRACE_ON,
SoapClientOptions::SOAP_CLIENT_EXCEPTIONS_ON, SoapClientOptions::SOAP_CLIENT_EXCEPTIONS_ON,
CurlOptions::DEFAULT_USER_AGENT, CurlOptions::DEFAULT_USER_AGENT,
SoapClientOptions::SOAP_CLIENT_COMPRESSION_NONE SoapClientOptions::SOAP_CLIENT_COMPRESSION_NONE

View File

@ -44,6 +44,11 @@ class SoapResponse extends CommonSoapResponse
$this->tracingData = $tracingData; $this->tracingData = $tracingData;
} }
public function hasRequest()
{
return $this->request !== null;
}
public function setRequest(SoapRequest $request) public function setRequest(SoapRequest $request)
{ {
$this->request = $request; $this->request = $request;

View File

@ -27,27 +27,24 @@ class SoapResponseFactory
/** /**
* Factory method for SoapClient\SoapResponse. * Factory method for SoapClient\SoapResponse.
* *
* @param SoapRequest $soapRequest related request object
* @param string $content Content * @param string $content Content
* @param string $location Location
* @param string $action SOAP action
* @param string $version SOAP version
* @param string $contentType Content type header * @param string $contentType Content type header
* @param SoapAttachment[] $attachments SOAP attachments * @param SoapAttachment[] $attachments SOAP attachments
* @return SoapResponse * @return SoapResponse
*/ */
public static function create( public static function create(
SoapRequest $soapRequest,
$content, $content,
$location,
$action,
$version,
$contentType, $contentType,
array $attachments = [] array $attachments = []
) { ) {
$response = new SoapResponse(); $response = new SoapResponse();
$response->setRequest($soapRequest);
$response->setContent($content); $response->setContent($content);
$response->setLocation($location); $response->setLocation($soapRequest->getLocation());
$response->setAction($action); $response->setAction($soapRequest->getAction());
$response->setVersion($version); $response->setVersion($soapRequest->getVersion());
$response->setContentType($contentType); $response->setContentType($contentType);
if (count($attachments) > 0) { if (count($attachments) > 0) {
$response->setAttachments( $response->setAttachments(
@ -82,9 +79,7 @@ class SoapResponseFactory
$response->setAction($soapRequest->getAction()); $response->setAction($soapRequest->getAction());
$response->setVersion($soapRequest->getVersion()); $response->setVersion($soapRequest->getVersion());
$response->setContentType($contentType); $response->setContentType($contentType);
if ($tracingData !== null) {
$response->setTracingData($tracingData); $response->setTracingData($tracingData);
}
if (count($attachments) > 0) { if (count($attachments) > 0) {
$response->setAttachments( $response->setAttachments(
PartFactory::createAttachmentParts($attachments) PartFactory::createAttachmentParts($attachments)

View File

@ -0,0 +1,33 @@
<?php
namespace BeSimple\SoapCommon\Mime;
use Exception;
class CouldNotParseMimeMessageException extends Exception
{
private $mimePartMessage;
private $headers;
public function __construct($message, $mimePartMessage, array $headers)
{
$this->mimePartMessage = $mimePartMessage;
$this->headers = $headers;
parent::__construct($message);
}
public function getMimePartMessage()
{
return $this->mimePartMessage;
}
public function hasHeaders()
{
return count($this->headers) > 0;
}
public function getHeaders()
{
return $this->headers;
}
}

View File

@ -31,21 +31,17 @@ class MultiPart extends PartHeader
{ {
/** /**
* Content-ID of main part. * Content-ID of main part.
*
* @var string * @var string
*/ */
protected $mainPartContentId; protected $mainPartContentId;
/** /**
* Mime parts. * Mime parts.
*
* @var \BeSimple\SoapCommon\Mime\Part[] * @var \BeSimple\SoapCommon\Mime\Part[]
*/ */
protected $parts = []; protected $parts;
/** /**
* Construct new mime object.
*
* @param string $boundary * @param string $boundary
*/ */
public function __construct($boundary = null) public function __construct($boundary = null)
@ -63,7 +59,6 @@ class MultiPart extends PartHeader
* Get mime message of this object (without headers). * Get mime message of this object (without headers).
* *
* @param boolean $withHeaders Returned mime message contains headers * @param boolean $withHeaders Returned mime message contains headers
*
* @return string * @return string
*/ */
public function getMimeMessage($withHeaders = false) public function getMimeMessage($withHeaders = false)
@ -79,30 +74,6 @@ class MultiPart extends PartHeader
return $message; return $message;
} }
/**
* Get string array with MIME headers for usage in HTTP header (with CURL).
* Only 'Content-Type' and 'Content-Description' headers are returned.
*
* @return string[]
*/
public function getHeadersForHttp()
{
$allowedHeaders = [
'Content-Type',
'Content-Description',
];
$headers = [];
foreach ($this->headers as $fieldName => $value) {
if (in_array($fieldName, $allowedHeaders)) {
$fieldValue = $this->generateHeaderFieldValue($value);
// for http only ISO-8859-1
$headers[] = $fieldName . ': '. iconv('utf-8', 'ISO-8859-1//TRANSLIT', $fieldValue);
}
}
return $headers;
}
/** /**
* Add new part to MIME message. * Add new part to MIME message.
* *

View File

@ -16,7 +16,6 @@ use BeSimple\SoapCommon\Mime\Boundary\MimeBoundaryAnalyser;
use BeSimple\SoapCommon\Mime\Parser\ContentTypeParser; use BeSimple\SoapCommon\Mime\Parser\ContentTypeParser;
use BeSimple\SoapCommon\Mime\Parser\ParsedPartList; use BeSimple\SoapCommon\Mime\Parser\ParsedPartList;
use BeSimple\SoapCommon\Mime\Parser\ParsedPartsGetter; use BeSimple\SoapCommon\Mime\Parser\ParsedPartsGetter;
use Exception;
/** /**
* Simple Multipart-Mime parser. * Simple Multipart-Mime parser.
@ -48,12 +47,14 @@ class Parser
} }
if (MimeBoundaryAnalyser::hasMessageBoundary($mimeMessageLines) === true) { if (MimeBoundaryAnalyser::hasMessageBoundary($mimeMessageLines) === true) {
if ($mimeMessageLineCount <= 1) { if ($mimeMessageLineCount <= 1) {
throw new Exception( throw new CouldNotParseMimeMessageException(
sprintf( sprintf(
'Cannot parse MultiPart message of %d characters: got unexpectable low number of lines: %s', 'Cannot parse MultiPart message of %d characters: got unexpectable low number of lines: %s',
mb_strlen($mimeMessage), mb_strlen($mimeMessage),
(string)$mimeMessageLineCount (string)$mimeMessageLineCount
) ),
$mimeMessage,
$headers
); );
} }
$parsedPartList = ParsedPartsGetter::getPartsFromMimeMessageLines( $parsedPartList = ParsedPartsGetter::getPartsFromMimeMessageLines(
@ -62,18 +63,22 @@ class Parser
$hasHttpRequestHeaders $hasHttpRequestHeaders
); );
if ($parsedPartList->hasParts() === false) { if ($parsedPartList->hasParts() === false) {
throw new Exception( throw new CouldNotParseMimeMessageException(
'Could not parse MimeMessage: no Parts for MultiPart given' 'Could not parse MimeMessage: no Parts for MultiPart given',
$mimeMessage,
$headers
); );
} }
if ($parsedPartList->hasExactlyOneMainPart() === false) { if ($parsedPartList->hasExactlyOneMainPart() === false) {
throw new Exception( throw new CouldNotParseMimeMessageException(
sprintf( sprintf(
'Could not parse MimeMessage %s HTTP headers: unexpected count of main ParsedParts: %s (total: %d)', 'Could not parse MimeMessage %s HTTP headers: unexpected count of main ParsedParts: %s (total: %d)',
$hasHttpRequestHeaders ? 'with' : 'w/o', $hasHttpRequestHeaders ? 'with' : 'w/o',
implode(', ', $parsedPartList->getPartContentIds()), implode(', ', $parsedPartList->getPartContentIds()),
$parsedPartList->getMainPartCount() $parsedPartList->getMainPartCount()
) ),
$mimeMessage,
$headers
); );
} }
self::appendPartsToMultiPart( self::appendPartsToMultiPart(

View File

@ -68,9 +68,7 @@ class Part extends PartHeader
} }
/** /**
* __toString. * @return string
*
* @return mixed
*/ */
public function __toString() public function __toString()
{ {
@ -98,11 +96,7 @@ class Part extends PartHeader
} }
/** /**
* Set mime content. * @param string $content
*
* @param mixed $content Content to set
*
* @return void
*/ */
public function setContent($content) public function setContent($content)
{ {
@ -111,7 +105,6 @@ class Part extends PartHeader
/** /**
* Get complete mime message of this object. * Get complete mime message of this object.
*
* @return string * @return string
*/ */
public function getMessagePart() public function getMessagePart()
@ -121,7 +114,6 @@ class Part extends PartHeader
/** /**
* Generate body. * Generate body.
*
* @return string * @return string
*/ */
protected function generateBody() protected function generateBody()

View File

@ -19,7 +19,10 @@ namespace BeSimple\SoapCommon\Mime;
*/ */
abstract class PartHeader abstract class PartHeader
{ {
protected $headers = []; /** @var string[] array of headers with lower-cased keys */
private $headers;
/** @var string[] array of lower-cased keys and their original variants */
private $headersOriginalKeys;
/** /**
* Add a new header to the mime part. * Add a new header to the mime part.
@ -32,19 +35,21 @@ abstract class PartHeader
*/ */
public function setHeader($name, $value, $subValue = null) public function setHeader($name, $value, $subValue = null)
{ {
if (isset($this->headers[$name]) && !is_null($subValue)) { $lowerCaseName = mb_strtolower($name);
if (!is_array($this->headers[$name])) { $this->headersOriginalKeys[$lowerCaseName] = $name;
$this->headers[$name] = [ if (isset($this->headers[$lowerCaseName]) && !is_null($subValue)) {
'@' => $this->headers[$name], if (!is_array($this->headers[$lowerCaseName])) {
$this->headers[$lowerCaseName] = [
'@' => $this->headers[$lowerCaseName],
$value => $subValue, $value => $subValue,
]; ];
} else { } else {
$this->headers[$name][$value] = $subValue; $this->headers[$lowerCaseName][$value] = $subValue;
} }
} elseif (isset($this->headers[$name]) && is_array($this->headers[$name]) && isset($this->headers[$name]['@'])) { } elseif (isset($this->headers[$lowerCaseName]) && is_array($this->headers[$lowerCaseName]) && isset($this->headers[$lowerCaseName]['@'])) {
$this->headers[$name]['@'] = $value; $this->headers[$lowerCaseName]['@'] = $value;
} else { } else {
$this->headers[$name] = $value; $this->headers[$lowerCaseName] = $value;
} }
} }
@ -58,17 +63,18 @@ abstract class PartHeader
*/ */
public function getHeader($name, $subValue = null) public function getHeader($name, $subValue = null)
{ {
if (isset($this->headers[$name])) { $lowerCaseName = mb_strtolower($name);
if (isset($this->headers[$lowerCaseName])) {
if (!is_null($subValue)) { if (!is_null($subValue)) {
if (is_array($this->headers[$name]) && isset($this->headers[$name][$subValue])) { if (is_array($this->headers[$lowerCaseName]) && isset($this->headers[$lowerCaseName][$subValue])) {
return $this->headers[$name][$subValue]; return $this->headers[$lowerCaseName][$subValue];
} else { } else {
return null; return null;
} }
} elseif (is_array($this->headers[$name]) && isset($this->headers[$name]['@'])) { } elseif (is_array($this->headers[$lowerCaseName]) && isset($this->headers[$lowerCaseName]['@'])) {
return $this->headers[$name]['@']; return $this->headers[$lowerCaseName]['@'];
} else { } else {
return $this->headers[$name]; return $this->headers[$lowerCaseName];
} }
} }
@ -80,6 +86,30 @@ abstract class PartHeader
return $this->headers; return $this->headers;
} }
/**
* Get string array with MIME headers for usage in HTTP header (with CURL).
* Only 'Content-Type' and 'Content-Description' headers are returned.
*
* @return string[]
*/
public function getHeadersForHttp()
{
$allowedHeadersLowerCase = [
'content-type',
'content-description',
];
$headers = [];
foreach ($this->headers as $fieldName => $value) {
if (in_array($fieldName, $allowedHeadersLowerCase)) {
$fieldValue = $this->generateHeaderFieldValue($value);
// for http only ISO-8859-1
$headers[] = $this->headersOriginalKeys[$fieldName] . ': '. iconv('utf-8', 'ISO-8859-1//TRANSLIT', $fieldValue);
}
}
return $headers;
}
/** /**
* Generate headers. * Generate headers.
* *
@ -90,7 +120,7 @@ abstract class PartHeader
$headers = ''; $headers = '';
foreach ($this->headers as $fieldName => $value) { foreach ($this->headers as $fieldName => $value) {
$fieldValue = $this->generateHeaderFieldValue($value); $fieldValue = $this->generateHeaderFieldValue($value);
$headers .= $fieldName . ': ' . $fieldValue . "\n"; $headers .= $this->headersOriginalKeys[$fieldName] . ': ' . $fieldValue . "\n";
} }
return $headers; return $headers;
@ -99,19 +129,18 @@ abstract class PartHeader
/** /**
* Generates a header field value from the given value paramater. * Generates a header field value from the given value paramater.
* *
* @param array(string=>string)|string $value Header value * @param string[]|string $value Header value
*
* @return string * @return string
*/ */
protected function generateHeaderFieldValue($value) protected function generateHeaderFieldValue($value)
{ {
$fieldValue = ''; $fieldValue = '';
if (is_array($value)) { if (is_array($value) === true) {
if (isset($value['@'])) { if (isset($value['@'])) {
$fieldValue .= $value['@']; $fieldValue .= $value['@'];
} }
foreach ($value as $subName => $subValue) { foreach ($value as $subName => $subValue) {
if ($subName != '@') { if ($subName !== '@') {
$fieldValue .= '; ' . $subName . '=' . $this->quoteValueString($subValue); $fieldValue .= '; ' . $subName . '=' . $this->quoteValueString($subValue);
} }
} }
@ -134,8 +163,8 @@ abstract class PartHeader
{ {
if (preg_match('~[()<>@,;:\\"/\[\]?=]~', $string)) { if (preg_match('~[()<>@,;:\\"/\[\]?=]~', $string)) {
return '"' . $string . '"'; return '"' . $string . '"';
} else { }
return $string; return $string;
} }
}
} }

View File

@ -75,7 +75,7 @@ class SoapClientTest extends PHPUnit_Framework_TestCase
public function testSoapCallWithCustomEndpointInvalidShouldFail() public function testSoapCallWithCustomEndpointInvalidShouldFail()
{ {
$this->setExpectedException(Exception::class, 'Could not resolve host'); $this->setExpectedException(Exception::class, 't resolve host');
$soapClient = $this->getSoapBuilder()->build( $soapClient = $this->getSoapBuilder()->build(
SoapClientOptionsBuilder::createWithEndpointLocation(self::TEST_REMOTE_ENDPOINT_NOT_WORKING), SoapClientOptionsBuilder::createWithEndpointLocation(self::TEST_REMOTE_ENDPOINT_NOT_WORKING),

View File

@ -115,6 +115,35 @@ class SoapServerTest extends PHPUnit_Framework_TestCase
self::assertCount(2, $response->getAttachments()); self::assertCount(2, $response->getAttachments());
} }
public function testHandleRequestWithSwaResponseAndLowerCaseHeaders()
{
$dummyService = new DummyService();
$classMap = new ClassMap();
foreach ($dummyService->getClassMap() as $type => $className) {
$classMap->add($type, $className);
}
$soapServerBuilder = new SoapServerBuilder();
$soapServerOptions = SoapServerOptionsBuilder::createWithDefaults($dummyService);
$soapOptions = SoapOptionsBuilder::createSwaWithClassMap($dummyService->getWsdlPath(), $classMap);
$soapServer = $soapServerBuilder->build($soapServerOptions, $soapOptions);
$request = $soapServer->createRequest(
$dummyService->getEndpoint(),
'DummyService.dummyServiceMethodWithAttachments',
'multipart/related; type="text/xml"; start="<rootpart@soapui.org>"; boundary="----=_Part_6_2094841787.1482231370463"',
file_get_contents(self::FIXTURES_DIR.'/Message/Request/dummyServiceMethodWithAttachmentsAndLowerCaseHeaders.request.mimepart.message')
);
$response = $soapServer->handleRequest($request);
file_put_contents(self::CACHE_DIR . '/SoapServerTestSwaResponseWithAttachmentsAndLowerCaseHeaders.xml', $response->getContent());
self::assertNotContains("\r\n", $response->getContent(), 'Response cannot contain CRLF line endings');
self::assertContains('dummyServiceMethodWithAttachmentsResponse', $response->getContent());
self::assertSame('DummyService.dummyServiceMethodWithAttachments', $response->getAction());
self::assertTrue($response->hasAttachments(), 'Response should contain attachments');
self::assertCount(2, $response->getAttachments());
}
public function getSoapServerBuilder() public function getSoapServerBuilder()
{ {
return new SoapServerBuilder(); return new SoapServerBuilder();

View File

@ -2,9 +2,11 @@
namespace BeSimple; namespace BeSimple;
use BeSimple\SoapClient\Curl\CurlOptions;
use BeSimple\SoapClient\SoapClientBuilder; use BeSimple\SoapClient\SoapClientBuilder;
use BeSimple\SoapClient\SoapClientOptionsBuilder; use BeSimple\SoapClient\SoapClientOptionsBuilder;
use BeSimple\SoapClient\SoapFaultWithTracingData; use BeSimple\SoapClient\SoapFaultWithTracingData;
use BeSimple\SoapClient\SoapOptions\SoapClientOptions;
use BeSimple\SoapCommon\ClassMap; use BeSimple\SoapCommon\ClassMap;
use BeSimple\SoapCommon\SoapOptions\SoapOptions; use BeSimple\SoapCommon\SoapOptions\SoapOptions;
use BeSimple\SoapCommon\SoapOptionsBuilder; use BeSimple\SoapCommon\SoapOptionsBuilder;
@ -34,9 +36,58 @@ class SoapServerAndSoapClientCommunicationSoapFaultsTest extends PHPUnit_Framewo
pclose($this->localWebServerProcess); pclose($this->localWebServerProcess);
} }
public function testSoapCallSwaWithLargeSwaResponseWithSoapFaultAndTracingOff()
{
$soapClient = $this->getSoapClientBuilder()->buildWithSoapHeader(
new SoapClientOptions(
SoapClientOptions::SOAP_CLIENT_TRACE_OFF,
SoapClientOptions::SOAP_CLIENT_EXCEPTIONS_ON,
CurlOptions::DEFAULT_USER_AGENT,
SoapClientOptions::SOAP_CLIENT_COMPRESSION_NONE,
SoapClientOptions::SOAP_CLIENT_AUTHENTICATION_NONE,
SoapClientOptions::SOAP_CLIENT_PROXY_NONE,
self::TEST_HTTP_URL.'/SwaSenderSoapFaultEndpoint.php'
),
SoapOptionsBuilder::createSwaWithClassMap(
self::TEST_HTTP_URL.'/SwaSenderEndpoint.php?wsdl',
new ClassMap([
'GenerateTestRequest' => GenerateTestRequest::class,
]),
SoapOptions::SOAP_CACHE_TYPE_NONE
),
new SoapHeader('http://schema.testcase', 'SoapHeader', [
'user' => 'admin',
])
);
$this->setExpectedException(SoapFault::class);
try {
$soapClient->soapCall('dummyServiceMethodWithOutgoingLargeSwa', []);
} catch (SoapFault $e) {
self::assertNotInstanceOf(
SoapFaultWithTracingData::class,
$e,
'SoapClient must not return tracing data when SoapClientOptions::trace is off.'
);
self::assertEquals(
'911',
$e->faultcode
);
self::assertContains(
'with HTTP response code 500 with Message: This is a dummy SoapFault. and Code: 911',
$e->getMessage()
);
throw $e;
}
self::fail('Expected SoapFault was not thrown');
}
public function testSoapCallSwaWithLargeSwaResponseWithSoapFault() public function testSoapCallSwaWithLargeSwaResponseWithSoapFault()
{ {
$soapClient = $this->getSoapBuilder()->buildWithSoapHeader( $soapClient = $this->getSoapClientBuilder()->buildWithSoapHeader(
SoapClientOptionsBuilder::createWithEndpointLocation( SoapClientOptionsBuilder::createWithEndpointLocation(
self::TEST_HTTP_URL.'/SwaSenderSoapFaultEndpoint.php' self::TEST_HTTP_URL.'/SwaSenderSoapFaultEndpoint.php'
), ),
@ -59,7 +110,8 @@ class SoapServerAndSoapClientCommunicationSoapFaultsTest extends PHPUnit_Framewo
} catch (SoapFault $e) { } catch (SoapFault $e) {
self::assertInstanceOf( self::assertInstanceOf(
SoapFaultWithTracingData::class, SoapFaultWithTracingData::class,
$e $e,
'SoapClient must return tracing data when SoapClientOptions::trace is on.'
); );
/** @var SoapFaultWithTracingData $e */ /** @var SoapFaultWithTracingData $e */
self::assertEquals( self::assertEquals(
@ -91,7 +143,7 @@ class SoapServerAndSoapClientCommunicationSoapFaultsTest extends PHPUnit_Framewo
public function testSoapCallSwaWithLargeSwaResponseWithNoResponseFromEndpoint() public function testSoapCallSwaWithLargeSwaResponseWithNoResponseFromEndpoint()
{ {
$soapClient = $this->getSoapBuilder()->buildWithSoapHeader( $soapClient = $this->getSoapClientBuilder()->buildWithSoapHeader(
SoapClientOptionsBuilder::createWithEndpointLocation( SoapClientOptionsBuilder::createWithEndpointLocation(
self::TEST_HTTP_URL.'/NoSuchEndpointExists' self::TEST_HTTP_URL.'/NoSuchEndpointExists'
), ),
@ -150,7 +202,7 @@ class SoapServerAndSoapClientCommunicationSoapFaultsTest extends PHPUnit_Framewo
public function testSoapCallSwaWithLargeSwaResponseWithNoResponseFromEndpointHost() public function testSoapCallSwaWithLargeSwaResponseWithNoResponseFromEndpointHost()
{ {
$soapClient = $this->getSoapBuilder()->buildWithSoapHeader( $soapClient = $this->getSoapClientBuilder()->buildWithSoapHeader(
SoapClientOptionsBuilder::createWithEndpointLocation( SoapClientOptionsBuilder::createWithEndpointLocation(
self::TEST_HTTP_URL_INVALID.'/NoSuchEndpointExists' self::TEST_HTTP_URL_INVALID.'/NoSuchEndpointExists'
), ),
@ -181,7 +233,7 @@ class SoapServerAndSoapClientCommunicationSoapFaultsTest extends PHPUnit_Framewo
$e->faultcode $e->faultcode
); );
self::assertContains( self::assertContains(
'Could not resolve host', 't resolve host',
$e->getMessage() $e->getMessage()
); );
self::assertNull( self::assertNull(
@ -204,7 +256,7 @@ class SoapServerAndSoapClientCommunicationSoapFaultsTest extends PHPUnit_Framewo
self::fail('Expected SoapFault was not thrown'); self::fail('Expected SoapFault was not thrown');
} }
private function getSoapBuilder() private function getSoapClientBuilder()
{ {
return new SoapClientBuilder(); return new SoapClientBuilder();
} }

View File

@ -3,12 +3,15 @@
namespace BeSimple; namespace BeSimple;
use BeSimple\SoapBundle\Soap\SoapAttachment; use BeSimple\SoapBundle\Soap\SoapAttachment;
use BeSimple\SoapClient\Curl\CurlOptions;
use BeSimple\SoapClient\SoapClientBuilder; use BeSimple\SoapClient\SoapClientBuilder;
use BeSimple\SoapClient\SoapClientOptionsBuilder; use BeSimple\SoapClient\SoapClientOptionsBuilder;
use BeSimple\SoapClient\SoapFaultWithTracingData; use BeSimple\SoapClient\SoapFaultWithTracingData;
use BeSimple\SoapClient\SoapOptions\SoapClientOptions;
use BeSimple\SoapCommon\ClassMap; use BeSimple\SoapCommon\ClassMap;
use BeSimple\SoapCommon\SoapOptions\SoapOptions; use BeSimple\SoapCommon\SoapOptions\SoapOptions;
use BeSimple\SoapCommon\SoapOptionsBuilder; use BeSimple\SoapCommon\SoapOptionsBuilder;
use BeSimple\SoapCommon\SoapRequest;
use BeSimple\SoapServer\SoapServerBuilder; use BeSimple\SoapServer\SoapServerBuilder;
use BeSimple\SoapServer\SoapServerOptionsBuilder; use BeSimple\SoapServer\SoapServerOptionsBuilder;
use Fixtures\DummyService; use Fixtures\DummyService;
@ -108,6 +111,65 @@ class SoapServerAndSoapClientCommunicationTest extends PHPUnit_Framework_TestCas
self::assertContains('</dummyServiceReturn>', $soapResponse->getResponseContent()); self::assertContains('</dummyServiceReturn>', $soapResponse->getResponseContent());
self::assertTrue($soapResponse->hasAttachments(), 'Response should contain attachments'); self::assertTrue($soapResponse->hasAttachments(), 'Response should contain attachments');
self::assertCount(3, $attachments); self::assertCount(3, $attachments);
self::assertInstanceOf(
SoapRequest::class,
$soapResponse->getRequest(),
'SoapResponse::request must be SoapRequest for SoapClient calls with enabled tracing'
);
file_put_contents(self::CACHE_DIR . '/multipart-message-soap-client-response.xml', $soapResponse->getContent());
foreach ($soapResponse->getAttachments() as $attachment) {
$fileName = preg_replace('/\<|\>/', '', $attachment->getContentId());
file_put_contents(self::CACHE_DIR . DIRECTORY_SEPARATOR . 'attachment-client-response-' . $fileName, $attachment->getContent());
self::assertRegExp('/filename\.(docx|html|txt)/', $fileName);
}
self::assertEquals(
filesize(self::LARGE_SWA_FILE),
filesize(self::CACHE_DIR.'/attachment-client-response-filename.docx'),
'File cannot differ after transport from SoapClient to SoapServer'
);
}
public function testSoapCallSwaWithLargeSwaResponseAndTracingOff()
{
$soapClient = $this->getSoapBuilder()->buildWithSoapHeader(
new SoapClientOptions(
SoapClientOptions::SOAP_CLIENT_TRACE_OFF,
SoapClientOptions::SOAP_CLIENT_EXCEPTIONS_ON,
CurlOptions::DEFAULT_USER_AGENT,
SoapClientOptions::SOAP_CLIENT_COMPRESSION_NONE,
SoapClientOptions::SOAP_CLIENT_AUTHENTICATION_NONE,
SoapClientOptions::SOAP_CLIENT_PROXY_NONE,
self::TEST_HTTP_URL.'/SwaSenderEndpoint.php'
),
SoapOptionsBuilder::createSwaWithClassMap(
self::TEST_HTTP_URL.'/SwaSenderEndpoint.php?wsdl',
new ClassMap([
'GenerateTestRequest' => GenerateTestRequest::class,
]),
SoapOptions::SOAP_CACHE_TYPE_NONE
),
new SoapHeader('http://schema.testcase', 'SoapHeader', [
'user' => 'admin',
])
);
$request = new DummyServiceMethodWithOutgoingLargeSwaRequest();
$request->dummyAttribute = 1;
$soapResponse = $soapClient->soapCall('dummyServiceMethodWithOutgoingLargeSwa', [$request]);
$attachments = $soapResponse->getAttachments();
self::assertContains('</dummyServiceReturn>', $soapResponse->getResponseContent());
self::assertTrue($soapResponse->hasAttachments(), 'Response should contain attachments');
self::assertCount(3, $attachments);
self::assertInstanceOf(
SoapRequest::class,
$soapResponse->getRequest(),
'SoapResponse::request must be SoapRequest for SoapClient calls with disabled tracing'
);
file_put_contents(self::CACHE_DIR . '/multipart-message-soap-client-response.xml', $soapResponse->getContent()); file_put_contents(self::CACHE_DIR . '/multipart-message-soap-client-response.xml', $soapResponse->getContent());
foreach ($soapResponse->getAttachments() as $attachment) { foreach ($soapResponse->getAttachments() as $attachment) {
@ -164,6 +226,7 @@ class SoapServerAndSoapClientCommunicationTest extends PHPUnit_Framework_TestCas
self::assertContains('</dummyServiceReturn>', $soapResponse->getResponseContent()); self::assertContains('</dummyServiceReturn>', $soapResponse->getResponseContent());
self::assertTrue($soapResponse->getRequest()->hasAttachments(), 'Response MUST contain attachments'); self::assertTrue($soapResponse->getRequest()->hasAttachments(), 'Response MUST contain attachments');
self::assertFalse($soapResponse->hasAttachments(), 'Response MUST NOT contain attachments'); self::assertFalse($soapResponse->hasAttachments(), 'Response MUST NOT contain attachments');
self::assertInstanceOf(SoapRequest::class, $soapResponse->getRequest());
foreach ($soapResponse->getRequest()->getAttachments() as $attachment) { foreach ($soapResponse->getRequest()->getAttachments() as $attachment) {
file_put_contents(self::CACHE_DIR . '/attachment-client-request-'.trim($attachment->getContentId(), '<>'), $attachment->getContent()); file_put_contents(self::CACHE_DIR . '/attachment-client-request-'.trim($attachment->getContentId(), '<>'), $attachment->getContent());

View File

@ -0,0 +1,62 @@
------=_Part_6_2094841787.1482231370463
Content-type: text/xml; charset=UTF-8
Content-transfer-Encoding: 8bit
Content-id: <rootpart@soapui.org>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sch="http://schema.testcase">
<soapenv:Header>
<sch:SoapHeader>
<user>admin</user>
</sch:SoapHeader>
</soapenv:Header>
<soapenv:Body>
<sch:dummyServiceMethodWithAttachments>
<request>
<dummyAttribute>3</dummyAttribute>
<includeAttachments>true</includeAttachments>
</request>
</sch:dummyServiceMethodWithAttachments>
</soapenv:Body>
</soapenv:Envelope>
------=_Part_6_2094841787.1482231370463
Content-type: text/html; charset=us-ascii; name=test-page.html
Content-transfer-Encoding: 7bit
Content-id: <test-page.html>
Content-disposition: attachment; name="test-page.html"; filename="test-page.html"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Test file page</title>
<style type="text/css">
<!--
h1 {
font-family: Arial, Helvetica, sans-serif;
font-size: 11pt;
}
-->
</style>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
------=_Part_6_2094841787.1482231370463
Content-type: application/x-sh; name=testscript.sh
Content-transfer-Encoding: binary
Content-id: <testscript.sh>
Content-disposition: attachment; name="testscript.sh"; filename="testscript.sh"
#!/bin/sh
### ====================================================================== ###
## ##
## Test Script ##
## ##
### ====================================================================== ###
------=_Part_6_2094841787.1482231370463--