kempo-server 3.0.4 → 3.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,189 @@
1
+ import http from 'http';
2
+ import path from 'path';
3
+ import {withTempDir} from './utils/temp-dir.js';
4
+ import {write} from './utils/file-writer.js';
5
+ import {randomPort} from './utils/port.js';
6
+ import {httpGet} from './utils/http.js';
7
+ import router from '../src/router.js';
8
+
9
+ const startServer = async (dir, flags, config) => {
10
+ await write(dir, `${flags.root}/.config.json`, JSON.stringify(config));
11
+ const handler = await router({...flags, logging: 0}, () => {});
12
+ const server = http.createServer(handler);
13
+ const port = randomPort();
14
+ await new Promise(r => server.listen(port, r));
15
+ await new Promise(r => setTimeout(r, 30));
16
+ return {server, port};
17
+ };
18
+
19
+ export default {
20
+ 'wildcard custom route renders .page.html via SSR': async ({pass, fail}) => {
21
+ try {
22
+ await withTempDir(async (dir) => {
23
+ const template = '<html><body><location name="main" /></body></html>';
24
+ const page = '<page template="default"><content location="main"><h1>Admin Page</h1></content></page>';
25
+
26
+ await write(dir, 'admin/default.template.html', template);
27
+ await write(dir, 'admin/dashboard.page.html', page);
28
+ await write(dir, 'public/index.html', '<h1>root</h1>');
29
+
30
+ const prev = process.cwd();
31
+ process.chdir(dir);
32
+ const {server, port} = await startServer(dir, {root: 'public'}, {
33
+ customRoutes: {'/admin/**': '../admin/**'},
34
+ templating: {ssr: true}
35
+ });
36
+
37
+ try {
38
+ const r = await httpGet(`http://localhost:${port}/admin/dashboard`);
39
+ if(r.res.statusCode !== 200) throw new Error(`expected 200, got ${r.res.statusCode}`);
40
+ const body = r.body.toString();
41
+ if(!body.includes('<h1>Admin Page</h1>')) throw new Error(`missing page content: ${body}`);
42
+ if(!body.includes('<html>')) throw new Error(`missing template wrapper: ${body}`);
43
+ } finally {
44
+ server.close();
45
+ process.chdir(prev);
46
+ }
47
+ });
48
+ pass('wildcard custom route renders .page.html via SSR');
49
+ } catch(e) {
50
+ fail(e.message);
51
+ }
52
+ },
53
+
54
+ 'wildcard custom route renders index.page.html for directory path': async ({pass, fail}) => {
55
+ try {
56
+ await withTempDir(async (dir) => {
57
+ const template = '<html><body><location name="main" /></body></html>';
58
+ const page = '<page template="default"><content location="main"><h1>Section Index</h1></content></page>';
59
+
60
+ await write(dir, 'admin/default.template.html', template);
61
+ await write(dir, 'admin/users/index.page.html', page);
62
+ await write(dir, 'public/index.html', '<h1>root</h1>');
63
+
64
+ const prev = process.cwd();
65
+ process.chdir(dir);
66
+ const {server, port} = await startServer(dir, {root: 'public'}, {
67
+ customRoutes: {'/admin/**': '../admin/**'},
68
+ templating: {ssr: true}
69
+ });
70
+
71
+ try {
72
+ const r = await httpGet(`http://localhost:${port}/admin/users`);
73
+ if(r.res.statusCode !== 200) throw new Error(`expected 200, got ${r.res.statusCode}`);
74
+ const body = r.body.toString();
75
+ if(!body.includes('<h1>Section Index</h1>')) throw new Error(`missing page content: ${body}`);
76
+ } finally {
77
+ server.close();
78
+ process.chdir(prev);
79
+ }
80
+ });
81
+ pass('wildcard custom route renders index.page.html for directory path');
82
+ } catch(e) {
83
+ fail(e.message);
84
+ }
85
+ },
86
+
87
+ 'SSR not attempted for custom route when templating.ssr is false': async ({pass, fail}) => {
88
+ try {
89
+ await withTempDir(async (dir) => {
90
+ const template = '<html><body><location name="main" /></body></html>';
91
+ const page = '<page template="default"><content location="main"><h1>Should Not Render</h1></content></page>';
92
+
93
+ await write(dir, 'admin/default.template.html', template);
94
+ await write(dir, 'admin/dashboard.page.html', page);
95
+ await write(dir, 'public/index.html', '<h1>root</h1>');
96
+
97
+ const prev = process.cwd();
98
+ process.chdir(dir);
99
+ const {server, port} = await startServer(dir, {root: 'public'}, {
100
+ customRoutes: {'/admin/**': '../admin/**'},
101
+ templating: {ssr: false}
102
+ });
103
+
104
+ try {
105
+ const r = await httpGet(`http://localhost:${port}/admin/dashboard`);
106
+ if(r.res.statusCode !== 404) throw new Error(`expected 404, got ${r.res.statusCode}`);
107
+ } finally {
108
+ server.close();
109
+ process.chdir(prev);
110
+ }
111
+ });
112
+ pass('SSR not attempted when templating.ssr is false');
113
+ } catch(e) {
114
+ fail(e.message);
115
+ }
116
+ },
117
+
118
+ 'wildcard custom route SSR uses custom root for template/fragment lookup': async ({pass, fail}) => {
119
+ try {
120
+ await withTempDir(async (dir) => {
121
+ const template = '<html><fragment name="nav" /><location name="main" /></html>';
122
+ const fragment = '<nav>Custom Nav</nav>';
123
+ const page = '<page template="default"><content location="main"><p>Content</p></content></page>';
124
+
125
+ await write(dir, 'admin/default.template.html', template);
126
+ await write(dir, 'admin/nav.fragment.html', fragment);
127
+ await write(dir, 'admin/about.page.html', page);
128
+ await write(dir, 'public/index.html', '<h1>root</h1>');
129
+
130
+ const prev = process.cwd();
131
+ process.chdir(dir);
132
+ const {server, port} = await startServer(dir, {root: 'public'}, {
133
+ customRoutes: {'/admin/**': '../admin/**'},
134
+ templating: {ssr: true}
135
+ });
136
+
137
+ try {
138
+ const r = await httpGet(`http://localhost:${port}/admin/about`);
139
+ if(r.res.statusCode !== 200) throw new Error(`expected 200, got ${r.res.statusCode}`);
140
+ const body = r.body.toString();
141
+ if(!body.includes('<nav>Custom Nav</nav>')) throw new Error(`fragment not resolved: ${body}`);
142
+ if(!body.includes('<p>Content</p>')) throw new Error(`page content missing: ${body}`);
143
+ } finally {
144
+ server.close();
145
+ process.chdir(prev);
146
+ }
147
+ });
148
+ pass('custom route SSR uses custom root for template/fragment lookup');
149
+ } catch(e) {
150
+ fail(e.message);
151
+ }
152
+ },
153
+
154
+ 'preRender renders .page.html files in wildcard custom route directory': async ({pass, fail}) => {
155
+ try {
156
+ await withTempDir(async (dir) => {
157
+ const template = '<html><body><location name="main" /></body></html>';
158
+ const page = '<page template="default"><content location="main"><h1>Pre-rendered</h1></content></page>';
159
+
160
+ await write(dir, 'admin/default.template.html', template);
161
+ await write(dir, 'admin/info.page.html', page);
162
+ await write(dir, 'public/index.html', '<h1>root</h1>');
163
+
164
+ const prev = process.cwd();
165
+ process.chdir(dir);
166
+ const {server, port} = await startServer(dir, {root: 'public'}, {
167
+ customRoutes: {'/admin/**': '../admin/**'},
168
+ templating: {preRender: true}
169
+ });
170
+
171
+ try {
172
+ // preRender should have written admin/info.html
173
+ const {readFile} = await import('fs/promises');
174
+ const rendered = await readFile(path.join(dir, 'admin', 'info.html'), 'utf8');
175
+ if(!rendered.includes('<h1>Pre-rendered</h1>')) throw new Error(`missing content: ${rendered}`);
176
+ // Serving the static pre-rendered file should also work
177
+ const r = await httpGet(`http://localhost:${port}/admin/info.html`);
178
+ if(r.res.statusCode !== 200) throw new Error(`expected 200, got ${r.res.statusCode}`);
179
+ } finally {
180
+ server.close();
181
+ process.chdir(prev);
182
+ }
183
+ });
184
+ pass('preRender renders .page.html files in wildcard custom route directory');
185
+ } catch(e) {
186
+ fail(e.message);
187
+ }
188
+ }
189
+ };