diff --git a/test/fixtures/rateLimiter/nuxt.config.ts b/test/fixtures/rateLimiter/nuxt.config.ts index 276ce3a0..de35fc0a 100644 --- a/test/fixtures/rateLimiter/nuxt.config.ts +++ b/test/fixtures/rateLimiter/nuxt.config.ts @@ -15,6 +15,68 @@ export default defineNuxtConfig({ tokensPerInterval: 10, } } - } + }, + '/whitelistBase': { + security: { + rateLimiter: { + tokensPerInterval: 1, + interval: 300000, + whiteList: [ + '127.0.0.1', + '192.168.0.1', + '172.16.0.1', + '10.0.0.1', + ], + } + } + }, + '/whitelistEmpty': { + security: { + rateLimiter: { + tokensPerInterval: 1, + interval: 300000, + whiteList: [], + } + } + }, + '/whitelistNotListed': { + security: { + rateLimiter: { + tokensPerInterval: 1, + interval: 300000, + whiteList: [ + '10.0.0.1', + '10.0.1.1', + '10.0.2.1', + '10.0.3.1', + '10.0.4.1', + '10.0.5.1', + '10.0.6.1', + '10.0.7.1', + '10.0.8.1', + '10.0.9.1', + '10.1.0.1', + '10.2.0.1', + '10.3.0.1', + '10.4.0.1', + '10.5.0.1', + '10.6.0.1', + '10.7.0.1', + '10.8.0.1', + '10.9.0.1', + '192.168.0.1', + '192.168.1.1', + '192.168.2.1', + '192.168.3.1', + '192.168.4.1', + '192.168.5.1', + '192.168.6.1', + '192.168.7.1', + '192.168.8.1', + '192.168.9.1', + ], + } + } + }, } }) diff --git a/test/fixtures/rateLimiter/pages/whitelistBase.vue b/test/fixtures/rateLimiter/pages/whitelistBase.vue new file mode 100644 index 00000000..0e6f9208 --- /dev/null +++ b/test/fixtures/rateLimiter/pages/whitelistBase.vue @@ -0,0 +1,3 @@ + diff --git a/test/fixtures/rateLimiter/pages/whitelistEmpty.vue b/test/fixtures/rateLimiter/pages/whitelistEmpty.vue new file mode 100644 index 00000000..63999ff6 --- /dev/null +++ b/test/fixtures/rateLimiter/pages/whitelistEmpty.vue @@ -0,0 +1,3 @@ + diff --git a/test/fixtures/rateLimiter/pages/whitelistNotListed.vue b/test/fixtures/rateLimiter/pages/whitelistNotListed.vue new file mode 100644 index 00000000..b00819ba --- /dev/null +++ b/test/fixtures/rateLimiter/pages/whitelistNotListed.vue @@ -0,0 +1,3 @@ + diff --git a/test/rateLimiter.test.ts b/test/rateLimiter.test.ts index fbe17974..0dda16f5 100644 --- a/test/rateLimiter.test.ts +++ b/test/rateLimiter.test.ts @@ -63,4 +63,43 @@ describe('[nuxt-security] Rate Limiter', async () => { expect(res6.status).toBe(200) expect(res6.statusText).toBe('OK') }) + + it ('should return 200 OK after multiple requests for a route with localhost ip whitelisted', async () => { + const res1 = await fetch('/whitelistBase') + await fetch('/whitelistBase') + await fetch('/whitelistBase') + await fetch('/whitelistBase') + const res5 = await fetch('/whitelistBase') + + expect(res1).toBeDefined() + expect(res1).toBeTruthy() + expect(res5.status).toBe(200) + expect(res5.statusText).toBe('OK') + }) + + it ('should return 429 when limit reached with an empty whitelist array', async () => { + const res1 = await fetch('/whitelistEmpty') + await fetch('/whitelistEmpty') + await fetch('/whitelistEmpty') + await fetch('/whitelistEmpty') + const res5 = await fetch('/whitelistEmpty') + + expect(res1).toBeDefined() + expect(res1).toBeTruthy() + expect(res5.status).toBe(429) + expect(res5.statusText).toBe('Too Many Requests') + }) + + it ('should return 429 when limit reached as localhost ip is not whitelisted', async () => { + const res1 = await fetch('/whitelistNotListed') + await fetch('/whitelistNotListed') + await fetch('/whitelistNotListed') + await fetch('/whitelistNotListed') + const res5 = await fetch('/whitelistNotListed') + + expect(res1).toBeDefined() + expect(res1).toBeTruthy() + expect(res5.status).toBe(429) + expect(res5.statusText).toBe('Too Many Requests') + }) })