123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427 |
- <?php
-
- require_once 'Swift/Tests/SwiftUnitTestCase.php';
- require_once 'Swift/Mime/Headers/ParameterizedHeader.php';
- require_once 'Swift/Mime/HeaderEncoder.php';
- require_once 'Swift/Encoder.php';
- require_once 'Swift/Mime/Grammar.php';
-
- class Swift_Mime_Headers_ParameterizedHeaderTest
- extends Swift_Tests_SwiftUnitTestCase
- {
-
- private $_charset = 'utf-8';
- private $_lang = 'en-us';
-
- public function testTypeIsParameterizedHeader()
- {
- $header = $this->_getHeader('Content-Type',
- $this->_getHeaderEncoder('Q', true), $this->_getParameterEncoder(true)
- );
- $this->assertEqual(Swift_Mime_Header::TYPE_PARAMETERIZED, $header->getFieldType());
- }
-
- public function testValueIsReturnedVerbatim()
- {
- $header = $this->_getHeader('Content-Type',
- $this->_getHeaderEncoder('Q', true), $this->_getParameterEncoder(true)
- );
- $header->setValue('text/plain');
- $this->assertEqual('text/plain', $header->getValue());
- }
-
- public function testParametersAreAppended()
- {
-
-
-
- $header = $this->_getHeader('Content-Type',
- $this->_getHeaderEncoder('Q', true), $this->_getParameterEncoder(true)
- );
- $header->setValue('text/plain');
- $header->setParameters(array('charset' => 'utf-8'));
- $this->assertEqual('text/plain; charset=utf-8', $header->getFieldBody());
- }
-
- public function testSpaceInParamResultsInQuotedString()
- {
- $header = $this->_getHeader('Content-Disposition',
- $this->_getHeaderEncoder('Q', true), $this->_getParameterEncoder(true)
- );
- $header->setValue('attachment');
- $header->setParameters(array('filename' => 'my file.txt'));
- $this->assertEqual('attachment; filename="my file.txt"',
- $header->getFieldBody()
- );
- }
-
- public function testLongParamsAreBrokenIntoMultipleAttributeStrings()
- {
-
-
-
- $value = str_repeat('a', 180);
-
- $encoder = $this->_getParameterEncoder();
- $this->_checking(Expectations::create()
- -> one($encoder)->encodeString($value, any(), 63)
- -> returns(str_repeat('a', 63) . "\r\n" .
- str_repeat('a', 63) . "\r\n" . str_repeat('a', 54))
-
- -> ignoring($encoder)
- );
-
- $header = $this->_getHeader('Content-Disposition',
- $this->_getHeaderEncoder('Q', true), $encoder
- );
- $header->setValue('attachment');
- $header->setParameters(array('filename' => $value));
- $header->setMaxLineLength(78);
- $this->assertEqual(
- 'attachment; ' .
- 'filename*0*=utf-8\'\'' . str_repeat('a', 63) . ";\r\n " .
- 'filename*1*=' . str_repeat('a', 63) . ";\r\n " .
- 'filename*2*=' . str_repeat('a', 54),
- $header->getFieldBody()
- );
- }
-
- public function testEncodedParamDataIncludesCharsetAndLanguage()
- {
-
-
-
- $value = str_repeat('a', 20) . pack('C', 0x8F) . str_repeat('a', 10);
-
- $encoder = $this->_getParameterEncoder();
- $this->_checking(Expectations::create()
- -> one($encoder)->encodeString($value, 12, 62)
- -> returns(str_repeat('a', 20) . '%8F' . str_repeat('a', 10))
-
- -> ignoring($encoder)
- );
-
- $header = $this->_getHeader('Content-Disposition',
- $this->_getHeaderEncoder('Q', true), $encoder
- );
- $header->setValue('attachment');
- $header->setParameters(array('filename' => $value));
- $header->setMaxLineLength(78);
- $header->setLanguage($this->_lang);
- $this->assertEqual(
- 'attachment; filename*=' . $this->_charset . "'" . $this->_lang . "'" .
- str_repeat('a', 20) . '%8F' . str_repeat('a', 10),
- $header->getFieldBody()
- );
- }
-
- public function testMultipleEncodedParamLinesAreFormattedCorrectly()
- {
-
-
-
- $value = str_repeat('a', 20) . pack('C', 0x8F) . str_repeat('a', 60);
-
- $encoder = $this->_getParameterEncoder();
- $this->_checking(Expectations::create()
- -> one($encoder)->encodeString($value, 12, 62)
- -> returns(str_repeat('a', 20) . '%8F' . str_repeat('a', 28) . "\r\n" .
- str_repeat('a', 32))
-
- -> ignoring($encoder)
- );
-
- $header = $this->_getHeader('Content-Disposition',
- $this->_getHeaderEncoder('Q', true), $encoder
- );
- $header->setValue('attachment');
- $header->setParameters(array('filename' => $value));
- $header->setMaxLineLength(78);
- $header->setLanguage($this->_lang);
- $this->assertEqual(
- 'attachment; filename*0*=' . $this->_charset . "'" . $this->_lang . "'" .
- str_repeat('a', 20) . '%8F' . str_repeat('a', 28) . ";\r\n " .
- 'filename*1*=' . str_repeat('a', 32),
- $header->getFieldBody()
- );
- }
-
- public function testToString()
- {
- $header = $this->_getHeader('Content-Type',
- $this->_getHeaderEncoder('Q', true), $this->_getParameterEncoder(true)
- );
- $header->setValue('text/html');
- $header->setParameters(array('charset' => 'utf-8'));
- $this->assertEqual('Content-Type: text/html; charset=utf-8' . "\r\n",
- $header->toString()
- );
- }
-
- public function testValueCanBeEncodedIfNonAscii()
- {
- $value = 'fo' . pack('C', 0x8F) .'bar';
-
- $encoder = $this->_getHeaderEncoder('Q');
- $this->_checking(Expectations::create()
- -> one($encoder)->encodeString($value, any(), any()) -> returns('fo=8Fbar')
- -> ignoring($encoder)
- );
-
- $header = $this->_getHeader('X-Foo', $encoder, $this->_getParameterEncoder(true));
- $header->setValue($value);
- $header->setParameters(array('lookslike' => 'foobar'));
- $this->assertEqual('X-Foo: =?utf-8?Q?fo=8Fbar?=; lookslike=foobar' . "\r\n",
- $header->toString()
- );
- }
-
- public function testValueAndParamCanBeEncodedIfNonAscii()
- {
- $value = 'fo' . pack('C', 0x8F) .'bar';
-
- $encoder = $this->_getHeaderEncoder('Q');
- $this->_checking(Expectations::create()
- -> one($encoder)->encodeString($value, any(), any()) -> returns('fo=8Fbar')
- -> ignoring($encoder)
- );
-
- $paramEncoder = $this->_getParameterEncoder();
- $this->_checking(Expectations::create()
- -> one($paramEncoder)->encodeString($value, any(), any()) -> returns('fo%8Fbar')
- -> ignoring($paramEncoder)
- );
-
- $header = $this->_getHeader('X-Foo', $encoder, $paramEncoder);
- $header->setValue($value);
- $header->setParameters(array('says' => $value));
- $this->assertEqual("X-Foo: =?utf-8?Q?fo=8Fbar?=; says*=utf-8''fo%8Fbar\r\n",
- $header->toString()
- );
- }
-
- public function testParamsAreEncodedWithEncodedWordsIfNoParamEncoderSet()
- {
- $value = 'fo' . pack('C', 0x8F) .'bar';
-
- $encoder = $this->_getHeaderEncoder('Q');
- $this->_checking(Expectations::create()
- -> one($encoder)->encodeString($value, any(), any()) -> returns('fo=8Fbar')
- -> ignoring($encoder)
- );
-
- $header = $this->_getHeader('X-Foo', $encoder, null);
- $header->setValue('bar');
- $header->setParameters(array('says' => $value));
- $this->assertEqual("X-Foo: bar; says=\"=?utf-8?Q?fo=8Fbar?=\"\r\n",
- $header->toString()
- );
- }
-
- public function testLanguageInformationAppearsInEncodedWords()
- {
-
-
-
- $value = 'fo' . pack('C', 0x8F) .'bar';
-
- $encoder = $this->_getHeaderEncoder('Q');
- $this->_checking(Expectations::create()
- -> one($encoder)->encodeString($value, any(), any()) -> returns('fo=8Fbar')
- -> ignoring($encoder)
- );
-
- $paramEncoder = $this->_getParameterEncoder();
- $this->_checking(Expectations::create()
- -> one($paramEncoder)->encodeString($value, any(), any()) -> returns('fo%8Fbar')
- -> ignoring($paramEncoder)
- );
-
- $header = $this->_getHeader('X-Foo', $encoder, $paramEncoder);
- $header->setLanguage('en');
- $header->setValue($value);
- $header->setParameters(array('says' => $value));
- $this->assertEqual("X-Foo: =?utf-8*en?Q?fo=8Fbar?=; says*=utf-8'en'fo%8Fbar\r\n",
- $header->toString()
- );
- }
-
- public function testSetBodyModel()
- {
- $header = $this->_getHeader('Content-Type',
- $this->_getHeaderEncoder('Q', true), $this->_getParameterEncoder(true)
- );
- $header->setFieldBodyModel('text/html');
- $this->assertEqual('text/html', $header->getValue());
- }
-
- public function testGetBodyModel()
- {
- $header = $this->_getHeader('Content-Type',
- $this->_getHeaderEncoder('Q', true), $this->_getParameterEncoder(true)
- );
- $header->setValue('text/plain');
- $this->assertEqual('text/plain', $header->getFieldBodyModel());
- }
-
- public function testSetParameter()
- {
- $header = $this->_getHeader('Content-Type',
- $this->_getHeaderEncoder('Q', true), $this->_getParameterEncoder(true)
- );
- $header->setParameters(array('charset'=>'utf-8', 'delsp' => 'yes'));
- $header->setParameter('delsp', 'no');
- $this->assertEqual(array('charset'=>'utf-8', 'delsp'=>'no'),
- $header->getParameters()
- );
- }
-
- public function testGetParameter()
- {
- $header = $this->_getHeader('Content-Type',
- $this->_getHeaderEncoder('Q', true), $this->_getParameterEncoder(true)
- );
- $header->setParameters(array('charset'=>'utf-8', 'delsp' => 'yes'));
- $this->assertEqual('utf-8', $header->getParameter('charset'));
- }
-
-
-
- private function _getHeader($name, $encoder, $paramEncoder)
- {
- $header = new Swift_Mime_Headers_ParameterizedHeader($name, $encoder,
- $paramEncoder, new Swift_Mime_Grammar()
- );
- $header->setCharset($this->_charset);
- return $header;
- }
-
- private function _getHeaderEncoder($type, $stub = false)
- {
- $encoder = $this->_mock('Swift_Mime_HeaderEncoder');
- $this->_checking(Expectations::create()
- -> ignoring($encoder)->getName() -> returns($type)
- );
- if ($stub)
- {
- $this->_checking(Expectations::create()
- -> ignoring($encoder)
- );
- }
- return $encoder;
- }
-
- private function _getParameterEncoder($stub = false)
- {
- if ($stub)
- {
- return $this->_stub('Swift_Encoder');
- }
- else
- {
- return $this->_mock('Swift_Encoder');
- }
- }
-
- }
|