forked from OKEAMAH/angular.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.js
More file actions
44 lines (40 loc) · 1.26 KB
/
middleware.js
File metadata and controls
44 lines (40 loc) · 1.26 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
'use strict';
var url = require('url');
var util = require('./util');
var fixture = require('./fixture');
module.exports = middlewareFactory;
function middlewareFactory(base) {
base = base || '/e2e';
while (base.length && base[base.length - 1] === '/') base = base.slice(0, base.length - 1);
var fixture_regexp = new RegExp('^' + base + '/fixtures/([a-zA-Z0-9_-]+)(/(index.html)?)?$');
var static_regexp = new RegExp('^' + base + '/fixtures/([a-zA-Z0-9_-]+)(/.*)$');
return function(req, res, next) {
var match;
var basicUrl = req.url;
var idx = basicUrl.indexOf('?');
if (idx >= 0) {
basicUrl = basicUrl.slice(0, idx);
}
if ((match = fixture_regexp.exec(basicUrl))) {
if (util.testExists(match[1])) {
try {
var query = url.parse(req.url, true).query;
res.write(fixture.generate(match[1], query));
res.end();
} catch (e) {
return next(e);
}
} else {
return next('Fixture ' + match[1] + ' not found.');
}
} else if ((match = static_regexp.exec(basicUrl))) {
var rewritten = util.rewriteTestFile(match[1], match[2]);
if (rewritten !== false) {
req.url = rewritten;
}
next();
} else {
return next();
}
};
}