SoapServer/Client now handle binary files correctly & large tests/fixtures update

Soap Server and Client were breaking binary files during transfer due to invalid Mime Message Parser. Now is it working fine with no errors, but the message parser is about to be rewritten into a better form.
This commit is contained in:
Petr Bechyně
2017-04-04 18:36:18 +02:00
parent 311f9e6d08
commit 564005da93
42 changed files with 1135 additions and 250 deletions

View File

@ -3,6 +3,8 @@
namespace BeSimple\SoapClient;
use BeSimple\SoapBundle\Soap\SoapAttachment;
use BeSimple\SoapClient\Curl\CurlOptions;
use BeSimple\SoapClient\SoapOptions\SoapClientOptions;
use BeSimple\SoapCommon\ClassMap;
use BeSimple\SoapCommon\SoapOptions\SoapOptions;
use BeSimple\SoapCommon\SoapOptionsBuilder;
@ -182,8 +184,14 @@ class SoapClientTest extends PHPUnit_Framework_TestCase
self::assertContains('start="<part-', $tracingData->getLastRequestHeaders(), 'Headers should link to first MultiPart');
self::assertContains('action="', $tracingData->getLastRequestHeaders(), 'Headers should contain SOAP action');
self::assertEquals(
$this->removeOneTimeData(file_get_contents(self::FIXTURES_DIR.'/soapRequestWithTwoAttachments.request')),
$this->removeOneTimeData($tracingData->getLastRequest()),
$this->removeOneTimeData(
file_get_contents(
self::FIXTURES_DIR.'/Message/Request/GetUKLocationByCounty.request.mimepart.message'
)
),
$this->removeOneTimeData(
$tracingData->getLastRequest()
),
'Requests must match after onetime data were removed'
);
}
@ -216,7 +224,7 @@ class SoapClientTest extends PHPUnit_Framework_TestCase
self::assertNotContains('start="<part-', $tracingData->getLastRequestHeaders(), 'Headers should link to first MultiPart');
self::assertContains('action="', $tracingData->getLastRequestHeaders(), 'Headers should contain SOAP action');
self::assertStringEqualsFile(
self::FIXTURES_DIR.'/soapRequestWithNoAttachments.request',
self::FIXTURES_DIR.'/Message/Request/GetUKLocationByCounty.request.message',
$tracingData->getLastRequest(),
'Requests must match'
);

View File

@ -0,0 +1,75 @@
<?php
namespace BeSimple\SoapCommon\Mime\Boundary;
use PHPUnit_Framework_TestCase;
class MimeBoundaryAnalyserTest extends PHPUnit_Framework_TestCase
{
const EXPECTED_HAS_BOUNDARY = true;
const EXPECTED_HAS_NO_BOUNDARY = false;
const EXPECTED_IS_BOUNDARY = true;
const EXPECTED_IS_NOT_BOUNDARY = false;
/**
* @dataProvider mimeMessageLinesDataProvider
* @param string[] $mimeMessageLines
* @param bool $expectHasBoundary
*/
public function testHasMessageBoundary(array $mimeMessageLines, $expectHasBoundary)
{
$hasMessageBoundary = MimeBoundaryAnalyser::hasMessageBoundary($mimeMessageLines);
self::assertEquals($expectHasBoundary, $hasMessageBoundary);
}
/**
* @dataProvider mimeMessageLineDataProvider
* @param string $mimeMessageLine
* @param bool $expectIsBoundary
*/
public function testIsMessageLineBoundary($mimeMessageLine, $expectIsBoundary)
{
$isMessageBoundary = MimeBoundaryAnalyser::isMessageLineBoundary($mimeMessageLine);
self::assertEquals($expectIsBoundary, $isMessageBoundary);
}
public function mimeMessageLinesDataProvider()
{
return [
[
[
'',
'mesage line -- has no boundary',
'-- this line is a boundary',
'',
'',
'-- this line is also a boundary --',
' -- this is not a boundary'
],
self::EXPECTED_HAS_BOUNDARY
],
[
[
'',
'mesage line -- has no boundary',
'',
'',
' -- this is not a boundary'
],
self::EXPECTED_HAS_NO_BOUNDARY
]
];
}
public function mimeMessageLineDataProvider()
{
return [
['-- this line is boundary', self::EXPECTED_IS_BOUNDARY],
['-- this line is also a boundary --', self::EXPECTED_IS_BOUNDARY],
['mesage line -- is not boundary', self::EXPECTED_IS_NOT_BOUNDARY],
[' -- mesage line -- is not boundary', self::EXPECTED_IS_NOT_BOUNDARY],
];
}
}

