@@ -117,112 +117,6 @@ describe("Custom Middleware E2E Tests", () => {
117117 } ) ;
118118
119119 describe ( "Gateway Level Middleware" , ( ) => {
120- it ( "should apply global middleware to all routes" , async ( ) => {
121- const requestLogs : string [ ] = [ ] ;
122- const responseLogs : string [ ] = [ ] ;
123-
124- // Custom logging middleware
125- const loggingMiddleware : RequestHandler = async ( req , next ) => {
126- requestLogs . push ( `Request: ${ req . method } ${ new URL ( req . url ) . pathname } ` ) ;
127- const response = await next ( ) ;
128- responseLogs . push ( `Response: ${ response . status } ` ) ;
129- return response ;
130- } ;
131-
132- // Authentication middleware
133- const authMiddleware : RequestHandler = async ( req , next ) => {
134- const authHeader = req . headers . get ( "Authorization" ) ;
135- if ( ! authHeader || ! authHeader . startsWith ( "Bearer " ) ) {
136- return new Response ( "Unauthorized" , { status : 401 } ) ;
137- }
138- return next ( ) ;
139- } ;
140-
141- // Custom header middleware
142- const headerMiddleware : RequestHandler = async ( req , next ) => {
143- const response = await next ( ) ;
144- // Clone response to add custom headers
145- const clonedResponse = new Response ( response . body , {
146- status : response . status ,
147- statusText : response . statusText ,
148- headers : response . headers ,
149- } ) ;
150- clonedResponse . headers . set ( "X-Custom-Gateway" , "BunGate" ) ;
151- clonedResponse . headers . set ( "X-Request-ID" , crypto . randomUUID ( ) ) ;
152- return clonedResponse ;
153- } ;
154-
155- // Apply global middlewares
156- gateway . use ( loggingMiddleware ) ;
157- gateway . use ( authMiddleware ) ;
158- gateway . use ( headerMiddleware ) ;
159-
160- // Add routes
161- gateway . addRoute ( {
162- pattern : "/api/users" ,
163- target : "http://localhost:9001" ,
164- proxy : {
165- pathRewrite : ( path ) => path ,
166- } ,
167- } ) ;
168-
169- gateway . addRoute ( {
170- pattern : "/api/posts" ,
171- target : "http://localhost:9001" ,
172- proxy : {
173- pathRewrite : ( path ) => path ,
174- } ,
175- } ) ;
176-
177- // Start server
178- await gateway . listen ( ) ;
179-
180- // Test unauthorized request
181- const unauthorizedResponse = await fetch ( `${ baseUrl } /api/users` ) ;
182- expect ( unauthorizedResponse . status ) . toBe ( 401 ) ;
183- expect ( await unauthorizedResponse . text ( ) ) . toBe ( "Unauthorized" ) ;
184-
185- // Test authorized request to users endpoint
186- const usersResponse = await fetch ( `${ baseUrl } /api/users` , {
187- headers : {
188- Authorization : "Bearer valid-token" ,
189- } ,
190- } ) ;
191-
192- expect ( usersResponse . status ) . toBe ( 200 ) ;
193- expect ( usersResponse . headers . get ( "X-Custom-Gateway" ) ) . toBe ( "BunGate" ) ;
194- expect ( usersResponse . headers . get ( "X-Request-ID" ) ) . toBeTruthy ( ) ;
195-
196- const usersData = await usersResponse . json ( ) ;
197- expect ( usersData ) . toEqual ( [
198- { id : 1 , name : "Alice" } ,
199- { id : 2 , name : "Bob" } ,
200- ] ) ;
201-
202- // Test authorized request to posts endpoint
203- const postsResponse = await fetch ( `${ baseUrl } /api/posts` , {
204- headers : {
205- Authorization : "Bearer valid-token" ,
206- } ,
207- } ) ;
208-
209- expect ( postsResponse . status ) . toBe ( 200 ) ;
210- expect ( postsResponse . headers . get ( "X-Custom-Gateway" ) ) . toBe ( "BunGate" ) ;
211- expect ( postsResponse . headers . get ( "X-Request-ID" ) ) . toBeTruthy ( ) ;
212-
213- const postsData = await postsResponse . json ( ) ;
214- expect ( postsData ) . toEqual ( [
215- { id : 1 , title : "Post 1" , author : "Alice" } ,
216- { id : 2 , title : "Post 2" , author : "Bob" } ,
217- ] ) ;
218-
219- // Verify middleware execution
220- expect ( requestLogs ) . toContain ( "Request: GET /api/users" ) ;
221- expect ( requestLogs ) . toContain ( "Request: GET /api/posts" ) ;
222- expect ( responseLogs ) . toContain ( "Response: 200" ) ;
223- expect ( responseLogs . length ) . toBeGreaterThan ( 0 ) ;
224- } ) ;
225-
226120 it ( "should handle middleware errors gracefully" , async ( ) => {
227121 const errorMiddleware : RequestHandler = async ( req , next ) => {
228122 if ( req . url . includes ( "/error" ) ) {
0 commit comments