@@ -85,6 +85,8 @@ function makeHttpsRequest(options) {
85
85
} ) ;
86
86
}
87
87
88
+ const responseMessage = 'Hello from target server'
89
+
88
90
describe ( 'Redbird Lets Encrypt SSL Certificate Generation' , ( ) => {
89
91
let proxy : Redbird ;
90
92
let targetServer : Server ;
@@ -94,7 +96,7 @@ describe('Redbird Lets Encrypt SSL Certificate Generation', () => {
94
96
// Start a simple HTTP server to act as the backend target
95
97
targetServer = http . createServer ( ( req , res ) => {
96
98
res . writeHead ( 200 , { 'Content-Type' : 'text/plain' } ) ;
97
- res . end ( 'Hello from target server' ) ;
99
+ res . end ( responseMessage ) ;
98
100
} ) ;
99
101
100
102
await new Promise ( ( resolve ) => {
@@ -156,7 +158,7 @@ describe('Redbird Lets Encrypt SSL Certificate Generation', () => {
156
158
157
159
const response = await makeHttpsRequest ( options ) ;
158
160
expect ( response . status ) . toBe ( 200 ) ;
159
- expect ( response . data ) . toBe ( 'Hello from target server' ) ;
161
+ expect ( response . data ) . toBe ( responseMessage ) ;
160
162
} ) ;
161
163
162
164
it ( 'should renew SSL certificates that are halfway to expire' , async ( ) => {
@@ -232,4 +234,58 @@ describe('Redbird Lets Encrypt SSL Certificate Generation', () => {
232
234
// Restore real timers
233
235
vi . useRealTimers ( ) ;
234
236
} ) ;
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
+ } ) ;
235
291
} ) ;
0 commit comments