Skip to content

Commit

Permalink
Merge pull request #7 from michealroberts/feature/internals/useOrigin…
Browse files Browse the repository at this point in the history
…AgentCluster

feat: Added useOriginAgentCluster() composable.
  • Loading branch information
michealroberts authored May 29, 2023
2 parents 87de579 + 30f5931 commit 48c64fa
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/internals/useOriginAgentCluster.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*****************************************************************************************************************/

// @author Michael Roberts <michael@observerly.com>
// @package @observerly/nitro-helmet
// @license Copyright © 2021-2023 observerly

/*****************************************************************************************************************/

import { type H3Event, setHeader } from 'h3'

import { type OriginAgentCluster } from './types'

/*****************************************************************************************************************/

export const defaultOriginAgentCluster = '?1'

/*****************************************************************************************************************/

export const useOriginAgentCluster = (
event: H3Event,
policy: OriginAgentCluster | boolean = defaultOriginAgentCluster
) => {
if (typeof policy === 'boolean' && !policy) {
return
}

// If the policy is a boolean, then use the default policy:
if (typeof policy === 'boolean') {
policy = defaultOriginAgentCluster
}

setHeader(event, 'Origin-Agent-Cluster', policy)
}
41 changes: 41 additions & 0 deletions tests/mocks/helmet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ import {
defaultCrossOriginResourcePolicy
} from '../../src/internals/useCrossOriginResourcePolicy'

import {
useOriginAgentCluster,
defaultOriginAgentCluster
} from '../../src/internals/useOriginAgentCluster'

/*****************************************************************************************************************/

export const helmetHandlers: Handler[] = [
Expand Down Expand Up @@ -160,6 +165,42 @@ export const helmetHandlers: Handler[] = [
handler: eventHandler(async event => {
useCrossOriginResourcePolicy(event, false)

return {
policy: false
}
})
},

//

{
method: 'GET',
url: '/helmet/origin-agent-cluster',
handler: eventHandler(async event => {
useOriginAgentCluster(event, '?1')

return {
policy: '?1'
}
})
},
{
method: 'GET',
url: '/helmet/default-origin-agent-cluster',
handler: eventHandler(async event => {
useOriginAgentCluster(event)

return {
policy: defaultOriginAgentCluster
}
})
},
{
method: 'GET',
url: '/helmet/no-origin-agent-cluster',
handler: eventHandler(async event => {
useOriginAgentCluster(event, false)

return {
policy: false
}
Expand Down
55 changes: 55 additions & 0 deletions tests/useOriginAgentCluster.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*****************************************************************************************************************/

// @author Michael Roberts <michael@observerly.com>
// @package @observerly/nitro-helmet
// @license Copyright © 2021-2023 observerly

/*****************************************************************************************************************/

import { beforeEach, describe, expect, it } from 'vitest'

import supertest, { type SuperTest, type Test } from 'supertest'

import { toNodeListener } from 'h3'

import { server } from './utilities/server'

import {
defaultOriginAgentCluster,
useOriginAgentCluster
} from '../src/internals/useOriginAgentCluster'

/*****************************************************************************************************************/

describe('useOriginAgentCluster', () => {
let request: SuperTest<Test>

beforeEach(() => {
request = supertest(toNodeListener(server))
})

it('should be defined', () => {
expect(useOriginAgentCluster).toBeDefined()
})

it('should set Origin-Agent-Cluster on the request', async () => {
const res = await request.get('/helmet/origin-agent-cluster', {
method: 'GET'
})
expect(res.header['origin-agent-cluster']).toEqual('?1')
})

it('should set Origin-Agent-Cluster on the request', async () => {
const res = await request.get('/helmet/default-origin-agent-cluster', {
method: 'GET'
})
expect(res.header['origin-agent-cluster']).toEqual(defaultOriginAgentCluster)
})

it('should not set Origin-Agent-Cluster on the request', async () => {
const res = await request.get('/helmet/no-origin-agent-cluster', {
method: 'GET'
})
expect(res.header['origin-agent-cluster']).toEqual(undefined)
})
})

0 comments on commit 48c64fa

Please sign in to comment.