|
| 1 | +import * as assert from 'node:assert/strict'; |
| 2 | +import { describe, it } from 'node:test'; |
| 3 | +import { NodeApp } from '../../../dist/core/app/node.js'; |
| 4 | + |
| 5 | +const mockNodeRequest = { |
| 6 | + url: '/', |
| 7 | + method: 'GET', |
| 8 | + headers: { |
| 9 | + host: 'example.com', |
| 10 | + }, |
| 11 | + socket: { |
| 12 | + encrypted: true, |
| 13 | + remoteAddress: '2.2.2.2', |
| 14 | + }, |
| 15 | +}; |
| 16 | + |
| 17 | +describe('NodeApp', () => { |
| 18 | + describe('createRequest', () => { |
| 19 | + describe('x-forwarded-for', () => { |
| 20 | + it('parses client IP from single-value x-forwarded-for header', () => { |
| 21 | + const result = NodeApp.createRequest({ |
| 22 | + ...mockNodeRequest, |
| 23 | + headers: { |
| 24 | + 'x-forwarded-for': '1.1.1.1', |
| 25 | + }, |
| 26 | + }); |
| 27 | + assert.equal(result[Symbol.for('astro.clientAddress')], '1.1.1.1'); |
| 28 | + }); |
| 29 | + |
| 30 | + it('parses client IP from multi-value x-forwarded-for header', () => { |
| 31 | + const result = NodeApp.createRequest({ |
| 32 | + ...mockNodeRequest, |
| 33 | + headers: { |
| 34 | + 'x-forwarded-for': '1.1.1.1,8.8.8.8', |
| 35 | + }, |
| 36 | + }); |
| 37 | + assert.equal(result[Symbol.for('astro.clientAddress')], '1.1.1.1'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('parses client IP from multi-value x-forwarded-for header with spaces', () => { |
| 41 | + const result = NodeApp.createRequest({ |
| 42 | + ...mockNodeRequest, |
| 43 | + headers: { |
| 44 | + 'x-forwarded-for': ' 1.1.1.1, 8.8.8.8, 8.8.8.2', |
| 45 | + }, |
| 46 | + }); |
| 47 | + assert.equal(result[Symbol.for('astro.clientAddress')], '1.1.1.1'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('fallbacks to remoteAddress when no x-forwarded-for header is present', () => { |
| 51 | + const result = NodeApp.createRequest({ |
| 52 | + ...mockNodeRequest, |
| 53 | + headers: {}, |
| 54 | + }); |
| 55 | + assert.equal(result[Symbol.for('astro.clientAddress')], '2.2.2.2'); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + describe('x-forwarded-host', () => { |
| 60 | + it('parses host from single-value x-forwarded-host header', () => { |
| 61 | + const result = NodeApp.createRequest({ |
| 62 | + ...mockNodeRequest, |
| 63 | + headers: { |
| 64 | + 'x-forwarded-host': 'www2.example.com', |
| 65 | + }, |
| 66 | + }); |
| 67 | + assert.equal(result.url, 'https://www2.example.com/'); |
| 68 | + }); |
| 69 | + |
| 70 | + it('parses host from multi-value x-forwarded-host header', () => { |
| 71 | + const result = NodeApp.createRequest({ |
| 72 | + ...mockNodeRequest, |
| 73 | + headers: { |
| 74 | + 'x-forwarded-host': 'www2.example.com,www3.example.com', |
| 75 | + }, |
| 76 | + }); |
| 77 | + assert.equal(result.url, 'https://www2.example.com/'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('fallbacks to host header when no x-forwarded-host header is present', () => { |
| 81 | + const result = NodeApp.createRequest({ |
| 82 | + ...mockNodeRequest, |
| 83 | + headers: { |
| 84 | + host: 'example.com', |
| 85 | + }, |
| 86 | + }); |
| 87 | + assert.equal(result.url, 'https://example.com/'); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + describe('x-forwarded-proto', () => { |
| 92 | + it('parses protocol from single-value x-forwarded-proto header', () => { |
| 93 | + const result = NodeApp.createRequest({ |
| 94 | + ...mockNodeRequest, |
| 95 | + headers: { |
| 96 | + host: 'example.com', |
| 97 | + 'x-forwarded-proto': 'http', |
| 98 | + 'x-forwarded-port': '80', |
| 99 | + }, |
| 100 | + }); |
| 101 | + assert.equal(result.url, 'http://example.com/'); |
| 102 | + }); |
| 103 | + |
| 104 | + it('parses protocol from multi-value x-forwarded-proto header', () => { |
| 105 | + const result = NodeApp.createRequest({ |
| 106 | + ...mockNodeRequest, |
| 107 | + headers: { |
| 108 | + host: 'example.com', |
| 109 | + 'x-forwarded-proto': 'http,https', |
| 110 | + 'x-forwarded-port': '80,443', |
| 111 | + }, |
| 112 | + }); |
| 113 | + assert.equal(result.url, 'http://example.com/'); |
| 114 | + }); |
| 115 | + |
| 116 | + it('fallbacks to encrypted property when no x-forwarded-proto header is present', () => { |
| 117 | + const result = NodeApp.createRequest({ |
| 118 | + ...mockNodeRequest, |
| 119 | + headers: { |
| 120 | + host: 'example.com', |
| 121 | + }, |
| 122 | + }); |
| 123 | + assert.equal(result.url, 'https://example.com/'); |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + describe('x-forwarded-port', () => { |
| 128 | + it('parses port from single-value x-forwarded-port header', () => { |
| 129 | + const result = NodeApp.createRequest({ |
| 130 | + ...mockNodeRequest, |
| 131 | + headers: { |
| 132 | + host: 'example.com', |
| 133 | + 'x-forwarded-port': '8443', |
| 134 | + }, |
| 135 | + }); |
| 136 | + assert.equal(result.url, 'https://example.com:8443/'); |
| 137 | + }); |
| 138 | + |
| 139 | + it('parses port from multi-value x-forwarded-port header', () => { |
| 140 | + const result = NodeApp.createRequest({ |
| 141 | + ...mockNodeRequest, |
| 142 | + headers: { |
| 143 | + host: 'example.com', |
| 144 | + 'x-forwarded-port': '8443,3000', |
| 145 | + }, |
| 146 | + }); |
| 147 | + assert.equal(result.url, 'https://example.com:8443/'); |
| 148 | + }); |
| 149 | + |
| 150 | + it('prefers port from host', () => { |
| 151 | + const result = NodeApp.createRequest({ |
| 152 | + ...mockNodeRequest, |
| 153 | + headers: { |
| 154 | + host: 'example.com:3000', |
| 155 | + 'x-forwarded-port': '443', |
| 156 | + }, |
| 157 | + }); |
| 158 | + assert.equal(result.url, 'https://example.com:3000/'); |
| 159 | + }); |
| 160 | + |
| 161 | + |
| 162 | + it('prefers port from x-forwarded-host', () => { |
| 163 | + const result = NodeApp.createRequest({ |
| 164 | + ...mockNodeRequest, |
| 165 | + headers: { |
| 166 | + host: 'example.com:443', |
| 167 | + 'x-forwarded-host': 'example.com:3000', |
| 168 | + 'x-forwarded-port': '443', |
| 169 | + }, |
| 170 | + }); |
| 171 | + assert.equal(result.url, 'https://example.com:3000/'); |
| 172 | + }); |
| 173 | + }); |
| 174 | + }); |
| 175 | +}); |
0 commit comments