Skip to content

Commit 4c139b7

Browse files
committed
test: fix lazy let's encrypt tests
1 parent b4efeba commit 4c139b7

3 files changed

+62
-114
lines changed

lib/proxy.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,9 @@ export class Redbird {
774774
}
775775

776776
async close() {
777+
this.proxy.close();
778+
this.agent && this.agent.destroy();
779+
777780
// Clear any renewal timers
778781
if (this.certs) {
779782
Object.keys(this.certs).forEach((domain) => {
@@ -788,11 +791,10 @@ export class Redbird {
788791
this.letsencryptServer?.close();
789792

790793
await Promise.all(
791-
[this.proxy, this.server, this.httpsServer]
794+
[this.server, this.httpsServer]
792795
.filter((s) => s)
793796
.map((server) => new Promise((resolve) => server.close(resolve)))
794797
);
795-
this.agent && this.agent.destroy();
796798
}
797799

798800
//

test/letsencrypt_certificates.spec.ts

+58-2
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ function makeHttpsRequest(options) {
8585
});
8686
}
8787

88+
const responseMessage = 'Hello from target server'
89+
8890
describe('Redbird Lets Encrypt SSL Certificate Generation', () => {
8991
let proxy: Redbird;
9092
let targetServer: Server;
@@ -94,7 +96,7 @@ describe('Redbird Lets Encrypt SSL Certificate Generation', () => {
9496
// Start a simple HTTP server to act as the backend target
9597
targetServer = http.createServer((req, res) => {
9698
res.writeHead(200, { 'Content-Type': 'text/plain' });
97-
res.end('Hello from target server');
99+
res.end(responseMessage);
98100
});
99101

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

157159
const response = await makeHttpsRequest(options);
158160
expect(response.status).toBe(200);
159-
expect(response.data).toBe('Hello from target server');
161+
expect(response.data).toBe(responseMessage);
160162
});
161163

162164
it('should renew SSL certificates that are halfway to expire', async () => {
@@ -232,4 +234,58 @@ describe('Redbird Lets Encrypt SSL Certificate Generation', () => {
232234
// Restore real timers
233235
vi.useRealTimers();
234236
});
237+
238+
it('should not request certificates immediately for lazy loaded domains', async () => {
239+
// Reset mocks
240+
getCertificatesMock.mockClear();
241+
242+
// Simulate registering a domain with lazy loading enabled
243+
await proxy.register('https://lazy.example.com', `http://localhost:${TEST_PORT}`, {
244+
ssl: {
245+
letsencrypt: {
246+
247+
production: false,
248+
lazy: true,
249+
},
250+
},
251+
});
252+
253+
// Check that certificates were not requested during registration
254+
expect(getCertificatesMock).not.toHaveBeenCalled();
255+
});
256+
257+
it('should request and cache certificates on first HTTPS request for lazy certificates', async () => {
258+
// Reset mocks
259+
getCertificatesMock.mockClear();
260+
261+
// Make an HTTPS request to trigger lazy loading of certificates
262+
const options = {
263+
hostname: 'localhost',
264+
port: 8443,
265+
path: '/',
266+
method: 'GET',
267+
headers: { Host: 'lazy.example.com' }, // Required for virtual hosts
268+
rejectUnauthorized: false, // Accept self-signed certificates
269+
};
270+
271+
const response = await new Promise<{ statusCode: number; data: string }>((resolve, reject) => {
272+
const req = https.request(options, (res) => {
273+
let data = '';
274+
res.on('data', (chunk) => {
275+
data += chunk;
276+
});
277+
res.on('end', () => {
278+
resolve({ statusCode: res.statusCode || 0, data });
279+
});
280+
});
281+
req.on('error', reject);
282+
req.end();
283+
});
284+
285+
expect(response.statusCode).toBe(200);
286+
expect(response.data).toBe(responseMessage);
287+
288+
// Ensure that certificates are now loaded
289+
expect(getCertificatesMock).toHaveBeenCalled();
290+
});
235291
});

test/letsencrypt_lazy_certificates.spec.ts

-110
This file was deleted.

0 commit comments

Comments
 (0)