Beispiel #1
0
        public void Write_PreservesNullParameters()
        {
            // Arrange
            var converter = new OpenIddictConverter();

            using var stream = new MemoryStream();
            using var reader = new StreamReader(stream);
            using var writer = new Utf8JsonWriter(stream);

            var message = new OpenIddictMessage();

            message.AddParameter("string", new OpenIddictParameter((string)null));
            message.AddParameter("bool", new OpenIddictParameter((bool?)null));
            message.AddParameter("long", new OpenIddictParameter((long?)null));
            message.AddParameter("array", new OpenIddictParameter((JsonElement?)null));
            message.AddParameter("object", new OpenIddictParameter((JsonElement?)null));

            // Act
            converter.Write(writer, value: message, options: null);

            // Assert
            writer.Flush();
            stream.Seek(0L, SeekOrigin.Begin);
            Assert.Equal(@"{""string"":null,""bool"":null,""long"":null,""array"":null,""object"":null}", reader.ReadToEnd());
        }
Beispiel #2
0
    public void AddParameter_PreservesEmptyParameters()
    {
        // Arrange
        var message = new OpenIddictMessage();

        // Act
        message.AddParameter("string", string.Empty);
        message.AddParameter("array", JsonSerializer.Deserialize <JsonElement>("[]"));
        message.AddParameter("object", JsonSerializer.Deserialize <JsonElement>("{}"));
        message.AddParameter("value", JsonSerializer.Deserialize <JsonElement>(
                                 @"{""property"":""""}").GetProperty("property").GetString());

#if SUPPORTS_JSON_NODES
        message.AddParameter("node_array", new JsonArray());
        message.AddParameter("node_object", new JsonObject());
        message.AddParameter("node_value", JsonValue.Create(string.Empty));
#endif

        // Assert
        Assert.Empty((string?)message.GetParameter("string"));
        Assert.NotNull((JsonElement?)message.GetParameter("array"));
        Assert.NotNull((JsonElement?)message.GetParameter("object"));
        Assert.NotNull((JsonElement?)message.GetParameter("value"));

#if SUPPORTS_JSON_NODES
        Assert.NotNull((JsonNode?)message.GetParameter("node_array"));
        Assert.NotNull((JsonNode?)message.GetParameter("node_object"));
        Assert.NotNull((JsonNode?)message.GetParameter("node_value"));
#endif
    }
Beispiel #3
0
        public void GetParameter_ReturnsNullForUnsetParameter()
        {
            // Arrange
            var message = new OpenIddictMessage();

            // Act and assert
            Assert.Null(message.GetParameter("parameter"));
        }
Beispiel #4
0
        public void TryGetParameter_ReturnsFalseForUnsetParameter()
        {
            // Arrange
            var message = new OpenIddictMessage();

            // Act and assert
            Assert.False(message.TryGetParameter("parameter", out OpenIddictParameter parameter));
            Assert.Null(parameter.Value);
        }
Beispiel #5
0
        public void GetParameter_IsCaseSensitive()
        {
            // Arrange
            var message = new OpenIddictMessage();

            message.SetParameter("parameter", 42);

            // Act and assert
            Assert.Null(message.GetParameter("PARAMETER"));
        }
Beispiel #6
0
        public void WriteTo_ThrowsAnExceptionForNullWriter()
        {
            // Arrange
            var message = new OpenIddictMessage();

            // Act and assert
            var exception = Assert.Throws <ArgumentNullException>(() => message.WriteTo(writer: null !));

            Assert.Equal("writer", exception.ParamName);
        }
Beispiel #7
0
        public void GetParameter_ReturnsExpectedParameter()
        {
            // Arrange
            var message = new OpenIddictMessage();

            message.SetParameter("parameter", 42);

            // Act and assert
            Assert.Equal(42, (int)message.GetParameter("parameter"));
        }
Beispiel #8
0
        public void HasParameter_ReturnsExpectedResult(string parameter, bool result)
        {
            // Arrange
            var message = new OpenIddictMessage();

            message.SetParameter("parameter", "value");

            // Act and assert
            Assert.Equal(result, message.HasParameter(parameter));
        }
Beispiel #9
0
    public void Constructor_IgnoresNullOrEmptyParameterNames(string name)
    {
        // Arrange and act
        var message = new OpenIddictMessage(new[]
        {
            new KeyValuePair <string, OpenIddictParameter>(name, "Fabrikam")
        });

        // Assert
        Assert.Equal(0, message.Count);
    }
        public void SetParameter_ThrowsAnExceptionForNullOrEmptyName(string name)
        {
            // Arrange
            var message = new OpenIddictMessage();

            // Act and assert
            var exception = Assert.Throws <ArgumentException>(() => message.SetParameter(name, null));

            Assert.Equal("name", exception.ParamName);
            Assert.StartsWith("The parameter name cannot be null or empty.", exception.Message);
        }
Beispiel #11
0
        public void SetParameter_RemovesNullParameters()
        {
            // Arrange
            var message = new OpenIddictMessage();

            // Act
            message.SetParameter("null", null);

            // Assert
            Assert.Empty(message.GetParameters());
        }
Beispiel #12
0
        public void TryGetParameter_ReturnsTrueAndExpectedParameter()
        {
            // Arrange
            var message = new OpenIddictMessage();

            message.SetParameter("parameter", 42);

            // Act and assert
            Assert.True(message.TryGetParameter("parameter", out var parameter));
            Assert.Equal(42, (long?)parameter.Value);
        }
Beispiel #13
0
        public void Constructor_ImportsParameters()
        {
            // Arrange and act
            var message = new OpenIddictMessage(new[]
            {
                new KeyValuePair <string, OpenIddictParameter>("parameter", 42)
            });

            // Assert
            Assert.Equal(42, (long)message.GetParameter("parameter"));
        }
Beispiel #14
0
        public void SetParameter_ThrowsAnExceptionForNullOrEmptyName(string name)
        {
            // Arrange
            var message = new OpenIddictMessage();

            // Act and assert
            var exception = Assert.Throws <ArgumentException>(() => message.SetParameter(name, null));

            Assert.Equal("name", exception.ParamName);
            Assert.StartsWith(SR.GetResourceString(SR.ID0190), exception.Message);
        }
Beispiel #15
0
        public void SetParameter_AddsExpectedParameter()
        {
            // Arrange
            var message = new OpenIddictMessage();

            // Act
            message.SetParameter("parameter", 42);

            // Assert
            Assert.Equal(42, message.GetParameter("parameter"));
        }
Beispiel #16
0
        public void Constructor_IgnoresNamelessParameters()
        {
            // Arrange and act
            var message = new OpenIddictMessage(new[]
            {
                new KeyValuePair <string, OpenIddictParameter>(null, new OpenIddictParameter()),
                new KeyValuePair <string, OpenIddictParameter>(string.Empty, new OpenIddictParameter())
            });

            // Assert
            Assert.Empty(message.GetParameters());
        }
Beispiel #17
0
        public void Constructor_PreservesEmptyParameters()
        {
            // Arrange and act
            var message = new OpenIddictMessage(new[]
            {
                new KeyValuePair <string, OpenIddictParameter>("null-parameter", (string?)null),
                new KeyValuePair <string, OpenIddictParameter>("empty-parameter", string.Empty)
            });

            // Assert
            Assert.Equal(2, message.Count);
        }
Beispiel #18
0
        public void Constructor_SupportsMultiValuedParameters()
        {
            // Arrange and act
            var message = new OpenIddictMessage(new[]
            {
                new KeyValuePair <string, string?[]?>("parameter", new[] { "Fabrikam", "Contoso" })
            });

            // Assert
            Assert.Equal(1, message.Count);
            Assert.Equal(new[] { "Fabrikam", "Contoso" }, (string[]?)message.GetParameter("parameter"));
        }
Beispiel #19
0
        public void Constructor_ExtractsSingleValuedParameters()
        {
            // Arrange and act
            var message = new OpenIddictMessage(new[]
            {
                new KeyValuePair <string, string?[]?>("parameter", new[] { "Fabrikam" })
            });

            // Assert
            Assert.Equal(1, message.Count);
            Assert.Equal("Fabrikam", message.GetParameter("parameter")?.Value);
        }
        public void Constructor_AllowsDuplicateParameters()
        {
            // Arrange and act
            var message = new OpenIddictMessage(new[]
            {
                new KeyValuePair <string, string>("parameter", "Fabrikam"),
                new KeyValuePair <string, string>("parameter", "Contoso")
            });

            // Assert
            Assert.Equal(1, message.Count);
            Assert.Equal(new[] { "Fabrikam", "Contoso" }, (string[])message.GetParameter("parameter"));
        }
Beispiel #21
0
        public void ToString_ExcludesSensitiveParameters(string parameter)
        {
            // Arrange
            var message = new OpenIddictMessage();

            message.AddParameter(parameter, "secret value");

            // Act and assert
            var element = JsonSerializer.Deserialize <JsonElement>(message.ToString());

            Assert.DoesNotContain("secret value", message.ToString());
            Assert.Equal("[redacted]", element.GetProperty(parameter).GetString());
        }
Beispiel #22
0
        public void Constructor_IgnoresDuplicateParameters()
        {
            // Arrange and act
            var message = new OpenIddictMessage(new[]
            {
                new KeyValuePair <string, OpenIddictParameter>("parameter", "Fabrikam"),
                new KeyValuePair <string, OpenIddictParameter>("parameter", "Contoso")
            });

            // Assert
            Assert.Single(message.GetParameters());
            Assert.Equal("Fabrikam", message.GetParameter("parameter"));
        }
Beispiel #23
0
        public void RemoveParameter_RemovesExpectedParameter()
        {
            // Arrange
            var message = new OpenIddictMessage();

            message.AddParameter("parameter", 42);

            // Act
            message.RemoveParameter("parameter");

            // Assert
            Assert.Null(message.GetParameter("parameter"));
        }
Beispiel #24
0
        public void SetParameter_RemovesEmptyParameters()
        {
            // Arrange
            var message = new OpenIddictMessage();

            // Act
            message.SetParameter("string", string.Empty);
            message.SetParameter("array", JsonSerializer.Deserialize <JsonElement>("[]"));
            message.SetParameter("object", JsonSerializer.Deserialize <JsonElement>("{}"));
            message.SetParameter("value", JsonSerializer.Deserialize <JsonElement>(
                                     @"{""property"":""""}").GetProperty("property").GetString());

            // Assert
            Assert.Empty(message.GetParameters());
        }
