Skip to content

Commit

Permalink
Add mechanism to specify custom headers for HTML test pages
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
robertknight committed Mar 31, 2022
1 parent 9bf96c3 commit f8100ac
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
7 changes: 7 additions & 0 deletions dev-server/documents/html/xhtml-test.mustache
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
The dev server serves all HTML content with the text/html MIME type
by default. Much (most?) "XHTML" content on the web is served this way.
However serving with the correct MIME type affects browser behavior, so
it is important to use that here.
-->
<!-- Header: Content-Type: application/xhtml+xml -->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>XHTML test document</title>
Expand Down
38 changes: 28 additions & 10 deletions dev-server/serve-dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,27 @@ function renderScript(context) {
return Mustache.render(scriptTemplate, context);
}

/**
* Read tags in test pages specifying custom headers to serve the content with.
*
* These tags look like `<!-- Header: <Key>: <Value> -->`.
*
* @param {string} content
* @return {[key: string, value: string][]}
*/
function readCustomHeaderTags(content) {
return content
.split('\n')
.map(line => {
const keyValue = line.match(/<!--\s*Header:\s*([A-Za-z-]+):(.*)-->/);
if (!keyValue) {
return null;
}
return [keyValue[1], keyValue[2]];
})
.filter(kv => kv !== null);
}

/**
* Build context for rendering templates in the defined views directory.
*
Expand Down Expand Up @@ -106,16 +127,13 @@ function serveDev(port, config) {

// Serve HTML documents with injected client script
app.get('/document/:document', (req, res, next) => {
// All HTML documents are served with the `text/html` mime type by default,
// even though some declare themselves to be XHTML. This mirrors how most
// content on the web is served. However we do have some test pages that
// need to be served with the correct XHTML mime type, as that alters
// browser behavior. See https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/XHTML.
if (req.params.document.startsWith('xhtml-')) {
res.set('Content-Type', 'application/xhtml+xml');
}

if (fs.existsSync(`${HTML_PATH}${req.params.document}.mustache`)) {
const path = `${HTML_PATH}${req.params.document}.mustache`;
if (fs.existsSync(path)) {
const content = fs.readFileSync(path, 'utf8');
const headers = readCustomHeaderTags(content);
for (let [key, value] of headers) {
res.set(key, value);
}
res.render(req.params.document, templateContext(config));
} else {
next();
Expand Down

0 comments on commit f8100ac

Please sign in to comment.