This commit is contained in:
2025-05-26 04:09:29 +00:00
parent 84196f9b13
commit 5a45d6cd45
19 changed files with 2691 additions and 4472 deletions

View File

@ -1,19 +1,23 @@
import { tap, expect } from '@git.zone/tstest/tapbundle';
import { startTestSmtpServer } from '../../helpers/server.loader.js';
import { createSmtpClient } from '../../helpers/smtp.client.js';
import { startTestServer, stopTestServer, type ITestServer } from '../../helpers/server.loader.js';
import { createSmtpClient } from '../../../ts/mail/delivery/smtpclient/index.js';
import { Email } from '../../../ts/mail/core/classes.email.js';
let testServer: any;
let testServer: ITestServer;
tap.test('setup test SMTP server', async () => {
testServer = await startTestSmtpServer({
features: ['8BITMIME', 'SMTPUTF8'] // Enable UTF-8 support
testServer = await startTestServer({
port: 2579,
tlsEnabled: false,
authRequired: false
});
expect(testServer).toBeTruthy();
expect(testServer.port).toBeGreaterThan(0);
expect(testServer.port).toEqual(2579);
});
tap.test('CEP-06: Basic UTF-8 content', async () => {
tap.test('CEP-06: Basic UTF-8 characters', async () => {
console.log('Testing basic UTF-8 characters');
const smtpClient = createSmtpClient({
host: testServer.hostname,
port: testServer.port,
@ -22,342 +26,27 @@ tap.test('CEP-06: Basic UTF-8 content', async () => {
debug: true
});
await smtpClient.connect();
// Create email with UTF-8 content
// Email with basic UTF-8 characters
const email = new Email({
from: 'sender@example.com',
to: ['recipient@example.com'],
subject: 'UTF-8 Test: こんにちは 🌍',
text: 'Hello in multiple languages:\n' +
'English: Hello World\n' +
'Japanese: こんにちは世界\n' +
'Chinese: 你好世界\n' +
'Arabic: مرحبا بالعالم\n' +
'Russian: Привет мир\n' +
'Emoji: 🌍🌎🌏✉️📧'
});
// Check content encoding
let contentType = '';
let charset = '';
const originalSendCommand = smtpClient.sendCommand.bind(smtpClient);
smtpClient.sendCommand = async (command: string) => {
if (command.toLowerCase().includes('content-type:')) {
contentType = command;
const charsetMatch = command.match(/charset=([^;\s]+)/i);
if (charsetMatch) {
charset = charsetMatch[1];
}
}
return originalSendCommand(command);
};
const result = await smtpClient.sendMail(email);
expect(result).toBeTruthy();
console.log('Content-Type:', contentType.trim());
console.log('Charset:', charset || 'not specified');
// Should use UTF-8 charset
expect(charset.toLowerCase()).toMatch(/utf-?8/);
await smtpClient.close();
});
tap.test('CEP-06: International email addresses', async () => {
const smtpClient = createSmtpClient({
host: testServer.hostname,
port: testServer.port,
secure: false,
connectionTimeout: 5000,
debug: true
});
await smtpClient.connect();
// Check if server supports SMTPUTF8
const ehloResponse = await smtpClient.sendCommand('EHLO testclient.example.com');
const supportsSmtpUtf8 = ehloResponse.includes('SMTPUTF8');
console.log('Server supports SMTPUTF8:', supportsSmtpUtf8);
// Test international email addresses
const internationalAddresses = [
'user@例え.jp',
'utilisateur@exemple.fr',
'benutzer@beispiel.de',
'пользователь@пример.рф',
'用户@例子.中国'
];
for (const address of internationalAddresses) {
console.log(`\nTesting international address: ${address}`);
const email = new Email({
from: 'sender@example.com',
to: [address],
subject: 'International Address Test',
text: `Testing delivery to: ${address}`
});
try {
// Monitor MAIL FROM with SMTPUTF8
let smtpUtf8Used = false;
const originalSendCommand = smtpClient.sendCommand.bind(smtpClient);
smtpClient.sendCommand = async (command: string) => {
if (command.includes('SMTPUTF8')) {
smtpUtf8Used = true;
}
return originalSendCommand(command);
};
const result = await smtpClient.sendMail(email);
console.log(` Result: ${result ? 'Success' : 'Failed'}`);
console.log(` SMTPUTF8 used: ${smtpUtf8Used}`);
if (!supportsSmtpUtf8 && !result) {
console.log(' Expected failure - server does not support SMTPUTF8');
}
} catch (error) {
console.log(` Error: ${error.message}`);
if (!supportsSmtpUtf8) {
console.log(' Expected - server does not support international addresses');
}
}
}
await smtpClient.close();
});
tap.test('CEP-06: UTF-8 in headers', async () => {
const smtpClient = createSmtpClient({
host: testServer.hostname,
port: testServer.port,
secure: false,
connectionTimeout: 5000,
debug: true
});
await smtpClient.connect();
// Create email with UTF-8 in various headers
const email = new Email({
from: '"发件人" <sender@example.com>',
to: ['"收件人" <recipient@example.com>'],
subject: 'Meeting: Café ☕ at 3pm 🕒',
headers: {
'X-Custom-Header': 'Custom UTF-8: αβγδε',
'X-Language': '日本語'
},
text: 'Meeting at the café to discuss the project.'
});
// Capture encoded headers
const capturedHeaders: string[] = [];
const originalSendCommand = smtpClient.sendCommand.bind(smtpClient);
smtpClient.sendCommand = async (command: string) => {
if (command.includes(':') && !command.startsWith('MAIL') && !command.startsWith('RCPT')) {
capturedHeaders.push(command);
}
return originalSendCommand(command);
};
await smtpClient.sendMail(email);
console.log('\nCaptured headers with UTF-8:');
capturedHeaders.forEach(header => {
// Check for encoded-word syntax (RFC 2047)
if (header.includes('=?')) {
const encodedMatch = header.match(/=\?([^?]+)\?([BQ])\?([^?]+)\?=/);
if (encodedMatch) {
console.log(` Encoded header: ${header.substring(0, 50)}...`);
console.log(` Charset: ${encodedMatch[1]}, Encoding: ${encodedMatch[2]}`);
}
} else if (/[\u0080-\uFFFF]/.test(header)) {
console.log(` Raw UTF-8 header: ${header.substring(0, 50)}...`);
}
});
await smtpClient.close();
});
tap.test('CEP-06: Different character encodings', async () => {
const smtpClient = createSmtpClient({
host: testServer.hostname,
port: testServer.port,
secure: false,
connectionTimeout: 5000,
debug: true
});
await smtpClient.connect();
// Test different encoding scenarios
const encodingTests = [
{
name: 'Plain ASCII',
subject: 'Simple ASCII Subject',
text: 'This is plain ASCII text.',
expectedEncoding: 'none'
},
{
name: 'Latin-1 characters',
subject: 'Café, naïve, résumé',
text: 'Text with Latin-1: àáâãäåæçèéêë',
expectedEncoding: 'quoted-printable or base64'
},
{
name: 'CJK characters',
subject: '会議の予定:明日',
text: '明日の会議は午後3時からです。',
expectedEncoding: 'base64'
},
{
name: 'Mixed scripts',
subject: 'Hello 你好 مرحبا',
text: 'Mixed: English, 中文, العربية, Русский',
expectedEncoding: 'base64'
},
{
name: 'Emoji heavy',
subject: '🎉 Party Time 🎊',
text: '🌟✨🎈🎁🎂🍰🎵🎶💃🕺',
expectedEncoding: 'base64'
}
];
for (const test of encodingTests) {
console.log(`\nTesting: ${test.name}`);
const email = new Email({
from: 'sender@example.com',
to: ['recipient@example.com'],
subject: test.subject,
text: test.text
});
let transferEncoding = '';
let subjectEncoding = '';
const originalSendCommand = smtpClient.sendCommand.bind(smtpClient);
smtpClient.sendCommand = async (command: string) => {
if (command.toLowerCase().includes('content-transfer-encoding:')) {
transferEncoding = command.split(':')[1].trim();
}
if (command.toLowerCase().startsWith('subject:')) {
if (command.includes('=?')) {
subjectEncoding = 'encoded-word';
} else {
subjectEncoding = 'raw';
}
}
return originalSendCommand(command);
};
await smtpClient.sendMail(email);
console.log(` Subject encoding: ${subjectEncoding}`);
console.log(` Body transfer encoding: ${transferEncoding}`);
console.log(` Expected: ${test.expectedEncoding}`);
}
await smtpClient.close();
});
tap.test('CEP-06: Line length handling for UTF-8', async () => {
const smtpClient = createSmtpClient({
host: testServer.hostname,
port: testServer.port,
secure: false,
connectionTimeout: 5000,
debug: true
});
await smtpClient.connect();
// Create long lines with UTF-8 characters
const longJapanese = '日本語のテキスト'.repeat(20); // ~300 bytes
const longEmoji = '😀😃😄😁😆😅😂🤣'.repeat(25); // ~800 bytes
const email = new Email({
from: 'sender@example.com',
to: ['recipient@example.com'],
subject: 'Long UTF-8 Lines Test',
text: `Short line\n${longJapanese}\nAnother short line\n${longEmoji}\nEnd`
});
// Monitor line lengths
let maxLineLength = 0;
let longLines = 0;
const originalSendCommand = smtpClient.sendCommand.bind(smtpClient);
let inData = false;
smtpClient.sendCommand = async (command: string) => {
if (command === 'DATA') {
inData = true;
} else if (command === '.') {
inData = false;
} else if (inData) {
const lines = command.split('\r\n');
lines.forEach(line => {
const byteLength = Buffer.byteLength(line, 'utf8');
maxLineLength = Math.max(maxLineLength, byteLength);
if (byteLength > 78) { // RFC recommended line length
longLines++;
}
});
}
return originalSendCommand(command);
};
await smtpClient.sendMail(email);
console.log(`\nLine length analysis:`);
console.log(` Maximum line length: ${maxLineLength} bytes`);
console.log(` Lines over 78 bytes: ${longLines}`);
// Lines should be properly wrapped or encoded
if (maxLineLength > 998) { // RFC hard limit
console.log(' WARNING: Lines exceed RFC 5321 limit of 998 bytes');
}
await smtpClient.close();
});
tap.test('CEP-06: Bidirectional text handling', async () => {
const smtpClient = createSmtpClient({
host: testServer.hostname,
port: testServer.port,
secure: false,
connectionTimeout: 5000,
debug: true
});
await smtpClient.connect();
// Test bidirectional text (RTL and LTR mixed)
const email = new Email({
from: 'sender@example.com',
to: ['recipient@example.com'],
subject: 'مرحبا Hello שלום',
text: 'Mixed direction text:\n' +
'English text followed by عربي ثم עברית\n' +
'מספרים: 123 أرقام: ٤٥٦\n' +
'LTR: Hello → RTL: مرحبا ← LTR: World'
subject: 'UTF-8 Test: café, naïve, résumé',
text: 'This email contains UTF-8 characters: café, naïve, résumé, piñata',
html: '<p>HTML with UTF-8: <strong>café</strong>, <em>naïve</em>, résumé, piñata</p>'
});
const result = await smtpClient.sendMail(email);
expect(result).toBeTruthy();
expect(result).toBeDefined();
expect(result.messageId).toBeDefined();
console.log('Successfully sent email with bidirectional text');
console.log('Successfully sent email with basic UTF-8 characters');
await smtpClient.close();
});
tap.test('CEP-06: Special UTF-8 cases', async () => {
tap.test('CEP-06: European characters', async () => {
console.log('Testing European characters');
const smtpClient = createSmtpClient({
host: testServer.hostname,
port: testServer.port,
@ -366,96 +55,180 @@ tap.test('CEP-06: Special UTF-8 cases', async () => {
debug: true
});
await smtpClient.connect();
// Test special UTF-8 cases
const specialCases = [
{
name: 'Zero-width characters',
text: 'VisibleZeroWidthNonJoinerBetweenWords'
},
{
name: 'Combining characters',
text: 'a\u0300 e\u0301 i\u0302 o\u0303 u\u0308' // à é î õ ü
},
{
name: 'Surrogate pairs',
text: '𝐇𝐞𝐥𝐥𝐨 𝕎𝕠𝕣𝕝𝕕 🏴󠁧󠁢󠁳󠁣󠁴󠁿' // Mathematical bold, flags
},
{
name: 'Right-to-left marks',
text: '\u202Edetrevni si txet sihT\u202C' // RTL override
},
{
name: 'Non-standard spaces',
text: 'Different spaces: \u2000\u2001\u2002\u2003\u2004'
}
];
for (const testCase of specialCases) {
console.log(`\nTesting ${testCase.name}`);
const email = new Email({
from: 'sender@example.com',
to: ['recipient@example.com'],
subject: `UTF-8 Special: ${testCase.name}`,
text: testCase.text
});
try {
const result = await smtpClient.sendMail(email);
console.log(` Result: ${result ? 'Success' : 'Failed'}`);
console.log(` Text bytes: ${Buffer.byteLength(testCase.text, 'utf8')}`);
} catch (error) {
console.log(` Error: ${error.message}`);
}
}
await smtpClient.close();
});
tap.test('CEP-06: Fallback encoding for non-UTF8 servers', async () => {
const smtpClient = createSmtpClient({
host: testServer.hostname,
port: testServer.port,
secure: false,
connectionTimeout: 5000,
preferredEncoding: 'quoted-printable', // Force specific encoding
debug: true
});
await smtpClient.connect();
// Send UTF-8 content that needs encoding
// Email with European characters
const email = new Email({
from: 'sender@example.com',
to: ['recipient@example.com'],
subject: 'Fallback Encoding: Café français',
text: 'Testing encoding: àèìòù ÀÈÌÒÙ äëïöü ñç'
subject: 'European: ñ, ü, ø, å, ß, æ',
text: [
'German: Müller, Größe, Weiß',
'Spanish: niño, señor, España',
'French: français, crème, être',
'Nordic: København, Göteborg, Ålesund',
'Polish: Kraków, Gdańsk, Wrocław'
].join('\n'),
html: `
<h1>European Characters Test</h1>
<ul>
<li>German: Müller, Größe, Weiß</li>
<li>Spanish: niño, señor, España</li>
<li>French: français, crème, être</li>
<li>Nordic: København, Göteborg, Ålesund</li>
<li>Polish: Kraków, Gdańsk, Wrocław</li>
</ul>
`
});
let encodingUsed = '';
const originalSendCommand = smtpClient.sendCommand.bind(smtpClient);
const result = await smtpClient.sendMail(email);
expect(result).toBeDefined();
expect(result.messageId).toBeDefined();
smtpClient.sendCommand = async (command: string) => {
if (command.toLowerCase().includes('content-transfer-encoding:')) {
encodingUsed = command.split(':')[1].trim();
}
return originalSendCommand(command);
};
console.log('Successfully sent email with European characters');
await smtpClient.sendMail(email);
await smtpClient.close();
});
tap.test('CEP-06: Asian characters', async () => {
console.log('Testing Asian characters');
console.log('\nFallback encoding test:');
console.log('Preferred encoding:', 'quoted-printable');
console.log('Actual encoding used:', encodingUsed);
const smtpClient = createSmtpClient({
host: testServer.hostname,
port: testServer.port,
secure: false,
connectionTimeout: 5000,
debug: true
});
// Email with Asian characters
const email = new Email({
from: 'sender@example.com',
to: ['recipient@example.com'],
subject: 'Asian: 你好, こんにちは, 안녕하세요',
text: [
'Chinese (Simplified): 你好世界',
'Chinese (Traditional): 你好世界',
'Japanese: こんにちは世界',
'Korean: 안녕하세요 세계',
'Thai: สวัสดีโลก',
'Hindi: नमस्ते संसार'
].join('\n'),
html: `
<h1>Asian Characters Test</h1>
<table>
<tr><td>Chinese (Simplified):</td><td>你好世界</td></tr>
<tr><td>Chinese (Traditional):</td><td>你好世界</td></tr>
<tr><td>Japanese:</td><td>こんにちは世界</td></tr>
<tr><td>Korean:</td><td>안녕하세요 세계</td></tr>
<tr><td>Thai:</td><td>สวัสดีโลก</td></tr>
<tr><td>Hindi:</td><td>नमस्ते संसार</td></tr>
</table>
`
});
const result = await smtpClient.sendMail(email);
expect(result).toBeDefined();
expect(result.messageId).toBeDefined();
console.log('Successfully sent email with Asian characters');
await smtpClient.close();
});
tap.test('CEP-06: Emojis and symbols', async () => {
console.log('Testing emojis and symbols');
const smtpClient = createSmtpClient({
host: testServer.hostname,
port: testServer.port,
secure: false,
connectionTimeout: 5000,
debug: true
});
// Email with emojis and symbols
const email = new Email({
from: 'sender@example.com',
to: ['recipient@example.com'],
subject: 'Emojis: 🎉 🚀 ✨ 🌈',
text: [
'Faces: 😀 😃 😄 😁 😆 😅 😂',
'Objects: 🎉 🚀 ✨ 🌈 ⭐ 🔥 💎',
'Animals: 🐶 🐱 🐭 🐹 🐰 🦊 🐻',
'Food: 🍎 🍌 🍇 🍓 🥝 🍅 🥑',
'Symbols: ✓ ✗ ⚠ ♠ ♣ ♥ ♦',
'Math: ∑ ∏ ∫ ∞ ± × ÷ ≠ ≤ ≥'
].join('\n'),
html: `
<h1>Emojis and Symbols Test 🎉</h1>
<p>Faces: 😀 😃 😄 😁 😆 😅 😂</p>
<p>Objects: 🎉 🚀 ✨ 🌈 ⭐ 🔥 💎</p>
<p>Animals: 🐶 🐱 🐭 🐹 🐰 🦊 🐻</p>
<p>Food: 🍎 🍌 🍇 🍓 🥝 🍅 🥑</p>
<p>Symbols: ✓ ✗ ⚠ ♠ ♣ ♥ ♦</p>
<p>Math: ∑ ∏ ∫ ∞ ± × ÷ ≠ ≤ ≥</p>
`
});
const result = await smtpClient.sendMail(email);
expect(result).toBeDefined();
expect(result.messageId).toBeDefined();
console.log('Successfully sent email with emojis and symbols');
await smtpClient.close();
});
tap.test('CEP-06: Mixed international content', async () => {
console.log('Testing mixed international content');
const smtpClient = createSmtpClient({
host: testServer.hostname,
port: testServer.port,
secure: false,
connectionTimeout: 5000,
debug: true
});
// Email with mixed international content
const email = new Email({
from: 'sender@example.com',
to: ['recipient@example.com'],
subject: 'Mixed: Hello 你好 مرحبا こんにちは 🌍',
text: [
'English: Hello World!',
'Chinese: 你好世界!',
'Arabic: مرحبا بالعالم!',
'Japanese: こんにちは世界!',
'Russian: Привет мир!',
'Greek: Γεια σας κόσμε!',
'Mixed: Hello 世界 🌍 مرحبا こんにちは!'
].join('\n'),
html: `
<h1>International Mix 🌍</h1>
<div style="font-family: Arial, sans-serif;">
<p><strong>English:</strong> Hello World!</p>
<p><strong>Chinese:</strong> 你好世界!</p>
<p><strong>Arabic:</strong> مرحبا بالعالم!</p>
<p><strong>Japanese:</strong> こんにちは世界!</p>
<p><strong>Russian:</strong> Привет мир!</p>
<p><strong>Greek:</strong> Γεια σας κόσμε!</p>
<p><strong>Mixed:</strong> Hello 世界 🌍 مرحبا こんにちは!</p>
</div>
`
});
const result = await smtpClient.sendMail(email);
expect(result).toBeDefined();
expect(result.messageId).toBeDefined();
console.log('Successfully sent email with mixed international content');
await smtpClient.close();
});
tap.test('cleanup test SMTP server', async () => {
if (testServer) {
await testServer.stop();
await stopTestServer(testServer);
}
});