-
-
Notifications
You must be signed in to change notification settings - Fork 257
/
test_helpers.js
214 lines (170 loc) · 5.52 KB
/
test_helpers.js
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
'use strict';
const ENV = process.env;
// We use BCDN_HEADERS to distinguish between production and debug CDN headers
ENV.BCDN_HEADERS = ENV.BCDN_HEADERS || 'production';
const assert = require('assert').strict;
const url = require('url');
const escapeStringRegexp = require('escape-string-regexp');
const { htmlEncode } = require('htmlencode');
const mockDate = require('mockdate');
const request = require('request');
const validator = require('html-validator');
const app = require('../app');
const config = require('../config').app;
// The server object holds the server instance across all tests;
// We start it in the first test and close it in the last one,
// otherwise test time increases a lot (more than 3x)
let server = {};
mockDate.set('03/05/2018');
function getExtension(str) {
// use two enclosing parts; one for the dot (.)
// and one for the extension itself.
// So, the result we want is the third Array element,
// since the first one is the whole match, the second one
// returns the first captured match, etc.
const match = str.match(/(\.)([a-zA-Z0-9]+)$/);
return match && match[2];
}
function cleanEndpoint(endpoint = '/') {
// Maybe we should use node's `url` in this function
if (!endpoint.startsWith('/')) {
endpoint = `/${endpoint}`;
}
if (!endpoint.endsWith('/') && !endpoint.includes('?') && !getExtension(endpoint)) {
endpoint = `${endpoint}/`;
}
return endpoint;
}
function getPort() {
// don't use configured port
const port = config.port < 3000 ? config.port + 3000 : config.port + 1;
return port;
}
function getURI(endpoint) {
const endp = cleanEndpoint(endpoint);
const port = getPort();
return `http://localhost:${port}${endp}`;
}
function startServer() {
const port = getPort();
if (server.listening) {
return server;
}
server = app.listen(port);
return server;
}
function stopServer(done) {
return server.listening && server.close(done);
}
function prefetch(uri, cb) {
const reqOpts = {
uri,
forever: true, // for 'connection: Keep-Alive'
followRedirect: false,
gzip: true,
strictSSL: true
};
if (ENV.BCDN_HEADERS === 'debug') {
reqOpts.headers = {
Pragma: 'debug'
};
}
request.get(reqOpts, (err, res) => {
if (err) {
return cb(err);
}
return cb(res);
});
}
function assertValidHTML(res) {
const options = {
data: res.body,
format: 'text'
};
return new Promise((resolve, reject) => {
validator(options)
.then((data) => {
// Return when successful.
if (data.includes('The document validates')) {
return resolve();
}
// Formatting output for readability.
const errStr = `HTML Validation for '${res.request.path}' failed with:\n\t${data.replace('Error: ', '').split('\n').join('\n\t')}\n`;
return reject(new Error(errStr));
})
.catch((error) => reject(error));
});
}
function assertItWorks(statusCode, cb) {
try {
assert.equal(statusCode, 200);
return cb();
} catch (error) {
return cb(error);
}
}
function assertPageHeader(txt, res, cb) {
const escapedTxt = escapeStringRegexp(txt);
const re = new RegExp(`<h[1-6]( class=".+")?>(${escapedTxt})(</h[1-6]>)`);
assert.ok(re.test(res.body), `Expects page header to be "${txt}"`);
cb();
}
function assertCanonicalUrl(str, res, cb) {
const expected = `<link rel="canonical" href="${url.resolve(config.siteurl, str)}">`;
assert.ok(res.body.includes(expected), 'Expects page to have a canonical URL');
cb();
}
function assertPageBodyClass(bodyClass, res, cb) {
const expected = `<body class="${bodyClass}`;
assert.ok(res.body.includes(expected), `Expects page body class to include "${bodyClass}"`);
cb();
}
function assertAuthors(res, cb) {
const authors = config.authors.map((author) => author.name).join(', ');
const authorsStr = `<meta name="author" content="${authors}">`;
assert.ok(res.body.includes(authorsStr), `Expects response body to include "${authorsStr}"`);
cb();
}
function cssHTML(uri, sri) {
return htmlEncode(`<link href="${uri}" rel="stylesheet" integrity="${sri}" crossorigin="anonymous">`);
}
function cssPug(uri, sri) {
return htmlEncode(`link(href="${uri}", rel="stylesheet", integrity="${sri}", crossorigin="anonymous")`);
}
function cssHAML(uri, sri) {
return htmlEncode(`%link{href: "${uri}", rel: "stylesheet", integrity: "${sri}", crossorigin: "anonymous"}`);
}
function jsHTML(uri, sri) {
return htmlEncode(`<script src="${uri}" integrity="${sri}" crossorigin="anonymous"></script>`);
}
function jsPug(uri, sri) {
return htmlEncode(`script(src="${uri}", integrity="${sri}", crossorigin="anonymous")`);
}
function jsHAML(uri, sri) {
return htmlEncode(`%script{src: "${uri}", integrity: "${sri}", crossorigin: "anonymous"}`);
}
module.exports = {
getExtension,
getURI,
startServer,
stopServer,
assert: {
authors: assertAuthors,
canonicalUrl: assertCanonicalUrl,
bodyClass: assertPageBodyClass,
itWorks: assertItWorks,
pageHeader: assertPageHeader,
validHTML: assertValidHTML
},
prefetch,
css: {
pug: cssPug,
html: cssHTML,
haml: cssHAML
},
javascript: {
pug: jsPug,
html: jsHTML,
haml: jsHAML
}
};