From e0049c906d6ca7c15c15a659206e3815dbd592aa Mon Sep 17 00:00:00 2001 From: Andreas Schamberger Date: Mon, 31 Oct 2011 11:55:14 +0100 Subject: [PATCH] unit tests --- .gitignore | 4 + src/BeSimple/SoapCommon/Mime/MultiPart.php | 15 +- src/BeSimple/SoapCommon/Mime/PartHeader.php | 54 +++++-- .../SoapCommon/Fixtures/MimePartHeader.php | 9 ++ .../Fixtures/SwA-response-amazon.txt | 29 ++++ .../SoapCommon/Fixtures/SwA-response-axis.txt | 13 ++ .../SoapCommon/Fixtures/WS-I-MTOM-request.txt | 22 +++ .../Fixtures/WS-I-MTOM-request_noheader.txt | 16 ++ .../Fixtures/WS-I-MTOM-response.txt | 17 ++ .../Tests/SoapCommon/Mime/MultiPartTest.php | 144 +++++++++++++++++ .../Tests/SoapCommon/Mime/ParserTest.php | 153 ++++++++++++++++++ .../Tests/SoapCommon/Mime/PartHeaderTest.php | 57 +++++++ .../Tests/SoapCommon/Mime/PartTest.php | 62 +++++++ 13 files changed, 569 insertions(+), 26 deletions(-) create mode 100644 tests/BeSimple/Tests/SoapCommon/Fixtures/MimePartHeader.php create mode 100644 tests/BeSimple/Tests/SoapCommon/Fixtures/SwA-response-amazon.txt create mode 100644 tests/BeSimple/Tests/SoapCommon/Fixtures/SwA-response-axis.txt create mode 100644 tests/BeSimple/Tests/SoapCommon/Fixtures/WS-I-MTOM-request.txt create mode 100644 tests/BeSimple/Tests/SoapCommon/Fixtures/WS-I-MTOM-request_noheader.txt create mode 100644 tests/BeSimple/Tests/SoapCommon/Fixtures/WS-I-MTOM-response.txt create mode 100644 tests/BeSimple/Tests/SoapCommon/Mime/MultiPartTest.php create mode 100644 tests/BeSimple/Tests/SoapCommon/Mime/ParserTest.php create mode 100644 tests/BeSimple/Tests/SoapCommon/Mime/PartHeaderTest.php create mode 100644 tests/BeSimple/Tests/SoapCommon/Mime/PartTest.php diff --git a/.gitignore b/.gitignore index 22d0d82..f8d178e 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,5 @@ vendor +/phpunit.xml +.buildpath +.project +.settings \ No newline at end of file diff --git a/src/BeSimple/SoapCommon/Mime/MultiPart.php b/src/BeSimple/SoapCommon/Mime/MultiPart.php index 810b655..1a3b7e1 100644 --- a/src/BeSimple/SoapCommon/Mime/MultiPart.php +++ b/src/BeSimple/SoapCommon/Mime/MultiPart.php @@ -80,6 +80,7 @@ class MultiPart extends PartHeader /** * Get string array with MIME headers for usage in HTTP header (with CURL). + * Only 'Content-Type' and 'Content-Description' headers are returned. * * @return arrray(string) */ @@ -92,19 +93,7 @@ class MultiPart extends PartHeader $headers = array(); foreach ($this->headers as $fieldName => $value) { if (in_array($fieldName, $allowed)) { - $fieldValue = ''; - if (is_array($value)) { - if (isset($value['@'])) { - $fieldValue .= $value['@']; - } - foreach ($value as $subName => $subValue) { - if ($subName != '@') { - $fieldValue .= '; ' . $subName . '="' . $subValue . '"'; - } - } - } else { - $fieldValue .= $value; - } + $fieldValue = $this->generateHeaderFieldValue($value); // for http only ISO-8859-1 $headers[] = $fieldName . ': '. iconv('utf-8', 'ISO-8859-1//TRANSLIT', $fieldValue); } diff --git a/src/BeSimple/SoapCommon/Mime/PartHeader.php b/src/BeSimple/SoapCommon/Mime/PartHeader.php index fea2694..b6458cd 100644 --- a/src/BeSimple/SoapCommon/Mime/PartHeader.php +++ b/src/BeSimple/SoapCommon/Mime/PartHeader.php @@ -92,23 +92,51 @@ abstract class PartHeader ); $headers = ''; foreach ($this->headers as $fieldName => $value) { - $fieldValue = ''; - if (is_array($value)) { - if (isset($value['@'])) { - $fieldValue .= $value['@']; - } - foreach ($value as $subName => $subValue) { - if ($subName != '@') { - $fieldValue .= '; ' . $subName . '=' . $subValue; - } - } - } else { - $fieldValue .= $value; - } + $fieldValue = $this->generateHeaderFieldValue($value); // do not use proper encoding as Apache Axis does not understand this // $headers .= iconv_mime_encode($field_name, $field_value, $preferences) . "\r\n"; $headers .= $fieldName . ': ' . $fieldValue . "\r\n"; } return $headers; } + + /** + * Generates a header field value from the given value paramater. + * + * @param array(string=>string)|string $value + * @return string + */ + protected function generateHeaderFieldValue($value) + { + $fieldValue = ''; + if (is_array($value)) { + if (isset($value['@'])) { + $fieldValue .= $value['@']; + } + foreach ($value as $subName => $subValue) { + if ($subName != '@') { + $fieldValue .= '; ' . $subName . '=' . $this->quoteValueString($subValue); + } + } + } else { + $fieldValue .= $value; + } + return $fieldValue; + } + + /** + * Quote string with '"' if it contains one of the special characters: + * "(" / ")" / "<" / ">" / "@" / "," / ";" / ":" / "\" / <"> / "/" / "[" / "]" / "?" / "=" + * + * @param string $string + * @return string + */ + private function quoteValueString($string) + { + if (preg_match('~[()<>@,;:\\"/\[\]?=]~', $string)) { + return '"' . $string . '"'; + } else { + return $string; + } + } } \ No newline at end of file diff --git a/tests/BeSimple/Tests/SoapCommon/Fixtures/MimePartHeader.php b/tests/BeSimple/Tests/SoapCommon/Fixtures/MimePartHeader.php new file mode 100644 index 0000000..2ecd558 --- /dev/null +++ b/tests/BeSimple/Tests/SoapCommon/Fixtures/MimePartHeader.php @@ -0,0 +1,9 @@ + +--xxx-MIME-Boundary-xxx-0xa36cb38-0a36cb38-xxx-END-xxx +Content-ID: <0x9d6ad00-0xa19ef48-0x9de7500-0xa4fae78-0xa382698> +Content-Type: application/binary + + + + +--xxx-MIME-Boundary-xxx-0xa36cb38-0a36cb38-xxx-END-xxx-- diff --git a/tests/BeSimple/Tests/SoapCommon/Fixtures/SwA-response-axis.txt b/tests/BeSimple/Tests/SoapCommon/Fixtures/SwA-response-axis.txt new file mode 100644 index 0000000..58cda6f --- /dev/null +++ b/tests/BeSimple/Tests/SoapCommon/Fixtures/SwA-response-axis.txt @@ -0,0 +1,13 @@ +HTTP/1.1 200 OK +Date: Sat, 11 Sep 2010 12:52:57 GMT +Server: Simple-Server/1.1 +Transfer-Encoding: chunked +Content-Type: multipart/related; boundary=MIMEBoundaryurn_uuid_2DB7ABF3DC5BED7FA51284209577582; type="application/soap+xml"; start="<0.urn:uuid:2DB7ABF3DC5BED7FA51284209577583@apache.org>"; action="urn:getVersionResponse" + +--MIMEBoundaryurn_uuid_2DB7ABF3DC5BED7FA51284209577582 +Content-Type: application/soap+xml; charset=utf-8 +Content-Transfer-Encoding: 8bit +Content-ID: <0.urn:uuid:2DB7ABF3DC5BED7FA51284209577583@apache.org> + +urn:getVersionResponseuuid:665aab53-4cef-4435-8934-4c10ed24fd42Hi - the Axis2 version is 1.5.1 +--MIMEBoundaryurn_uuid_2DB7ABF3DC5BED7FA51284209577582-- \ No newline at end of file diff --git a/tests/BeSimple/Tests/SoapCommon/Fixtures/WS-I-MTOM-request.txt b/tests/BeSimple/Tests/SoapCommon/Fixtures/WS-I-MTOM-request.txt new file mode 100644 index 0000000..efe9192 --- /dev/null +++ b/tests/BeSimple/Tests/SoapCommon/Fixtures/WS-I-MTOM-request.txt @@ -0,0 +1,22 @@ +POST http://131.107.72.15/Mtom/svc/service.svc/Soap12MtomUTF8 HTTP/1.1 +Content-Type: multipart/related; type="application/xop+xml";start="";boundary="uuid:0ca0e16e-feb1-426c-97d8-c4508ada5e82+id=7";start-info="application/soap+xml" +Host: 131.107.72.15 +Content-Length: 1941 +Expect: 100-continue +HTTP/1.1 100 Continue + +--uuid:0ca0e16e-feb1-426c-97d8-c4508ada5e82+id=7 +Content-ID: +Content-Transfer-Encoding: 8bit +Content-Type: application/xop+xml;charset=utf-8;type="application/soap+xml" + +http://xmlsoap.org/echoBinaryAsStringurn:uuid:1bf061d6-d532-4b0c-930b-8b8202c38b8ahttp://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymoushttp://131.107.72.15/Mtom/svc/service.svc/Soap12MtomUTF8 + +--uuid:0ca0e16e-feb1-426c-97d8-c4508ada5e82+id=7 +Content-ID: +Content-Transfer-Encoding: binary +Content-Type: application/octet-stream + +H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! + +--uuid:0ca0e16e-feb1-426c-97d8-c4508ada5e82+id=7-- diff --git a/tests/BeSimple/Tests/SoapCommon/Fixtures/WS-I-MTOM-request_noheader.txt b/tests/BeSimple/Tests/SoapCommon/Fixtures/WS-I-MTOM-request_noheader.txt new file mode 100644 index 0000000..f6d9942 --- /dev/null +++ b/tests/BeSimple/Tests/SoapCommon/Fixtures/WS-I-MTOM-request_noheader.txt @@ -0,0 +1,16 @@ + +--uuid:0ca0e16e-feb1-426c-97d8-c4508ada5e82+id=7 +Content-ID: +Content-Transfer-Encoding: 8bit +Content-Type: application/xop+xml;charset=utf-8;type="application/soap+xml" + +http://xmlsoap.org/echoBinaryAsStringurn:uuid:1bf061d6-d532-4b0c-930b-8b8202c38b8ahttp://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymoushttp://131.107.72.15/Mtom/svc/service.svc/Soap12MtomUTF8 + +--uuid:0ca0e16e-feb1-426c-97d8-c4508ada5e82+id=7 +Content-ID: +Content-Transfer-Encoding: binary +Content-Type: application/octet-stream + +H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! H e l l o W o r l d ! + +--uuid:0ca0e16e-feb1-426c-97d8-c4508ada5e82+id=7-- diff --git a/tests/BeSimple/Tests/SoapCommon/Fixtures/WS-I-MTOM-response.txt b/tests/BeSimple/Tests/SoapCommon/Fixtures/WS-I-MTOM-response.txt new file mode 100644 index 0000000..7a76cb4 --- /dev/null +++ b/tests/BeSimple/Tests/SoapCommon/Fixtures/WS-I-MTOM-response.txt @@ -0,0 +1,17 @@ +HTTP/1.1 200 OK +Proxy-Connection: Keep-Alive +Connection: Keep-Alive +Content-Length: 1166 +Via: 1.1 RED-PRXY-03 +Date: Fri, 09 Sep 2005 06:57:22 GMT +Content-Type: multipart/related; type="application/xop+xml";start="";boundary="uuid:b71dc628-ec8f-4422-8a4a-992f041cb94c+id=46";start-info="application/soap+xml" + + + +--uuid:b71dc628-ec8f-4422-8a4a-992f041cb94c+id=46 +Content-ID: +Content-Transfer-Encoding: 8bit +Content-Type: application/xop+xml;charset=utf-8;type="application/soap+xml" + +*urn:uuid:1bf061d6-d532-4b0c-930b-8b8202c38b8ahttp://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymousHello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World! +--uuid:b71dc628-ec8f-4422-8a4a-992f041cb94c+id=46-- diff --git a/tests/BeSimple/Tests/SoapCommon/Mime/MultiPartTest.php b/tests/BeSimple/Tests/SoapCommon/Mime/MultiPartTest.php new file mode 100644 index 0000000..1d1ab16 --- /dev/null +++ b/tests/BeSimple/Tests/SoapCommon/Mime/MultiPartTest.php @@ -0,0 +1,144 @@ + + * (c) Francis Besset + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace BeSimple\Tests\SoapCommon\Soap; + +use BeSimple\SoapCommon\Mime\MultiPart; +use BeSimple\SoapCommon\Mime\Part; +use BeSimple\SoapCommon\Mime\PartHeader; + +class MultiPartTest extends \PHPUnit_Framework_TestCase +{ + public function testConstructor() + { + $mp = new MultiPart(); + + $this->assertEquals('1.0', $mp->getHeader('MIME-Version')); + $this->assertEquals('multipart/related', $mp->getHeader('Content-Type')); + $this->assertEquals('text/xml', $mp->getHeader('Content-Type', 'type')); + $this->assertEquals('utf-8', $mp->getHeader('Content-Type', 'charset')); + $this->assertRegExp('~urn:uuid:.*~', $mp->getHeader('Content-Type', 'boundary')); + } + + public function testGetMimeMessage() + { + $mp = new MultiPart(); + + /* + string(51) " + --urn:uuid:a81ca327-591e-4656-91a1-8f177ada95b0--" + */ + $this->assertEquals(51, strlen($mp->getMimeMessage())); + + $p = new Part('test'); + $mp->addPart($p, true); + + /* + string(259) " + --urn:uuid:a81ca327-591e-4656-91a1-8f177ada95b0 + Content-Type: application/octet-stream; charset=utf-8 + Content-Transfer-Encoding: binary + Content-ID: + + test + --urn:uuid:a81ca327-591e-4656-91a1-8f177ada95b0--" + */ + $this->assertEquals(259, strlen($mp->getMimeMessage())); + } + + public function testGetMimeMessageWithHeaders() + { + $mp = new MultiPart(); + + /* + string(189) "MIME-Version: 1.0 + Content-Type: multipart/related; type="text/xml"; charset=utf-8; boundary="urn:uuid:231833e2-a23b-410a-862e-250524fc38f6" + + --urn:uuid:231833e2-a23b-410a-862e-250524fc38f6--" + */ + $this->assertEquals(193, strlen($mp->getMimeMessage(true))); + + $p = new Part('test'); + $mp->addPart($p, true); + + /* + string(452) "MIME-Version: 1.0 + Content-Type: multipart/related; type="text/xml"; charset=utf-8; boundary="urn:uuid:231833e2-a23b-410a-862e-250524fc38f6"; start="" + + --urn:uuid:231833e2-a23b-410a-862e-250524fc38f6 + Content-Type: application/octet-stream; charset=utf-8 + Content-Transfer-Encoding: binary + Content-ID: + + test + --urn:uuid:231833e2-a23b-410a-862e-250524fc38f6--" + */ + $this->assertEquals(458, strlen($mp->getMimeMessage(true))); + } + + public function testGetHeadersForHttp() + { + $mp = new MultiPart(); + + $result = array( + 'Content-Type: multipart/related; type="text/xml"; charset=utf-8; boundary="' . $mp->getHeader('Content-Type', 'boundary') . '"', + ); + $this->assertEquals($result, $mp->getHeadersForHttp()); + + $result = array( + 'Content-Type: multipart/related; type="text/xml"; charset=utf-8; boundary="' . $mp->getHeader('Content-Type', 'boundary') . '"', + 'Content-Description: test', + ); + $mp->setHeader('Content-Description', 'test'); + $this->assertEquals($result, $mp->getHeadersForHttp()); + } + + public function testAddGetPart() + { + $mp = new MultiPart(); + + $p = new Part('test'); + $p->setHeader('Content-ID', 'mycontentid'); + $mp->addPart($p); + $this->assertEquals($p, $mp->getPart('mycontentid')); + } + + public function testAddGetPartWithMain() + { + $mp = new MultiPart(); + + $p = new Part('test'); + $mp->addPart($p, true); + $this->assertEquals($p, $mp->getPart()); + } + + public function testGetParts() + { + $mp = new MultiPart(); + + $p1 = new Part('test'); + $mp->addPart($p1, true); + $p2 = new Part('test'); + $mp->addPart($p2); + + $withoutMain = array( + trim($p2->getHeader('Content-ID'),'<>') => $p2, + ); + $this->assertEquals($withoutMain, $mp->getParts()); + + $withMain = array( + trim($p1->getHeader('Content-ID'),'<>') => $p1, + trim($p2->getHeader('Content-ID'),'<>') => $p2, + ); + $this->assertEquals($withMain, $mp->getParts(true)); + } +} \ No newline at end of file diff --git a/tests/BeSimple/Tests/SoapCommon/Mime/ParserTest.php b/tests/BeSimple/Tests/SoapCommon/Mime/ParserTest.php new file mode 100644 index 0000000..8efa1f9 --- /dev/null +++ b/tests/BeSimple/Tests/SoapCommon/Mime/ParserTest.php @@ -0,0 +1,153 @@ + + * (c) Francis Besset + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace BeSimple\Tests\SoapCommon\Soap; + +use BeSimple\SoapCommon\Mime\MultiPart; +use BeSimple\SoapCommon\Mime\Parser; +use BeSimple\SoapCommon\Mime\Part; +use BeSimple\SoapCommon\Mime\PartHeader; + +class ParserTest extends \PHPUnit_Framework_TestCase +{ + public function testParserRequestWsi() + { + $filename = dirname(__DIR__).DIRECTORY_SEPARATOR.'Fixtures/WS-I-MTOM-request.txt'; + $mimeMessage = file_get_contents($filename); + + $mp = Parser::parseMimeMessage($mimeMessage); + $this->assertsForWsiMtomRequest($mp); + } + + public function testParserResponseAmazon() + { + $filename = dirname(__DIR__).DIRECTORY_SEPARATOR.'Fixtures/SwA-response-amazon.txt'; + $mimeMessage = file_get_contents($filename); + + $mp = Parser::parseMimeMessage($mimeMessage); + $this->assertEquals('Fri, 12 Feb 2010 15:46:00 GMT', $mp->getHeader('Date')); + $this->assertEquals('Server', $mp->getHeader('Server')); + $this->assertEquals('1.0', $mp->getHeader('MIME-Version')); + $this->assertEquals('close', $mp->getHeader('Cneonction')); + $this->assertEquals('chunked', $mp->getHeader('Transfer-Encoding')); + $this->assertEquals('multipart/related', $mp->getHeader('Content-Type')); + $this->assertEquals('text/xml', $mp->getHeader('Content-Type', 'type')); + $this->assertEquals('utf-8', $mp->getHeader('Content-Type', 'charset')); + $this->assertEquals('xxx-MIME-Boundary-xxx-0xa36cb38-0a36cb38-xxx-END-xxx', $mp->getHeader('Content-Type', 'boundary')); + + $p1 = $mp->getPart(); + $this->assertEquals('text/xml', $p1->getHeader('Content-Type')); + $this->assertEquals('UTF-8', $p1->getHeader('Content-Type', 'charset')); + $this->assertEquals(389, strlen($p1->getContent())); + + $p2 = $mp->getPart('0x9d6ad00-0xa19ef48-0x9de7500-0xa4fae78-0xa382698'); + $this->assertEquals('binary', $p2->getHeader('Content-Transfer-Encoding')); + $this->assertEquals('application/binary', $p2->getHeader('Content-Type')); + $this->assertEquals(81, strlen($p2->getContent())); + } + + public function testParserResponseAxis() + { + $filename = dirname(__DIR__).DIRECTORY_SEPARATOR.'Fixtures/SwA-response-axis.txt'; + $mimeMessage = file_get_contents($filename); + + $mp = Parser::parseMimeMessage($mimeMessage); + $this->assertEquals('Sat, 11 Sep 2010 12:52:57 GMT', $mp->getHeader('Date')); + $this->assertEquals('Simple-Server/1.1', $mp->getHeader('Server')); + $this->assertEquals('1.0', $mp->getHeader('MIME-Version')); + $this->assertEquals('chunked', $mp->getHeader('Transfer-Encoding')); + $this->assertEquals('multipart/related', $mp->getHeader('Content-Type')); + $this->assertEquals('application/soap+xml', $mp->getHeader('Content-Type', 'type')); + $this->assertEquals('utf-8', $mp->getHeader('Content-Type', 'charset')); + $this->assertEquals('<0.urn:uuid:2DB7ABF3DC5BED7FA51284209577583@apache.org>', $mp->getHeader('Content-Type', 'start')); + $this->assertEquals('urn:getVersionResponse', $mp->getHeader('Content-Type', 'action')); + $this->assertEquals('MIMEBoundaryurn_uuid_2DB7ABF3DC5BED7FA51284209577582', $mp->getHeader('Content-Type', 'boundary')); + + $p1 = $mp->getPart('0.urn:uuid:2DB7ABF3DC5BED7FA51284209577583@apache.org'); + $this->assertEquals('8bit', $p1->getHeader('Content-Transfer-Encoding')); + $this->assertEquals('application/soap+xml', $p1->getHeader('Content-Type')); + $this->assertEquals('utf-8', $p1->getHeader('Content-Type', 'charset')); + $this->assertEquals(499, strlen($p1->getContent())); + } + + public function testParserResponseWsi() + { + $filename = dirname(__DIR__).DIRECTORY_SEPARATOR.'Fixtures/WS-I-MTOM-response.txt'; + $mimeMessage = file_get_contents($filename); + + $mp = Parser::parseMimeMessage($mimeMessage); + $this->assertEquals('Keep-Alive', $mp->getHeader('Proxy-Connection')); + $this->assertEquals('Keep-Alive', $mp->getHeader('Connection')); + $this->assertEquals('1166', $mp->getHeader('Content-Length')); + $this->assertEquals('1.1 RED-PRXY-03', $mp->getHeader('Via')); + $this->assertEquals('Fri, 09 Sep 2005 06:57:22 GMT', $mp->getHeader('Date')); + $this->assertEquals('multipart/related', $mp->getHeader('Content-Type')); + $this->assertEquals('application/xop+xml', $mp->getHeader('Content-Type', 'type')); + $this->assertEquals('utf-8', $mp->getHeader('Content-Type', 'charset')); + $this->assertEquals('', $mp->getHeader('Content-Type', 'start')); + $this->assertEquals('application/soap+xml', $mp->getHeader('Content-Type', 'start-info')); + $this->assertEquals('uuid:b71dc628-ec8f-4422-8a4a-992f041cb94c+id=46', $mp->getHeader('Content-Type', 'boundary')); + + $p1 = $mp->getPart('http://tempuri.org/0'); + $this->assertEquals('8bit', $p1->getHeader('Content-Transfer-Encoding')); + $this->assertEquals('application/xop+xml', $p1->getHeader('Content-Type')); + $this->assertEquals('utf-8', $p1->getHeader('Content-Type', 'charset')); + $this->assertEquals('application/soap+xml', $p1->getHeader('Content-Type', 'type')); + $this->assertEquals(910, strlen($p1->getContent())); + } + + public function testParserWithHeaderArray() + { + $filename = dirname(__DIR__).DIRECTORY_SEPARATOR.'Fixtures/WS-I-MTOM-request_noheader.txt'; + $mimeMessage = file_get_contents($filename); + + $headers = array( + 'Content-Type' => 'multipart/related; type="application/xop+xml";start="";boundary="uuid:0ca0e16e-feb1-426c-97d8-c4508ada5e82+id=7";start-info="application/soap+xml"', + 'Content-Length' => 1941, + 'Host' => '131.107.72.15', + 'Expect' => '100-continue', + ); + + $mp = Parser::parseMimeMessage($mimeMessage, $headers); + $this->assertsForWsiMtomRequest($mp); + } + + /* + * used in: + * - testParserWithHeaderArray + * - testParserRequestWsi + */ + private function assertsForWsiMtomRequest(MultiPart $mp) + { + $this->assertEquals('multipart/related', $mp->getHeader('Content-Type')); + $this->assertEquals('application/xop+xml', $mp->getHeader('Content-Type', 'type')); + $this->assertEquals('utf-8', $mp->getHeader('Content-Type', 'charset')); + $this->assertEquals('', $mp->getHeader('Content-Type', 'start')); + $this->assertEquals('application/soap+xml', $mp->getHeader('Content-Type', 'start-info')); + $this->assertEquals('uuid:0ca0e16e-feb1-426c-97d8-c4508ada5e82+id=7', $mp->getHeader('Content-Type', 'boundary')); + $this->assertEquals('1941', $mp->getHeader('Content-Length')); + $this->assertEquals('131.107.72.15', $mp->getHeader('Host')); + $this->assertEquals('100-continue', $mp->getHeader('Expect')); + + $p1 = $mp->getPart('http://tempuri.org/0'); + $this->assertEquals('8bit', $p1->getHeader('Content-Transfer-Encoding')); + $this->assertEquals('application/xop+xml', $p1->getHeader('Content-Type')); + $this->assertEquals('utf-8', $p1->getHeader('Content-Type', 'charset')); + $this->assertEquals('application/soap+xml', $p1->getHeader('Content-Type', 'type')); + $this->assertEquals(737, strlen($p1->getContent())); + + $p2 = $mp->getPart('http://tempuri.org/1/632618206527087310'); + $this->assertEquals('binary', $p2->getHeader('Content-Transfer-Encoding')); + $this->assertEquals('application/octet-stream', $p2->getHeader('Content-Type')); + $this->assertEquals(769, strlen($p2->getContent())); + } +} diff --git a/tests/BeSimple/Tests/SoapCommon/Mime/PartHeaderTest.php b/tests/BeSimple/Tests/SoapCommon/Mime/PartHeaderTest.php new file mode 100644 index 0000000..ef9f08d --- /dev/null +++ b/tests/BeSimple/Tests/SoapCommon/Mime/PartHeaderTest.php @@ -0,0 +1,57 @@ + + * (c) Francis Besset + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace BeSimple\Tests\SoapCommon\Soap; + +use BeSimple\SoapCommon\Mime\PartHeader; +use BeSimple\Tests\SoapCommon\Fixtures\MimePartHeader; + +class PartHeaderTest extends \PHPUnit_Framework_TestCase +{ + public function testSetGetHeader() + { + $ph = new MimePartHeader(); + $ph->setHeader('Content-Type', 'text/xml'); + $this->assertEquals('text/xml', $ph->getHeader('Content-Type')); + } + + public function testSetGetHeaderSubvalue() + { + $ph = new MimePartHeader(); + $ph->setHeader('Content-Type', 'utf-8', 'charset'); + $this->assertEquals(null, $ph->getHeader('Content-Type', 'charset')); + + $ph->setHeader('Content-Type', 'text/xml'); + $ph->setHeader('Content-Type', 'charset', 'utf-8'); + $this->assertEquals('utf-8', $ph->getHeader('Content-Type', 'charset')); + } + + public function testGenerateHeaders() + { + $ph = new MimePartHeader(); + + $class = new \ReflectionClass($ph); + $method = $class->getMethod('generateHeaders'); + $method->setAccessible(true); + + $this->assertEquals('', $method->invoke($ph)); + + $ph->setHeader('Content-Type', 'text/xml'); + $this->assertEquals("Content-Type: text/xml\r\n", $method->invoke($ph)); + + $ph->setHeader('Content-Type', 'charset', 'utf-8'); + $this->assertEquals("Content-Type: text/xml; charset=utf-8\r\n", $method->invoke($ph)); + + $ph->setHeader('Content-Type', 'type', 'text/xml'); + $this->assertEquals("Content-Type: text/xml; charset=utf-8; type=\"text/xml\"\r\n", $method->invoke($ph)); + } +} \ No newline at end of file diff --git a/tests/BeSimple/Tests/SoapCommon/Mime/PartTest.php b/tests/BeSimple/Tests/SoapCommon/Mime/PartTest.php new file mode 100644 index 0000000..031ba5b --- /dev/null +++ b/tests/BeSimple/Tests/SoapCommon/Mime/PartTest.php @@ -0,0 +1,62 @@ + + * (c) Francis Besset + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace BeSimple\Tests\SoapCommon\Soap; + +use BeSimple\SoapCommon\Mime\Part; +use BeSimple\SoapCommon\Mime\PartHeader; + +class PartTest extends \PHPUnit_Framework_TestCase +{ + public function testConstructor() + { + $p = new Part('', 'text/xml', 'utf-8', Part::ENCODING_BINARY, 'urn:myuniqueresource'); + + $this->assertEquals('', $p->getContent()); + $this->assertEquals('text/xml', $p->getHeader('Content-Type')); + $this->assertEquals('utf-8', $p->getHeader('Content-Type', 'charset')); + $this->assertEquals(Part::ENCODING_BINARY, $p->getHeader('Content-Transfer-Encoding')); + $this->assertEquals('', $p->getHeader('Content-ID')); + } + + public function testDefaultConstructor() + { + $p = new Part(); + + $this->assertNull($p->getContent()); + $this->assertEquals('application/octet-stream', $p->getHeader('Content-Type')); + $this->assertEquals('utf-8', $p->getHeader('Content-Type', 'charset')); + $this->assertEquals(Part::ENCODING_BINARY, $p->getHeader('Content-Transfer-Encoding')); + $this->assertRegExp('~~', $p->getHeader('Content-ID')); + } + + public function testSetContent() + { + $p = new Part(); + + $p->setContent(''); + $this->assertEquals('', $p->getContent()); + } + + public function testGetMessagePart() + { + $p = new Part('', 'text/xml', 'utf-8', Part::ENCODING_BINARY, 'urn:myuniqueresource'); + + $messagePart = "Content-Type: text/xml; charset=utf-8\r\n" . + "Content-Transfer-Encoding: binary\r\n" . + "Content-ID: \r\n" . + "\r\n". + ""; + + $this->assertEquals($messagePart, $p->getMessagePart()); + } +} \ No newline at end of file