PageRenderTime 25ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/test/Swift/tests/unit/Swift/Mime/Headers/UnstructuredHeaderTest.php

https://github.com/chacha13/runningmate
PHP | 368 lines | 223 code | 54 blank | 91 comment | 1 complexity | dc526501671850640d1cf77da312762c MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1
  1. <?php
  2. require_once 'Swift/Tests/SwiftUnitTestCase.php';
  3. require_once 'Swift/Mime/Headers/UnstructuredHeader.php';
  4. require_once 'Swift/Mime/HeaderEncoder.php';
  5. class Swift_Mime_Headers_UnstructuredHeaderTest
  6. extends Swift_Tests_SwiftUnitTestCase
  7. {
  8. private $_charset = 'utf-8';
  9. public function testTypeIsTextHeader()
  10. {
  11. $header = $this->_getHeader('Subject', $this->_getEncoder('Q', true));
  12. $this->assertEqual(Swift_Mime_Header::TYPE_TEXT, $header->getFieldType());
  13. }
  14. public function testGetNameReturnsNameVerbatim()
  15. {
  16. $header = $this->_getHeader('Subject', $this->_getEncoder('Q', true));
  17. $this->assertEqual('Subject', $header->getFieldName());
  18. }
  19. public function testGetValueReturnsValueVerbatim()
  20. {
  21. $header = $this->_getHeader('Subject', $this->_getEncoder('Q', true));
  22. $header->setValue('Test');
  23. $this->assertEqual('Test', $header->getValue());
  24. }
  25. public function testBasicStructureIsKeyValuePair()
  26. {
  27. /* -- RFC 2822, 2.2
  28. Header fields are lines composed of a field name, followed by a colon
  29. (":"), followed by a field body, and terminated by CRLF.
  30. */
  31. $header = $this->_getHeader('Subject', $this->_getEncoder('Q', true));
  32. $header->setValue('Test');
  33. $this->assertEqual('Subject: Test' . "\r\n", $header->toString());
  34. }
  35. public function testLongHeadersAreFoldedAtWordBoundary()
  36. {
  37. /* -- RFC 2822, 2.2.3
  38. Each header field is logically a single line of characters comprising
  39. the field name, the colon, and the field body. For convenience
  40. however, and to deal with the 998/78 character limitations per line,
  41. the field body portion of a header field can be split into a multiple
  42. line representation; this is called "folding". The general rule is
  43. that wherever this standard allows for folding white space (not
  44. simply WSP characters), a CRLF may be inserted before any WSP.
  45. */
  46. $value = 'The quick brown fox jumped over the fence, he was a very very ' .
  47. 'scary brown fox with a bushy tail';
  48. $header = $this->_getHeader('X-Custom-Header',
  49. $this->_getEncoder('Q', true)
  50. );
  51. $header->setValue($value);
  52. $header->setMaxLineLength(78); //A safe [RFC 2822, 2.2.3] default
  53. /*
  54. X-Custom-Header: The quick brown fox jumped over the fence, he was a very very
  55. scary brown fox with a bushy tail
  56. */
  57. $this->assertEqual(
  58. 'X-Custom-Header: The quick brown fox jumped over the fence, he was a' .
  59. ' very very' . "\r\n" . //Folding
  60. ' scary brown fox with a bushy tail' . "\r\n",
  61. $header->toString(), '%s: The header should have been folded at 78th char'
  62. );
  63. }
  64. public function testPrintableAsciiOnlyAppearsInHeaders()
  65. {
  66. /* -- RFC 2822, 2.2.
  67. A field name MUST be composed of printable US-ASCII characters (i.e.,
  68. characters that have values between 33 and 126, inclusive), except
  69. colon. A field body may be composed of any US-ASCII characters,
  70. except for CR and LF.
  71. */
  72. $nonAsciiChar = pack('C', 0x8F);
  73. $header = $this->_getHeader('X-Test', $this->_getEncoder('Q', true));
  74. $header->setValue($nonAsciiChar);
  75. $this->assertPattern(
  76. '~^[^:\x00-\x20\x80-\xFF]+: [^\x80-\xFF\r\n]+\r\n$~s',
  77. $header->toString()
  78. );
  79. }
  80. public function testEncodedWordsFollowGeneralStructure()
  81. {
  82. /* -- RFC 2047, 1.
  83. Generally, an "encoded-word" is a sequence of printable ASCII
  84. characters that begins with "=?", ends with "?=", and has two "?"s in
  85. between.
  86. */
  87. $nonAsciiChar = pack('C', 0x8F);
  88. $header = $this->_getHeader('X-Test', $this->_getEncoder('Q', true));
  89. $header->setValue($nonAsciiChar);
  90. $this->assertPattern(
  91. '~^X-Test: \=?.*?\?.*?\?.*?\?=\r\n$~s',
  92. $header->toString()
  93. );
  94. }
  95. public function testEncodedWordIncludesCharsetAndEncodingMethodAndText()
  96. {
  97. /* -- RFC 2047, 2.
  98. An 'encoded-word' is defined by the following ABNF grammar. The
  99. notation of RFC 822 is used, with the exception that white space
  100. characters MUST NOT appear between components of an 'encoded-word'.
  101. encoded-word = "=?" charset "?" encoding "?" encoded-text "?="
  102. */
  103. $nonAsciiChar = pack('C', 0x8F);
  104. $encoder = $this->_getEncoder('Q');
  105. $this->_checking(Expectations::create()
  106. -> one($encoder)->encodeString($nonAsciiChar, any(), any()) -> returns('=8F')
  107. -> ignoring($encoder)
  108. );
  109. $header = $this->_getHeader('X-Test', $encoder);
  110. $header->setValue($nonAsciiChar);
  111. $this->assertEqual(
  112. 'X-Test: =?' . $this->_charset . '?Q?=8F?=' . "\r\n",
  113. $header->toString()
  114. );
  115. }
  116. public function testEncodedWordsAreUsedToEncodedNonPrintableAscii()
  117. {
  118. //SPACE and TAB permitted
  119. $nonPrintableBytes = array_merge(
  120. range(0x00, 0x08), range(0x10, 0x19), array(0x7F)
  121. );
  122. foreach ($nonPrintableBytes as $byte)
  123. {
  124. $char = pack('C', $byte);
  125. $encodedChar = sprintf('=%02X', $byte);
  126. $encoder = $this->_getEncoder('Q');
  127. $this->_checking(Expectations::create()
  128. -> one($encoder)->encodeString($char, any(), any()) -> returns($encodedChar)
  129. -> ignoring($encoder)
  130. );
  131. $header = $this->_getHeader('X-A', $encoder);
  132. $header->setValue($char);
  133. $this->assertEqual(
  134. 'X-A: =?' . $this->_charset . '?Q?' . $encodedChar . '?=' . "\r\n",
  135. $header->toString(), '%s: Non-printable ascii should be encoded'
  136. );
  137. }
  138. }
  139. public function testEncodedWordsAreUsedToEncode8BitOctets()
  140. {
  141. $_8BitBytes = range(0x80, 0xFF);
  142. foreach ($_8BitBytes as $byte)
  143. {
  144. $char = pack('C', $byte);
  145. $encodedChar = sprintf('=%02X', $byte);
  146. $encoder = $this->_getEncoder('Q');
  147. $this->_checking(Expectations::create()
  148. -> one($encoder)->encodeString($char, any(), any()) -> returns($encodedChar)
  149. -> ignoring($encoder)
  150. );
  151. $header = $this->_getHeader('X-A', $encoder);
  152. $header->setValue($char);
  153. $this->assertEqual(
  154. 'X-A: =?' . $this->_charset . '?Q?' . $encodedChar . '?=' . "\r\n",
  155. $header->toString(), '%s: 8-bit octets should be encoded'
  156. );
  157. }
  158. }
  159. public function testEncodedWordsAreNoMoreThan75CharsPerLine()
  160. {
  161. /* -- RFC 2047, 2.
  162. An 'encoded-word' may not be more than 75 characters long, including
  163. 'charset', 'encoding', 'encoded-text', and delimiters.
  164. ... SNIP ...
  165. While there is no limit to the length of a multiple-line header
  166. field, each line of a header field that contains one or more
  167. 'encoded-word's is limited to 76 characters.
  168. */
  169. $nonAsciiChar = pack('C', 0x8F);
  170. $encoder = $this->_getEncoder('Q');
  171. $this->_checking(Expectations::create()
  172. -> one($encoder)->encodeString($nonAsciiChar, 8, 63) -> returns('=8F')
  173. -> ignoring($encoder)
  174. );
  175. //Note that multi-line headers begin with LWSP which makes 75 + 1 = 76
  176. //Note also that =?utf-8?q??= is 12 chars which makes 75 - 12 = 63
  177. //* X-Test: is 8 chars
  178. $header = $this->_getHeader('X-Test', $encoder);
  179. $header->setValue($nonAsciiChar);
  180. $this->assertEqual(
  181. 'X-Test: =?' . $this->_charset . '?Q?=8F?=' . "\r\n",
  182. $header->toString()
  183. );
  184. }
  185. public function testFWSPIsUsedWhenEncoderReturnsMultipleLines()
  186. {
  187. /* --RFC 2047, 2.
  188. If it is desirable to encode more text than will fit in an 'encoded-word' of
  189. 75 characters, multiple 'encoded-word's (separated by CRLF SPACE) may
  190. be used.
  191. */
  192. //Note the Mock does NOT return 8F encoded, the 8F merely triggers
  193. // encoding for the sake of testing
  194. $nonAsciiChar = pack('C', 0x8F);
  195. $encoder = $this->_getEncoder('Q');
  196. $this->_checking(Expectations::create()
  197. -> one($encoder)->encodeString($nonAsciiChar, 8, 63)
  198. -> returns('line_one_here' . "\r\n" . 'line_two_here')
  199. -> ignoring($encoder)
  200. );
  201. //Note that multi-line headers begin with LWSP which makes 75 + 1 = 76
  202. //Note also that =?utf-8?q??= is 12 chars which makes 75 - 12 = 63
  203. //* X-Test: is 8 chars
  204. $header = $this->_getHeader('X-Test', $encoder);
  205. $header->setValue($nonAsciiChar);
  206. $this->assertEqual(
  207. 'X-Test: =?' . $this->_charset . '?Q?line_one_here?=' . "\r\n" .
  208. ' =?' . $this->_charset . '?Q?line_two_here?=' . "\r\n",
  209. $header->toString()
  210. );
  211. }
  212. public function testAdjacentWordsAreEncodedTogether()
  213. {
  214. /* -- RFC 2047, 5 (1)
  215. Ordinary ASCII text and 'encoded-word's may appear together in the
  216. same header field. However, an 'encoded-word' that appears in a
  217. header field defined as '*text' MUST be separated from any adjacent
  218. 'encoded-word' or 'text' by 'linear-white-space'.
  219. -- RFC 2047, 2.
  220. IMPORTANT: 'encoded-word's are designed to be recognized as 'atom's
  221. by an RFC 822 parser. As a consequence, unencoded white space
  222. characters (such as SPACE and HTAB) are FORBIDDEN within an
  223. 'encoded-word'.
  224. */
  225. //It would be valid to encode all words needed, however it's probably
  226. // easiest to encode the longest amount required at a time
  227. $word = 'w' . pack('C', 0x8F) . 'rd';
  228. $text = 'start ' . $word . ' ' . $word . ' then end ' . $word;
  229. // 'start', ' word word', ' and end', ' word'
  230. $encoder = $this->_getEncoder('Q');
  231. $this->_checking(Expectations::create()
  232. -> one($encoder)->encodeString($word . ' ' . $word, any(), any())
  233. -> returns('w=8Frd_w=8Frd')
  234. -> one($encoder)->encodeString($word, any(), any()) -> returns('w=8Frd')
  235. -> ignoring($encoder)
  236. );
  237. $header = $this->_getHeader('X-Test', $encoder);
  238. $header->setValue($text);
  239. $headerString = $header->toString();
  240. $this->assertEqual('X-Test: start =?' . $this->_charset . '?Q?' .
  241. 'w=8Frd_w=8Frd?= then end =?' . $this->_charset . '?Q?'.
  242. 'w=8Frd?=' . "\r\n", $headerString,
  243. '%s: Adjacent encoded words should appear grouped with WSP encoded'
  244. );
  245. }
  246. public function testLanguageInformationAppearsInEncodedWords()
  247. {
  248. /* -- RFC 2231, 5.
  249. 5. Language specification in Encoded Words
  250. RFC 2047 provides support for non-US-ASCII character sets in RFC 822
  251. message header comments, phrases, and any unstructured text field.
  252. This is done by defining an encoded word construct which can appear
  253. in any of these places. Given that these are fields intended for
  254. display, it is sometimes necessary to associate language information
  255. with encoded words as well as just the character set. This
  256. specification extends the definition of an encoded word to allow the
  257. inclusion of such information. This is simply done by suffixing the
  258. character set specification with an asterisk followed by the language
  259. tag. For example:
  260. From: =?US-ASCII*EN?Q?Keith_Moore?= <moore@cs.utk.edu>
  261. */
  262. $value = 'fo' . pack('C', 0x8F) . 'bar';
  263. $encoder = $this->_getEncoder('Q');
  264. $this->_checking(Expectations::create()
  265. -> one($encoder)->encodeString($value, any(), any()) -> returns('fo=8Fbar')
  266. -> ignoring($encoder)
  267. );
  268. $header = $this->_getHeader('Subject', $encoder);
  269. $header->setLanguage('en');
  270. $header->setValue($value);
  271. $this->assertEqual("Subject: =?utf-8*en?Q?fo=8Fbar?=\r\n",
  272. $header->toString()
  273. );
  274. }
  275. public function testSetBodyModel()
  276. {
  277. $header = $this->_getHeader('Subject', $this->_getEncoder('Q', true));
  278. $header->setFieldBodyModel('test');
  279. $this->assertEqual('test', $header->getValue());
  280. }
  281. public function testGetBodyModel()
  282. {
  283. $header = $this->_getHeader('Subject', $this->_getEncoder('Q', true));
  284. $header->setValue('test');
  285. $this->assertEqual('test', $header->getFieldBodyModel());
  286. }
  287. // -- Private methods
  288. private function _getHeader($name, $encoder)
  289. {
  290. $header = new Swift_Mime_Headers_UnstructuredHeader($name, $encoder);
  291. $header->setCharset($this->_charset);
  292. return $header;
  293. }
  294. private function _getEncoder($type, $stub = false)
  295. {
  296. $encoder = $this->_mock('Swift_Mime_HeaderEncoder');
  297. $this->_checking(Expectations::create()
  298. -> ignoring($encoder)->getName() -> returns($type)
  299. );
  300. if ($stub)
  301. {
  302. $this->_checking(Expectations::create()
  303. -> ignoring($encoder)
  304. );
  305. }
  306. return $encoder;
  307. }
  308. }