View File

@ -0,0 +1,104 @@
<?php
namespace BeSimple\SoapCommon\Mime\Parser;
use BeSimple\SoapCommon\Mime\MultiPart;
use Exception;
use PHPUnit_Framework_TestCase;
class ParsedPartsGetterTest extends PHPUnit_Framework_TestCase
{
const TEST_CASE_SHOULD_FAIL = true;
const TEST_CASE_SHOULD_NOT_FAIL = false;
/**
* @dataProvider provideMimeMessageLines
* @param MultiPart $multiPart
* @param array $mimeMessageLines
* @param bool $hasHttpRequestHeaders
* @param bool $testCaseShouldFail
* @param string|null $failedTestCaseFailMessage
*/
public function testGetPartsFromMimeMessageLines(
MultiPart $multiPart,
array $mimeMessageLines,
$hasHttpRequestHeaders,
$testCaseShouldFail,
$failedTestCaseFailMessage = null
) {
if ($testCaseShouldFail === true) {
$this->setExpectedException(Exception::class, $failedTestCaseFailMessage);
}
$parsedPartsList = ParsedPartsGetter::getPartsFromMimeMessageLines(
$multiPart,
$mimeMessageLines,
$hasHttpRequestHeaders
);
if ($testCaseShouldFail === false) {
self::assertInstanceOf(ParsedPartList::class, $parsedPartsList);
self::assertGreaterThanOrEqual(3, $parsedPartsList->getPartCount());
self::assertTrue($parsedPartsList->hasExactlyOneMainPart());
}
}
public function provideMimeMessageLines()
{
$mimePartWithHeadersForSwaResponse = new MultiPart();
$mimePartWithHeadersForSwaResponse->setHeader('Content-Type', 'boundary', 'Part_13_58e3bc35f3743.58e3bc35f376f');
$mimePartWithHeadersForSwaResponse->setHeader('Content-Type', 'start', '<part-424dbe68-e2da-450f-9a82-cc3e82742503@response.info>');
$mimePartWithWrongHeadersForSwaResponse = new MultiPart();
$mimePartWithWrongHeadersForSwaResponse->setHeader('Content-Type', 'boundary', 'non-existing');
$mimePartWithWrongHeadersForSwaResponse->setHeader('Content-Type', 'start', '<does-not-exist>');
$mimePartWithHeadersForSwaRequest = new MultiPart();
$mimePartWithHeadersForSwaRequest->setHeader('Content-Type', 'boundary', '----=_Part_6_2094841787.1482231370463');
$mimePartWithHeadersForSwaRequest->setHeader('Content-Type', 'start', '<rootpart@soapui.org>');
return [
'ParseSwaResponseWith2FilesAnd1BinaryFile' => [
$mimePartWithHeadersForSwaResponse,
$this->getMessageLinesFromMimeMessage(__DIR__.'/../../../../Fixtures/Message/Response/dummyServiceMethodWithOutgoingLargeSwa.response.mimepart.message'),
ParsedPartsGetter::HAS_HTTP_REQUEST_HEADERS,
self::TEST_CASE_SHOULD_NOT_FAIL
],
'ParseSwaResponseWith2FilesAnd1BinaryFileShouldFailWithNoHeaders' => [
new MultiPart(),
$this->getMessageLinesFromMimeMessage(__DIR__.'/../../../../Fixtures/Message/Response/dummyServiceMethodWithOutgoingLargeSwa.response.mimepart.message'),
ParsedPartsGetter::HAS_NO_HTTP_REQUEST_HEADERS,
self::TEST_CASE_SHOULD_FAIL,
'Unable to get Content-Type boundary'
],
'ParseSwaResponseWith2FilesAnd1BinaryFileShouldFailWithWrongHeaders' => [
$mimePartWithWrongHeadersForSwaResponse,
$this->getMessageLinesFromMimeMessage(__DIR__.'/../../../../Fixtures/Message/Response/dummyServiceMethodWithOutgoingLargeSwa.response.mimepart.message'),
ParsedPartsGetter::HAS_HTTP_REQUEST_HEADERS,
self::TEST_CASE_SHOULD_FAIL,
'cannot parse headers before hitting the first boundary'
],
'ParseSwaRequestWith2Files' => [
$mimePartWithHeadersForSwaRequest,
$this->getMessageLinesFromMimeMessage(__DIR__ . '/../../../../Fixtures/Message/Request/dummyServiceMethodWithAttachments.request.mimepart.message'),
ParsedPartsGetter::HAS_HTTP_REQUEST_HEADERS,
self::TEST_CASE_SHOULD_NOT_FAIL
],
'ParseSwaRequestWith2FilesShouldFailWithNoHeaders' => [
new MultiPart(),
$this->getMessageLinesFromMimeMessage(__DIR__ . '/../../../../Fixtures/Message/Request/dummyServiceMethodWithAttachments.request.mimepart.message'),
ParsedPartsGetter::HAS_NO_HTTP_REQUEST_HEADERS,
self::TEST_CASE_SHOULD_FAIL,
'Unable to get Content-Type boundary'
],
];
}
private function getMessageLinesFromMimeMessage($filePath)
{
if (file_exists($filePath) === false) {
self::fail('Please, update tests data provider - file not found: '.$filePath);
}
return explode("\n", file_get_contents($filePath));
}
}

