ParameterizedHeaderTest.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. <?php
  2. require_once 'Swift/Tests/SwiftUnitTestCase.php';
  3. require_once 'Swift/Mime/Headers/ParameterizedHeader.php';
  4. require_once 'Swift/Mime/HeaderEncoder.php';
  5. require_once 'Swift/Encoder.php';
  6. require_once 'Swift/Mime/Grammar.php';
  7. class Swift_Mime_Headers_ParameterizedHeaderTest
  8. extends Swift_Tests_SwiftUnitTestCase
  9. {
  10. private $_charset = 'utf-8';
  11. private $_lang = 'en-us';
  12. public function testTypeIsParameterizedHeader()
  13. {
  14. $header = $this->_getHeader('Content-Type',
  15. $this->_getHeaderEncoder('Q', true), $this->_getParameterEncoder(true)
  16. );
  17. $this->assertEqual(Swift_Mime_Header::TYPE_PARAMETERIZED, $header->getFieldType());
  18. }
  19. public function testValueIsReturnedVerbatim()
  20. {
  21. $header = $this->_getHeader('Content-Type',
  22. $this->_getHeaderEncoder('Q', true), $this->_getParameterEncoder(true)
  23. );
  24. $header->setValue('text/plain');
  25. $this->assertEqual('text/plain', $header->getValue());
  26. }
  27. public function testParametersAreAppended()
  28. {
  29. /* -- RFC 2045, 5.1
  30. parameter := attribute "=" value
  31. attribute := token
  32. ; Matching of attributes
  33. ; is ALWAYS case-insensitive.
  34. value := token / quoted-string
  35. token := 1*<any (US-ASCII) CHAR except SPACE, CTLs,
  36. or tspecials>
  37. tspecials := "(" / ")" / "<" / ">" / "@" /
  38. "," / ";" / ":" / "\" / <">
  39. "/" / "[" / "]" / "?" / "="
  40. ; Must be in quoted-string,
  41. ; to use within parameter values
  42. */
  43. $header = $this->_getHeader('Content-Type',
  44. $this->_getHeaderEncoder('Q', true), $this->_getParameterEncoder(true)
  45. );
  46. $header->setValue('text/plain');
  47. $header->setParameters(array('charset' => 'utf-8'));
  48. $this->assertEqual('text/plain; charset=utf-8', $header->getFieldBody());
  49. }
  50. public function testSpaceInParamResultsInQuotedString()
  51. {
  52. $header = $this->_getHeader('Content-Disposition',
  53. $this->_getHeaderEncoder('Q', true), $this->_getParameterEncoder(true)
  54. );
  55. $header->setValue('attachment');
  56. $header->setParameters(array('filename' => 'my file.txt'));
  57. $this->assertEqual('attachment; filename="my file.txt"',
  58. $header->getFieldBody()
  59. );
  60. }
  61. public function testLongParamsAreBrokenIntoMultipleAttributeStrings()
  62. {
  63. /* -- RFC 2231, 3.
  64. The asterisk character ("*") followed
  65. by a decimal count is employed to indicate that multiple parameters
  66. are being used to encapsulate a single parameter value. The count
  67. starts at 0 and increments by 1 for each subsequent section of the
  68. parameter value. Decimal values are used and neither leading zeroes
  69. nor gaps in the sequence are allowed.
  70. The original parameter value is recovered by concatenating the
  71. various sections of the parameter, in order. For example, the
  72. content-type field
  73. Content-Type: message/external-body; access-type=URL;
  74. URL*0="ftp://";
  75. URL*1="cs.utk.edu/pub/moore/bulk-mailer/bulk-mailer.tar"
  76. is semantically identical to
  77. Content-Type: message/external-body; access-type=URL;
  78. URL="ftp://cs.utk.edu/pub/moore/bulk-mailer/bulk-mailer.tar"
  79. Note that quotes around parameter values are part of the value
  80. syntax; they are NOT part of the value itself. Furthermore, it is
  81. explicitly permitted to have a mixture of quoted and unquoted
  82. continuation fields.
  83. */
  84. $value = str_repeat('a', 180);
  85. $encoder = $this->_getParameterEncoder();
  86. $this->_checking(Expectations::create()
  87. -> one($encoder)->encodeString($value, any(), 63)
  88. -> returns(str_repeat('a', 63) . "\r\n" .
  89. str_repeat('a', 63) . "\r\n" . str_repeat('a', 54))
  90. -> ignoring($encoder)
  91. );
  92. $header = $this->_getHeader('Content-Disposition',
  93. $this->_getHeaderEncoder('Q', true), $encoder
  94. );
  95. $header->setValue('attachment');
  96. $header->setParameters(array('filename' => $value));
  97. $header->setMaxLineLength(78);
  98. $this->assertEqual(
  99. 'attachment; ' .
  100. 'filename*0*=utf-8\'\'' . str_repeat('a', 63) . ";\r\n " .
  101. 'filename*1*=' . str_repeat('a', 63) . ";\r\n " .
  102. 'filename*2*=' . str_repeat('a', 54),
  103. $header->getFieldBody()
  104. );
  105. }
  106. public function testEncodedParamDataIncludesCharsetAndLanguage()
  107. {
  108. /* -- RFC 2231, 4.
  109. Asterisks ("*") are reused to provide the indicator that language and
  110. character set information is present and encoding is being used. A
  111. single quote ("'") is used to delimit the character set and language
  112. information at the beginning of the parameter value. Percent signs
  113. ("%") are used as the encoding flag, which agrees with RFC 2047.
  114. Specifically, an asterisk at the end of a parameter name acts as an
  115. indicator that character set and language information may appear at
  116. the beginning of the parameter value. A single quote is used to
  117. separate the character set, language, and actual value information in
  118. the parameter value string, and an percent sign is used to flag
  119. octets encoded in hexadecimal. For example:
  120. Content-Type: application/x-stuff;
  121. title*=us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A
  122. Note that it is perfectly permissible to leave either the character
  123. set or language field blank. Note also that the single quote
  124. delimiters MUST be present even when one of the field values is
  125. omitted.
  126. */
  127. $value = str_repeat('a', 20) . pack('C', 0x8F) . str_repeat('a', 10);
  128. $encoder = $this->_getParameterEncoder();
  129. $this->_checking(Expectations::create()
  130. -> one($encoder)->encodeString($value, 12, 62)
  131. -> returns(str_repeat('a', 20) . '%8F' . str_repeat('a', 10))
  132. -> ignoring($encoder)
  133. );
  134. $header = $this->_getHeader('Content-Disposition',
  135. $this->_getHeaderEncoder('Q', true), $encoder
  136. );
  137. $header->setValue('attachment');
  138. $header->setParameters(array('filename' => $value));
  139. $header->setMaxLineLength(78);
  140. $header->setLanguage($this->_lang);
  141. $this->assertEqual(
  142. 'attachment; filename*=' . $this->_charset . "'" . $this->_lang . "'" .
  143. str_repeat('a', 20) . '%8F' . str_repeat('a', 10),
  144. $header->getFieldBody()
  145. );
  146. }
  147. public function testMultipleEncodedParamLinesAreFormattedCorrectly()
  148. {
  149. /* -- RFC 2231, 4.1.
  150. Character set and language information may be combined with the
  151. parameter continuation mechanism. For example:
  152. Content-Type: application/x-stuff
  153. title*0*=us-ascii'en'This%20is%20even%20more%20
  154. title*1*=%2A%2A%2Afun%2A%2A%2A%20
  155. title*2="isn't it!"
  156. Note that:
  157. (1) Language and character set information only appear at
  158. the beginning of a given parameter value.
  159. (2) Continuations do not provide a facility for using more
  160. than one character set or language in the same
  161. parameter value.
  162. (3) A value presented using multiple continuations may
  163. contain a mixture of encoded and unencoded segments.
  164. (4) The first segment of a continuation MUST be encoded if
  165. language and character set information are given.
  166. (5) If the first segment of a continued parameter value is
  167. encoded the language and character set field delimiters
  168. MUST be present even when the fields are left blank.
  169. */
  170. $value = str_repeat('a', 20) . pack('C', 0x8F) . str_repeat('a', 60);
  171. $encoder = $this->_getParameterEncoder();
  172. $this->_checking(Expectations::create()
  173. -> one($encoder)->encodeString($value, 12, 62)
  174. -> returns(str_repeat('a', 20) . '%8F' . str_repeat('a', 28) . "\r\n" .
  175. str_repeat('a', 32))
  176. -> ignoring($encoder)
  177. );
  178. $header = $this->_getHeader('Content-Disposition',
  179. $this->_getHeaderEncoder('Q', true), $encoder
  180. );
  181. $header->setValue('attachment');
  182. $header->setParameters(array('filename' => $value));
  183. $header->setMaxLineLength(78);
  184. $header->setLanguage($this->_lang);
  185. $this->assertEqual(
  186. 'attachment; filename*0*=' . $this->_charset . "'" . $this->_lang . "'" .
  187. str_repeat('a', 20) . '%8F' . str_repeat('a', 28) . ";\r\n " .
  188. 'filename*1*=' . str_repeat('a', 32),
  189. $header->getFieldBody()
  190. );
  191. }
  192. public function testToString()
  193. {
  194. $header = $this->_getHeader('Content-Type',
  195. $this->_getHeaderEncoder('Q', true), $this->_getParameterEncoder(true)
  196. );
  197. $header->setValue('text/html');
  198. $header->setParameters(array('charset' => 'utf-8'));
  199. $this->assertEqual('Content-Type: text/html; charset=utf-8' . "\r\n",
  200. $header->toString()
  201. );
  202. }
  203. public function testValueCanBeEncodedIfNonAscii()
  204. {
  205. $value = 'fo' . pack('C', 0x8F) .'bar';
  206. $encoder = $this->_getHeaderEncoder('Q');
  207. $this->_checking(Expectations::create()
  208. -> one($encoder)->encodeString($value, any(), any()) -> returns('fo=8Fbar')
  209. -> ignoring($encoder)
  210. );
  211. $header = $this->_getHeader('X-Foo', $encoder, $this->_getParameterEncoder(true));
  212. $header->setValue($value);
  213. $header->setParameters(array('lookslike' => 'foobar'));
  214. $this->assertEqual('X-Foo: =?utf-8?Q?fo=8Fbar?=; lookslike=foobar' . "\r\n",
  215. $header->toString()
  216. );
  217. }
  218. public function testValueAndParamCanBeEncodedIfNonAscii()
  219. {
  220. $value = 'fo' . pack('C', 0x8F) .'bar';
  221. $encoder = $this->_getHeaderEncoder('Q');
  222. $this->_checking(Expectations::create()
  223. -> one($encoder)->encodeString($value, any(), any()) -> returns('fo=8Fbar')
  224. -> ignoring($encoder)
  225. );
  226. $paramEncoder = $this->_getParameterEncoder();
  227. $this->_checking(Expectations::create()
  228. -> one($paramEncoder)->encodeString($value, any(), any()) -> returns('fo%8Fbar')
  229. -> ignoring($paramEncoder)
  230. );
  231. $header = $this->_getHeader('X-Foo', $encoder, $paramEncoder);
  232. $header->setValue($value);
  233. $header->setParameters(array('says' => $value));
  234. $this->assertEqual("X-Foo: =?utf-8?Q?fo=8Fbar?=; says*=utf-8''fo%8Fbar\r\n",
  235. $header->toString()
  236. );
  237. }
  238. public function testParamsAreEncodedWithEncodedWordsIfNoParamEncoderSet()
  239. {
  240. $value = 'fo' . pack('C', 0x8F) .'bar';
  241. $encoder = $this->_getHeaderEncoder('Q');
  242. $this->_checking(Expectations::create()
  243. -> one($encoder)->encodeString($value, any(), any()) -> returns('fo=8Fbar')
  244. -> ignoring($encoder)
  245. );
  246. $header = $this->_getHeader('X-Foo', $encoder, null);
  247. $header->setValue('bar');
  248. $header->setParameters(array('says' => $value));
  249. $this->assertEqual("X-Foo: bar; says=\"=?utf-8?Q?fo=8Fbar?=\"\r\n",
  250. $header->toString()
  251. );
  252. }
  253. public function testLanguageInformationAppearsInEncodedWords()
  254. {
  255. /* -- RFC 2231, 5.
  256. 5. Language specification in Encoded Words
  257. RFC 2047 provides support for non-US-ASCII character sets in RFC 822
  258. message header comments, phrases, and any unstructured text field.
  259. This is done by defining an encoded word construct which can appear
  260. in any of these places. Given that these are fields intended for
  261. display, it is sometimes necessary to associate language information
  262. with encoded words as well as just the character set. This
  263. specification extends the definition of an encoded word to allow the
  264. inclusion of such information. This is simply done by suffixing the
  265. character set specification with an asterisk followed by the language
  266. tag. For example:
  267. From: =?US-ASCII*EN?Q?Keith_Moore?= <moore@cs.utk.edu>
  268. */
  269. $value = 'fo' . pack('C', 0x8F) .'bar';
  270. $encoder = $this->_getHeaderEncoder('Q');
  271. $this->_checking(Expectations::create()
  272. -> one($encoder)->encodeString($value, any(), any()) -> returns('fo=8Fbar')
  273. -> ignoring($encoder)
  274. );
  275. $paramEncoder = $this->_getParameterEncoder();
  276. $this->_checking(Expectations::create()
  277. -> one($paramEncoder)->encodeString($value, any(), any()) -> returns('fo%8Fbar')
  278. -> ignoring($paramEncoder)
  279. );
  280. $header = $this->_getHeader('X-Foo', $encoder, $paramEncoder);
  281. $header->setLanguage('en');
  282. $header->setValue($value);
  283. $header->setParameters(array('says' => $value));
  284. $this->assertEqual("X-Foo: =?utf-8*en?Q?fo=8Fbar?=; says*=utf-8'en'fo%8Fbar\r\n",
  285. $header->toString()
  286. );
  287. }
  288. public function testSetBodyModel()
  289. {
  290. $header = $this->_getHeader('Content-Type',
  291. $this->_getHeaderEncoder('Q', true), $this->_getParameterEncoder(true)
  292. );
  293. $header->setFieldBodyModel('text/html');
  294. $this->assertEqual('text/html', $header->getValue());
  295. }
  296. public function testGetBodyModel()
  297. {
  298. $header = $this->_getHeader('Content-Type',
  299. $this->_getHeaderEncoder('Q', true), $this->_getParameterEncoder(true)
  300. );
  301. $header->setValue('text/plain');
  302. $this->assertEqual('text/plain', $header->getFieldBodyModel());
  303. }
  304. public function testSetParameter()
  305. {
  306. $header = $this->_getHeader('Content-Type',
  307. $this->_getHeaderEncoder('Q', true), $this->_getParameterEncoder(true)
  308. );
  309. $header->setParameters(array('charset'=>'utf-8', 'delsp' => 'yes'));
  310. $header->setParameter('delsp', 'no');
  311. $this->assertEqual(array('charset'=>'utf-8', 'delsp'=>'no'),
  312. $header->getParameters()
  313. );
  314. }
  315. public function testGetParameter()
  316. {
  317. $header = $this->_getHeader('Content-Type',
  318. $this->_getHeaderEncoder('Q', true), $this->_getParameterEncoder(true)
  319. );
  320. $header->setParameters(array('charset'=>'utf-8', 'delsp' => 'yes'));
  321. $this->assertEqual('utf-8', $header->getParameter('charset'));
  322. }
  323. // -- Private helper
  324. private function _getHeader($name, $encoder, $paramEncoder)
  325. {
  326. $header = new Swift_Mime_Headers_ParameterizedHeader($name, $encoder,
  327. $paramEncoder, new Swift_Mime_Grammar()
  328. );
  329. $header->setCharset($this->_charset);
  330. return $header;
  331. }
  332. private function _getHeaderEncoder($type, $stub = false)
  333. {
  334. $encoder = $this->_mock('Swift_Mime_HeaderEncoder');
  335. $this->_checking(Expectations::create()
  336. -> ignoring($encoder)->getName() -> returns($type)
  337. );
  338. if ($stub)
  339. {
  340. $this->_checking(Expectations::create()
  341. -> ignoring($encoder)
  342. );
  343. }
  344. return $encoder;
  345. }
  346. private function _getParameterEncoder($stub = false)
  347. {
  348. if ($stub)
  349. {
  350. return $this->_stub('Swift_Encoder');
  351. }
  352. else
  353. {
  354. return $this->_mock('Swift_Encoder');
  355. }
  356. }
  357. }