Skip to content

Commit f8100ac

Browse files
committed
Add mechanism to specify custom headers for HTML test pages
Add support for specifying custom headers to serve test pages with via inline `<!-- Header: <Key>:<Value> -->` comments in the HTML/XML, and change the existing XHTML test case to use it.
1 parent 9bf96c3 commit f8100ac

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

dev-server/documents/html/xhtml-test.mustache

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
The dev server serves all HTML content with the text/html MIME type
4+
by default. Much (most?) "XHTML" content on the web is served this way.
5+
However serving with the correct MIME type affects browser behavior, so
6+
it is important to use that here.
7+
-->
8+
<!-- Header: Content-Type: application/xhtml+xml -->
29
<html xmlns="http://www.w3.org/1999/xhtml">
310
<head>
411
<title>XHTML test document</title>

dev-server/serve-dev.js

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,27 @@ function renderScript(context) {
4141
return Mustache.render(scriptTemplate, context);
4242
}
4343

44+
/**
45+
* Read tags in test pages specifying custom headers to serve the content with.
46+
*
47+
* These tags look like `<!-- Header: <Key>: <Value> -->`.
48+
*
49+
* @param {string} content
50+
* @return {[key: string, value: string][]}
51+
*/
52+
function readCustomHeaderTags(content) {
53+
return content
54+
.split('\n')
55+
.map(line => {
56+
const keyValue = line.match(/<!--\s*Header:\s*([A-Za-z-]+):(.*)-->/);
57+
if (!keyValue) {
58+
return null;
59+
}
60+
return [keyValue[1], keyValue[2]];
61+
})
62+
.filter(kv => kv !== null);
63+
}
64+
4465
/**
4566
* Build context for rendering templates in the defined views directory.
4667
*
@@ -106,16 +127,13 @@ function serveDev(port, config) {
106127

107128
// Serve HTML documents with injected client script
108129
app.get('/document/:document', (req, res, next) => {
109-
// All HTML documents are served with the `text/html` mime type by default,
110-
// even though some declare themselves to be XHTML. This mirrors how most
111-
// content on the web is served. However we do have some test pages that
112-
// need to be served with the correct XHTML mime type, as that alters
113-
// browser behavior. See https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/XHTML.
114-
if (req.params.document.startsWith('xhtml-')) {
115-
res.set('Content-Type', 'application/xhtml+xml');
116-
}
117-
118-
if (fs.existsSync(`${HTML_PATH}${req.params.document}.mustache`)) {
130+
const path = `${HTML_PATH}${req.params.document}.mustache`;
131+
if (fs.existsSync(path)) {
132+
const content = fs.readFileSync(path, 'utf8');
133+
const headers = readCustomHeaderTags(content);
134+
for (let [key, value] of headers) {
135+
res.set(key, value);
136+
}
119137
res.render(req.params.document, templateContext(config));
120138
} else {
121139
next();

0 commit comments

Comments
 (0)