View File

@ -46,7 +46,7 @@ class SoapServerTest extends PHPUnit_Framework_TestCase
$dummyService->getEndpoint(),
'DummyService.dummyServiceMethod',
'text/xml;charset=UTF-8',
file_get_contents(self::FIXTURES_DIR.DIRECTORY_SEPARATOR.'testHandleRequest.message')
file_get_contents(self::FIXTURES_DIR.'/Message/Request/dummyServiceMethod.message.request')
);
$response = $soapServer->handleRequest($request);
@ -74,7 +74,7 @@ class SoapServerTest extends PHPUnit_Framework_TestCase
$dummyService->getEndpoint(),
'DummyService.dummyServiceMethodWithAttachments',
'text/xml;charset=UTF-8',
file_get_contents(self::FIXTURES_DIR.DIRECTORY_SEPARATOR.'testHandleRequestWithSwa.message')
file_get_contents(self::FIXTURES_DIR.'/Message/Request/dummyServiceMethodWithAttachments.request.message')
);
$response = $soapServer->handleRequest($request);
@ -83,7 +83,7 @@ class SoapServerTest extends PHPUnit_Framework_TestCase
self::assertNotContains("\r\n", $response->getContent(), 'Response cannot contain CRLF line endings');
self::assertContains('dummyServiceMethodWithAttachmentsResponse', $response->getContent());
self::assertSame('DummyService.dummyServiceMethodWithAttachments', $response->getAction());
self::assertFalse($response->hasAttachments(), 'Response should contain attachments');
self::assertFalse($response->hasAttachments(), 'Response should not contain attachments');
}
public function testHandleRequestWithSwaResponse()
@ -102,7 +102,7 @@ class SoapServerTest extends PHPUnit_Framework_TestCase
$dummyService->getEndpoint(),
'DummyService.dummyServiceMethodWithAttachments',
'multipart/related; type="text/xml"; start="<rootpart@soapui.org>"; boundary="----=_Part_6_2094841787.1482231370463"',
file_get_contents(self::FIXTURES_DIR.DIRECTORY_SEPARATOR.'testHandleRequestWithSwa.mimepart.message')
file_get_contents(self::FIXTURES_DIR.'/Message/Request/dummyServiceMethodWithAttachments.request.mimepart.message')
);
$response = $soapServer->handleRequest($request);

View File

