1
0

reverse-proxy.test.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { describe, it, expect } from 'vitest';
  2. import { buildProxyConfig, buildCertCommand, type ReverseProxyOptions } from './reverse-proxy';
  3. const base: ReverseProxyOptions = {
  4. server: 'nginx',
  5. domain: 'panel.example.com',
  6. panelPort: '2053',
  7. panelPath: '/panel',
  8. certTool: 'certbot',
  9. };
  10. describe('buildProxyConfig — nginx', () => {
  11. it('includes WebSocket upgrade headers and the upstream port', () => {
  12. const cfg = buildProxyConfig(base);
  13. expect(cfg).toContain('server_name panel.example.com;');
  14. expect(cfg).toContain('proxy_pass http://127.0.0.1:2053;');
  15. expect(cfg).toContain('proxy_set_header Upgrade $http_upgrade;');
  16. expect(cfg).toContain('proxy_set_header Connection "upgrade";');
  17. expect(cfg).toContain('location /panel/');
  18. });
  19. });
  20. describe('buildProxyConfig — caddy', () => {
  21. it('produces a path-scoped reverse_proxy', () => {
  22. const cfg = buildProxyConfig({ ...base, server: 'caddy' });
  23. expect(cfg).toContain('panel.example.com {');
  24. expect(cfg).toContain('reverse_proxy /panel/* 127.0.0.1:2053');
  25. });
  26. it('proxies the whole site when path is root', () => {
  27. const cfg = buildProxyConfig({ ...base, server: 'caddy', panelPath: '/' });
  28. expect(cfg).toContain('reverse_proxy 127.0.0.1:2053');
  29. });
  30. });
  31. describe('buildCertCommand', () => {
  32. it('supports certbot and acme.sh', () => {
  33. expect(buildCertCommand(base)).toContain('certbot certonly --nginx -d panel.example.com');
  34. expect(buildCertCommand({ ...base, certTool: 'acme.sh' })).toContain(
  35. 'acme.sh --issue -d panel.example.com',
  36. );
  37. });
  38. });