Rfc2231EncoderTest.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. $char = pack('C', $octet);
  22. $string .= $char;
  23. $this->_checking(Expectations::create()
  24. -> one($charStream)->read(optional()) -> inSequence($seq) -> returns($char)
  25. );
  26. }
  27. $this->_checking(Expectations::create()
  28. -> atLeast(1)->of($charStream)->read(optional()) -> inSequence($seq) -> returns(false)
  29. -> one($charStream)->importString($string)
  30. -> ignoring($charStream)->flushContents()
  31. );
  32. $encoder = new Swift_Encoder_Rfc2231Encoder($charStream);
  33. $encoded = $encoder->encodeString($string);
  34. foreach (explode("\r\n", $encoded) as $line) {
  35. $this->assertPattern($this->_rfc2045Token, $line,
  36. '%s: Encoder should always return a valid RFC 2045 token.');
  37. }
  38. }
  39. public function testEncodingNonAsciiCharactersProducesValidToken()
  40. {
  41. $charStream = $this->_mock('Swift_CharacterStream');
  42. $seq = $this->_sequence('byte-sequence');
  43. $string = '';
  44. foreach (range(0x80, 0xFF) as $octet) {
  45. $char = pack('C', $octet);
  46. $string .= $char;
  47. $this->_checking(Expectations::create()
  48. -> one($charStream)->read(optional()) -> inSequence($seq) -> returns($char)
  49. );
  50. }
  51. $this->_checking(Expectations::create()
  52. -> atLeast(1)->of($charStream)->read(optional()) -> inSequence($seq) -> returns(false)
  53. -> one($charStream)->importString($string)
  54. -> ignoring($charStream)->flushContents()
  55. );
  56. $encoder = new Swift_Encoder_Rfc2231Encoder($charStream);
  57. $encoded = $encoder->encodeString($string);
  58. foreach (explode("\r\n", $encoded) as $line) {
  59. $this->assertPattern($this->_rfc2045Token, $line,
  60. '%s: Encoder should always return a valid RFC 2045 token.');
  61. }
  62. }
  63. public function testMaximumLineLengthCanBeSet()
  64. {
  65. $charStream = $this->_mock('Swift_CharacterStream');
  66. $seq = $this->_sequence('byte-sequence');
  67. $string = '';
  68. for ($x = 0; $x < 200; ++$x) {
  69. $char = 'a';
  70. $string .= $char;
  71. $this->_checking(Expectations::create()
  72. -> one($charStream)->read(optional()) -> inSequence($seq) -> returns($char)
  73. );
  74. }
  75. $this->_checking(Expectations::create()
  76. -> atLeast(1)->of($charStream)->read(optional()) -> inSequence($seq) -> returns(false)
  77. -> one($charStream)->importString($string)
  78. -> ignoring($charStream)->flushContents()
  79. );
  80. $encoder = new Swift_Encoder_Rfc2231Encoder($charStream);
  81. $encoded = $encoder->encodeString($string, 0, 75);
  82. $this->assertEqual(
  83. str_repeat('a', 75) . "\r\n" .
  84. str_repeat('a', 75) . "\r\n" .
  85. str_repeat('a', 50),
  86. $encoded,
  87. '%s: Lines should be wrapped at each 75 characters'
  88. );
  89. }
  90. public function testFirstLineCanHaveShorterLength()
  91. {
  92. $charStream = $this->_mock('Swift_CharacterStream');
  93. $seq = $this->_sequence('byte-sequence');
  94. $string = '';
  95. for ($x = 0; $x < 200; ++$x) {
  96. $char = 'a';
  97. $string .= $char;
  98. $this->_checking(Expectations::create()
  99. -> one($charStream)->read(optional()) -> inSequence($seq) -> returns($char)
  100. );
  101. }
  102. $this->_checking(Expectations::create()
  103. -> atLeast(1)->of($charStream)->read(optional()) -> inSequence($seq) -> returns(false)
  104. -> one($charStream)->importString($string)
  105. -> ignoring($charStream)->flushContents()
  106. );
  107. $encoder = new Swift_Encoder_Rfc2231Encoder($charStream);
  108. $encoded = $encoder->encodeString($string, 25, 75);
  109. $this->assertEqual(
  110. str_repeat('a', 50) . "\r\n" .
  111. str_repeat('a', 75) . "\r\n" .
  112. str_repeat('a', 75),
  113. $encoded,
  114. '%s: First line should be 25 bytes shorter than the others.'
  115. );
  116. }
  117. }