Skip to content

Commit

Permalink
test: fix lazy let's encrypt tests
Browse files Browse the repository at this point in the history
  • Loading branch information
manast committed Sep 18, 2024
1 parent b4efeba commit 4c139b7
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 114 deletions.
6 changes: 4 additions & 2 deletions lib/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,9 @@ export class Redbird {
}

async close() {
this.proxy.close();
this.agent && this.agent.destroy();

// Clear any renewal timers
if (this.certs) {
Object.keys(this.certs).forEach((domain) => {
Expand All @@ -788,11 +791,10 @@ export class Redbird {
this.letsencryptServer?.close();

await Promise.all(
[this.proxy, this.server, this.httpsServer]
[this.server, this.httpsServer]
.filter((s) => s)
.map((server) => new Promise((resolve) => server.close(resolve)))
);
this.agent && this.agent.destroy();
}

//
Expand Down
60 changes: 58 additions & 2 deletions test/letsencrypt_certificates.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ function makeHttpsRequest(options) {
});
}

const responseMessage = 'Hello from target server'

describe('Redbird Lets Encrypt SSL Certificate Generation', () => {
let proxy: Redbird;
let targetServer: Server;
Expand All @@ -94,7 +96,7 @@ describe('Redbird Lets Encrypt SSL Certificate Generation', () => {
// Start a simple HTTP server to act as the backend target
targetServer = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello from target server');
res.end(responseMessage);
});

await new Promise((resolve) => {
Expand Down Expand Up @@ -156,7 +158,7 @@ describe('Redbird Lets Encrypt SSL Certificate Generation', () => {

const response = await makeHttpsRequest(options);
expect(response.status).toBe(200);
expect(response.data).toBe('Hello from target server');
expect(response.data).toBe(responseMessage);
});

it('should renew SSL certificates that are halfway to expire', async () => {
Expand Down Expand Up @@ -232,4 +234,58 @@ describe('Redbird Lets Encrypt SSL Certificate Generation', () => {
// Restore real timers
vi.useRealTimers();
});

it('should not request certificates immediately for lazy loaded domains', async () => {
// Reset mocks
getCertificatesMock.mockClear();

// Simulate registering a domain with lazy loading enabled
await proxy.register('https://lazy.example.com', `http://localhost:${TEST_PORT}`, {
ssl: {
letsencrypt: {
email: '[email protected]',
production: false,
lazy: true,
},
},
});

// Check that certificates were not requested during registration
expect(getCertificatesMock).not.toHaveBeenCalled();
});

it('should request and cache certificates on first HTTPS request for lazy certificates', async () => {
// Reset mocks
getCertificatesMock.mockClear();

// Make an HTTPS request to trigger lazy loading of certificates
const options = {
hostname: 'localhost',
port: 8443,
path: '/',
method: 'GET',
headers: { Host: 'lazy.example.com' }, // Required for virtual hosts
rejectUnauthorized: false, // Accept self-signed certificates
};

const response = await new Promise<{ statusCode: number; data: string }>((resolve, reject) => {
const req = https.request(options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
resolve({ statusCode: res.statusCode || 0, data });
});
});
req.on('error', reject);
req.end();
});

expect(response.statusCode).toBe(200);
expect(response.data).toBe(responseMessage);

// Ensure that certificates are now loaded
expect(getCertificatesMock).toHaveBeenCalled();
});
});
110 changes: 0 additions & 110 deletions test/letsencrypt_lazy_certificates.spec.ts

This file was deleted.

0 comments on commit 4c139b7

Please sign in to comment.