Skip to content

Commit

Permalink
feat: add support for X-Download-Options
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Oct 26, 2019
1 parent 0c7e5ab commit ca80ed9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/noOpen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* @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 { ContentTypeSniffingOptions } from '@ioc:Adonis/Addons/Shield'
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
import { noop } from './noop'

/**
* Adds `X-Download-Options` header based upon given
* user options
*/
export function noOpen (options: ContentTypeSniffingOptions) {
if (!options.enabled) {
return noop
}

return function noOpenMiddlewareFn ({ response }: HttpContextContract) {
response.header('X-Download-Options', 'noopen')
}
}
30 changes: 30 additions & 0 deletions test/no-open.spec.ts
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.
*/

import test from 'japa'
import { HttpContext } from '@adonisjs/http-server/build/standalone'
import { noOpen } from '../src/noOpen'

test.group('No Open', () => {
test('return noop function when enabled is false', (assert) => {
const middlewareFn = noOpen({ enabled: false })
const ctx = HttpContext.create('/', {}, {}, {}, {})
middlewareFn(ctx)

assert.isUndefined(ctx.response.getHeader('X-Download-Options'))
})

test('set X-Download-Options header', (assert) => {
const middlewareFn = noOpen({ enabled: true })
const ctx = HttpContext.create('/', {}, {}, {}, {})
middlewareFn(ctx)

assert.equal(ctx.response.getHeader('X-Download-Options'), 'noopen')
})
})

0 comments on commit ca80ed9

Please sign in to comment.