-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(smtp): support multiline headers (RFC822 section 3.1.3) (#160)
- Loading branch information
Showing
3 changed files
with
144 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,26 +9,83 @@ | |
use Buggregator\Trap\Traffic\Message; | ||
use Buggregator\Trap\Traffic\Parser; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
#[CoversClass(Parser\Smtp::class)] | ||
final class SmtpParserTest extends TestCase | ||
{ | ||
use FiberTrait; | ||
|
||
public function testParseSimpleBody(): void | ||
public static function provideHeaders() | ||
{ | ||
yield [ | ||
"To: User1 <[email protected]>, User2 <[email protected]>, User3\r\n <[email protected]>\r\n", | ||
['To' => ['User1 <[email protected]>, User2 <[email protected]>, User3 <[email protected]>']], | ||
]; | ||
} | ||
|
||
#[DataProvider('provideHeaders')] | ||
public function testParseHeader(string $headerString, array $expected): void | ||
{ | ||
self::assertSame($expected, Parser\Smtp::parseHeaders($headerString)); | ||
} | ||
|
||
public function testMultilineHeaders(): void | ||
{ | ||
$data = \str_split(<<<SMTP | ||
From: Some User <[email protected]>\r | ||
To: User1 <[email protected]>\r | ||
Subject: Very important theme\r | ||
Content-Type: text/plain\r | ||
To: User1 <[email protected]>, User2 <[email protected]>, User3\r | ||
<[email protected]>\r | ||
Subject: ABCDEFGHIJKLMNOPQRSTUVWXYZ- ABCDEFGHIJKLMNOPQRSTUVWXYZ-\r | ||
ABCDEFGHIJKLMNOPQRSTUVWXYZ- ABCDEFGHIJKLMNOPQRSTUVWXYZ-\r | ||
ABCDEFGHIJKLMNOPQRSTUVWXYZ-\r | ||
From: sandbox producer <[email protected]>\r | ||
Content-Type: multipart/alternative; boundary=_ZwZ4SBw\r | ||
\r | ||
--_ZwZ4SBw\r | ||
Content-Type: text/plain; charset=utf-8\r | ||
Content-Transfer-Encoding: quoted-printable\r | ||
\r | ||
Hello from Producer!\r | ||
\r | ||
Hi!\r | ||
.\r\n | ||
--_ZwZ4SBw\r | ||
Content-Type: text/html; charset=utf-8\r | ||
Content-Transfer-Encoding: quoted-printable\r | ||
\r | ||
<h1>Hello from Producer!</h1>\r | ||
\r | ||
--_ZwZ4SBw--\r | ||
\r\n.\r\n | ||
SMTP, 10); | ||
$message = $this->parse($data); | ||
|
||
self::assertSame(\implode('', $data), (string) $message->getBody()); | ||
self::assertCount(2, $message->getMessages()); | ||
// Check headers | ||
self::assertEquals([ | ||
'From' => ['sandbox producer <[email protected]>'], | ||
'To' => ['User1 <[email protected]>, User2 <[email protected]>, User3 <[email protected]>'], | ||
'Subject' => ['ABCDEFGHIJKLMNOPQRSTUVWXYZ- ABCDEFGHIJKLMNOPQRSTUVWXYZ- ABCDEFGHIJKLMNOPQRSTUVWXYZ- ABCDEFGHIJKLMNOPQRSTUVWXYZ- ABCDEFGHIJKLMNOPQRSTUVWXYZ-'], | ||
'Content-Type' => ['multipart/alternative; boundary=_ZwZ4SBw'], | ||
], $message->getHeaders()); | ||
// Check body | ||
self::assertSame("Hello from Producer!\r\n", $message->getMessages()[0]->getValue()); | ||
self::assertSame(" <h1>Hello from Producer!</h1>\r\n", $message->getMessages()[1]->getValue()); | ||
} | ||
|
||
public function testParseSimpleBody(): void | ||
{ | ||
$data = \str_split(<<<SMTP | ||
From: Some User <[email protected]>\r | ||
To: User1 <[email protected]>\r | ||
Subject: Very important theme\r | ||
Content-Type: text/plain\r | ||
\r | ||
Hi!\r | ||
.\r\n | ||
SMTP, 10); | ||
$message = $this->parse($data); | ||
|
||
self::assertSame(\implode('', $data), (string) $message->getBody()); | ||
self::assertCount(1, $message->getMessages()); | ||
// Check headers | ||
|
@@ -45,43 +102,43 @@ public function testParseSimpleBody(): void | |
public function testParseMultipart(): void | ||
{ | ||
$data = \str_split(<<<SMTP | ||
From: [email protected]\r | ||
To: [email protected]\r | ||
Subject: Multipart Email Example\r | ||
Content-Type: multipart/alternative; boundary="boundary-string"\r | ||
\r | ||
--boundary-string\r | ||
Content-Type: text/plain; charset="utf-8"\r | ||
Content-Transfer-Encoding: quoted-printable\r | ||
Content-Disposition: inline\r | ||
\r | ||
Plain text email goes here!\r | ||
This is the fallback if email client does not support HTML\r | ||
\r | ||
--boundary-string\r | ||
Content-Type: text/html; charset="utf-8"\r | ||
Content-Transfer-Encoding: quoted-printable\r | ||
Content-Disposition: inline\r | ||
\r | ||
<h1>This is the HTML Section!</h1>\r | ||
<p>This is what displays in most modern email clients</p>\r | ||
\r | ||
--boundary-string--\r | ||
Content-Type: image/x-icon\r | ||
Content-Transfer-Encoding: base64\r | ||
Content-Disposition: attachment;filename=logo.ico\r | ||
\r | ||
123456789098765432123456789\r | ||
\r | ||
--boundary-string--\r | ||
Content-Type: text/watch-html; charset="utf-8"\r | ||
Content-Transfer-Encoding: quoted-printable\r | ||
Content-Disposition: inline\r | ||
\r | ||
<b>Apple Watch formatted content</b>\r | ||
\r | ||
--boundary-string--\r\n\r\n | ||
SMTP, 10); | ||
From: [email protected]\r | ||
To: [email protected]\r | ||
Subject: Multipart Email Example\r | ||
Content-Type: multipart/alternative; boundary="boundary-string"\r | ||
\r | ||
--boundary-string\r | ||
Content-Type: text/plain; charset="utf-8"\r | ||
Content-Transfer-Encoding: quoted-printable\r | ||
Content-Disposition: inline\r | ||
\r | ||
Plain text email goes here!\r | ||
This is the fallback if email client does not support HTML\r | ||
\r | ||
--boundary-string\r | ||
Content-Type: text/html; charset="utf-8"\r | ||
Content-Transfer-Encoding: quoted-printable\r | ||
Content-Disposition: inline\r | ||
\r | ||
<h1>This is the HTML Section!</h1>\r | ||
<p>This is what displays in most modern email clients</p>\r | ||
\r | ||
--boundary-string--\r | ||
Content-Type: image/x-icon\r | ||
Content-Transfer-Encoding: base64\r | ||
Content-Disposition: attachment;filename=logo.ico\r | ||
\r | ||
123456789098765432123456789\r | ||
\r | ||
--boundary-string--\r | ||
Content-Type: text/watch-html; charset="utf-8"\r | ||
Content-Transfer-Encoding: quoted-printable\r | ||
Content-Disposition: inline\r | ||
\r | ||
<b>Apple Watch formatted content</b>\r | ||
\r | ||
--boundary-string--\r\n\r\n | ||
SMTP, 10); | ||
$message = $this->parse($data, [ | ||
'FROM' => ['<[email protected]>'], | ||
'BCC' => ['<[email protected]>', '<[email protected]>'], | ||
|