Skip to content

Commit

Permalink
Merge 848b13f into c12f2f3
Browse files Browse the repository at this point in the history
  • Loading branch information
jimlambie committed Apr 15, 2019
2 parents c12f2f3 + 848b13f commit 75cfabe
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 28 deletions.
8 changes: 6 additions & 2 deletions config.js
Expand Up @@ -378,8 +378,12 @@ const conf = convict({
},
forceDomain: {
doc: 'The domain to force requests to',
format: String,
default: ''
format: Object,
default: {
hostname: '',
port: 80,
type: 301
}
}
},
security: {
Expand Down
7 changes: 2 additions & 5 deletions dadi/lib/controller/forceDomain.js
Expand Up @@ -15,7 +15,7 @@ const forceDomain = function (options) {
return next()
}

statusCode = newRoute.type === 'temporary' ? 307 : 301
statusCode = newRoute.type

res.writeHead(statusCode, {
Location: newRoute.url
Expand All @@ -36,10 +36,7 @@ const domainRedirect = function (protocol, hostHeader, url, options) {
let rewrittenRoute
let route

options = Object.assign({}, options, {
protocol: 'http',
type: 'permanent'
})
options = Object.assign({}, { protocol: 'http', type: 301 }, options)

const hostHeaderParts = (hostHeader || '').split(':')
const hostname = hostHeaderParts[0] || ''
Expand Down
11 changes: 2 additions & 9 deletions dadi/lib/index.js
Expand Up @@ -136,15 +136,8 @@ Server.prototype.start = function (done) {
})

// add middleware for domain redirects
if (config.get('rewrites.forceDomain') !== '') {
const domain = config.get('rewrites.forceDomain')

app.use(
forceDomain({
hostname: domain,
port: 80
})
)
if (config.get('rewrites.forceDomain.hostname') !== '') {
app.use(forceDomain(config.get('rewrites.forceDomain')))
}

app.use(apiMiddleware.handleHostHeader())
Expand Down
10 changes: 1 addition & 9 deletions package.json
Expand Up @@ -88,7 +88,7 @@
"mocha": "^5.2.0",
"nock": "~10.0.0",
"nodeunit": "~0.5.1",
"prettier-standard": "~8.0.1",
"prettier-standard": "^9.1.1",
"should": "~13.2.0",
"sinon": "~2.3.2",
"sinon-test": "~1.0.2",
Expand All @@ -114,14 +114,6 @@
"workspace"
]
},
"greenkeeper": {
"ignore": [
"sinon",
"mocha",
"lint-staged",
"snyk"
]
},
"author": "DADI <team@dadi.tech>",
"license": "SEE LICENSE IN GPL.md",
"snyk": true,
Expand Down
67 changes: 64 additions & 3 deletions test/acceptance/routing.js
Expand Up @@ -39,7 +39,9 @@ describe('Routing', done => {
protocol: 'http'
},
rewrites: {
forceDomain: ''
forceDomain: {
hostname: ''
}
}
}).then(() => {
TestHelper.stopServer(done)
Expand Down Expand Up @@ -629,7 +631,9 @@ describe('Routing', done => {

const configUpdate = {
rewrites: {
forceDomain: 'example.com'
forceDomain: {
hostname: 'example.com'
}
}
}

Expand All @@ -651,7 +655,9 @@ describe('Routing', done => {

const configUpdate = {
rewrites: {
forceDomain: 'example.com:81'
forceDomain: {
hostname: 'example.com:81'
}
}
}

Expand All @@ -667,5 +673,60 @@ describe('Routing', done => {
})
})
})

it('should use specified redirect type when configured with rewrites.forceDomain', done => {
const pages = TestHelper.setUpPages()

const configUpdate = {
rewrites: {
forceDomain: {
hostname: 'example.com',
type: 302
}
}
}

TestHelper.updateConfig(configUpdate).then(() => {
TestHelper.startServer(pages).then(() => {
client.get('/test').end((err, res) => {
should.exist(res.headers.location)
res.headers.location.should.eql('http://example.com:80/test')
res.statusCode.should.eql(302)
if (err) return done(err)
done()
})
})
})
})

it('should handle a redirect from www to root when configured with rewrites.forceDomain', done => {
const pages = TestHelper.setUpPages()

const configUpdate = {
rewrites: {
forceDomain: {
hostname: 'example.com',
port: 443,
protocol: 'https',
type: 302
}
}
}

TestHelper.updateConfig(configUpdate).then(() => {
TestHelper.startServer(pages).then(() => {
client
.get('/')
.set('Host', 'www.example.com')
.end((err, res) => {
should.exist(res.headers.location)
res.headers.location.should.eql('https://example.com:443/')
res.statusCode.should.eql(302)
if (err) return done(err)
done()
})
})
})
})
})
})

0 comments on commit 75cfabe

Please sign in to comment.