|
| 1 | +import { describe, it, expect } from 'vitest' |
| 2 | + |
| 3 | +import { startServer } from './helpers.js' |
| 4 | + |
| 5 | +describe('CORS Headers', () => { |
| 6 | + it('should include CORS headers on GET requests', async (t) => { |
| 7 | + const { localBaseUrl } = await startServer(t) |
| 8 | + const response = await fetch(`${localBaseUrl}/maps/custom/style.json`) |
| 9 | + expect(response.status).toBe(200) |
| 10 | + expect(response.headers.get('access-control-allow-origin')).toBe('*') |
| 11 | + }) |
| 12 | + |
| 13 | + it('should include CORS headers on 404 responses', async (t) => { |
| 14 | + const { localBaseUrl } = await startServer(t) |
| 15 | + const response = await fetch(`${localBaseUrl}/unknown-route`) |
| 16 | + expect(response.status).toBe(404) |
| 17 | + expect(response.headers.get('access-control-allow-origin')).toBe('*') |
| 18 | + }) |
| 19 | + |
| 20 | + it('should include CORS headers on error responses', async (t) => { |
| 21 | + const { localBaseUrl } = await startServer(t) |
| 22 | + const response = await fetch(`${localBaseUrl}/maps/custom`, { |
| 23 | + method: 'PUT', |
| 24 | + body: Buffer.from('invalid'), |
| 25 | + headers: { 'Content-Type': 'application/octet-stream' }, |
| 26 | + }) |
| 27 | + expect(response.status).toBe(400) |
| 28 | + expect(response.headers.get('access-control-allow-origin')).toBe('*') |
| 29 | + }) |
| 30 | + |
| 31 | + it('should handle OPTIONS preflight requests', async (t) => { |
| 32 | + const { localBaseUrl } = await startServer(t) |
| 33 | + const response = await fetch(`${localBaseUrl}/maps/custom/style.json`, { |
| 34 | + method: 'OPTIONS', |
| 35 | + }) |
| 36 | + expect(response.status).toBe(204) |
| 37 | + expect(response.headers.get('access-control-allow-origin')).toBe('*') |
| 38 | + expect(response.headers.get('access-control-allow-methods')).toBe( |
| 39 | + 'GET,POST,DELETE,OPTIONS', |
| 40 | + ) |
| 41 | + expect(response.headers.get('access-control-allow-headers')).toBe( |
| 42 | + 'Content-Type', |
| 43 | + ) |
| 44 | + }) |
| 45 | + |
| 46 | + it('should handle OPTIONS preflight for POST endpoints', async (t) => { |
| 47 | + const { localBaseUrl } = await startServer(t) |
| 48 | + const response = await fetch(`${localBaseUrl}/mapShares`, { |
| 49 | + method: 'OPTIONS', |
| 50 | + }) |
| 51 | + expect(response.status).toBe(204) |
| 52 | + expect(response.headers.get('access-control-allow-origin')).toBe('*') |
| 53 | + expect(response.headers.get('access-control-allow-methods')).toBe( |
| 54 | + 'GET,POST,DELETE,OPTIONS', |
| 55 | + ) |
| 56 | + }) |
| 57 | + |
| 58 | + it('should handle OPTIONS preflight for DELETE endpoints', async (t) => { |
| 59 | + const { localBaseUrl } = await startServer(t) |
| 60 | + const response = await fetch(`${localBaseUrl}/maps/custom`, { |
| 61 | + method: 'OPTIONS', |
| 62 | + }) |
| 63 | + expect(response.status).toBe(204) |
| 64 | + expect(response.headers.get('access-control-allow-origin')).toBe('*') |
| 65 | + expect(response.headers.get('access-control-allow-methods')).toBe( |
| 66 | + 'GET,POST,DELETE,OPTIONS', |
| 67 | + ) |
| 68 | + }) |
| 69 | + |
| 70 | + it('should include CORS headers on POST responses', async (t) => { |
| 71 | + const { localBaseUrl } = await startServer(t) |
| 72 | + // This will fail validation, but should still have CORS headers |
| 73 | + const response = await fetch(`${localBaseUrl}/mapShares`, { |
| 74 | + method: 'POST', |
| 75 | + headers: { 'Content-Type': 'application/json' }, |
| 76 | + body: JSON.stringify({}), |
| 77 | + }) |
| 78 | + // 400 due to missing required fields |
| 79 | + expect(response.status).toBe(400) |
| 80 | + expect(response.headers.get('access-control-allow-origin')).toBe('*') |
| 81 | + }) |
| 82 | + |
| 83 | + it('should include CORS headers on DELETE responses', async (t) => { |
| 84 | + const { localBaseUrl } = await startServer(t) |
| 85 | + const response = await fetch(`${localBaseUrl}/maps/custom`, { |
| 86 | + method: 'DELETE', |
| 87 | + }) |
| 88 | + expect(response.status).toBe(204) |
| 89 | + expect(response.headers.get('access-control-allow-origin')).toBe('*') |
| 90 | + }) |
| 91 | +}) |
0 commit comments