[go: up one dir, main page]

Skip to content

Commit

Permalink
fix connect functoin and add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mat-sz committed Oct 3, 2023
1 parent b09b022 commit 2d529df
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
30 changes: 27 additions & 3 deletions __tests__/client/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let server: SMTPServer;
describe('client', () => {
// TODO: TLS tests
// TODO: Error handling tests
// TODO: Auth tests
// TODO: Auth tests
// TODO: Capability tests
// TODO: Greeting tests

Expand All @@ -19,7 +19,7 @@ describe('client', () => {
});

describe('SMTPClient', () => {
it('sends emails', () => {
it('should send a message: events', () => {
const message = {
sender: 'a@localhost',
recipients: ['b@localhost'],
Expand All @@ -40,10 +40,34 @@ describe('client', () => {
});
});
});

it('should send a message: async/await', () => {
const message = {
sender: 'a@localhost',
recipients: ['b@localhost'],
message: 'Test',
};

return new Promise(resolve => {
server.once('connection', connection => {
connection.on('message', msg => {
expect(msg).toMatchObject(message);
resolve(true);
});
});

const fn = async () => {
const client = new SMTPClient({ hostname: HOST, port: PORT });
await client.connect();
client.mail(message);
};
fn();
});
});
});

describe('sendmail', () => {
it('sends emails', () => {
it('should send a message', () => {
const message = {
sender: 'a@localhost',
recipients: ['b@localhost'],
Expand Down
17 changes: 8 additions & 9 deletions src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export class SMTPClient extends EventEmitter {
private wire: Wire;
private capabilities: string[] = [];
private maxSize = defaultSize;
private welcomed = false;

constructor(options: SMTPClientOptions) {
super();
Expand All @@ -62,7 +63,7 @@ export class SMTPClient extends EventEmitter {
}

get connected() {
return this.wire.readable;
return this.wire.readable && this.welcomed;
}

close(): void {
Expand Down Expand Up @@ -120,6 +121,7 @@ export class SMTPClient extends EventEmitter {
}

private async init() {
this.welcomed = false;
const welcome = await this.smtpRead();
if (welcome[0] !== 220) {
throw new Error(
Expand All @@ -142,6 +144,7 @@ export class SMTPClient extends EventEmitter {
await this.smtpSend('HELO');
}

this.welcomed = true;
this.emit('ready');
}

Expand All @@ -160,14 +163,10 @@ export class SMTPClient extends EventEmitter {
}
}

function sendTo(options: SMTPClientOptions, message: SMTPMessage) {
return new Promise((resolve, reject) => {
const client = new SMTPClient(options);
client.once('error', reject);
client.on('ready', () => {
client.mail(message).then(resolve).catch(reject);
});
});
async function sendTo(options: SMTPClientOptions, message: SMTPMessage) {
const client = new SMTPClient(options);
await client.connect();
await client.mail(message);
}

function mx(hostname: string): Promise<string> {
Expand Down

0 comments on commit 2d529df

Please sign in to comment.