Skip to content

Commit 01dec36

Browse files
committed
Start with some tests!
1 parent 12e9a27 commit 01dec36

File tree

6 files changed

+104
-28
lines changed

6 files changed

+104
-28
lines changed

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"env": {
3-
"node": true
3+
"node": true,
4+
"mocha": true
45
},
56
"rules": {
67
"indent": [2, "tab", { "SwitchCase": 1 }],

lib/GetFilenameFromUrl.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var pathJoin = require("./PathJoin");
2+
3+
function getFilenameFromUrl(publicPath, outputPath, url) {
4+
// publicPrefix is the folder our bundle should be in
5+
var localPrefix = publicPath || "/";
6+
if(url.indexOf(localPrefix) !== 0) {
7+
if(/^(https?:)?\/\//.test(localPrefix)) {
8+
localPrefix = "/" + localPrefix.replace(/^(https?:)?\/\/[^\/]+\//, "");
9+
// fast exit if another directory requested
10+
if(url.indexOf(localPrefix) !== 0) return false;
11+
} else return false;
12+
}
13+
// get filename from request
14+
var filename = url.substr(localPrefix.length);
15+
if(filename.indexOf("?") >= 0) {
16+
filename = filename.substr(0, filename.indexOf("?"));
17+
}
18+
return filename ? pathJoin(outputPath, filename) : outputPath;
19+
}
20+
21+
module.exports = getFilenameFromUrl;

lib/PathJoin.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function pathJoin(a, b) {
2+
return a == "/" ? "/" + b : (a || "") + "/" + b;
3+
}
4+
5+
module.exports = pathJoin;

middleware.js

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
var MemoryFileSystem = require("memory-fs");
66
var mime = require("mime");
77
var parseRange = require("range-parser");
8+
var getFilenameFromUrl = require("./lib/GetFilenameFromUrl");
9+
var pathJoin = require("./lib/PathJoin");
810

911
var HASH_REGEXP = /[0-9a-f]{10,}/;
1012

@@ -146,28 +148,6 @@ module.exports = function(compiler, options) {
146148
}
147149
}
148150

149-
function pathJoin(a, b) {
150-
return a == "/" ? "/" + b : (a || "") + "/" + b;
151-
}
152-
153-
function getFilenameFromUrl(url) {
154-
// publicPrefix is the folder our bundle should be in
155-
var localPrefix = options.publicPath || "/";
156-
if(url.indexOf(localPrefix) !== 0) {
157-
if(/^(https?:)?\/\//.test(localPrefix)) {
158-
localPrefix = "/" + localPrefix.replace(/^(https?:)?\/\/[^\/]+\//, "");
159-
// fast exit if another directory requested
160-
if(url.indexOf(localPrefix) !== 0) return false;
161-
} else return false;
162-
}
163-
// get filename from request
164-
var filename = url.substr(localPrefix.length);
165-
if(filename.indexOf("?") >= 0) {
166-
filename = filename.substr(0, filename.indexOf("?"));
167-
}
168-
return filename ? pathJoin(compiler.outputPath, filename) : compiler.outputPath;
169-
}
170-
171151
function handleRangeHeaders(content, req, res) {
172152
res.setHeader('Accept-Ranges', 'bytes');
173153
if(req.headers.range) {
@@ -210,7 +190,7 @@ module.exports = function(compiler, options) {
210190
return goNext();
211191
}
212192

213-
var filename = getFilenameFromUrl(req.url);
193+
var filename = getFilenameFromUrl(options.publicPath, compiler.outputPath, req.url);
214194
if(filename === false) return goNext();
215195

216196
// in lazy mode, rebuild on bundle request
@@ -261,7 +241,7 @@ module.exports = function(compiler, options) {
261241
}
262242
}
263243

264-
webpackDevMiddleware.getFilenameFromUrl = getFilenameFromUrl;
244+
webpackDevMiddleware.getFilenameFromUrl = getFilenameFromUrl.bind(this, options.publicPath, compiler.outputPath);
265245

266246
webpackDevMiddleware.waitUntilValid = function(callback) {
267247
callback = callback || function() {};

package.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
"range-parser": "^1.0.3"
1313
},
1414
"devDependencies": {
15-
"eslint": "^3.4.0"
15+
"eslint": "^3.4.0",
16+
"mocha": "^3.0.2",
17+
"should": "^11.1.0"
1618
},
1719
"license": "MIT",
1820
"engines": {
@@ -24,9 +26,15 @@
2426
"type": "git",
2527
"url": "https://github.com/webpack/webpack-dev-middleware.git"
2628
},
29+
"files": [
30+
"middleware.js",
31+
"lib/"
32+
],
2733
"scripts": {
28-
"lint": "eslint *.js",
34+
"lint": "eslint *.js test",
35+
"pretest": "npm run -s lint",
36+
"test": "mocha --harmony --full-trace --check-leaks",
2937
"beautify": "npm run lint -- --fix",
30-
"travis": "npm run lint && node middleware.js"
38+
"travis": "npm test && node middleware.js"
3139
}
3240
}

test/GetFilenameFromUrl.test.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
var should = require("should");
2+
var getFilenameFromUrl = require("../lib/GetFilenameFromUrl");
3+
4+
function testUrl(options) {
5+
var url = getFilenameFromUrl(options.publicPath, options.outputPath, options.url);
6+
should.strictEqual(url, options.expected);
7+
}
8+
9+
describe("GetFilenameFromUrl", function() {
10+
it("should handle urls", function() {
11+
var results = [
12+
{
13+
url: "/foo.js",
14+
outputPath: "/",
15+
publicPath: "/",
16+
expected: "/foo.js"
17+
}, {
18+
url: "/0.19dc5d417382d73dd190.hot-update.js",
19+
outputPath: "/",
20+
publicPath: "http://localhost:8080/",
21+
expected: "/0.19dc5d417382d73dd190.hot-update.js"
22+
}, {
23+
url: "/bar.js",
24+
outputPath: "/",
25+
publicPath: "https://localhost:8080/",
26+
expected: "/bar.js"
27+
}, {
28+
url: "/test.html?foo=bar",
29+
outputPath: "/",
30+
publicPath: "/",
31+
expected: "/test.html",
32+
}, {
33+
url: "/a.js",
34+
outputPath: "/dist",
35+
publicPath: "/",
36+
expected: "/dist/a.js",
37+
}, {
38+
url: "/b.js",
39+
outputPath: "/",
40+
publicPath: undefined,
41+
expected: "/b.js",
42+
}, {
43+
url: "/b.js",
44+
outputPath: undefined,
45+
publicPath: undefined,
46+
expected: "/b.js",
47+
}, {
48+
url: "/more/complex/path.js",
49+
outputPath: "/a",
50+
publicPath: "/",
51+
expected: "/a/more/complex/path.js",
52+
}, {
53+
url: "c.js",
54+
outputPath: "/dist",
55+
publicPath: "/",
56+
expected: false, // publicPath is not in url, so it should fail
57+
}
58+
];
59+
results.forEach(testUrl);
60+
});
61+
});

0 commit comments

Comments
 (0)