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