-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-exchange-rates.js
More file actions
158 lines (132 loc) · 5.28 KB
/
test-exchange-rates.js
File metadata and controls
158 lines (132 loc) · 5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import { fileURLToPath } from 'url';
import path from 'path';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// Base URL for API endpoints
const API_BASE_URL = 'http://localhost:3000/api';
/**
* Test the exchange rate endpoints
*/
async function testExchangeRateEndpoints() {
console.log('🚀 Starting Exchange Rate API tests...\n');
const results = {
getSingleRate: false,
getBatchRates: false,
getSupportedCurrencies: false
};
// Test 1: GET single exchange rate
try {
console.log('📊 Testing GET /api/exchange-rates/BTC/USD...');
const response = await fetch(`${API_BASE_URL}/exchange-rates/BTC/USD`);
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
const data = await response.json();
console.log('✅ Single rate response:', JSON.stringify(data, null, 2));
// Validate response structure
if (data.crypto && data.fiat && data.rate && data.timestamp) {
results.getSingleRate = true;
console.log('✅ Single rate test PASSED\n');
} else {
console.log('❌ Single rate test FAILED - Invalid response structure\n');
}
} catch (error) {
console.log(`❌ Single rate test FAILED: ${error.message}\n`);
}
// Test 2: POST batch exchange rates
try {
console.log('📊 Testing POST /api/exchange-rates (batch)...');
const requestBody = {
pairs: [
{ crypto: 'BTC', fiat: 'USD' },
{ crypto: 'ETH', fiat: 'EUR' },
{ crypto: 'ADA', fiat: 'GBP' }
]
};
const response = await fetch(`${API_BASE_URL}/exchange-rates`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(requestBody)
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
const data = await response.json();
console.log('✅ Batch rates response:', JSON.stringify(data, null, 2));
// Validate response structure
if (data.rates && Array.isArray(data.rates) && data.timestamp) {
results.getBatchRates = true;
console.log('✅ Batch rates test PASSED\n');
} else {
console.log('❌ Batch rates test FAILED - Invalid response structure\n');
}
} catch (error) {
console.log(`❌ Batch rates test FAILED: ${error.message}\n`);
}
// Test 3: GET supported currencies
try {
console.log('📊 Testing GET /api/exchange-rates/supported...');
const response = await fetch(`${API_BASE_URL}/exchange-rates/supported`);
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
const data = await response.json();
console.log('✅ Supported currencies response:', JSON.stringify(data, null, 2));
// Validate response structure
if (data.cryptos && Array.isArray(data.cryptos) && data.fiats && Array.isArray(data.fiats)) {
results.getSupportedCurrencies = true;
console.log('✅ Supported currencies test PASSED\n');
} else {
console.log('❌ Supported currencies test FAILED - Invalid response structure\n');
}
} catch (error) {
console.log(`❌ Supported currencies test FAILED: ${error.message}\n`);
}
// Test 4: Error handling - Invalid crypto symbol
try {
console.log('📊 Testing error handling with invalid crypto...');
const response = await fetch(`${API_BASE_URL}/exchange-rates/INVALID/USD`);
if (response.status === 400 || response.status === 500) {
console.log('✅ Error handling test PASSED - Got expected error status\n');
} else {
console.log('❌ Error handling test FAILED - Expected error status\n');
}
} catch (error) {
console.log(`❌ Error handling test FAILED: ${error.message}\n`);
}
// Test 5: Error handling - Empty batch request
try {
console.log('📊 Testing error handling with empty batch...');
const response = await fetch(`${API_BASE_URL}/exchange-rates`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ pairs: [] })
});
if (response.status === 400) {
console.log('✅ Empty batch error handling test PASSED\n');
} else {
console.log('❌ Empty batch error handling test FAILED\n');
}
} catch (error) {
console.log(`❌ Empty batch error handling test FAILED: ${error.message}\n`);
}
// Summary
console.log('📋 Test Results Summary:');
console.log(`GET Single Rate: ${results.getSingleRate ? '✅ PASSED' : '❌ FAILED'}`);
console.log(`POST Batch Rates: ${results.getBatchRates ? '✅ PASSED' : '❌ FAILED'}`);
console.log(`GET Supported Currencies: ${results.getSupportedCurrencies ? '✅ PASSED' : '❌ FAILED'}`);
const passedTests = Object.values(results).filter(Boolean).length;
const totalTests = Object.keys(results).length;
console.log(`\n🎯 Overall: ${passedTests}/${totalTests} tests passed`);
if (passedTests === totalTests) {
console.log('🎉 All exchange rate tests passed successfully!');
} else {
console.log('⚠️ Some exchange rate tests failed. Check the error messages above.');
}
}
// Wait for the server to start before running the tests
console.log('⏳ Waiting for server to start...');
setTimeout(testExchangeRateEndpoints, 3000);