コード例 #1
0
        public void DeserializeThrowsIfSerializedValueIsEmpty()
        {
            // Arrange
            MvcSerializer serializer = new MvcSerializer();

            // Act & assert
            Assert.ThrowsArgumentNullOrEmpty(
                delegate { serializer.Deserialize("", SerializationMode.Signed); }, "serializedValue");
        }
コード例 #2
0
        public void DeserializeThrowsIfSerializedValueIsEmpty()
        {
            // Arrange
            MvcSerializer serializer = new MvcSerializer();

            // Act & assert
            Assert.ThrowsArgumentNullOrEmpty(
                delegate { serializer.Deserialize(""); }, "serializedValue");
        }
コード例 #3
0
        public void DeserializeThrowsIfSerializedValueIsNull()
        {
            // Arrange
            MvcSerializer serializer = new MvcSerializer();

            // Act & assert
            Assert.ThrowsArgumentNullOrEmpty(
                delegate { serializer.Deserialize(null); }, "serializedValue");
        }
コード例 #4
0
        public void DeserializeThrowsIfSerializedValueIsNull()
        {
            // Arrange
            MvcSerializer serializer = new MvcSerializer();

            // Act & assert
            Assert.ThrowsArgumentNullOrEmpty(
                delegate { serializer.Deserialize(null, SerializationMode.Signed); }, "serializedValue");
        }
コード例 #5
0
        public void SerializeAllowsNullValues()
        {
            // Arrange
            IMachineKey machineKey = new MockMachineKey();

            // Act
            string serializedValue = MvcSerializer.Serialize(null, SerializationMode.EncryptedAndSigned, machineKey);

            // Assert
            Assert.AreEqual(@"All-LPgGI1dzEbp3B2FueVR5cGUuA25pbIYJAXozaHR0cDovL3NjaGVtYXMubWljcm9zb2Z0LmNvbS8yMDAzLzEwL1NlcmlhbGl6YXRpb24vCQFpKWh0dHA6Ly93d3cudzMub3JnLzIwMDEvWE1MU2NoZW1hLWluc3RhbmNlAQ==", serializedValue);
        }
コード例 #6
0
        public void DeserializeThrowsIfSerializedValueIsNull()
        {
            // Arrange
            MvcSerializer serializer = new MvcSerializer();

            // Act & assert
            ExceptionHelper.ExpectArgumentExceptionNullOrEmpty(
                delegate {
                serializer.Deserialize(null, SerializationMode.Signed);
            }, "serializedValue");
        }
コード例 #7
0
        public void SerializeAllowsNullValues()
        {
            // Arrange
            IMachineKey machineKey = new MockMachineKey();

            // Act
            string serializedValue = MvcSerializer.Serialize(null, machineKey);

            // Assert
            Assert.Equal(@"Microsoft.Web.Mvc.MvcSerializer.v1-dwdhbnlUeXBlLgNuaWyGCQF6M2h0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vMjAwMy8xMC9TZXJpYWxpemF0aW9uLwkBaSlodHRwOi8vd3d3LnczLm9yZy8yMDAxL1hNTFNjaGVtYS1pbnN0YW5jZQE=", serializedValue);
        }
コード例 #8
0
        public void DeserializeThrowsIfModeIsOutOfRange()
        {
            // Arrange
            MvcSerializer serializer = new MvcSerializer();

            // Act & assert            
            Assert.ThrowsArgumentOutOfRange(
                delegate { serializer.Serialize("blah", (SerializationMode)(-1)); },
                "mode",
                @"The provided SerializationMode is invalid.");
        }
コード例 #9
0
        public void SerializeAllowsNullValues()
        {
            // Arrange
            MvcSerializer serializer = new MvcSerializer();

            // Act
            string serializedValue = serializer.Serialize(null, SerializationMode.Plaintext);

            // Assert
            Assert.AreEqual(@"/wFk", serializedValue);
        }
