Skip to content

Commit

Permalink
test(smtp): cover RSET
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Jan 19, 2025
1 parent fd080da commit 64dc421
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
3 changes: 0 additions & 3 deletions src/Traffic/Dispatcher/Smtp.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ public function dispatch(StreamClient $stream): iterable
$stream->sendData($this->createResponse(self::CLOSING));
$stream->disconnect();
return;
} elseif (\str_starts_with($response, 'RSET')) {
$stream->sendData($this->createResponse(self::OK));
$protocol = [];
} elseif (\str_starts_with($response, 'DATA')) {
$stream->sendData($this->createResponse(self::START_MAIL_INPUT));
$message = $this->parser->parseStream($protocol, $stream);
Expand Down
35 changes: 33 additions & 2 deletions tests/Unit/Traffic/Dispatcher/SmtpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,42 @@ public function testDispatchOneMail(): void
$stream = StreamClientMock::createFromGenerator($this->mailMe(quit: true));

$this->runInFiber(static function () use ($stream): void {
$cnt = 0;
foreach ((new Smtp())->dispatch($stream) as $frame) {
self::assertInstanceOf(SmtpFrame::class, $frame);
self::assertSame('Test email', $frame->message->getSubject());
return;
++$cnt;
}

self::fail('No frame was yielded.');
self::assertSame(1, $cnt, 'Only one frame should be yielded.');
});
}

public function testDispatchOneMailWithRset(): void
{
$stream = StreamClientMock::createFromGenerator((function (\Generator ...$generators) {
yield "EHLO\r\n";
yield "MAIL FROM: <[email protected]>\r\n";
yield "RCPT TO: <[email protected]>\r\n";
yield "RCPT TO: <[email protected]>\r\n";
yield "RSET\r\n";
yield from $this->mailMe('Test email');
})());

$this->runInFiber(static function () use ($stream): void {
$cnt = 0;
foreach ((new Smtp())->dispatch($stream) as $frame) {
self::assertInstanceOf(SmtpFrame::class, $frame);
self::assertSame("Test email", $frame->message->getSubject());
self::assertSame(
['[email protected]', '[email protected]'],
$frame->message->getProtocol()['BCC'],
'RSET should clear the buffered data.',
);
++$cnt;
}

self::assertSame(1, $cnt, 'Only one frame should be yielded.');
});
}

Expand Down Expand Up @@ -64,6 +93,7 @@ private function mailMe(string $subject = 'Test email', bool $quit = false): \Ge
yield "MAIL FROM: <[email protected]>\r\n";
yield "RCPT TO: <[email protected]>\r\n";
yield "RCPT TO: <[email protected]>\r\n";
yield "NOOP\r\n";
yield "DATA\r\n";
yield "From: [email protected]\r\n";
yield "To: [email protected]\r\n";
Expand All @@ -72,6 +102,7 @@ private function mailMe(string $subject = 'Test email', bool $quit = false): \Ge
yield "\r\n";
yield "Hello, this is a test email.\r\n";
yield ".\r\n";
yield "NOOP\r\n";
if ($quit) {
yield "QUIT\r\n";
}
Expand Down

0 comments on commit 64dc421

Please sign in to comment.