|
| 1 | +const { assert } = require('chai'); |
| 2 | +const sinon = require('sinon'); |
| 3 | +const Promise = require('bluebird'); |
| 4 | +const fs = require('fs'); |
| 5 | +const { join, resolve } = require('path'); |
| 6 | +const createHandler = require('../../lib/handler'); |
| 7 | + |
| 8 | +describe('handler', () => { |
| 9 | + let sandbox; |
| 10 | + let mockFiles; |
| 11 | + |
| 12 | + before(() => { |
| 13 | + sandbox = sinon.sandbox.create(); |
| 14 | + // Stub accessSync to act like only files in mockFiles exist |
| 15 | + sandbox.stub(fs, 'accessSync', path => { |
| 16 | + if (mockFiles.indexOf(path) === -1) { |
| 17 | + throw new Error('Doesn\'t exist'); |
| 18 | + } |
| 19 | + }); |
| 20 | + }); |
| 21 | + |
| 22 | + after(() => { |
| 23 | + sandbox.restore(); |
| 24 | + }); |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + mockFiles = []; |
| 28 | + }); |
| 29 | + |
| 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)); |
| 40 | + |
| 41 | + handler({ url }, ({ path, error }) => { |
| 42 | + if (path !== undefined) { |
| 43 | + resolve(path); |
| 44 | + } else { |
| 45 | + reject(error); |
| 46 | + } |
| 47 | + }); |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + it('works', () => { |
| 52 | + mockFiles.push('script.js'); |
| 53 | + return handlerExec('serve://dist/script.js').then(path => { |
| 54 | + assert.equal(path, 'script.js'); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + it('serves directoryIndexFile from the root', () => { |
| 59 | + mockFiles.push('foo.html'); |
| 60 | + return handlerExec('serve://dist', { |
| 61 | + directoryIndexFile: 'foo.html', |
| 62 | + }).then(path => { |
| 63 | + assert.equal(path, 'foo.html'); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + it('serves directoryIndexFile for missing files', () => { |
| 68 | + mockFiles.push('foo.html'); |
| 69 | + return handlerExec('serve://dist/missing.js', { |
| 70 | + directoryIndexFile: 'foo.html', |
| 71 | + }).then(path => { |
| 72 | + assert.equal(path, 'foo.html'); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + it('respects relative cwd', () => { |
| 77 | + mockFiles.push(join('foo', 'bar', 'script.js')); |
| 78 | + return handlerExec('serve://dist/script.js', { |
| 79 | + cwd: join('foo', 'bar'), |
| 80 | + }).then(path => { |
| 81 | + assert.equal(path, join('foo', 'bar', 'script.js')); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + it('respects absolute cwd', () => { |
| 86 | + mockFiles.push(resolve('foo', 'bar', 'script.js')); |
| 87 | + return handlerExec('serve://dist/script.js', { |
| 88 | + cwd: resolve('foo', 'bar'), |
| 89 | + }).then(path => { |
| 90 | + assert.equal(path, resolve('foo', 'bar', 'script.js')); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + it('respects endpoint', () => { |
| 95 | + mockFiles.push('script.js'); |
| 96 | + return handlerExec('serve://custom/script.js', { |
| 97 | + endpoint: 'custom', |
| 98 | + }).then(path => { |
| 99 | + assert.equal(path, 'script.js'); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + it('respects indexPath for missing files', () => { |
| 104 | + mockFiles.push(join('foo', 'bar.html')); |
| 105 | + return handlerExec('serve://dist/missing.js', { |
| 106 | + indexPath: join('foo', 'bar.html'), |
| 107 | + }).then(path => { |
| 108 | + assert.equal(path, join('foo', 'bar.html')); |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + it('ignores hashes', () => { |
| 113 | + mockFiles.push('script.js'); |
| 114 | + return handlerExec('serve://dist/script.js#hash').then(path => { |
| 115 | + assert.equal(path, 'script.js'); |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + it('ignores query params', () => { |
| 120 | + mockFiles.push('script.js'); |
| 121 | + return handlerExec('serve://dist/script.js?query=param').then(path => { |
| 122 | + assert.equal(path, 'script.js'); |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + it('errors on an unrecognized endpoint', () => |
| 127 | + handlerExec('service://notdist/script.js').then(() => { |
| 128 | + assert.ok(false); |
| 129 | + }).catch(() => { |
| 130 | + assert.ok(true); |
| 131 | + }) |
| 132 | + ); |
| 133 | +}); |
0 commit comments