-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ca80ed9
commit f7b466a
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* @adonisjs/shield | ||
* | ||
* (c) Harminder Virk <virk@adonisjs.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
/// <reference path="../adonis-typings/index.ts" /> | ||
|
||
import { DnsPrefetchOptions } from '@ioc:Adonis/Addons/Shield' | ||
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext' | ||
import { noop } from './noop' | ||
|
||
/** | ||
* Adds `X-Content-Type-Options` header based upon given | ||
* user options | ||
*/ | ||
export function dnsPrefetch (options: DnsPrefetchOptions) { | ||
if (!options.enabled) { | ||
return noop | ||
} | ||
|
||
const value = options.allow ? 'on' : 'off' | ||
|
||
return function dnsPrefetchMiddlewareFn ({ response }: HttpContextContract) { | ||
response.header('X-DNS-Prefetch-Control', value) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* @adonisjs/shield | ||
* | ||
* (c) Harminder Virk <virk@adonisjs.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import test from 'japa' | ||
import { HttpContext } from '@adonisjs/http-server/build/standalone' | ||
import { dnsPrefetch } from '../src/dnsPrefetch' | ||
|
||
test.group('Dns Prefetch', () => { | ||
test('return noop function when enabled is false', (assert) => { | ||
const middlewareFn = dnsPrefetch({ enabled: false }) | ||
const ctx = HttpContext.create('/', {}, {}, {}, {}) | ||
middlewareFn(ctx) | ||
|
||
assert.isUndefined(ctx.response.getHeader('X-DNS-Prefetch-Control')) | ||
}) | ||
|
||
test('set X-DNS-Prefetch-Control header', (assert) => { | ||
const middlewareFn = dnsPrefetch({ enabled: true, allow: true }) | ||
const ctx = HttpContext.create('/', {}, {}, {}, {}) | ||
middlewareFn(ctx) | ||
|
||
assert.equal(ctx.response.getHeader('X-DNS-Prefetch-Control'), 'on') | ||
}) | ||
|
||
test('set X-DNS-Prefetch-Control header to off', (assert) => { | ||
const middlewareFn = dnsPrefetch({ enabled: true, allow: false }) | ||
const ctx = HttpContext.create('/', {}, {}, {}, {}) | ||
middlewareFn(ctx) | ||
|
||
assert.equal(ctx.response.getHeader('X-DNS-Prefetch-Control'), 'off') | ||
}) | ||
}) |