public void FSS008() { // arrange var fixture = new Fixture(); var sessionKeys = fixture.CreateMany <string>(); var path = fixture.Create <string>(); var filePaths = sessionKeys .Select(sessionKey => Path.Combine(path, sessionKey)) .ToList(); var sessionStateSerializer = new BinarySessionStateSerializer(); var sessionState = new Dictionary <string, object>(); var directoryMock = new Mock <IDirectory>(); directoryMock .Setup(m => m.Exists(path)) .Returns(true); directoryMock .Setup(m => m.GetFileNames(path)) .Returns(filePaths); var fileMock = new Mock <IFile>(); var storage = new FileSessionStorage(directoryMock.Object, fileMock.Object, sessionStateSerializer, path); // act var storedSessions = storage.StoredSessions; // assert storedSessions.Should().BeEquivalentTo(sessionKeys); }
public void FSS007() { // arrange var fixture = new Fixture(); var sessionKey = fixture.Create <string>(); var path = fixture.Create <string>(); var filePath = Path.Combine(path, sessionKey); var sessionStateSerializer = new BinarySessionStateSerializer(); var sessionState = new Dictionary <string, object>(); var directoryMock = new Mock <IDirectory>(); var fileMock = new Mock <IFile>(); fileMock.Setup(m => m.Exists(filePath)) .Returns(false); var storage = new FileSessionStorage(directoryMock.Object, fileMock.Object, sessionStateSerializer, path); // act storage.Delete(sessionKey); // assert fileMock.Verify(m => m.Delete(filePath), Times.Never); }
public void FSS001() { // arrange var fixture = new Fixture(); var sessionKey = fixture.Create <string>(); var path = fixture.Create <string>(); var expectedPath = Path.Combine(path, sessionKey); var sessionStateSerializer = new BinarySessionStateSerializer(); var directoryMock = new Mock <IDirectory>(); var fileMock = new Mock <IFile>(); fileMock .Setup(m => m.Exists(expectedPath)) .Returns(false); var storage = new FileSessionStorage(directoryMock.Object, fileMock.Object, sessionStateSerializer, path); // act var sessionState = storage.Load(sessionKey); // assert sessionState.Should().NotBeNull(); sessionState.Count.Should().Be(0); fileMock.Verify(m => m.Exists(expectedPath), Times.Once); }
static void Main(string[] args) { // determine root folder for saving session data var path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "SessionState"); var sessionStateSerializer = new SessionStateProtector(new BinarySessionStateSerializer()); var sessionStorage = new FileSessionStorage(new SystemDirectory(), new SystemFile(), sessionStateSerializer, path); // create Session for generating correct session key var sessionMananager = new SessionManager(sessionStorage, new SystemProcessIdProvider()); // create state manager var stateManager = new StateManager(sessionMananager); if (args.Length == 2) { // read a value from state var scope = Enum.Parse <StateScope>(args[0]); Console.WriteLine($"Value for {args[1]} : {stateManager.GetValue<string>(scope, args[1], "")}"); } else if (args.Length == 3) { // set a value to state var scope = Enum.Parse <StateScope>(args[0]); stateManager.SetValue(scope, args[1], args[2]); stateManager.Save(); Console.WriteLine($"Setting value for {args[1]} to {args[2]}"); } }
public static HTTPServer CreateServer(string[] args) { // either parse the settings from the program args or initialize them manually var settings = ServerSettings.Parse(args); settings.Compression = false; var sessionStorage = new FileSessionStorage("session") { CookieExpiration = TimeSpan.FromHours(6) }; // instantiate a new site, the second argument is the relative file path where the public site contents will be found return(new HTTPServer(settings, ConsoleLogger.Write, sessionStorage)); }
public void FSS003() { // arrange var fixture = new Fixture(); var sessionKey = fixture.Create <string>(); var path = fixture.Create <string>(); var expectedPath = Path.Combine(path, sessionKey); var sessionStateSerializer = new BinarySessionStateSerializer(); var sessionState = new Dictionary <string, object>() { { "one", 1 } }; var serializedSessionState = sessionStateSerializer.Serialize(sessionState); var directoryMock = new Mock <IDirectory>(); var fileMock = new Mock <IFile>(); fileMock .Setup(m => m.Exists(expectedPath)) .Returns(true); fileMock .Setup(m => m.WriteAllBytes(expectedPath, It.IsAny <byte[]>())) .Callback <string, byte[]>((sessionKey, bytes) => serializedSessionState = bytes); var storage = new FileSessionStorage(directoryMock.Object, fileMock.Object, sessionStateSerializer, path); // act storage.Save(sessionKey, sessionState); // assert fileMock.Verify(m => m.WriteAllBytes(expectedPath, It.IsAny <byte[]>()), Times.Once); var expectedSessionState = new Dictionary <string, object>() { { "one", 1 }, }; var deserializedSessionState = sessionStateSerializer.Deserialize(serializedSessionState); deserializedSessionState.Should().BeEquivalentTo(expectedSessionState); }
public void FSS002() { // arrange var fixture = new Fixture(); var sessionKey = fixture.Create <string>(); var path = fixture.Create <string>(); var expectedPath = Path.Combine(path, sessionKey); var sessionStateSerializer = new BinarySessionStateSerializer(); var sessionState = new Dictionary <string, object>() { { "one", 1 } }; var serializedSessionState = sessionStateSerializer.Serialize(sessionState); var directoryMock = new Mock <IDirectory>(); var fileMock = new Mock <IFile>(); fileMock .Setup(m => m.Exists(expectedPath)) .Returns(true); fileMock .Setup(m => m.ReadAllBytes(expectedPath)) .Returns(serializedSessionState); var storage = new FileSessionStorage(directoryMock.Object, fileMock.Object, sessionStateSerializer, path); // act var loadedSessionState = storage.Load(sessionKey); // assert loadedSessionState.Should().NotBeNull(); loadedSessionState.Should().BeEquivalentTo(sessionState); fileMock.Verify(m => m.Exists(expectedPath), Times.Once); fileMock.Verify(m => m.ReadAllBytes(expectedPath), Times.Once); }
public void Setup() { fileManager = new DefaultFileManager("Storage", null); sessionStorage = new FileSessionStorage(fileManager); sessionStorage.ClearSession(SessionHolderId); }