public void Read_ReturnsRequestedType(Type type)
        {
            // Arrange
            var converter = new OpenIddictConverter();
            var reader    = new Utf8JsonReader(Encoding.UTF8.GetBytes(@"{""name"":""value""}"));

            // Act
            var message = converter.Read(ref reader, type, options: null);

            // Assert
            Assert.IsType(type, message);
            Assert.Equal("value", (string)message.GetParameter("name"));
        }
        public void Read_ThrowsAnExceptionForNullType()
        {
            // Arrange
            var converter = new OpenIddictConverter();

            // Act and assert
            var exception = Assert.Throws <ArgumentNullException>(delegate
            {
                var reader = new Utf8JsonReader();
                return(converter.Read(ref reader, type: null, options: null));
            });

            Assert.Equal("type", exception.ParamName);
        }
Exemple #3
0
        public void Read_ThrowsAnExceptionForUnexpectedJsonToken()
        {
            // Arrange
            var converter = new OpenIddictConverter();

            // Act and assert
            var exception = Assert.Throws <JsonException>(() =>
            {
                var reader = new Utf8JsonReader(Encoding.UTF8.GetBytes("[0,1,2,3]"));
                converter.Read(ref reader, type: typeof(OpenIddictRequest), options: null);
            });

            Assert.Equal("An error occurred while reading the JSON payload.", exception.Message);
        }
        public void Read_ThrowsAnExceptionForUnsupportedType(Type type)
        {
            // Arrange
            var converter = new OpenIddictConverter();

            // Act and assert
            var exception = Assert.Throws <ArgumentException>(delegate
            {
                var reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(@"{""name"":""value""}"));
                return(converter.Read(ref reader, type, options: null));
            });

            Assert.StartsWith("The specified type is not supported.", exception.Message);
            Assert.Equal("type", exception.ParamName);
        }
        public void Read_PreservesEmptyParameters()
        {
            // Arrange
            var converter = new OpenIddictConverter();
            var reader    = new Utf8JsonReader(Encoding.UTF8.GetBytes(@"{""string"":"""",""array"":[],""object"":{}}"));

            // Act
            var message = converter.Read(ref reader, typeof(OpenIddictMessage), options: null);

            // Assert
            Assert.Equal(3, message.Count);
            Assert.NotNull(message.GetParameter("string"));
            Assert.NotNull(message.GetParameter("array"));
            Assert.NotNull(message.GetParameter("object"));
            Assert.Empty((string)message.GetParameter("string"));
            Assert.NotNull((JsonElement?)message.GetParameter("array"));
            Assert.NotNull((JsonElement?)message.GetParameter("object"));
        }
        public void Read_PreservesNullParameters()
        {
            // Arrange
            var converter = new OpenIddictConverter();
            var reader    = new Utf8JsonReader(Encoding.UTF8.GetBytes(
                                                   @"{""string"":null,""bool"":null,""long"":null,""array"":null,""object"":null}"));

            // Act
            var message = converter.Read(ref reader, typeof(OpenIddictMessage), options: null);

            // Assert
            Assert.Equal(5, message.Count);
            Assert.NotNull(message.GetParameter("string"));
            Assert.NotNull(message.GetParameter("bool"));
            Assert.NotNull(message.GetParameter("long"));
            Assert.NotNull(message.GetParameter("array"));
            Assert.NotNull(message.GetParameter("object"));
            Assert.Null((string)message.GetParameter("string"));
            Assert.Null((bool?)message.GetParameter("bool"));
            Assert.Null((long?)message.GetParameter("long"));
            Assert.Equal(JsonValueKind.Null, ((JsonElement)message.GetParameter("array")).ValueKind);
            Assert.Equal(JsonValueKind.Null, ((JsonElement)message.GetParameter("object")).ValueKind);
        }