11import { delay } from '@httptoolkit/util' ;
2- import { HttpHandler , HttpEndpoint } from '../http-index.js' ;
2+ import { HttpHandler } from '../http-index.js' ;
33import { buildHttpBinAnythingEndpoint } from '../../httpbin-compat.js' ;
44
55const matchPath = ( path : string ) => path . startsWith ( '/delay/' ) ;
@@ -8,17 +8,26 @@ const defaultAnythingEndpoint = buildHttpBinAnythingEndpoint({
88 fieldFilter : [ "url" , "args" , "form" , "data" , "origin" , "headers" , "files" ]
99} ) ;
1010
11- const handle : HttpHandler = async ( req , res , { path } ) => {
12- const delayMs = parseFloat ( path . slice ( '/delay/' . length ) ) ;
11+ const handle : HttpHandler = async ( req , res , { path, handleRequest } ) => {
12+ const followingSlashIndex = path . indexOf ( '/' , '/delay/' . length ) ;
13+ const followingUrl = followingSlashIndex !== - 1 ? path . slice ( followingSlashIndex ) : '' ;
14+ const endOfDelay = followingSlashIndex === - 1 ? path . length : followingSlashIndex ;
15+ const delayMs = parseFloat ( path . slice ( '/delay/' . length , endOfDelay ) ) ;
1316
1417 if ( isNaN ( delayMs ) ) {
1518 res . writeHead ( 400 ) ;
1619 res . end ( 'Invalid delay duration' ) ;
1720 }
1821
19- await delay ( Math . min ( delayMs , 10 * 1000 ) ) ; // 10s max
22+ await delay ( Math . min ( delayMs , 10 ) * 1000 ) ; // 10s max
2023
21- return defaultAnythingEndpoint ( req , res ) ;
24+ if ( followingUrl ) {
25+ req . url = followingUrl ;
26+ handleRequest ( req , res ) ;
27+ return ;
28+ } else {
29+ return defaultAnythingEndpoint ( req , res ) ;
30+ }
2231}
2332
2433export const delayEndpoint = {
0 commit comments