1- import { describe , it , expect } from "vitest" ;
1+ import { beforeEach , describe , it , expect } from "vitest" ;
22import { fileURLToPath } from "node:url" ;
33import { setup , $fetch } from "@nuxt/test-utils/e2e" ;
4- import { beforeEach } from "vitest" ;
54import { readFile , rm } from "node:fs/promises" ;
65
76// TODO get these from the config
87const nuxtConfigDuration = 3 ;
98const nuxtConfigBan = 10 ;
109
11- beforeEach ( async ( ) => {
12- // await useStorage("shield").clear(); TODO waiting for https://github.com/nuxt/test-utils/issues/531
13- // this is a workaround to clean the storage
14- const storagePath = fileURLToPath ( new URL ( "../_testBasciShield" , import . meta. url ) ) ;
15- await rm ( storagePath , { recursive : true , force : true } ) ;
16- } ) ;
1710
1811describe ( "shield" , async ( ) => {
1912 await setup ( {
2013 rootDir : fileURLToPath ( new URL ( "./fixtures/basic" , import . meta. url ) ) ,
2114 } ) ;
2215
16+ beforeEach ( async ( ) => {
17+ // await useStorage("shield").clear(); TODO waiting for https://github.com/nuxt/test-utils/issues/531
18+ // this is a workaround to clean the storage
19+ const storagePath = fileURLToPath ( new URL ( "../_testBasicShield" , import . meta. url ) ) ;
20+ await rm ( storagePath , { recursive : true , force : true } ) ;
21+ } ) ;
22+
2323 it ( "respond to api call 2 times (limit.max, limit.duration) and rejects the 3rd call" , async ( ) => {
2424 // req.count = 1
25- let response = await $fetch ( "/api/example ?c=1/1" , {
25+ let response = await $fetch ( "/api/basicexample ?c=1/1" , {
2626 method : "GET" ,
2727 retryStatusCodes : [ ] ,
2828 } ) ;
2929 expect ( ( response as any ) . name ) . toBe ( "Gauranga" ) ;
3030
3131 // req.count = 2
32- response = await $fetch ( "/api/example ?c=1/2" , {
32+ response = await $fetch ( "/api/basicexample ?c=1/2" , {
3333 method : "GET" ,
3434 retryStatusCodes : [ ] ,
3535 } ) ;
@@ -39,7 +39,7 @@ describe("shield", async () => {
3939 // req.count = 3
4040 // as limit.max = 2, this should throw 429 and ban for 3 seconds (limit.ban)
4141 expect ( async ( ) =>
42- $fetch ( "/api/example ?c=1/3" , { method : "GET" , retryStatusCodes : [ ] } )
42+ $fetch ( "/api/basicexample ?c=1/3" , { method : "GET" , retryStatusCodes : [ ] } )
4343 ) . rejects . toThrowError ( ) ;
4444 } catch ( err ) {
4545 const typedErr = err as { statusCode : number ; statusMessage : string } ;
@@ -49,18 +49,15 @@ describe("shield", async () => {
4949 } ) ;
5050
5151 it ( "respond to the 2nd api call when more then limit.duration time passes" , async ( ) => {
52- // here we should wait for the 3 sec ban to expire
53- await new Promise ( ( resolve ) => setTimeout ( resolve , ( nuxtConfigBan + 1 ) * 1000 ) ) ;
54-
5552 // see #13
5653 // req.count = 1
57- let response = await $fetch ( "/api/example ?c=2/1" , {
54+ let response = await $fetch ( "/api/basicexample ?c=2/1" , {
5855 method : "GET" ,
5956 retryStatusCodes : [ ] ,
6057 } ) ;
6158
6259 // req.count = 2
63- response = await $fetch ( "/api/example ?c=2/2" , {
60+ response = await $fetch ( "/api/basicexample ?c=2/2" , {
6461 method : "GET" ,
6562 retryStatusCodes : [ ] ,
6663 } ) ;
@@ -69,12 +66,12 @@ describe("shield", async () => {
6966
7067 it ( "respond to api call after limit.ban expires" , async ( ) => {
7168 // req.count reset here
72- await $fetch ( "/api/example ?c=3/1" , { method : "GET" , retryStatusCodes : [ ] } ) ; // req.count = 1
73- await $fetch ( "/api/example ?c=3/2" , { method : "GET" , retryStatusCodes : [ ] } ) ; // req.count = 2
69+ await $fetch ( "/api/basicexample ?c=3/1" , { method : "GET" , retryStatusCodes : [ ] } ) ; // req.count = 1
70+ await $fetch ( "/api/basicexample ?c=3/2" , { method : "GET" , retryStatusCodes : [ ] } ) ; // req.count = 2
7471 try {
7572 // req.count = 3
7673 expect ( async ( ) =>
77- $fetch ( "/api/example ?c=3/3" , { method : "GET" , retryStatusCodes : [ ] } )
74+ $fetch ( "/api/basicexample ?c=3/3" , { method : "GET" , retryStatusCodes : [ ] } )
7875 ) . rejects . toThrowError ( ) ;
7976 } catch ( err ) {
8077 const typedErr = err as {
@@ -90,7 +87,7 @@ describe("shield", async () => {
9087
9188 // here we should wait for the 3 sec ban to expire
9289 await new Promise ( ( resolve ) => setTimeout ( resolve , nuxtConfigBan * 1000 ) ) ;
93- const response = await $fetch ( "/api/example ?c=3/4" , {
90+ const response = await $fetch ( "/api/basicexample ?c=3/4" , {
9491 method : "GET" ,
9592 retryStatusCodes : [ ] ,
9693 } ) ;
0 commit comments