|
| 1 | +import { expect, test, describe } from 'vitest' |
| 2 | +import { |
| 3 | + extractRequestForLogging, |
| 4 | + translateErrorToResponseCode |
| 5 | +} from '../src/server/utils.js' |
| 6 | +import { FastifyRequest } from 'fastify' |
| 7 | + |
| 8 | +describe('server utils', () => { |
| 9 | + describe('extractRequestForLogging', () => { |
| 10 | + test('should handle valid pg connection string', () => { |
| 11 | + const mockRequest = { |
| 12 | + method: 'POST', |
| 13 | + url: '/test', |
| 14 | + headers: { |
| 15 | + pg: 'postgresql://user:pass@localhost:5432/db', |
| 16 | + 'x-supabase-info': 'test-info', |
| 17 | + }, |
| 18 | + } as FastifyRequest |
| 19 | + |
| 20 | + const result = extractRequestForLogging(mockRequest) |
| 21 | + |
| 22 | + expect(result).toEqual({ |
| 23 | + method: 'POST', |
| 24 | + url: '/test', |
| 25 | + pg: 'localhost', |
| 26 | + opt: 'test-info', |
| 27 | + }) |
| 28 | + }) |
| 29 | + |
| 30 | + test('should handle missing x-supabase-info header', () => { |
| 31 | + const mockRequest = { |
| 32 | + method: 'DELETE', |
| 33 | + url: '/test', |
| 34 | + headers: { |
| 35 | + pg: 'postgresql://user:pass@localhost:5432/db', |
| 36 | + }, |
| 37 | + } as FastifyRequest |
| 38 | + |
| 39 | + const result = extractRequestForLogging(mockRequest) |
| 40 | + |
| 41 | + expect(result).toEqual({ |
| 42 | + method: 'DELETE', |
| 43 | + url: '/test', |
| 44 | + pg: 'localhost', |
| 45 | + opt: '', |
| 46 | + }) |
| 47 | + }) |
| 48 | + }) |
| 49 | + |
| 50 | + describe('translateErrorToResponseCode', () => { |
| 51 | + test('should return 504 for connection timeout', () => { |
| 52 | + const error = { message: 'Connection terminated due to connection timeout' } |
| 53 | + const result = translateErrorToResponseCode(error) |
| 54 | + expect(result).toBe(504) |
| 55 | + }) |
| 56 | + |
| 57 | + test('should return 503 for too many clients', () => { |
| 58 | + const error = { message: 'sorry, too many clients already' } |
| 59 | + const result = translateErrorToResponseCode(error) |
| 60 | + expect(result).toBe(503) |
| 61 | + }) |
| 62 | + }) |
| 63 | +}) |
0 commit comments