Skip to content

Commit 2e247a4

Browse files
author
Ben Demboski
committed
Fix URL caching
1 parent 30a67ca commit 2e247a4

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

lib/handler.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { join } = require('path');
22
const fs = require('fs');
3-
const url = require('url');
3+
const { parse: urlParse } = require('url');
44

55
module.exports = function createHandler({
66
cwd,
@@ -12,11 +12,11 @@ module.exports = function createHandler({
1212
indexPath = indexPath || join(cwd, directoryIndexFile);
1313
const cache = {};
1414

15-
return (request, callback) => {
15+
return ({ url }, callback) => {
1616
let {
1717
host,
1818
pathname,
19-
} = url.parse(request.url);
19+
} = urlParse(url);
2020

2121
pathname = pathname || '';
2222

tests/unit/handler-test.js

+33-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const sinon = require('sinon');
33
const Promise = require('bluebird');
44
const fs = require('fs');
55
const { join, resolve } = require('path');
6-
const createHandler = require('../../lib/handler');
6+
const subject = require('../../lib/handler');
77

88
describe('handler', () => {
99
let sandbox;
@@ -27,17 +27,20 @@ describe('handler', () => {
2727
mockFiles = [];
2828
});
2929

30-
// Create a handler with the specified options and call it with the given URL
31-
function handlerExec(url, options = {}) {
32-
return new Promise((resolve, reject) => {
33-
let handler = createHandler(Object.assign({
34-
cwd: '.',
35-
name: 'serve',
36-
endpoint: 'dist',
37-
directoryIndexFile: 'index.html',
38-
indexPath: undefined,
39-
}, options));
30+
// Create a handler with the specified options
31+
function createHandler(options) {
32+
return subject(Object.assign({
33+
cwd: '.',
34+
name: 'serve',
35+
endpoint: 'dist',
36+
directoryIndexFile: 'index.html',
37+
indexPath: undefined,
38+
}, options));
39+
}
4040

41+
// Call the handler with the given URL
42+
function callHandler(handler, url) {
43+
return new Promise((resolve, reject) => {
4144
handler({ url }, ({ path, error }) => {
4245
if (path !== undefined) {
4346
resolve(path);
@@ -48,13 +51,32 @@ describe('handler', () => {
4851
});
4952
}
5053

54+
// Create a handler with the specified options and call it with the given URL
55+
function handlerExec(url, options = {}) {
56+
return callHandler(createHandler(options), url);
57+
}
58+
5159
it('works', () => {
5260
mockFiles.push('script.js');
5361
return handlerExec('serve://dist/script.js').then(path => {
5462
assert.equal(path, 'script.js');
5563
});
5664
});
5765

66+
it('works with multiple requests', () => {
67+
mockFiles.push('script1.js');
68+
mockFiles.push('script2.js');
69+
70+
let handler = createHandler();
71+
return callHandler(handler, 'serve://dist/script1.js').then(path => {
72+
assert.equal(path, 'script1.js');
73+
74+
return callHandler(handler, 'serve://dist/script2.js');
75+
}).then(path => {
76+
assert.equal(path, 'script2.js');
77+
});
78+
});
79+
5880
it('serves directoryIndexFile from the root', () => {
5981
mockFiles.push('foo.html');
6082
return handlerExec('serve://dist', {

0 commit comments

Comments
 (0)