@ -0,0 +1,266 @@
<?php
namespace BeSimple;
use BeSimple\SoapBundle\Soap\SoapAttachment;
use BeSimple\SoapClient\SoapClientBuilder;
use BeSimple\SoapClient\SoapClientBuilderTest;
use BeSimple\SoapClient\SoapClientOptionsBuilder;
use BeSimple\SoapClient\SoapFaultWithTracingData;
use BeSimple\SoapCommon\ClassMap;
use BeSimple\SoapCommon\SoapOptions\SoapOptions;
use BeSimple\SoapCommon\SoapOptionsBuilder;
use BeSimple\SoapServer\SoapServerBuilder;
use BeSimple\SoapServer\SoapServerOptionsBuilder;
use Fixtures\DummyService;
use Fixtures\DummyServiceMethodWithIncomingLargeSwaRequest;
use Fixtures\DummyServiceMethodWithOutgoingLargeSwaRequest;
use Fixtures\GenerateTestRequest;
use PHPUnit_Framework_TestCase;
use SoapHeader;
class SoapServerAndSoapClientCommunicationTest extends PHPUnit_Framework_TestCase
{
const CACHE_DIR = __DIR__ . '/../../cache';
const FIXTURES_DIR = __DIR__ . '/../Fixtures';
const TEST_HTTP_URL = 'http://localhost:8000/tests';
const TEST_LOCAL_WSDL_UK = SoapClientBuilderTest::TEST_LOCAL_WSDL_UK;
const LARGE_SWA_FILE = self::FIXTURES_DIR.'/large-test-file.docx';
private $localWebServerProcess;
public function setUp()
{
$this->localWebServerProcess = popen('php -S localhost:8000 > /dev/null 2>&1 &', 'r');
}
public function tearDown()
{
pclose($this->localWebServerProcess);
}
public function testHandleRequestWithLargeSwaResponse()
{
$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.dummyServiceMethodWithOutgoingLargeSwa',
'text/xml;charset=UTF-8',
file_get_contents(self::FIXTURES_DIR.'/Message/Request/dummyServiceMethodWithOutgoingLargeSwa.request.message')
);
$response = $soapServer->handleRequest($request);
file_put_contents(self::CACHE_DIR . '/content-type-soap-server-response.xml', $response->getContentType());
file_put_contents(self::CACHE_DIR . '/multipart-message-soap-server-response.xml', $response->getContent());
if ($response->hasAttachments() === true) {
foreach ($response->getAttachments() as $attachment) {
$fileName = preg_replace('/\<|\>/', '', $attachment->getContentId());
file_put_contents(self::CACHE_DIR . DIRECTORY_SEPARATOR . 'attachment-server-response-' . $fileName, $attachment->getContent());
self::assertRegExp('/filename\.(docx|html|txt)/', $fileName);
}
} else {
self::fail('Response should contain attachments');
}
self::assertContains('dummyServiceMethodWithOutgoingLargeSwaResponse', $response->getContent());
self::assertSame('DummyService.dummyServiceMethodWithOutgoingLargeSwa', $response->getAction());
self::assertEquals(
filesize(self::LARGE_SWA_FILE),
filesize(self::CACHE_DIR.'/attachment-server-response-filename.docx'),
'File cannot differ after transport from SoapClient to SoapServer'
);
}
public function testSoapCallSwaWithLargeSwaResponse()
{
$soapClient = $this->getSoapBuilder()->buildWithSoapHeader(
SoapClientOptionsBuilder::createWithEndpointLocation(
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);
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 testSoapCallWithLargeSwaRequest()
{
$soapClient = $this->getSoapBuilder()->buildWithSoapHeader(
SoapClientOptionsBuilder::createWithEndpointLocation(
self::TEST_HTTP_URL.'/SwaReceiverEndpoint.php'
),
SoapOptionsBuilder::createSwaWithClassMap(
self::TEST_HTTP_URL.'/SwaReceiverEndpoint.php?wsdl',
new ClassMap([
'DummyServiceMethodWithIncomingLargeSwaRequest' => DummyServiceMethodWithIncomingLargeSwaRequest::class,
]),
SoapOptions::SOAP_CACHE_TYPE_NONE
),
new SoapHeader('http://schema.testcase', 'SoapHeader', [
'user' => 'admin',
])
);
$request = new DummyServiceMethodWithIncomingLargeSwaRequest();
$request->dummyAttribute = 1;
try {
$soapResponse = $soapClient->soapCall(
'dummyServiceMethodWithIncomingLargeSwa',
[$request],
[
new SoapAttachment('filename.txt', 'text/plain', 'plaintext file'),
new SoapAttachment('filename.html', 'text/html', '<html><body>Hello world</body></html>'),
new SoapAttachment(
'filename.docx',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
file_get_contents(self::LARGE_SWA_FILE)
),
]
);
self::assertContains('dummyServiceMethodWithIncomingLargeSwa', $soapResponse->getRequest()->getContent());
self::assertContains('</dummyServiceReturn>', $soapResponse->getResponseContent());
self::assertTrue($soapResponse->getRequest()->hasAttachments(), 'Response MUST contain attachments');
self::assertFalse($soapResponse->hasAttachments(), 'Response MUST NOT contain attachments');
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 . '/content-type-soap-client-request.xml', $soapResponse->getRequest()->getContentType());
file_put_contents(self::CACHE_DIR.'/multipart-message-soap-client-request.xml', $soapResponse->getRequest()->getContent());
self::assertEquals(
filesize(self::LARGE_SWA_FILE),
filesize(self::CACHE_DIR.'/attachment-client-request-filename.docx'),
'File cannot differ after transport from SoapClient to SoapServer'
);
} catch (SoapFaultWithTracingData $e) {
self::fail(
'Endpoint did not return expected response: '.var_export($e->getSoapResponseTracingData()->getLastResponse(), true)
);
}
}
public function testHandleRequestWithLargeSwaRequest()
{
$previousSoapClientCallContentTypeCacheFile = self::CACHE_DIR.'/content-type-soap-client-request.xml';
$previousSoapClientCallMessageBodyCacheFile = self::CACHE_DIR.'/multipart-message-soap-client-request.xml';
if (file_exists($previousSoapClientCallContentTypeCacheFile) === false || file_exists($previousSoapClientCallMessageBodyCacheFile) === false) {
self::fail('Cannot load data from cache: run testSoapCallWithLargeSwaRequest to get the data.');
}
$previousSoapClientCallContentType = file_get_contents($previousSoapClientCallContentTypeCacheFile);
$previousSoapClientCallMessageBody = file_get_contents($previousSoapClientCallMessageBodyCacheFile);
$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.dummyServiceMethodWithIncomingLargeSwa',
$previousSoapClientCallContentType,
$previousSoapClientCallMessageBody
);
$response = $soapServer->handleRequest($request);
self::assertContains('dummyServiceMethodWithIncomingLargeSwaResponse', $response->getContent());
self::assertSame('DummyService.dummyServiceMethodWithIncomingLargeSwa', $response->getAction());
self::assertEquals(
filesize(self::LARGE_SWA_FILE),
filesize(self::CACHE_DIR.'/attachment-server-request-filename.docx'),
'File cannot differ after transport from SoapClient to SoapServer'
);
}
public function testHandleRequestWithLargeSwaRequestAndMixedCrLf()
{
$soapClientCallContentType = file_get_contents(self::FIXTURES_DIR.'/Message/Request/dummyServiceMethodWithIncomingLargeSwaAndMixedCrLf.contenttypeheaders');
$soapClientCallMessageBody = file_get_contents(self::FIXTURES_DIR.'/Message/Request/dummyServiceMethodWithIncomingLargeSwaAndMixedCrLf.request.mimepart.message');
$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.dummyServiceMethodWithIncomingLargeSwa',
$soapClientCallContentType,
$soapClientCallMessageBody
);
$response = $soapServer->handleRequest($request);
self::assertContains('dummyServiceMethodWithIncomingLargeSwaResponse', $response->getContent());
self::assertSame('DummyService.dummyServiceMethodWithIncomingLargeSwa', $response->getAction());
self::assertEquals(
filesize(self::LARGE_SWA_FILE),
filesize(self::CACHE_DIR.'/attachment-server-request-oldfilename.docx'),
'File cannot differ after transport from SoapClient to SoapServer'
);
}
private function getSoapBuilder()
{
return new SoapClientBuilder();
}
public function getSoapServerBuilder()
{
return new SoapServerBuilder();
}
}