Skip to content

Commit

Permalink
FIXED all unit tests failing cause of the new middleware oriented ses…
Browse files Browse the repository at this point in the history
…sion
  • Loading branch information
danieleteti committed Jan 20, 2025
1 parent e9ee7bb commit 8d58413
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 17 deletions.
1 change: 0 additions & 1 deletion sources/MVCFramework.ActiveRecord.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1229,7 +1229,6 @@ function TMVCConnectionsRepository.GetCurrent(const RaiseExceptionIfNotAvailable
else
Result := nil;
end;
Result.Connected := True;
finally
fMREW.EndRead;
end;
Expand Down
5 changes: 1 addition & 4 deletions sources/MVCFramework.Session.pas
Original file line number Diff line number Diff line change
Expand Up @@ -741,14 +741,11 @@ function TMVCWebSessionFactory.GetTimeout: Integer;

initialization

//TMVCSessionFactory.GetInstance.RegisterSessionType('memory', TMVCWebSessionMemory);
//TMVCSessionFactory.GetInstance.RegisterSessionType('file', TMVCWebSessionFile);
GlCriticalSection := TCriticalSection.Create;

finalization

//FreeAndNil(TMVCSessionFactory.cInstance);
//FreeAndNil(GlCriticalSection);
FreeAndNil(GlCriticalSection);

if Assigned(GlSessionList) then
FreeAndNil(GlSessionList);
Expand Down
14 changes: 10 additions & 4 deletions sources/MVCFramework.pas
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ TWebContext = class
fIntfObject: IInterface;
fServiceContainerResolver: IMVCServiceContainerResolver;
fSessionFactory: TMVCWebSessionFactory;
procedure CheckSessionFactory; inline;
function GetWebSession: TMVCWebSession;
function GetLoggedUser: TUser;
function GetParamsTable: TMVCRequestParamsTable;
Expand All @@ -555,10 +556,6 @@ TWebContext = class
protected
fActionQualifiedName: String;
procedure Flush; virtual;
// procedure BindToSession(const ASessionId: string);
// function SendSessionCookie(const AContext: TWebContext): string;
// function AddSessionToTheSessionList(const ASessionType, ASessionId: string;
// const ASessionTimeout: Integer): TMVCWebSession;
function GetData: TMVCStringDictionary;
procedure InternalSessionStart(var Session: TMVCWebSession);
procedure FreeSession;
Expand Down Expand Up @@ -2205,6 +2202,14 @@ procedure TUser.SetLoggedSince(const AValue: TDateTime);

{ TWebContext }

procedure TWebContext.CheckSessionFactory;
begin
if fSessionFactory = nil then
begin
raise EMVCConfigException.Create('Session middleware has not been set - session cannot be used without a proper session middleware');
end;
end;

constructor TWebContext.Create(const AServiceContainerResolver: IMVCServiceContainerResolver; const ARequest: TWebRequest; const AResponse: TWebResponse;
const AConfig: TMVCConfig; const ASerializers: TDictionary<string, IMVCSerializer>);
begin
Expand Down Expand Up @@ -2371,6 +2376,7 @@ function TWebContext.GetWebSession: TMVCWebSession;
var
lSessionIDCookie: string;
begin
CheckSessionFactory;
if not Assigned(FWebSession) then
begin
lSessionIDCookie := TMVCEngine.ExtractSessionIdFromWebRequest(FRequest.RawWebRequest);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
object TestWebModule1: TTestWebModule1
OldCreateOrder = False
OnCreate = WebModuleCreate
OnDestroy = WebModuleDestroy
Actions = <
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ implementation
uses
MVCFramework.Tests.RESTClient,
MVCFramework.Middleware.Authentication,
MVCFramework.Middleware.Session,
MVCFramework.Tests.AppController,
MVCFramework.Server,
MVCFramework.Server.Impl;
Expand All @@ -64,7 +65,7 @@ procedure TTestWebModule1.WebModuleCreate(Sender: TObject);

// Add Controller
FMVCEngine.AddController(TAppController);

FMVCEngine.AddMiddleware(UseMemorySessionMiddleware(0));
FMVCEngine.AddMiddleware(TMVCBasicAuthenticationMiddleware.Create(
TMVCDefaultAuthenticationHandler.New
.SetOnAuthentication(
Expand Down
7 changes: 5 additions & 2 deletions unittests/general/Several/ActiveRecordTestsU.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1994,12 +1994,15 @@ procedure TTestActiveRecordBase.LoadData(const JustAFew: boolean);
var
lCustomer: TCustomer;
I: Integer;
lConn: TFDConnection;
begin
// ActiveRecordConnectionsRegistry.AddDefaultConnection(TFDConnection.Create(nil), True);
ActiveRecordConnectionsRegistry.AddConnection('load', TFDConnection.Create(nil), True);
lConn := TFDConnection.Create(nil);
ActiveRecordConnectionsRegistry.AddConnection('load', lConn, True);
try
lConn.ConnectionDefName := fConDefName;
ActiveRecordConnectionsRegistry.SetCurrent('load');
ActiveRecordConnectionsRegistry.GetCurrent.ConnectionDefName := fConDefName;
//ActiveRecordConnectionsRegistry.GetCurrent.ConnectionDefName := fConDefName;
for I := 1 to 30 do
begin
lCustomer := TCustomer.Create;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ implementation
MVCFramework.ActiveRecordController,
MVCFramework.ActiveRecord,
MVCFramework.Middleware.StaticFiles,
MVCFramework.Middleware.Session,
MVCFramework.Middleware.ActiveRecord,
FDConnectionConfigU;

Expand All @@ -59,8 +60,6 @@ procedure TActiveRecordControllerWebModule.WebModuleCreate(Sender: TObject);
FMVC := TMVCEngine.Create(Self,
procedure(Config: TMVCConfig)
begin
// session timeout (0 means session cookie)
Config[TMVCConfigKey.SessionTimeout] := '0';
// default content-type
Config[TMVCConfigKey.DefaultContentType] := TMVCConstants.DEFAULT_CONTENT_TYPE;
// default content charset
Expand All @@ -74,6 +73,7 @@ procedure TActiveRecordControllerWebModule.WebModuleCreate(Sender: TObject);
// Enable Server Signature in response
Config[TMVCConfigKey.ExposeServerSignature] := 'true';
end);
FMVC.AddMiddleware(UseMemorySessionMiddleware(0));
FMVC.AddMiddleware(TMVCActiveRecordMiddleware.Create(AR_CONTROLLER_CON_DEF_NAME));
FMVC.AddController(TMVCActiveRecordController, '/api/entities');
end;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ implementation

uses
MVCFramework.Middleware.Authentication,
MVCFramework.Middleware.Session,
MVCFramework.Server,
MVCFramework.Server.Impl,
StandaloneServerTestU;
Expand All @@ -46,7 +47,9 @@ procedure TTestWebModule2.WebModuleCreate(Sender: TObject);
end
);

FMVCEngine.AddMiddleware(TMVCBasicAuthenticationMiddleware.Create(
FMVCEngine
.AddMiddleware(UseMemorySessionMiddleware(0))
.AddMiddleware(TMVCBasicAuthenticationMiddleware.Create(
TMVCDefaultAuthenticationHandler.New
.SetOnAuthentication(
procedure(const AUserName, APassword: string;
Expand Down
3 changes: 2 additions & 1 deletion unittests/general/TestServer/WebModuleUnit.pas
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ implementation
MVCFramework.View.Renderers.Mustache,
{$ENDIF}
MVCFramework.Middleware.Compression,
MVCFramework.Middleware.Session,
MVCFramework.Middleware.StaticFiles,
FireDAC.Comp.Client,
MVCFramework.ActiveRecord,
Expand All @@ -80,7 +81,6 @@ procedure TMainWebModule.WebModuleCreate(Sender: TObject);
procedure(Config: TMVCConfig)
begin
// no config here
Config[TMVCConfigKey.SessionTimeout] := '0'; // setting cookie
Config[TMVCConfigKey.PathPrefix] := '';
Config[TMVCConfigKey.ViewPath] := TPath.Combine(AppPath, '..\templates');
Config[TMVCConfigKey.DefaultViewFileExtension] := 'html';
Expand Down Expand Up @@ -128,6 +128,7 @@ procedure TMainWebModule.WebModuleCreate(Sender: TObject);
begin
Result := TTestFault2Controller.Create; // this will raise an exception
end)
.AddMiddleware(UseMemorySessionMiddleware())
.AddMiddleware(TMVCSpeedMiddleware.Create)
.AddMiddleware(TMVCCustomAuthenticationMiddleware.Create(TCustomAuthHandler.Create, '/system/users/logged'))
.AddMiddleware(TMVCStaticFilesMiddleware.Create('/static', 'www', 'index.html', False))
Expand Down

0 comments on commit 8d58413

Please sign in to comment.