Rfc2231EncoderTest.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. require_once 'Swift/Encoder/Rfc2231Encoder.php';
  3. require_once 'Swift/CharacterStream.php';
  4. require_once 'Swift/Tests/SwiftUnitTestCase.php';
  5. class Swift_Encoder_Rfc2231EncoderTest extends Swift_Tests_SwiftUnitTestCase
  6. {
  7. private $_rfc2045Token = '/^[\x21\x23-\x27\x2A\x2B\x2D\x2E\x30-\x39\x41-\x5A\x5E-\x7E]+$/D';
  8. /* --
  9. This algorithm is described in RFC 2231, but is barely touched upon except
  10. for mentioning bytes can be represented as their octet values (e.g. %20 for
  11. the SPACE character).
  12. The tests here focus on how to use that representation to always generate text
  13. which matches RFC 2045's definition of "token".
  14. */
  15. public function testEncodingAsciiCharactersProducesValidToken()
  16. {
  17. $charStream = $this->_mock('Swift_CharacterStream');
  18. $seq = $this->_sequence('byte-sequence');
  19. $string = '';
  20. foreach (range(0x00, 0x7F) as $octet)
  21. {
  22. $char = pack('C', $octet);
  23. $string .= $char;
  24. $this->_checking(Expectations::create()
  25. -> one($charStream)->read(optional()) -> inSequence($seq) -> returns($char)
  26. );
  27. }
  28. $this->_checking(Expectations::create()
  29. -> atLeast(1)->of($charStream)->read(optional()) -> inSequence($seq) -> returns(false)
  30. -> one($charStream)->importString($string)
  31. -> ignoring($charStream)->flushContents()
  32. );
  33. $encoder = new Swift_Encoder_Rfc2231Encoder($charStream);
  34. $encoded = $encoder->encodeString($string);
  35. foreach (explode("\r\n", $encoded) as $line)
  36. {
  37. $this->assertPattern($this->_rfc2045Token, $line,
  38. '%s: Encoder should always return a valid RFC 2045 token.');
  39. }
  40. }
  41. public function testEncodingNonAsciiCharactersProducesValidToken()
  42. {
  43. $charStream = $this->_mock('Swift_CharacterStream');
  44. $seq = $this->_sequence('byte-sequence');
  45. $string = '';
  46. foreach (range(0x80, 0xFF) as $octet)
  47. {
  48. $char = pack('C', $octet);
  49. $string .= $char;
  50. $this->_checking(Expectations::create()
  51. -> one($charStream)->read(optional()) -> inSequence($seq) -> returns($char)
  52. );
  53. }
  54. $this->_checking(Expectations::create()
  55. -> atLeast(1)->of($charStream)->read(optional()) -> inSequence($seq) -> returns(false)
  56. -> one($charStream)->importString($string)
  57. -> ignoring($charStream)->flushContents()
  58. );
  59. $encoder = new Swift_Encoder_Rfc2231Encoder($charStream);
  60. $encoded = $encoder->encodeString($string);
  61. foreach (explode("\r\n", $encoded) as $line)
  62. {
  63. $this->assertPattern($this->_rfc2045Token, $line,
  64. '%s: Encoder should always return a valid RFC 2045 token.');
  65. }
  66. }
  67. public function testMaximumLineLengthCanBeSet()
  68. {
  69. $charStream = $this->_mock('Swift_CharacterStream');
  70. $seq = $this->_sequence('byte-sequence');
  71. $string = '';
  72. for ($x = 0; $x < 200; ++$x)
  73. {
  74. $char = 'a';
  75. $string .= $char;
  76. $this->_checking(Expectations::create()
  77. -> one($charStream)->read(optional()) -> inSequence($seq) -> returns($char)
  78. );
  79. }
  80. $this->_checking(Expectations::create()
  81. -> atLeast(1)->of($charStream)->read(optional()) -> inSequence($seq) -> returns(false)
  82. -> one($charStream)->importString($string)
  83. -> ignoring($charStream)->flushContents()
  84. );
  85. $encoder = new Swift_Encoder_Rfc2231Encoder($charStream);
  86. $encoded = $encoder->encodeString($string, 0, 75);
  87. $this->assertEqual(
  88. str_repeat('a', 75) . "\r\n" .
  89. str_repeat('a', 75) . "\r\n" .
  90. str_repeat('a', 50),
  91. $encoded,
  92. '%s: Lines should be wrapped at each 75 characters'
  93. );
  94. }
  95. public function testFirstLineCanHaveShorterLength()
  96. {
  97. $charStream = $this->_mock('Swift_CharacterStream');
  98. $seq = $this->_sequence('byte-sequence');
  99. $string = '';
  100. for ($x = 0; $x < 200; ++$x)
  101. {
  102. $char = 'a';
  103. $string .= $char;
  104. $this->_checking(Expectations::create()
  105. -> one($charStream)->read(optional()) -> inSequence($seq) -> returns($char)
  106. );
  107. }
  108. $this->_checking(Expectations::create()
  109. -> atLeast(1)->of($charStream)->read(optional()) -> inSequence($seq) -> returns(false)
  110. -> one($charStream)->importString($string)
  111. -> ignoring($charStream)->flushContents()
  112. );
  113. $encoder = new Swift_Encoder_Rfc2231Encoder($charStream);
  114. $encoded = $encoder->encodeString($string, 25, 75);
  115. $this->assertEqual(
  116. str_repeat('a', 50) . "\r\n" .
  117. str_repeat('a', 75) . "\r\n" .
  118. str_repeat('a', 75),
  119. $encoded,
  120. '%s: First line should be 25 bytes shorter than the others.'
  121. );
  122. }
  123. }