forked from JavaScriptSolidServer/JavaScriptSolidServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubdomain-base-files.test.js
More file actions
109 lines (94 loc) · 3.83 KB
/
Copy pathsubdomain-base-files.test.js
File metadata and controls
109 lines (94 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* Regression tests for #307 — buildResourceUrl rewriting the base-domain
* root file paths into non-existent pod subdomains.
*
* Unit tests against buildResourceUrl directly, since Node's fetch() overrides
* the Host header with the TCP target, which makes end-to-end tests of
* subdomain routing impossible without a real reverse proxy.
*/
import { describe, it } from 'node:test';
import assert from 'node:assert';
import { buildResourceUrl } from '../src/auth/middleware.js';
function makeRequest({ hostname, baseDomain, subdomainsEnabled = true, podName = null, protocol = 'https' }) {
return {
protocol,
hostname,
headers: { host: hostname },
subdomainsEnabled,
baseDomain,
podName
};
}
describe('buildResourceUrl — base-domain files (#307)', () => {
const baseDomain = 'example.com';
it('base-domain root (/) — no rewrite', () => {
const req = makeRequest({ hostname: baseDomain, baseDomain });
assert.strictEqual(buildResourceUrl(req, '/'), 'https://example.com/');
});
it('base-domain /welcome.js — no rewrite (filename has extension)', () => {
const req = makeRequest({ hostname: baseDomain, baseDomain });
assert.strictEqual(buildResourceUrl(req, '/welcome.js'), 'https://example.com/welcome.js');
});
it('base-domain /mashlib.js — no rewrite', () => {
const req = makeRequest({ hostname: baseDomain, baseDomain });
assert.strictEqual(buildResourceUrl(req, '/mashlib.js'), 'https://example.com/mashlib.js');
});
it('base-domain /terms.html — no rewrite', () => {
const req = makeRequest({ hostname: baseDomain, baseDomain });
assert.strictEqual(buildResourceUrl(req, '/terms.html'), 'https://example.com/terms.html');
});
it('base-domain /.well-known/foo — no rewrite (leading dot)', () => {
const req = makeRequest({ hostname: baseDomain, baseDomain });
assert.strictEqual(
buildResourceUrl(req, '/.well-known/foo'),
'https://example.com/.well-known/foo'
);
});
});
describe('buildResourceUrl — pod routing still works', () => {
const baseDomain = 'example.com';
it('base-domain /alice/ — rewrites to alice.example.com (no dot → pod name)', () => {
const req = makeRequest({ hostname: baseDomain, baseDomain });
assert.strictEqual(buildResourceUrl(req, '/alice/'), 'https://alice.example.com/');
});
it('base-domain /alice — rewrites (bare pod-root without trailing slash)', () => {
const req = makeRequest({ hostname: baseDomain, baseDomain });
assert.strictEqual(buildResourceUrl(req, '/alice'), 'https://alice.example.com/');
});
it('base-domain /alice/profile/card.jsonld — rewrites to alice.example.com/profile/card.jsonld', () => {
const req = makeRequest({ hostname: baseDomain, baseDomain });
assert.strictEqual(
buildResourceUrl(req, '/alice/profile/card.jsonld'),
'https://alice.example.com/profile/card.jsonld'
);
});
it('already-on-subdomain request — no rewrite (hostname !== baseDomain)', () => {
const req = makeRequest({
hostname: 'alice.example.com',
baseDomain,
podName: 'alice'
});
assert.strictEqual(
buildResourceUrl(req, '/profile/card.jsonld'),
'https://alice.example.com/profile/card.jsonld'
);
});
});
describe('buildResourceUrl — subdomain mode disabled', () => {
it('no rewrite when subdomainsEnabled is false', () => {
const req = makeRequest({
hostname: 'example.com',
baseDomain: 'example.com',
subdomainsEnabled: false
});
assert.strictEqual(buildResourceUrl(req, '/alice/'), 'https://example.com/alice/');
});
it('no rewrite when baseDomain is not set', () => {
const req = makeRequest({
hostname: 'example.com',
baseDomain: null,
subdomainsEnabled: true
});
assert.strictEqual(buildResourceUrl(req, '/alice/'), 'https://example.com/alice/');
});
});