From f3816873528ed135b4dfdfca9b3515e15abf8380 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miha=20Marki=C4=8D?= Date: Sat, 4 May 2024 11:57:38 +0200 Subject: [PATCH 1/5] Implements IAsyncParseNodeFactory interface --- src/FormParseNodeFactory.cs | 19 +++++++++-- tests/FormAsyncParseNodeFactoryTests.cs | 45 +++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 tests/FormAsyncParseNodeFactoryTests.cs diff --git a/src/FormParseNodeFactory.cs b/src/FormParseNodeFactory.cs index 1cb1c0a..26707fd 100644 --- a/src/FormParseNodeFactory.cs +++ b/src/FormParseNodeFactory.cs @@ -3,9 +3,9 @@ namespace Microsoft.Kiota.Serialization.Form; /// -/// The implementation for form content types +/// The implementation for form content types /// -public class FormParseNodeFactory : IParseNodeFactory +public class FormParseNodeFactory : IAsyncParseNodeFactory { /// public string ValidContentType => "application/x-www-form-urlencoded"; @@ -22,4 +22,19 @@ public IParseNode GetRootParseNode(string contentType, Stream content) { var rawValue = reader.ReadToEnd(); return new FormParseNode(rawValue); } + /// + public async Task GetRootParseNodeAsync(string contentType, Stream content, + CancellationToken cancellationToken = default) + { + if(string.IsNullOrEmpty(contentType)) + throw new ArgumentNullException(nameof(contentType)); + if(!ValidContentType.Equals(contentType, StringComparison.OrdinalIgnoreCase)) + throw new ArgumentOutOfRangeException($"expected a {ValidContentType} content type"); + if(content == null) + throw new ArgumentNullException(nameof(content)); + + using var reader = new StreamReader(content); + var rawValue = await reader.ReadToEndAsync(); + return new FormParseNode(rawValue); + } } diff --git a/tests/FormAsyncParseNodeFactoryTests.cs b/tests/FormAsyncParseNodeFactoryTests.cs new file mode 100644 index 0000000..88d451d --- /dev/null +++ b/tests/FormAsyncParseNodeFactoryTests.cs @@ -0,0 +1,45 @@ +using System.Text; + +namespace Microsoft.Kiota.Serialization.Form.Tests; + +public class FormAsyncParseNodeFactoryTests +{ + private readonly FormParseNodeFactory _formParseNodeFactory = new(); + private const string TestJsonString = "key=value"; + [Fact] + public async Task GetsWriterForFormContentType() + { + using var formStream = new MemoryStream(Encoding.UTF8.GetBytes(TestJsonString)); + var formParseNode = await _formParseNodeFactory.GetRootParseNodeAsync(_formParseNodeFactory.ValidContentType, formStream); + + // Assert + Assert.NotNull(formParseNode); + Assert.IsAssignableFrom(formParseNode); + } + [Fact] + public async Task ThrowsArgumentOutOfRangeExceptionForInvalidContentType() + { + var streamContentType = "application/octet-stream"; + using var jsonStream = new MemoryStream(Encoding.UTF8.GetBytes(TestJsonString)); + var exception = await Assert.ThrowsAsync( + async () => await _formParseNodeFactory.GetRootParseNodeAsync(streamContentType, jsonStream)); + + // Assert + Assert.NotNull(exception); + Assert.Equal($"expected a {_formParseNodeFactory.ValidContentType} content type", exception.ParamName); + } + + [Theory] + [InlineData(null)] + [InlineData("")] + public async Task ThrowsArgumentNullExceptionForNoContentType(string? contentType) + { + using var jsonStream = new MemoryStream(Encoding.UTF8.GetBytes(TestJsonString)); + var exception = await Assert.ThrowsAsync( + async () => await _formParseNodeFactory.GetRootParseNodeAsync(contentType!, jsonStream)); + + // Assert + Assert.NotNull(exception); + Assert.Equal("contentType", exception.ParamName); + } +} \ No newline at end of file From 49f9d964484d79e55ca7a1058617162e1e0683e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miha=20Marki=C4=8D?= Date: Tue, 7 May 2024 10:49:13 +0200 Subject: [PATCH 2/5] Updates changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1176196..d3957dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,8 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [1.2.0] - 2024-05-13 + ### Added +- Implements IAsyncParseNodeFactory interface which adds async support + ## [1.1.6] - 2024-04-19 ### Changed From e8556b935e29b0315a82588b1396a60c2347debc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miha=20Marki=C4=8D?= Date: Tue, 7 May 2024 10:49:29 +0200 Subject: [PATCH 3/5] Updates version --- src/Microsoft.Kiota.Serialization.Form.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.Kiota.Serialization.Form.csproj b/src/Microsoft.Kiota.Serialization.Form.csproj index 153a26b..bfef6d2 100644 --- a/src/Microsoft.Kiota.Serialization.Form.csproj +++ b/src/Microsoft.Kiota.Serialization.Form.csproj @@ -16,7 +16,7 @@ https://aka.ms/kiota/docs true true - 1.1.6 + 1.2.0 true true From b5014c3f364673dd9f836925ec12c86f6ae8f261 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miha=20Marki=C4=8D?= Date: Tue, 7 May 2024 10:49:53 +0200 Subject: [PATCH 4/5] Updates reference to kiota-abstractions-dotnet --- src/Microsoft.Kiota.Serialization.Form.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.Kiota.Serialization.Form.csproj b/src/Microsoft.Kiota.Serialization.Form.csproj index bfef6d2..dbc6cf4 100644 --- a/src/Microsoft.Kiota.Serialization.Form.csproj +++ b/src/Microsoft.Kiota.Serialization.Form.csproj @@ -45,7 +45,7 @@ - + From 1ef7f3858684b852ce69c2e478dc65482478a9f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miha=20Marki=C4=8D?= Date: Thu, 9 May 2024 08:26:59 +0200 Subject: [PATCH 5/5] Adds ConfigureAwait(false) --- src/FormParseNodeFactory.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/FormParseNodeFactory.cs b/src/FormParseNodeFactory.cs index 26707fd..8a24c81 100644 --- a/src/FormParseNodeFactory.cs +++ b/src/FormParseNodeFactory.cs @@ -34,7 +34,7 @@ public async Task GetRootParseNodeAsync(string contentType, Stream c throw new ArgumentNullException(nameof(content)); using var reader = new StreamReader(content); - var rawValue = await reader.ReadToEndAsync(); + var rawValue = await reader.ReadToEndAsync().ConfigureAwait(false); return new FormParseNode(rawValue); } }