|
| 1 | +import { describe, it, expect } from 'bun:test'; |
| 2 | + |
| 3 | +function stripSslMode(connectionString: string): string { |
| 4 | + const url = new URL(connectionString); |
| 5 | + url.searchParams.delete('sslmode'); |
| 6 | + return url.toString(); |
| 7 | +} |
| 8 | + |
| 9 | +describe('stripSslMode', () => { |
| 10 | + it('removes sslmode=require from the connection string', () => { |
| 11 | + const input = |
| 12 | + 'postgresql://user:pass@host.rds.amazonaws.com:5432/mydb?sslmode=require'; |
| 13 | + const result = stripSslMode(input); |
| 14 | + expect(result).toBe( |
| 15 | + 'postgresql://user:pass@host.rds.amazonaws.com:5432/mydb', |
| 16 | + ); |
| 17 | + }); |
| 18 | + |
| 19 | + it('removes sslmode when it is one of multiple params', () => { |
| 20 | + const input = |
| 21 | + 'postgresql://user:pass@host:5432/mydb?sslmode=require&connection_limit=50'; |
| 22 | + const result = stripSslMode(input); |
| 23 | + expect(result).toBe( |
| 24 | + 'postgresql://user:pass@host:5432/mydb?connection_limit=50', |
| 25 | + ); |
| 26 | + }); |
| 27 | + |
| 28 | + it('preserves other query params when sslmode is first', () => { |
| 29 | + const input = |
| 30 | + 'postgresql://user:pass@host:5432/mydb?sslmode=require&pgbouncer=true&connection_limit=10'; |
| 31 | + const result = stripSslMode(input); |
| 32 | + expect(result).toBe( |
| 33 | + 'postgresql://user:pass@host:5432/mydb?pgbouncer=true&connection_limit=10', |
| 34 | + ); |
| 35 | + }); |
| 36 | + |
| 37 | + it('preserves other query params when sslmode is in the middle', () => { |
| 38 | + const input = |
| 39 | + 'postgresql://user:pass@host:5432/mydb?pgbouncer=true&sslmode=require&connection_limit=10'; |
| 40 | + const result = stripSslMode(input); |
| 41 | + expect(result).toBe( |
| 42 | + 'postgresql://user:pass@host:5432/mydb?pgbouncer=true&connection_limit=10', |
| 43 | + ); |
| 44 | + }); |
| 45 | + |
| 46 | + it('preserves other query params when sslmode is last', () => { |
| 47 | + const input = |
| 48 | + 'postgresql://user:pass@host:5432/mydb?connection_limit=10&sslmode=require'; |
| 49 | + const result = stripSslMode(input); |
| 50 | + expect(result).toBe( |
| 51 | + 'postgresql://user:pass@host:5432/mydb?connection_limit=10', |
| 52 | + ); |
| 53 | + }); |
| 54 | + |
| 55 | + it('handles different sslmode values', () => { |
| 56 | + const input = |
| 57 | + 'postgresql://user:pass@host:5432/mydb?sslmode=verify-full'; |
| 58 | + const result = stripSslMode(input); |
| 59 | + expect(result).toBe('postgresql://user:pass@host:5432/mydb'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('returns url unchanged when no sslmode is present', () => { |
| 63 | + const input = |
| 64 | + 'postgresql://user:pass@host:5432/mydb?connection_limit=50'; |
| 65 | + const result = stripSslMode(input); |
| 66 | + expect(result).toBe( |
| 67 | + 'postgresql://user:pass@host:5432/mydb?connection_limit=50', |
| 68 | + ); |
| 69 | + }); |
| 70 | + |
| 71 | + it('handles url with no query params', () => { |
| 72 | + const input = 'postgresql://user:pass@host:5432/mydb'; |
| 73 | + const result = stripSslMode(input); |
| 74 | + expect(result).toBe('postgresql://user:pass@host:5432/mydb'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('preserves password with special characters', () => { |
| 78 | + const input = |
| 79 | + 'postgresql://user:p%40ss%23word@host:5432/mydb?sslmode=require&connection_limit=50'; |
| 80 | + const result = stripSslMode(input); |
| 81 | + expect(result).toBe( |
| 82 | + 'postgresql://user:p%40ss%23word@host:5432/mydb?connection_limit=50', |
| 83 | + ); |
| 84 | + }); |
| 85 | +}); |
0 commit comments