Skip to content
This repository has been archived by the owner on Jul 9, 2024. It is now read-only.

Implements IAsyncParseNodeFactory interface #137

Merged
merged 5 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 17 additions & 2 deletions src/FormParseNodeFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace Microsoft.Kiota.Serialization.Form;

/// <summary>
/// The <see cref="IParseNodeFactory"/> implementation for form content types
/// The <see cref="IAsyncParseNodeFactory"/> implementation for form content types
/// </summary>
public class FormParseNodeFactory : IParseNodeFactory
public class FormParseNodeFactory : IAsyncParseNodeFactory
{
/// <inheritdoc/>
public string ValidContentType => "application/x-www-form-urlencoded";
Expand All @@ -22,4 +22,19 @@ public IParseNode GetRootParseNode(string contentType, Stream content) {
var rawValue = reader.ReadToEnd();
return new FormParseNode(rawValue);
}
/// <inheritdoc/>
public async Task<IParseNode> 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().ConfigureAwait(false);
return new FormParseNode(rawValue);
}
}
4 changes: 2 additions & 2 deletions src/Microsoft.Kiota.Serialization.Form.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<PackageProjectUrl>https://aka.ms/kiota/docs</PackageProjectUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<Deterministic>true</Deterministic>
<VersionPrefix>1.1.6</VersionPrefix>
<VersionPrefix>1.2.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down Expand Up @@ -45,7 +45,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.Kiota.Abstractions" Version="1.8.4" />
<PackageReference Include="Microsoft.Kiota.Abstractions" Version="1.9.0" />
</ItemGroup>

<PropertyGroup Condition="'$(TF_BUILD)' == 'true'">
Expand Down
45 changes: 45 additions & 0 deletions tests/FormAsyncParseNodeFactoryTests.cs
Original file line number Diff line number Diff line change
@@ -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>(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<ArgumentOutOfRangeException>(
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<ArgumentNullException>(
async () => await _formParseNodeFactory.GetRootParseNodeAsync(contentType!, jsonStream));

// Assert
Assert.NotNull(exception);
Assert.Equal("contentType", exception.ParamName);
}
}
Loading