Skip to content

Commit

Permalink
fix(web-server): Ensure filesPromise is always resolvable
Browse files Browse the repository at this point in the history
Closes #1544
  • Loading branch information
dignifiedquire committed Aug 5, 2015
1 parent 184f12e commit 892fa89
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/file-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,14 +221,14 @@ Object.defineProperty(List.prototype, 'files', {
return pattern.served
})
.map(function (p) {
return from(self.buckets.get(p.pattern)).sort(byPath)
return from(self.buckets.get(p.pattern) || []).sort(byPath)
})

var included = this._patterns.filter(function (pattern) {
return pattern.included
})
.map(function (p) {
return from(self.buckets.get(p.pattern)).sort(byPath)
return from(self.buckets.get(p.pattern) || []).sort(byPath)
})

var uniqFlat = function (list) {
Expand Down
2 changes: 1 addition & 1 deletion lib/middleware/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var PromiseContainer = function () {
var promise

this.then = function (success, error) {
return promise.then(success, error)
return promise.then(success).catch(error)
}

this.set = function (newPromise) {
Expand Down
6 changes: 5 additions & 1 deletion lib/web-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,16 @@ var createCustomHandler = function (customFileHandlers, /* config.basePath */ ba

createCustomHandler.$inject = ['customFileHandlers', 'config.basePath']

var createWebServer = function (injector, emitter) {
var createWebServer = function (injector, emitter, fileList) {
var config = injector.get('config')
var serveStaticFile = common.createServeFile(fs, path.normalize(__dirname + '/../static'))
var serveFile = common.createServeFile(fs)
var filesPromise = new common.PromiseContainer()

// Set an empty list of files to avoid race issues with
// file_list_modified not having been emitted yet
filesPromise.set(Promise.resolve(fileList.files))

emitter.on('file_list_modified', function (files) {
filesPromise.set(Promise.resolve(files))
})
Expand Down
17 changes: 15 additions & 2 deletions test/unit/web-server.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('web-server', () => {
config: ['value', {basePath: '/base/path', urlRoot: '/'}],
customFileHandlers: ['value', customFileHandlers],
emitter: ['value', emitter],
fileList: ['value', null],
fileList: ['value', {files: {served: [], included: []}}],
capturedBrowsers: ['value', null],
reporter: ['value', null],
executor: ['value', null],
Expand Down Expand Up @@ -82,6 +82,19 @@ describe('web-server', () => {
.expect(200, 'new-js-source')
})

it('should serve no files when they are not available yet', () => {
return request(server)
.get('/base/new.js')
.expect(404)
.then(() => {
servedFiles(new Set([new File('/base/path/new.js')]))

return request(server)
.get('/base/new.js')
.expect(200, 'new-js-source')
})
})

it('should load custom handlers', () => {
servedFiles(new Set())

Expand Down Expand Up @@ -125,7 +138,7 @@ describe('web-server', () => {
config: ['value', {basePath: '/base/path', urlRoot: '/', protocol: 'https:', httpsServerOptions: credentials}],
customFileHandlers: ['value', customFileHandlers],
emitter: ['value', emitter],
fileList: ['value', null],
fileList: ['value', {files: {served: [], included: []}}],
capturedBrowsers: ['value', null],
reporter: ['value', null],
executor: ['value', null],
Expand Down

0 comments on commit 892fa89

Please sign in to comment.