Skip to content

Commit

Permalink
fix: show default page if app has no public/ (#627)
Browse files Browse the repository at this point in the history
  • Loading branch information
minrwhite authored and gr2m committed Nov 3, 2016
1 parent 8cc6897 commit 047a8ff
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 5 deletions.
3 changes: 3 additions & 0 deletions cli/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var yargs = require('yargs')

var getAppDefaults = require('./app-defaults')
var getHoodieDefaults = require('./hoodie-defaults')
var webrootLocator = require('./webroot-locator')

function getCliOptions (projectPath) {
var hoodieDefaults = getHoodieDefaults()
Expand Down Expand Up @@ -98,6 +99,8 @@ function getCliOptions (projectPath) {
options.address = options.bindAddress
}

options.public = webrootLocator(options.public)

// rc & yargs are setting keys we are not interested in, like in-memory or _
// so we only pick the relevant ones based on they keys of the default options.
return pick(options, Object.keys(hoodieDefaults))
Expand Down
12 changes: 12 additions & 0 deletions cli/webroot-locator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = locateWebroot

var fs = require('fs')
var pathModule = require('path')

function locateWebroot (path) {
if (fs.existsSync(path) && fs.statSync(path).isDirectory()) {
return path
}

return pathModule.resolve(__dirname, '../public')
}
2 changes: 1 addition & 1 deletion server/plugins/public.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var fs = require('fs')
var path = require('path')

function register (server, options, next) {
var publicFolder = options.config.public || 'public'
var publicFolder = options.config.paths.public || 'public'
var app = path.join(publicFolder, 'index.html')
var hoodieVersion
try {
Expand Down
3 changes: 2 additions & 1 deletion test/integration/force-gzip-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ var hapiPluginOptions = {
register: hoodie,
options: {
inMemory: true,
loglevel: 'error'
loglevel: 'error',
paths: {}
}
}

Expand Down
3 changes: 2 additions & 1 deletion test/integration/handle-404-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ var hapiPluginOptions = {
register: hoodie,
options: {
inMemory: true,
loglevel: 'error'
loglevel: 'error',
paths: {}
}
}

Expand Down
3 changes: 2 additions & 1 deletion test/integration/handle-hoodie-request-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ var hapiPluginOptions = {
register: hoodie,
options: {
inMemory: true,
loglevel: 'error'
loglevel: 'error',
paths: {}
}
}

Expand Down
3 changes: 2 additions & 1 deletion test/integration/smoke-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ var hapiPluginOptions = {
register: hoodie,
options: {
inMemory: true,
loglevel: 'error'
loglevel: 'error',
paths: {}
}
}

Expand Down
13 changes: 13 additions & 0 deletions test/unit/cli/options-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ var packageJsonMock = {
version: '1.0.0'
}

var mockWebrootLocator = simple.stub()

function createCliOptionsProxy (yargsApi) {
return proxyquire('../../../cli/options', {
'npmlog': { warn: simple.spy() },
Expand All @@ -48,6 +50,7 @@ function createCliOptionsProxy (yargsApi) {
}
},
'./app-defaults': mockAppDefaults(),
'./webroot-locator': mockWebrootLocator,
'yargs': yargsApi,
'../package.json': packageJsonMock
})
Expand Down Expand Up @@ -99,6 +102,16 @@ test('config', function (group) {
t.end()
})

group.test('public option', function (t) {
var fallbackValue = 'fallback-public'
mockWebrootLocator.returnWith(fallbackValue)

var options = getCliOptions(yargsApi)

t.equal(options.public, fallbackValue, 'uses the value returned from webroot locator')
t.end()
})

group.end()
})

Expand Down
74 changes: 74 additions & 0 deletions test/unit/cli/webroot-locator-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
var proxyquire = require('proxyquire').noCallThru()
var simple = require('simple-mock')
var test = require('tap').test
var path = require('path')

var fsMock = {
'existsSync': simple.stub(),
'statSync': simple.stub()
}

var statsMock = {
'isDirectory': simple.stub()
}

test('webroot locator', function (group) {
var webrootLocator = proxyquire('../../../cli/webroot-locator', {
fs: fsMock
})

group.afterEach(function (done) {
fsMock.existsSync.actions = []
fsMock.existsSync.reset()
fsMock.statSync.actions = []
fsMock.statSync.reset()
statsMock.isDirectory.actions = []
statsMock.isDirectory.reset()
done()
})

group.test('will return the passed location if it exists and is a directory', function (t) {
fsMock.existsSync.returnWith(true)
fsMock.statSync.returnWith(statsMock)
statsMock.isDirectory.returnWith(true)

var configuredLocation = 'i-exist'
var webroot = webrootLocator(configuredLocation)

t.equal(fsMock.existsSync.lastCall.args[0], configuredLocation, 'path existence check')
t.equal(statsMock.isDirectory.callCount, 1, 'path directory check')
t.equal(webroot, configuredLocation, 'correct location returned')

t.end()
})

group.test('will return the default if the passed location exists but is not a directory', function (t) {
fsMock.existsSync.returnWith(true)
fsMock.statSync.returnWith(statsMock)
statsMock.isDirectory.returnWith(false)

var configuredLocation = 'im-a-file'
var webroot = webrootLocator(configuredLocation)

t.equal(fsMock.existsSync.lastCall.args[0], configuredLocation, 'path existence check')
t.equal(statsMock.isDirectory.callCount, 1, 'path directory check')
t.equal(webroot, path.resolve(__dirname, '../../../public'), 'correct location returned')

t.end()
})

group.test('will return the default if the passed location does not exist', function (t) {
fsMock.existsSync.returnWith(false)

var configuredLocation = 'i-cant-be-found'
var webroot = webrootLocator(configuredLocation)

t.equal(fsMock.existsSync.lastCall.args[0], configuredLocation, 'path existence check')
t.equal(fsMock.statSync.callCount, 0, 'no stat check')
t.equal(webroot, path.resolve(__dirname, '../../../public'), 'correct location returned')

t.end()
})

group.end()
})
35 changes: 35 additions & 0 deletions test/unit/server/public-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var simple = require('simple-mock')
var test = require('tap').test

test('public', function (group) {
var serverMock

group.beforeEach(function (done) {
serverMock = {
register: simple.stub(),
route: simple.stub(),
ext: simple.stub()
}

require('../../../server/plugins/public').register(serverMock, {
config: {
paths: {
public: 'my-custom-public-directory'
}
}
}, function () { return })

done()
})

group.test('configured public route', function (t) {
t.equal(serverMock.route.callCount, 1, 'route was called')
t.type(serverMock.route.lastCall.args[0], Array, 'route was called with an array')
t.contains(serverMock.route.lastCall.args[0][0],
{'handler': {'directory': {'path': /^my-custom-public-directory$/}}},
'route was configured to correct public directory')
t.end()
})

group.end()
})

0 comments on commit 047a8ff

Please sign in to comment.