コード例 #10
0
        public void SerializeThrowsIfModeIsOutOfRange()
        {
            // Arrange
            MvcSerializer serializer = new MvcSerializer();

            // Act & assert
            Assert.ThrowsArgumentOutOfRange(
                delegate { serializer.Serialize(null, (SerializationMode)(-1)); },
                "mode",
                @"The provided SerializationMode is invalid.");
        }
コード例 #11
0
        public void SerializeAndDeserializeRoundTripsValue()
        {
            // Arrange
            MvcSerializer serializer = new MvcSerializer();

            // Act
            string serializedValue   = serializer.Serialize(42, SerializationMode.Plaintext);
            object deserializedValue = serializer.Deserialize(serializedValue, SerializationMode.Plaintext);

            // Assert
            Assert.AreEqual(42, deserializedValue, "Value was not round-tripped properly.");
        }
コード例 #12
0
        public void SerializeAndDeserializeRoundTripsValue()
        {
            // Arrange
            IMachineKey machineKey = new MockMachineKey();

            // Act
            string serializedValue   = MvcSerializer.Serialize(42, SerializationMode.EncryptedAndSigned, machineKey);
            object deserializedValue = MvcSerializer.Deserialize(serializedValue, SerializationMode.EncryptedAndSigned, machineKey);

            // Assert
            Assert.AreEqual(42, deserializedValue, "Value was not round-tripped properly.");
        }
コード例 #13
0
        public void SerializeAndDeserializeRoundTripsValue()
        {
            // Arrange
            IMachineKey machineKey = new MockMachineKey();

            // Act
            string serializedValue   = MvcSerializer.Serialize(42, machineKey);
            object deserializedValue = MvcSerializer.Deserialize(serializedValue, machineKey);

            // Assert
            Assert.Equal(42, deserializedValue);
        }
コード例 #14
0
        public void DeserializeThrowsIfSerializedValueIsCorrupt()
        {
            // Arrange
            IMachineKey machineKey = new MockMachineKey();

            // Act & assert
            Exception exception = Assert.Throws <SerializationException>(
                delegate { MvcSerializer.Deserialize("This is a corrupted value.", machineKey); },
                @"Deserialization failed. Verify that the data is being deserialized using the same SerializationMode with which it was serialized. Otherwise see the inner exception.");

            Assert.NotNull(exception.InnerException);
        }
コード例 #15
0
        public void DeserializeThrowsIfSerializedValueIsCorrupt()
        {
            // Arrange
            IMachineKey machineKey = new MockMachineKey();

            // Act & assert
            Exception exception = ExceptionHelper.ExpectException <SerializationException>(
                delegate {
                MvcSerializer.Deserialize("This is a corrupted value.", SerializationMode.Signed, machineKey);
            },
                @"Deserialization failed. Verify that the data is being deserialized using the same SerializationMode with which it was serialized. Otherwise see the inner exception.");

            Assert.IsNotNull(exception.InnerException, "Inner exception was not propagated correctly.");
        }
コード例 #16
0
        public void SerializeThrowsIfModeIsOutOfRange()
        {
            // Arrange
            MvcSerializer serializer = new MvcSerializer();

            // Act & assert
            ExceptionHelper.ExpectArgumentOutOfRangeException(
                delegate {
                serializer.Serialize(null, (SerializationMode)(-1));
            },
                "mode",
                @"The provided SerializationMode is invalid.
Parameter name: mode");
        }
コード例 #17
0
        public static string Serialize <T>(this T toSerialize)
        {
            var serializer = new MvcSerializer();

            return(serializer.Serialize(toSerialize, MvcSerializer.DefaultSerializationMode));
        }
コード例 #18
0
        public static T Deserialize <T>(this string toDeserialize)
        {
            var serializer = new MvcSerializer();

            return((T)serializer.Deserialize(toDeserialize, MvcSerializer.DefaultSerializationMode));
        }