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()); }
public void CanConvert_ReturnsExpectedResult(Type type, bool result) { // Arrange var converter = new OpenIddictConverter(); // Act and assert Assert.Equal(result, converter.CanConvert(type)); }
public void CanConvert_ThrowsAnExceptionForNullType() { // Arrange var converter = new OpenIddictConverter(); // Act and assert var exception = Assert.Throws <ArgumentNullException>(() => converter.CanConvert(typeToConvert: null !)); Assert.Equal("typeToConvert", exception.ParamName); }
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, typeToConvert: null !, options: null !)); });
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 Write_ThrowsAnExceptionForNullValue() { // Arrange var converter = new OpenIddictConverter(); // Act and assert var exception = Assert.Throws <ArgumentNullException>(() => { converter.Write(writer: new Utf8JsonWriter(Stream.Null), value: null, options: null); }); Assert.Equal("value", exception.ParamName); }
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_ThrowsAnExceptionForNullType() { // Arrange var converter = new OpenIddictConverter(); // Act and assert var exception = Assert.Throws <ArgumentNullException>(() => { var reader = new Utf8JsonReader(); converter.Read(ref reader, type: null, options: null); }); Assert.Equal("type", exception.ParamName); }
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 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()); }
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 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()); }
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); }