/** * Test script for running Hastebin locally with file storage * No need for Redis/KeyDB for local testing */ // Set environment variables for testing process.env.NODE_ENV = process.env.NODE_ENV || 'test'; process.env.HASTEBIN_STORAGE_TYPE = 'file'; process.env.HASTEBIN_PORT = '7777'; process.env.HASTEBIN_HOST = 'localhost'; process.env.HASTEBIN_STORAGE_PATH = './test-data'; // Security settings - these should NOT have defaults to allow tests to control them // Only set them if they're not already set by the test if (process.env.HASTEBIN_ENABLE_CSP === undefined) { process.env.HASTEBIN_ENABLE_CSP = 'true'; } if (process.env.HASTEBIN_ENABLE_HSTS === undefined) { process.env.HASTEBIN_ENABLE_HSTS = 'true'; } if (process.env.HASTEBIN_ENABLE_CROSS_ORIGIN_ISOLATION === undefined) { process.env.HASTEBIN_ENABLE_CROSS_ORIGIN_ISOLATION = 'true'; } if (process.env.HASTEBIN_BYPASS_CSP_IN_DEV === undefined) { process.env.HASTEBIN_BYPASS_CSP_IN_DEV = 'false'; } if (process.env.HASTEBIN_ALLOW_UNSAFE_HASHES === undefined) { process.env.HASTEBIN_ALLOW_UNSAFE_HASHES = 'true'; } // Create test data directory if it doesn't exist const fs = require('fs'); const path = require('path'); // Use absolute paths to avoid any path resolution issues const rootDir = path.resolve(__dirname, '../..'); const testDataDir = path.join(rootDir, 'test-data'); function cleanTestData() { if (fs.existsSync(testDataDir)) { try { const files = fs.readdirSync(testDataDir); for (const file of files) { fs.unlinkSync(path.join(testDataDir, file)); } } catch (err) { console.error('Error cleaning test data:', err); } } else { try { fs.mkdirSync(testDataDir, { recursive: true }); } catch (err) { console.error('Error creating test data directory:', err); } } } // Clean up test data before starting cleanTestData(); // Change working directory to root to ensure proper path resolution process.chdir(rootDir); // Log environment variables for debugging console.log('Starting server with environment:'); console.log('- NODE_ENV:', process.env.NODE_ENV); console.log('- HASTEBIN_ENABLE_CSP:', process.env.HASTEBIN_ENABLE_CSP); console.log('- HASTEBIN_ENABLE_HSTS:', process.env.HASTEBIN_ENABLE_HSTS); console.log('- HASTEBIN_ENABLE_CROSS_ORIGIN_ISOLATION:', process.env.HASTEBIN_ENABLE_CROSS_ORIGIN_ISOLATION); console.log('- HASTEBIN_BYPASS_CSP_IN_DEV:', process.env.HASTEBIN_BYPASS_CSP_IN_DEV); // Run the server let server; try { server = require(path.join(rootDir, 'server.js')); console.log('Test server running on http://localhost:7777'); } catch (err) { console.error('Error starting server:', err); process.exit(1); } // Handle cleanup function cleanup(exitProcess = false) { return new Promise((resolve) => { console.log('Shutting down test server...'); if (server && typeof server.close === 'function') { server.close(() => { cleanTestData(); if (exitProcess) { process.exit(0); } resolve(); }); } else { cleanTestData(); if (exitProcess) { process.exit(0); } resolve(); } }); } // Handle various exit scenarios process.on('SIGINT', () => cleanup(true)); process.on('SIGTERM', () => cleanup(true)); process.on('exit', () => { try { if (server && typeof server.close === 'function') { server.close(); } } catch (err) { console.error('Error during cleanup:', err); } }); process.on('uncaughtException', (err) => { console.error('Uncaught Exception:', err); cleanup(true); }); module.exports = { server, cleanup, cleanTestData };