Skip to content

Commit

Permalink
spread options for storage drivers
Browse files Browse the repository at this point in the history
  • Loading branch information
vejja committed May 21, 2024
1 parent 738ce9b commit 6831dda
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,12 @@ function registerRateLimiterStorage(nuxt: Nuxt, securityOptions: ModuleOptions)
securityOptions.rateLimiter ? securityOptions.rateLimiter.driver : undefined,
{ name: 'lruCache' }
)
const { name, options } = driver
const { name, options = {} } = driver
config.storage = defu(
{
'#rate-limiter-storage': {
driver: name,
options
...options
}
},
config.storage
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/storageOptions/app.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<div>
<NuxtPage />
</div>
</template>
17 changes: 17 additions & 0 deletions test/fixtures/storageOptions/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export default defineNuxtConfig({
modules: [
'../../../src/module'
],
security: {
rateLimiter: {
tokensPerInterval: 3,
interval: 1000000,
driver: {
name: 'fsLite',
options: {
base: './test/fixtures/storageOptions/.nuxt/test/.data/db'
}
}
}
}
})
5 changes: 5 additions & 0 deletions test/fixtures/storageOptions/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"private": true,
"name": "basic",
"type": "module"
}
3 changes: 3 additions & 0 deletions test/fixtures/storageOptions/pages/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<div>basic</div>
</template>
36 changes: 36 additions & 0 deletions test/storageOptions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { describe, it, expect } from 'vitest'
import { fileURLToPath } from 'node:url'
import { rm } from 'node:fs/promises'
import { setup, fetch } from '@nuxt/test-utils'

describe('[nuxt-security] Storage options', async () => {
await setup({
rootDir: fileURLToPath(new URL('./fixtures/storageOptions', import.meta.url)),
})

const dbPath = './test/fixtures/storageOptions/.nuxt/test/.data/db'
await rm(dbPath, { recursive: true, force: true })

it('should pass options to a custom driver', async () => {
const res1 = await fetch('/')
const res2 = await fetch('/')

expect(res1).toBeDefined()
expect(res1).toBeTruthy()
expect(res2.status).toBe(200)
expect(res2.statusText).toBe('OK')
})

it('should return 429 with the custom driver', async () => {
const res1 = await fetch('/')
await fetch('/')
await fetch('/')
await fetch('/')
const res5 = await fetch('/')

expect(res1).toBeDefined()
expect(res1).toBeTruthy()
expect(res5.status).toBe(429)
expect(res5.statusText).toBe('Too Many Requests')
})
})

0 comments on commit 6831dda

Please sign in to comment.