Spaces:
Sleeping
Sleeping
File size: 12,204 Bytes
75a60d6 | 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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 | import { beforeAll, afterAll, describe, it, expect } from 'vitest';
import { startServer, stopServer, request } from './helpers';
beforeAll(async () => {
await startServer();
}, 20000);
afterAll(() => {
stopServer();
});
describe('Health & Root', () => {
it('GET /health returns 200 with server status', async () => {
const res = await request('/health');
expect(res.status).toBe(200);
expect(res.body.status).toBe('ok');
expect(res.body.tables).toEqual(
expect.arrayContaining(['users', 'posts', 'comments', 'albums', 'photos', 'todos']),
);
expect(res.body.rateLimit).toBeDefined();
expect(res.body.rateLimit.enabled).toBe(false);
});
it('GET /api/health works as alias', async () => {
const res = await request('/api/health');
expect(res.status).toBe(200);
expect(res.body.status).toBe('ok');
});
it('GET / returns API info', async () => {
const res = await request('/');
expect(res.status).toBe(200);
expect(res.body.message).toContain('json-api-server');
expect(res.body.version).toBe('1.0.0');
expect(res.body.endpoints).toBeInstanceOf(Array);
});
it('GET /api works as alias', async () => {
const res = await request('/api');
expect(res.status).toBe(200);
expect(res.body.version).toBe('1.0.0');
});
});
describe('CORS', () => {
it('OPTIONS returns 204 with CORS headers', async () => {
const res = await request('/api/users', { method: 'OPTIONS' });
expect(res.status).toBe(204);
expect(res.headers['access-control-allow-origin']).toBe('*');
});
});
describe('List resources', () => {
it('GET /api/users returns all users', async () => {
const res = await request('/api/users');
expect(res.status).toBe(200);
expect(Array.isArray(res.body)).toBe(true);
expect(res.body.length).toBe(2);
expect(res.body[0]).toHaveProperty('name');
});
it('GET /api/posts returns all posts', async () => {
const res = await request('/api/posts');
expect(res.status).toBe(200);
expect(Array.isArray(res.body)).toBe(true);
expect(res.body.length).toBe(3);
});
it('GET /api/comments returns all comments', async () => {
const res = await request('/api/comments');
expect(res.status).toBe(200);
expect(res.body.length).toBe(3);
});
it('GET /api/albums returns all albums', async () => {
const res = await request('/api/albums');
expect(res.status).toBe(200);
expect(res.body.length).toBe(3);
});
it('GET /api/photos returns all photos', async () => {
const res = await request('/api/photos');
expect(res.status).toBe(200);
expect(res.body.length).toBe(2);
});
it('GET /api/todos returns all todos', async () => {
const res = await request('/api/todos');
expect(res.status).toBe(200);
expect(res.body.length).toBe(3);
});
});
describe('Get single resource', () => {
it('GET /api/users/1 returns the user', async () => {
const res = await request('/api/users/1');
expect(res.status).toBe(200);
expect(res.body.id).toBe(1);
expect(res.body.name).toBe('Alice Johnson');
expect(res.body.username).toBe('alice');
expect(res.body.email).toBe('alice@example.com');
expect(typeof res.body.address).toBe('object');
expect(typeof res.body.company).toBe('object');
});
it('GET /api/users/2 returns another user', async () => {
const res = await request('/api/users/2');
expect(res.status).toBe(200);
expect(res.body.name).toBe('Bob Smith');
});
it('GET /api/posts/1 returns fields correctly', async () => {
const res = await request('/api/posts/1');
expect(res.status).toBe(200);
expect(res.body.userId).toBe(1);
expect(res.body.title).toBe('First Post');
});
it('GET /api/todos/1 returns completed as boolean', async () => {
const res = await request('/api/todos/1');
expect(res.status).toBe(200);
expect(res.body.completed).toBe(false);
});
it('GET /api/todos/2 returns completed as boolean (true)', async () => {
const res = await request('/api/todos/2');
expect(res.status).toBe(200);
expect(res.body.completed).toBe(true);
});
});
describe('404 handling', () => {
it('GET /api/users/9999 returns 404', async () => {
const res = await request('/api/users/9999');
expect(res.status).toBe(404);
expect(res.body.error).toBeDefined();
});
it('GET /api/unknown returns 404', async () => {
const res = await request('/api/unknown');
expect(res.status).toBe(404);
expect(res.body.error).toContain('Unknown route');
});
});
describe('Filtering', () => {
it('GET /api/todos?userId=1 filters by userId', async () => {
const res = await request('/api/todos?userId=1');
expect(res.status).toBe(200);
expect(res.body.length).toBe(2);
res.body.forEach((t) => expect(t.userId).toBe(1));
});
it('GET /api/todos?userId=1&completed=false supports multiple filters', async () => {
const res = await request('/api/todos?userId=1&completed=false');
expect(res.status).toBe(200);
expect(res.body.length).toBe(1);
expect(res.body[0].title).toBe('Buy groceries');
expect(res.body[0].completed).toBe(false);
});
it('GET /api/todos?completed=true returns only completed', async () => {
const res = await request('/api/todos?completed=true');
expect(res.status).toBe(200);
expect(res.body.every((t) => t.completed === true)).toBe(true);
});
it('GET /api/posts?userId=2 filters by user', async () => {
const res = await request('/api/posts?userId=2');
expect(res.status).toBe(200);
expect(res.body.length).toBe(1);
expect(res.body[0].title).toBe('Bobs Post');
});
});
describe('Nested routes', () => {
it('GET /api/users/1/posts returns user posts', async () => {
const res = await request('/api/users/1/posts');
expect(res.status).toBe(200);
expect(res.body.length).toBe(2);
res.body.forEach((p) => expect(p.userId).toBe(1));
});
it('GET /api/users/2/posts returns other user posts', async () => {
const res = await request('/api/users/2/posts');
expect(res.status).toBe(200);
expect(res.body.length).toBe(1);
});
it('GET /api/posts/1/comments returns post comments', async () => {
const res = await request('/api/posts/1/comments');
expect(res.status).toBe(200);
expect(res.body.length).toBe(2);
res.body.forEach((c) => expect(c.postId).toBe(1));
});
it('GET /api/posts/2/comments returns other post comments', async () => {
const res = await request('/api/posts/2/comments');
expect(res.status).toBe(200);
expect(res.body.length).toBe(1);
});
it('GET /api/users/1/albums returns user albums', async () => {
const res = await request('/api/users/1/albums');
expect(res.status).toBe(200);
expect(res.body.length).toBe(2);
res.body.forEach((a) => expect(a.userId).toBe(1));
});
it('GET /api/users/1/todos returns user todos', async () => {
const res = await request('/api/users/1/todos');
expect(res.status).toBe(200);
expect(res.body.length).toBe(2);
});
it('GET /api/albums/1/photos returns album photos', async () => {
const res = await request('/api/albums/1/photos');
expect(res.status).toBe(200);
expect(res.body.length).toBe(2);
res.body.forEach((p) => expect(p.albumId).toBe(1));
});
it('GET /api/users/9999/posts returns 404 when parent not found', async () => {
const res = await request('/api/users/9999/posts');
expect(res.status).toBe(404);
});
it('GET /api/users/1/unknown returns 404 for unknown nested route', async () => {
const res = await request('/api/users/1/unknown');
expect(res.status).toBe(404);
});
});
describe('POST — Create', () => {
let createdId;
it('POST /api/posts creates a new post', async () => {
const res = await request('/api/posts', {
method: 'POST',
body: { userId: 1, title: 'New Post', body: 'Created via test' },
});
expect(res.status).toBe(201);
expect(res.body.title).toBe('New Post');
expect(res.body.userId).toBe(1);
expect(res.body.id).toBeGreaterThan(0);
createdId = res.body.id;
});
it('Created post is retrievable via GET', async () => {
const res = await request('/api/posts/' + createdId);
expect(res.status).toBe(200);
expect(res.body.title).toBe('New Post');
});
it('POST with explicit id creates with that id', async () => {
const res = await request('/api/posts', {
method: 'POST',
body: { id: 100, userId: 2, title: 'Custom ID', body: 'Body' },
});
expect(res.status).toBe(201);
expect(res.body.id).toBe(100);
});
it('POST with invalid JSON body returns 400', async () => {
const res = await request('/api/posts', {
method: 'POST',
rawBody: 'not-json',
headers: { 'Content-Type': 'text/plain' },
});
expect(res.status).toBe(400);
expect(res.body.error).toBe('Invalid JSON body');
});
});
describe('PATCH — Partial update', () => {
it('PATCH /api/posts/1 updates title only', async () => {
const res = await request('/api/posts/1', {
method: 'PATCH',
body: { title: 'Patched Title' },
});
expect(res.status).toBe(200);
expect(res.body.title).toBe('Patched Title');
expect(res.body.body).toBe('This is the body of the first post');
});
it('PATCH without id returns 400', async () => {
const res = await request('/api/posts', { method: 'PATCH', body: {} });
expect(res.status).toBe(400);
});
it('PATCH non-existent returns 404', async () => {
const res = await request('/api/posts/9999', { method: 'PATCH', body: {} });
expect(res.status).toBe(404);
});
});
describe('PUT — Full replacement', () => {
it('PUT /api/posts/2 replaces the resource', async () => {
const res = await request('/api/posts/2', {
method: 'PUT',
body: { userId: 2, title: 'Replaced', body: 'Completely new content' },
});
expect(res.status).toBe(200);
expect(res.body.title).toBe('Replaced');
expect(res.body.body).toBe('Completely new content');
expect(res.body.userId).toBe(2);
});
it('PUT without id returns 400', async () => {
const res = await request('/api/posts', { method: 'PUT', body: {} });
expect(res.status).toBe(400);
});
it('PUT non-existent returns 404', async () => {
const res = await request('/api/posts/9999', { method: 'PUT', body: {} });
expect(res.status).toBe(404);
});
});
describe('DELETE', () => {
let deleteId;
it('DELETE /api/posts/3 deletes the resource', async () => {
const res = await request('/api/posts/3', { method: 'DELETE' });
expect(res.status).toBe(200);
});
it('Deleted resource returns 404', async () => {
const res = await request('/api/posts/3');
expect(res.status).toBe(404);
});
it('DELETE without id returns 400', async () => {
const res = await request('/api/posts', { method: 'DELETE' });
expect(res.status).toBe(400);
});
it('DELETE non-existent returns 404', async () => {
const res = await request('/api/posts/9999', { method: 'DELETE' });
expect(res.status).toBe(404);
});
});
describe('Method not allowed', () => {
it('TRACE returns 405', async () => {
const res = await request('/api/users', { method: 'TRACE' });
expect(res.status).toBe(405);
expect(res.body.error).toContain('TRACE');
});
});
describe('Response headers', () => {
it('Includes CORS headers on GET', async () => {
const res = await request('/api/users');
expect(res.headers['access-control-allow-origin']).toBe('*');
expect(res.headers['content-type']).toContain('application/json');
expect(res.headers['x-powered-by']).toContain('json-api-server');
});
it('Rate limiting is disabled — no rate limit headers', async () => {
const res = await request('/api/users');
expect(res.headers['x-ratelimit-limit']).toBeUndefined();
});
});
describe('CORS headers on error responses', () => {
it('404 response includes CORS headers', async () => {
const res = await request('/api/users/9999');
expect(res.status).toBe(404);
expect(res.headers['access-control-allow-origin']).toBe('*');
});
});
|