From f7b7795bc0ece6f84532bc03d4c100b77af712a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gys=20Borges=20da=20Silveira?= Date: Thu, 29 Oct 2026 16:52:23 -0300 Subject: [PATCH] test: align middleware exception regression with core fallback --- .../src/tests/Tests.Horse.Core.Middleware.pas | 51 ++++++++++++++----- 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/tests/src/tests/Tests.Horse.Core.Middleware.pas b/tests/src/tests/Tests.Horse.Core.Middleware.pas index cea59f6c..2a2cb916 100644 --- a/tests/src/tests/Tests.Horse.Core.Middleware.pas +++ b/tests/src/tests/Tests.Horse.Core.Middleware.pas @@ -6,6 +6,7 @@ interface uses DUnitX.TestFramework, Horse.Core.RouterTree, Horse.Request, Horse.Response, + Horse.Core.Router.Contract, Horse.Core.Router.Radix, System.SysUtils, System.Generics.Collections, Horse.Exception.Interrupted, {$IF DEFINED(FPC)} HTTPApp {$ELSE} Web.HTTPApp {$ENDIF}, Horse.Commons, @@ -157,31 +158,55 @@ procedure TTestHorseCoreMiddleware.TestMiddlewareEarlyInterruption; procedure TTestHorseCoreMiddleware.TestMiddlewareExceptionPropagation; var LCallback: THorseCallback; - LMethod: TProc; + LRouter: IHorseRouter; + LCaught: Boolean; + LErrorMessage: string; + LRouteExecuted: Boolean; begin - // Middleware lança exceção +{$IF DEFINED(HORSE_RADIX_ROUTER)} + LRouter := THorseRadixRouter.Create; +{$ELSE} + LRouter := THorseRouterTree.Create; +{$ENDIF} + LCaught := False; + LRouteExecuted := False; + + // A exceção deve chegar ao middleware externo antes do fallback do core. + LCallback := procedure(Req: THorseRequest; Res: THorseResponse; Next: TNextProc) + begin + try + Next(); + except + on E: EOSError do + begin + LCaught := True; + LErrorMessage := E.Message; + raise; + end; + end; + end; + LRouter.RegisterMiddleware(LCallback); + LCallback := procedure(Req: THorseRequest; Res: THorseResponse; Next: TNextProc) begin raise EOSError.Create('Database connection error'); end; - FRouterTree.RegisterMiddleware(LCallback); + LRouter.RegisterMiddleware(LCallback); LCallback := procedure(Req: THorseRequest; Res: THorseResponse; Next: TNextProc) begin - // Rota final + LRouteExecuted := True; end; - FRouterTree.RegisterRoute(mtGet, '/test', LCallback); + LRouter.RegisterRoute(mtGet, '/test', LCallback); FRequest.Populate('GET', mtGet, '/test', '', ''); - // A exceção deve propagar para fora do Execute - LMethod := procedure - var - LRet: Boolean; - begin - LRet := FRouterTree.Execute(FRequest, FResponse); - end; - Assert.WillRaise(LMethod, EOSError); + // O middleware relança; Execute aplica o fallback sem deixar escapar o erro. + Assert.IsTrue(LRouter.Execute(FRequest, FResponse)); + Assert.IsTrue(LCaught, 'A exceção deve atravessar a cadeia de middleware'); + Assert.AreEqual('Database connection error', LErrorMessage); + Assert.IsFalse(LRouteExecuted, 'A rota não deve executar após a exceção'); + Assert.AreEqual(Integer(THTTPStatus.InternalServerError), FResponse.Status); end; procedure TTestHorseCoreMiddleware.TestMultipleMiddlewaresInRouterTree;