Beispiel #25
0
        public void GetParameters_EnumeratesParameters()
        {
            // Arrange
            var parameters = new Dictionary <string, OpenIddictParameter>
            {
                ["int"]    = int.MaxValue,
                ["long"]   = long.MaxValue,
                ["string"] = "value"
            };

            var message = new OpenIddictMessage(parameters);

            // Act and assert
            Assert.Equal(parameters, message.GetParameters());
        }
        public void Write_WritesEmptyPayloadForEmptyMessages()
        {
            // Arrange
            var message   = new OpenIddictMessage();
            var converter = new OpenIddictConverter();

            using var stream = new MemoryStream();
            using var reader = new StreamReader(stream);
            using var writer = new Utf8JsonWriter(stream);

            // Act
            converter.Write(writer, value: message, options: null);

            // Assert
            writer.Flush();
            stream.Seek(0L, SeekOrigin.Begin);
            Assert.Equal("{}", reader.ReadToEnd());
        }
Beispiel #27
0
        public void WriteTo_WritesUtf8JsonRepresentation()
        {
            // Arrange
            var message = new OpenIddictMessage
            {
                ["redirect_uris"] = new[] { "https://abc.org/callback" },
                ["client_name"]   = "My Example Client"
            };

            using var stream = new MemoryStream();
            using var writer = new Utf8JsonWriter(stream);

            // Act
            message.WriteTo(writer);
            writer.Flush();

            // Assert
            Assert.Equal(@"{""redirect_uris"":[""https://abc.org/callback""],""client_name"":""My Example Client""}",
                         Encoding.UTF8.GetString(stream.ToArray()));
        }
        public void Write_WritesExpectedPayload()
        {
            // Arrange
            var converter = new OpenIddictConverter();

            using var stream = new MemoryStream();
            using var reader = new StreamReader(stream);
            using var writer = new Utf8JsonWriter(stream);

            var message = new OpenIddictMessage();

            message.AddParameter("string", "value");
            message.AddParameter("array", new[] { "value" });

            // Act
            converter.Write(writer, value: message, options: null);

            // Assert
            writer.Flush();
            stream.Seek(0L, SeekOrigin.Begin);
            Assert.Equal(@"{""string"":""value"",""array"":[""value""]}", reader.ReadToEnd());
        }
        public void Write_PreservesEmptyParameters()
        {
            // Arrange
            var converter = new OpenIddictConverter();

            using var stream = new MemoryStream();
            using var reader = new StreamReader(stream);
            using var writer = new Utf8JsonWriter(stream);

            var message = new OpenIddictMessage();

            message.AddParameter("string", new OpenIddictParameter(string.Empty));
            message.AddParameter("array", new OpenIddictParameter(JsonSerializer.Deserialize <JsonElement>("[]")));
            message.AddParameter("object", new OpenIddictParameter(JsonSerializer.Deserialize <JsonElement>("{}")));

            // Act
            converter.Write(writer, value: message, options: null);

            // Assert
            writer.Flush();
            stream.Seek(0L, SeekOrigin.Begin);
            Assert.Equal(@"{""string"":"""",""array"":[],""object"":{}}", reader.ReadToEnd());
        }