138 lines
3.5 KiB
JavaScript
138 lines
3.5 KiB
JavaScript
// API 테스트 스크립트
|
|
const http = require('http');
|
|
|
|
const BASE_URL = 'http://localhost:9771';
|
|
|
|
// HTTP 요청 헬퍼 함수
|
|
function makeRequest(options, data = null) {
|
|
return new Promise((resolve, reject) => {
|
|
const req = http.request(options, (res) => {
|
|
let body = '';
|
|
res.on('data', (chunk) => {
|
|
body += chunk;
|
|
});
|
|
res.on('end', () => {
|
|
try {
|
|
const parsedBody = JSON.parse(body);
|
|
resolve({
|
|
statusCode: res.statusCode,
|
|
body: parsedBody
|
|
});
|
|
} catch (e) {
|
|
resolve({
|
|
statusCode: res.statusCode,
|
|
body: body
|
|
});
|
|
}
|
|
});
|
|
});
|
|
|
|
req.on('error', (err) => {
|
|
reject(err);
|
|
});
|
|
|
|
if (data) {
|
|
req.write(JSON.stringify(data));
|
|
}
|
|
req.end();
|
|
});
|
|
}
|
|
|
|
// 테스트 함수들
|
|
async function testHealthCheck() {
|
|
console.log('🔍 헬스 체크 테스트...');
|
|
try {
|
|
const response = await makeRequest({
|
|
hostname: 'localhost',
|
|
port: 9771,
|
|
path: '/api/health',
|
|
method: 'GET'
|
|
});
|
|
|
|
console.log(`✅ 상태 코드: ${response.statusCode}`);
|
|
console.log(`📄 응답:`, response.body);
|
|
return response.statusCode === 200;
|
|
} catch (error) {
|
|
console.log('❌ 헬스 체크 실패:', error.message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
async function testCreateData() {
|
|
console.log('\n📝 데이터 생성 테스트...');
|
|
try {
|
|
const testData = {
|
|
name: '테스트 데이터',
|
|
description: 'API 테스트용 데이터입니다',
|
|
dataValue: '{"test": true, "timestamp": "' + new Date().toISOString() + '"}'
|
|
};
|
|
|
|
const response = await makeRequest({
|
|
hostname: 'localhost',
|
|
port: 9771,
|
|
path: '/api/data',
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
}
|
|
}, testData);
|
|
|
|
console.log(`✅ 상태 코드: ${response.statusCode}`);
|
|
console.log(`📄 응답:`, response.body);
|
|
return response.statusCode === 201;
|
|
} catch (error) {
|
|
console.log('❌ 데이터 생성 실패:', error.message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
async function testGetAllData() {
|
|
console.log('\n📋 모든 데이터 조회 테스트...');
|
|
try {
|
|
const response = await makeRequest({
|
|
hostname: 'localhost',
|
|
port: 9771,
|
|
path: '/api/data',
|
|
method: 'GET'
|
|
});
|
|
|
|
console.log(`✅ 상태 코드: ${response.statusCode}`);
|
|
console.log(`📄 응답:`, response.body);
|
|
return response.statusCode === 200;
|
|
} catch (error) {
|
|
console.log('❌ 데이터 조회 실패:', error.message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// 메인 테스트 실행
|
|
async function runTests() {
|
|
console.log('🚀 REST API 테스트 시작\n');
|
|
console.log('=' .repeat(50));
|
|
|
|
const results = [];
|
|
|
|
// 서버가 실행 중인지 확인
|
|
results.push(await testHealthCheck());
|
|
|
|
// 기본 API 테스트
|
|
if (results[0]) {
|
|
results.push(await testCreateData());
|
|
results.push(await testGetAllData());
|
|
}
|
|
|
|
console.log('\n' + '='.repeat(50));
|
|
console.log('📊 테스트 결과 요약:');
|
|
console.log(`✅ 성공: ${results.filter(r => r).length}/${results.length}`);
|
|
console.log(`❌ 실패: ${results.filter(r => !r).length}/${results.length}`);
|
|
|
|
if (results.every(r => r)) {
|
|
console.log('🎉 모든 테스트가 성공했습니다!');
|
|
} else {
|
|
console.log('⚠️ 일부 테스트가 실패했습니다. 서버가 실행 중인지 확인해주세요.');
|
|
}
|
|
}
|
|
|
|
// 테스트 실행
|
|
runTests().catch(console.error);
|