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

Commit

Permalink
Merge pull request #137 from MihaMarkic/feature/async
Browse files Browse the repository at this point in the history
Implements IAsyncParseNodeFactory interface
  • Loading branch information
andrueastman authored May 9, 2024
2 parents 0438994 + 1ef7f38 commit f30c4ef
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
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);
}
}
2 changes: 1 addition & 1 deletion 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
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);
}
}

0 comments on commit f30c4ef

Please sign in to comment.