#!/usr/bin/env node const fetch = require('node-fetch'); // Test configuration const BASE_URL = 'http://localhost:8080'; const ENDPOINT = '/api/v1/idnot/user'; // Base endpoint, idnot will be added as path parameter // Test cases for the rattachements endpoint const testCases = [ { name: 'Valid IDNot parameter', idNot: '12345', expectedStatus: 200, description: 'Should return rattachements data for valid IDNot' }, { name: 'Missing IDNot parameter', idNot: undefined, expectedStatus: 404, // Without idnot in path, this should return 404 description: 'Should handle missing IDNot parameter in path' }, { name: 'Empty IDNot parameter', idNot: '', expectedStatus: 404, // Empty idnot in path should return 404 description: 'Should handle empty IDNot parameter in path' }, { name: 'Special characters in IDNot', idNot: 'test-id_with+special=chars', expectedStatus: 200, description: 'Should handle special characters in IDNot' }, { name: 'Very long IDNot', idNot: 'a'.repeat(100), expectedStatus: 200, description: 'Should handle very long IDNot values' } ]; async function testRattachementsEndpoint(testCase) { console.log(`\n๐Ÿงช Testing: ${testCase.name}`); console.log(`๐Ÿ“ Description: ${testCase.description}`); console.log(`๐Ÿ†” IDNot: ${testCase.idNot || 'undefined'}`); try { // Build URL with path parameter let url = `${BASE_URL}${ENDPOINT}`; if (testCase.idNot !== undefined) { url += `/${encodeURIComponent(testCase.idNot)}/rattachements`; } else { url += '/rattachements'; // Test without idnot parameter } console.log(`๐Ÿ“ URL: ${url}`); const response = await fetch(url, { method: 'GET', headers: { 'Accept': 'application/json' } }); const responseText = await response.text(); let responseData; try { responseData = JSON.parse(responseText); } catch (e) { responseData = { rawResponse: responseText }; } console.log(`๐Ÿ“Š Status: ${response.status} ${response.statusText}`); if (response.status === testCase.expectedStatus) { console.log(`โœ… PASS: Expected status ${testCase.expectedStatus}`); } else { console.log(`โŒ FAIL: Expected status ${testCase.expectedStatus}, got ${response.status}`); } // Analyze the response if (responseData.error) { console.log(`๐Ÿšจ Error: ${responseData.error}`); if (responseData.message) { console.log(`๐Ÿ“„ Message: ${responseData.message}`); } } else if (Array.isArray(responseData)) { console.log(`๐Ÿ“‹ Response: Array with ${responseData.length} items`); if (responseData.length > 0) { console.log(`๐Ÿ” First item keys: ${Object.keys(responseData[0]).join(', ')}`); } } else if (typeof responseData === 'object') { console.log(`๐Ÿ“‹ Response: Object with keys: ${Object.keys(responseData).join(', ')}`); if (responseData.result) { console.log(`๐Ÿ” Result type: ${Array.isArray(responseData.result) ? 'Array' : typeof responseData.result}`); } } else { console.log(`๐Ÿ“‹ Response type: ${typeof responseData}`); console.log(`๐Ÿ“„ Content: ${responseText.substring(0, 200)}${responseText.length > 200 ? '...' : ''}`); } return { testCase, status: response.status, expectedStatus: testCase.expectedStatus, passed: response.status === testCase.expectedStatus, response: responseData, url: url }; } catch (error) { console.log(`๐Ÿ’ฅ Network Error: ${error.message}`); return { testCase, status: 'NETWORK_ERROR', expectedStatus: testCase.expectedStatus, passed: false, error: error.message, url: url }; } } async function runTests() { console.log('๐Ÿš€ Starting Rattachements Endpoint Tests...\n'); console.log(`๐Ÿ“ Testing against: ${BASE_URL}${ENDPOINT}`); console.log('=' .repeat(70)); const results = []; for (const testCase of testCases) { const result = await testRattachementsEndpoint(testCase); results.push(result); // Add a small delay between tests await new Promise(resolve => setTimeout(resolve, 100)); } // Summary console.log('\n' + '=' .repeat(70)); console.log('๐Ÿ“Š TEST SUMMARY'); console.log('=' .repeat(70)); const passed = results.filter(r => r.passed).length; const total = results.length; console.log(`โœ… Passed: ${passed}/${total}`); console.log(`โŒ Failed: ${total - passed}/${total}`); if (passed === total) { console.log('๐ŸŽ‰ All tests passed!'); } else { console.log('โš ๏ธ Some tests failed. Check the output above for details.'); } // Failed tests details const failedTests = results.filter(r => !r.passed); if (failedTests.length > 0) { console.log('\n๐Ÿ” FAILED TESTS:'); failedTests.forEach(result => { console.log(` - ${result.testCase.name}: Expected ${result.testCase.expectedStatus}, got ${result.status}`); }); } } // Check if server is running async function checkServerHealth() { try { const response = await fetch(`${BASE_URL}/api/v1/health`); if (response.ok) { console.log('โœ… Server is running and responding'); return true; } else { console.log(`โš ๏ธ Server responded with status: ${response.status}`); return false; } } catch (error) { console.log('โŒ Server is not responding'); console.log('๐Ÿ’ก Make sure to start your server first with: npm run dev'); return false; } } // Main execution async function main() { console.log('๐Ÿ” Checking server health...'); const serverRunning = await checkServerHealth(); if (!serverRunning) { console.log('โŒ Server health check failed. Exiting.'); process.exit(1); } console.log('๐Ÿš€ Starting tests...'); await runTests(); console.log('๐Ÿ Tests completed.'); } // Handle command line arguments if (process.argv.includes('--help') || process.argv.includes('-h')) { console.log(` Usage: node test-rattachements-endpoint.js [options] Options: --help, -h Show this help message --url Set custom base URL (default: http://localhost:8080) Examples: node test-rattachements-endpoint.js node test-rattachements-endpoint.js --url http://localhost:3000 `); process.exit(0); } // Parse custom URL if provided const urlIndex = process.argv.indexOf('--url'); if (urlIndex !== -1 && process.argv[urlIndex + 1]) { BASE_URL = process.argv[urlIndex + 1]; console.log(`๐Ÿ”ง Using custom URL: ${BASE_URL}`); } main().catch(console.error);