diff --git a/src/routes.js b/src/routes.js index bd758c6..d9c26c2 100644 --- a/src/routes.js +++ b/src/routes.js @@ -64,7 +64,8 @@ router.get('/calendar/:name', async (req, res) => { } const icsFilePath = path.join(MERGED_CALENDARS_DIR, `${calendarName}.ics`); - + const fullUrl = `${req.protocol}://${req.get('host')}${req.originalUrl}`; + try { // Check if the .ics file exists console.log(`Serving calendar for: ${calendarName}`); @@ -78,7 +79,11 @@ router.get('/calendar/:name', async (req, res) => { } res.setHeader('Content-Type', 'text/calendar'); res.sendFile(icsFilePath); + + // Log the successful request with URL format and status code + console.log(`Serving calendar ${calendarName} for ${fullUrl}: 200`); } else { + console.log(`Calendar not found: ${calendarName} for ${fullUrl}: 404`); res.status(404).json({ error: 'Calendar not found.' }); } } catch (error) { diff --git a/test/calendar.test.js b/test/calendar.test.js index 79c48a2..7d35ec2 100644 --- a/test/calendar.test.js +++ b/test/calendar.test.js @@ -280,4 +280,58 @@ describe('Calendar Merging API', () => { expect(actualOutput).toBe(expectedOutput); }); + // Test accessing calendar with and without .ics extension + test('Access calendar with and without .ics extension', async () => { + // Create a test calendar + const response = await request(server) + .post('/merge') + .send({ + linkGroupName: 'Extension Test Calendar', + calendars: [ + { + url: getTestCalendarFilename('sf_public_holidays.ics'), + prefix: 'Test', + override: false, + }, + ], + }); + + expect(response.status).toBe(200); + + // Check if the file was created + const filePath = path.join(CALENDARS_DIR, 'Extension_Test_Calendar.ics'); + expect(fs.existsSync(filePath)).toBe(true); + + // Test accessing without .ics extension + const responseWithoutExtension = await request(server) + .get('/calendar/Extension_Test_Calendar'); + + expect(responseWithoutExtension.status).toBe(200); + expect(responseWithoutExtension.headers['content-type']).toMatch(/text\/calendar/); + + // Test accessing with .ics extension + const responseWithExtension = await request(server) + .get('/calendar/Extension_Test_Calendar.ics'); + + expect(responseWithExtension.status).toBe(200); + expect(responseWithExtension.headers['content-type']).toMatch(/text\/calendar/); + + // Verify both responses contain the same content + expect(responseWithoutExtension.text).toBe(responseWithExtension.text); + }); + + // Test 404 response for non-existent calendar + test('Return 404 for non-existent calendar with and without .ics extension', async () => { + // Test accessing non-existent calendar without .ics extension + const responseWithoutExtension = await request(server) + .get('/calendar/NonExistentCalendar'); + + expect(responseWithoutExtension.status).toBe(404); + + // Test accessing non-existent calendar with .ics extension + const responseWithExtension = await request(server) + .get('/calendar/NonExistentCalendar.ics'); + + expect(responseWithExtension.status).toBe(404); + }); });