Example #1
0
        public void Setting_a_key_with_the_same_value_should_not_mark_it_as_changed()
        {
          // Given
          var session = new Session(new Dictionary<string, object> { { "key", "SomeValue" } } );

          // When
          session["key"] = "SomeValue";

          // Then
          session.HasChanged.ShouldBeFalse();
        }
Example #2
0
        public void Clearing_the_session_should_not_mark_it_as_changed_if_it_as_empty()
        {
            // Given
            var session = new Session();

            // When
            session.DeleteAll();

            // Then
            session.HasChanged.ShouldBeFalse();
        }
Example #3
0
        public void Setting_a_session_should_mark_it_as_changed()
        {
            // Given
            var session = new Session();

            // When
            session["key"] = "SomeValue";

            // Then
            session.HasChanged.ShouldBeTrue();
        }
Example #4
0
        public void Deleting_a_key_should_mark_it_as_changed()
        {
            // Given
            var session = new Session(new Dictionary<string, object> { { "key", 1 } });

            // When
            session.Delete("key");

            // Then
            session.HasChanged.ShouldBeTrue();
        }
Example #5
0
        public void Deleting_an_invalid_key_should_not_mark_it_as_changed()
        {
            // Given
            var session = new Session(new Dictionary<string, object> { { "key", 1 } });

            // When
            session.Delete("something");

            // Then
            session.HasChanged.ShouldBeFalse();
        }
Example #6
0
        public void Clearing_the_session_should_mark_it_as_changed_if_it_was_not_empty()
        {
            // Given
            var session = new Session(new Dictionary<string, object> { { "key", 1 } });

            // When
            session.DeleteAll();

            // Then
            session.HasChanged.ShouldBeTrue();
        }
        public void Should_be_able_to_load_an_object_previously_saved_to_session()
        {
            var response = new Response();
            var session = new Session(new Dictionary<string, object>());
            var payload = new DefaultSessionObjectFormatterFixture.Payload(27, true, "Test string");
            var store = new CookieBasedSessions(new DefaultEncryptionProvider(), "the passphrase", "the salt", new DefaultSessionObjectFormatter());
            session["testObject"] = payload;
            store.Save(session, response);
            var request = new Request("GET", "/", "http");
            request.Cookies.Add(Helpers.HttpUtility.UrlEncode(response.Cookies.First().Name), Helpers.HttpUtility.UrlEncode(response.Cookies.First().Value));

            var result = store.Load(request);

            result["testObject"].ShouldEqual(payload);
        }
        public void Should_be_able_to_save_a_complex_object_to_session()
        {
            var response = new Response();
            var session = new Session(new Dictionary<string, object>());
            var payload = new DefaultSessionObjectFormatterFixture.Payload(27, true, "Test string");
            var store = new CookieBasedSessions(new DefaultEncryptionProvider(), "the passphrase", "the salt", new DefaultSessionObjectFormatter());
            session["testObject"] = payload;

            store.Save(session, response);

            response.Cookies.Count.ShouldEqual(1);
            var cookie = response.Cookies.First();
            cookie.Name.ShouldEqual(Nancy.Session.CookieBasedSessions.GetCookieName());
            cookie.Value.ShouldNotBeNull();
            cookie.Value.ShouldNotBeEmpty();
        }
        public void Should_save_the_session_cookie()
        {
            var response = new Response();
            var session = new Session(new Dictionary<string, object>
                                      {
                                          {"key1", "val1"},
                                      });
            session["key2"] = "val2";
            A.CallTo(() => this.encryptionProvider.Encrypt("key1=val1;key2=val2;", A<string>.Ignored, A<byte[]>.Ignored)).Returns("encrypted=key1=val1;key2=val2;");

            cookieStore.Save(session, response);

            response.Cookies.Count.ShouldEqual(1);
            var cookie = response.Cookies.First();
            cookie.Name.ShouldEqual(Nancy.Session.CookieBasedSessions.GetCookieName());
            cookie.Value.ShouldEqual("encrypted=key1=val1;key2=val2;");
            cookie.Expires.ShouldBeNull();
            cookie.Path.ShouldBeNull();
            cookie.Domain.ShouldBeNull();
        }
        public void Should_save_cookie_as_http_only()
        {
            var response = new Response();
            var session = new Session();
            session["key 1"] = "val=1";
            A.CallTo(() => this.encryptionProvider.Encrypt("key+1=val%3d1;", A<string>.Ignored, A<byte[]>.Ignored)).Returns("encryptedkey+1=val%3d1;");

            cookieStore.Save(session, response);

            response.Cookies.First().HttpOnly.ShouldEqual(true);
        }
        public void Should_saves_url_safe_keys_and_values()
        {
            var response = new Response();
            var session = new Session();
            session["key 1"] = "val=1";
            A.CallTo(() => this.encryptionProvider.Encrypt("key+1=val%3d1;", A<string>.Ignored, A<byte[]>.Ignored)).Returns("encryptedkey+1=val%3d1;");

            cookieStore.Save(session, response);

            response.Cookies.First().Value.ShouldEqual("encryptedkey+1=val%3d1;");
        }
        public void Should_call_the_formatter_on_save()
        {
            var response = new Response();
            var session = new Session(new Dictionary<string, object>());
            session["key1"] = "value1";
            var fakeFormatter = A.Fake<ISessionObjectFormatter>();
            var store = new Nancy.Session.CookieBasedSessions(this.encryptionProvider, "the passphrase", "the salt", fakeFormatter);

            store.Save(session, response);

            A.CallTo(() => fakeFormatter.Serialize("value1")).MustHaveHappened(Repeated.Exactly.Once);
        }
        public void Should_generate_hmac()
        {
            var response = new Response();
            var session = new Session(new Dictionary<string, object>
                                      {
                                          {"key1", "val1"},
                                      });
            session["key2"] = "val2";

            cookieStore.Save(session, response);

            A.CallTo(() => this.hmacProvider.GenerateHmac(A<string>.Ignored, A<string>.Ignored))
                .MustHaveHappened(Repeated.Exactly.Once);
        }
        public void Should_encrypt_data()
        {
            var response = new Response();
            var session = new Session(new Dictionary<string, object>
                                      {
                                          {"key1", "val1"},
                                      });
            session["key2"] = "val2";

            cookieStore.Save(session, response);

            A.CallTo(() => this.encryptionProvider.Encrypt(A<string>.Ignored, A<string>.Ignored, A<byte[]>.Ignored))
                .MustHaveHappened(Repeated.Exactly.Once);
        }
Example #15
0
        public void Setting_a_key_with_a_different_value_should_mark_it_as_changed()
        {
          // Given
          var session = new Session(new Dictionary<string, object> { { "key", "SomeValue" } });

          // When
          session["key"] = "SomeValue2";

          // Then
          session.HasChanged.ShouldBeTrue();
        }