Skip to content

Commit

Permalink
Migrated Dapr.AspNetCore.Test
Browse files Browse the repository at this point in the history
Signed-off-by: Whit Waldo <[email protected]>
  • Loading branch information
WhitWaldo committed Jan 18, 2025
1 parent a4fd19b commit 7183f7f
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 58 deletions.
64 changes: 34 additions & 30 deletions test/Dapr.AspNetCore.Test/CloudEventsMiddlewareTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Dapr.AspNetCore.Test
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using FluentAssertions;
using Shouldly;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Xunit;
Expand All @@ -44,8 +44,8 @@ public async Task InvokeAsync_IgnoresOtherContentTypes(string contentType)
// Do verification in the scope of the middleware
app.Run(httpContext =>
{
httpContext.Request.ContentType.Should().Be(contentType);
ReadBody(httpContext.Request.Body).Should().Be("Hello, world!");
httpContext.Request.ContentType.ShouldBe(contentType);
ReadBody(httpContext.Request.Body).ShouldBe("Hello, world!");
return Task.CompletedTask;
});

Expand Down Expand Up @@ -77,8 +77,8 @@ public async Task InvokeAsync_ReplacesBodyJson(string dataContentType, string ch
// Do verification in the scope of the middleware
app.Run(httpContext =>
{
httpContext.Request.ContentType.Should().Be(dataContentType ?? "application/json");
ReadBody(httpContext.Request.Body).Should().Be("{\"name\":\"jimmy\"}");
httpContext.Request.ContentType.ShouldBe(dataContentType ?? "application/json");
ReadBody(httpContext.Request.Body).ShouldBe("{\"name\":\"jimmy\"}");
return Task.CompletedTask;
});

Expand Down Expand Up @@ -117,8 +117,8 @@ public async Task InvokeAsync_ReplacesPascalCasedBodyJson(string dataContentType
// Do verification in the scope of the middleware
app.Run(httpContext =>
{
httpContext.Request.ContentType.Should().Be(dataContentType ?? "application/json");
ReadBody(httpContext.Request.Body).Should().Be("{\"name\":\"jimmy\"}");
httpContext.Request.ContentType.ShouldBe(dataContentType ?? "application/json");
ReadBody(httpContext.Request.Body).ShouldBe("{\"name\":\"jimmy\"}");
return Task.CompletedTask;
});

Expand Down Expand Up @@ -160,11 +160,13 @@ public async Task InvokeAsync_ForwardsJsonPropertiesAsHeaders(string dataContent
// Do verification in the scope of the middleware
app.Run(httpContext =>
{
httpContext.Request.ContentType.Should().Be(dataContentType ?? "application/json");
ReadBody(httpContext.Request.Body).Should().Be("{\"name\":\"jimmy\"}");
httpContext.Request.ContentType.ShouldBe(dataContentType ?? "application/json");
ReadBody(httpContext.Request.Body).ShouldBe("{\"name\":\"jimmy\"}");

httpContext.Request.Headers.Should().ContainKey("Cloudevent.type").WhichValue.Should().BeEquivalentTo("Test.Type");
httpContext.Request.Headers.Should().ContainKey("Cloudevent.subject").WhichValue.Should().BeEquivalentTo("Test.Subject");
httpContext.Request.Headers.ShouldContainKey("Cloudevent.type");
httpContext.Request.Headers["Cloudevent.type"].ShouldBeEquivalentTo("Test.Type");
httpContext.Request.Headers.ShouldContainKey("Cloudevent.subject");
httpContext.Request.Headers["Cloudevent.subject"].ShouldBeEquivalentTo("Test.Subject");
return Task.CompletedTask;
});

Expand Down Expand Up @@ -207,11 +209,12 @@ public async Task InvokeAsync_ForwardsIncludedJsonPropertiesAsHeaders(string dat
// Do verification in the scope of the middleware
app.Run(httpContext =>
{
httpContext.Request.ContentType.Should().Be(dataContentType ?? "application/json");
ReadBody(httpContext.Request.Body).Should().Be("{\"name\":\"jimmy\"}");
httpContext.Request.ContentType.ShouldBe(dataContentType ?? "application/json");
ReadBody(httpContext.Request.Body).ShouldBe("{\"name\":\"jimmy\"}");

httpContext.Request.Headers.Should().ContainKey("Cloudevent.type").WhichValue.Should().BeEquivalentTo("Test.Type");
httpContext.Request.Headers.Should().NotContainKey("Cloudevent.subject");
httpContext.Request.Headers.ShouldContainKey("Cloudevent.type");
httpContext.Request.Headers["Cloudevent.type"].ShouldBeEquivalentTo("Test.Type");
httpContext.Request.Headers.ShouldNotContainKey("Cloudevent.subject");
return Task.CompletedTask;
});

Expand Down Expand Up @@ -254,11 +257,12 @@ public async Task InvokeAsync_DoesNotForwardExcludedJsonPropertiesAsHeaders(stri
// Do verification in the scope of the middleware
app.Run(httpContext =>
{
httpContext.Request.ContentType.Should().Be(dataContentType ?? "application/json");
ReadBody(httpContext.Request.Body).Should().Be("{\"name\":\"jimmy\"}");
httpContext.Request.ContentType.ShouldBe(dataContentType ?? "application/json");
ReadBody(httpContext.Request.Body).ShouldBe("{\"name\":\"jimmy\"}");

httpContext.Request.Headers.Should().NotContainKey("Cloudevent.type");
httpContext.Request.Headers.Should().ContainKey("Cloudevent.subject").WhichValue.Should().BeEquivalentTo("Test.Subject");
httpContext.Request.Headers.ShouldNotContainKey("Cloudevent.type");
httpContext.Request.Headers.ShouldContainKey("Cloudevent.subject");
httpContext.Request.Headers["Cloudevent.subject"].ShouldBeEquivalentTo("Test.Subject");
return Task.CompletedTask;
});

Expand Down Expand Up @@ -297,8 +301,8 @@ public async Task InvokeAsync_ReplacesBodyNonJsonData()
// Do verification in the scope of the middleware
app.Run(httpContext =>
{
httpContext.Request.ContentType.Should().Be("text/plain");
ReadBody(httpContext.Request.Body).Should().Be(expected);
httpContext.Request.ContentType.ShouldBe("text/plain");
ReadBody(httpContext.Request.Body).ShouldBe(expected);
return Task.CompletedTask;
});

Expand Down Expand Up @@ -331,8 +335,8 @@ public async Task InvokeAsync_ReplacesBodyNonJsonData_ExceptWhenSuppressed()
// Do verification in the scope of the middleware
app.Run(httpContext =>
{
httpContext.Request.ContentType.Should().Be("text/plain");
ReadBody(httpContext.Request.Body).Should().Be(expected);
httpContext.Request.ContentType.ShouldBe("text/plain");
ReadBody(httpContext.Request.Body).ShouldBe(expected);
return Task.CompletedTask;
});

Expand Down Expand Up @@ -366,8 +370,8 @@ public async Task InvokeAsync_ReplacesBodyJson_NormalizesPayloadCharset()
// Do verification in the scope of the middleware
app.Run(httpContext =>
{
httpContext.Request.ContentType.Should().Be("application/person+json");
ReadBody(httpContext.Request.Body).Should().Be("{\"name\":\"jimmy\"}");
httpContext.Request.ContentType.ShouldBe("application/person+json");
ReadBody(httpContext.Request.Body).ShouldBe("{\"name\":\"jimmy\"}");
return Task.CompletedTask;
});

Expand Down Expand Up @@ -396,14 +400,14 @@ public async Task InvokeAsync_ReadsBinaryData()
// Do verification in the scope of the middleware
app.Run(httpContext =>
{
httpContext.Request.ContentType.Should().Be(dataContentType);
httpContext.Request.ContentType.ShouldBe(dataContentType);
var bytes = new byte[httpContext.Request.Body.Length];
#if NET9_0
httpContext.Request.Body.ReadExactly(bytes, 0, bytes.Length);
#else
httpContext.Request.Body.Read(bytes, 0, bytes.Length);
#endif
bytes.Should().Equal(data);
bytes.ShouldBe(data);
return Task.CompletedTask;
});

Expand Down Expand Up @@ -432,9 +436,9 @@ public async Task InvokeAsync_DataAndData64Set_ReturnsBadRequest()
// Do verification in the scope of the middleware
app.Run(httpContext =>
{
httpContext.Request.ContentType.Should().Be("application/json");
httpContext.Request.ContentType.ShouldBe("application/json");
var body = ReadBody(httpContext.Request.Body);
body.Should().Equals(data);
body.ShouldBe(data);
return Task.CompletedTask;
});

Expand All @@ -447,7 +451,7 @@ public async Task InvokeAsync_DataAndData64Set_ReturnsBadRequest()
MakeBody($"{{ \"datacontenttype\": \"{dataContentType}\", \"data_base64\": \"{base64Str}\", \"data\": {data} }}");

await pipeline.Invoke(context);
context.Response.StatusCode.Should().Be((int)HttpStatusCode.BadRequest);
context.Response.StatusCode.ShouldBe((int)HttpStatusCode.BadRequest);
}

private static Stream MakeBody(string text, Encoding encoding = null)
Expand Down
2 changes: 1 addition & 1 deletion test/Dapr.AspNetCore.Test/Dapr.AspNetCore.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="FluentAssertions"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Shouldly"/>
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio">
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Dapr.AspNetCore.Test
using System.Linq;
using System.Reflection;
using Dapr.AspNetCore.Resources;
using FluentAssertions;
using Shouldly;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Mvc.ModelBinding;
Expand All @@ -34,9 +34,7 @@ public void OnProvidersExecuted_NullActionsBindingSource()

Action action = () => provider.OnProvidersExecuted(context);

action
.Should()
.NotThrow<NullReferenceException>();
action.ShouldNotThrow();
}

[Fact]
Expand All @@ -48,8 +46,7 @@ public void OnProvidersExecuted_StateEntryParameterThrows()
Action action = () => provider.OnProvidersExecuted(context);

action
.Should()
.Throw<InvalidOperationException>(SR.ErrorStateStoreNameNotProvidedForStateEntry);
.ShouldThrow<InvalidOperationException>(SR.ErrorStateStoreNameNotProvidedForStateEntry);
}

private ApplicationModelProviderContext CreateContext(string methodName)
Expand Down
42 changes: 21 additions & 21 deletions test/Dapr.AspNetCore.Test/StateEntryModelBinderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Dapr.AspNetCore.Test
using System.Threading.Tasks;
using Dapr.Client;
using Dapr.Client.Autogen.Grpc.v1;
using FluentAssertions;
using Shouldly;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
Expand All @@ -38,9 +38,9 @@ public async Task BindAsync_WithoutMatchingRouteValue_ReportsError()

await binder.BindModelAsync(context);

context.Result.IsModelSet.Should().BeFalse();
context.ModelState.ErrorCount.Should().Be(1);
context.ModelState["testParameter"].Errors.Count.Should().Be(1);
context.Result.IsModelSet.ShouldBeFalse();
context.ModelState.ErrorCount.ShouldBe(1);
context.ModelState["testParameter"].Errors.Count.ShouldBe(1);

// No request to state store, validated by disposing client
}
Expand All @@ -66,12 +66,12 @@ public async Task BindAsync_CanBindValue()
await SendResponseWithState(state, request);

// Get response and validate
context.Result.IsModelSet.Should().BeTrue();
context.Result.Model.As<Widget>().Size.Should().Be("small");
context.Result.Model.As<Widget>().Color.Should().Be("yellow");
context.Result.IsModelSet.ShouldBeTrue();
((Widget)context.Result.Model).Size.ShouldBe("small");
((Widget)context.Result.Model).Color.ShouldBe("yellow");

context.ValidationState.Count.Should().Be(1);
context.ValidationState[context.Result.Model].SuppressValidation.Should().BeTrue();
context.ValidationState.Count.ShouldBe(1);
context.ValidationState[context.Result.Model].SuppressValidation.ShouldBeTrue();
}

[Fact]
Expand All @@ -95,13 +95,13 @@ public async Task BindAsync_CanBindStateEntry()
await SendResponseWithState(state, request);

// Get response and validate
context.Result.IsModelSet.Should().BeTrue();
context.Result.Model.As<StateEntry<Widget>>().Key.Should().Be("test");
context.Result.Model.As<StateEntry<Widget>>().Value.Size.Should().Be("small");
context.Result.Model.As<StateEntry<Widget>>().Value.Color.Should().Be("yellow");
context.Result.IsModelSet.ShouldBeTrue();
((StateEntry<Widget>)context.Result.Model).Key.ShouldBe("test");
((StateEntry<Widget>)context.Result.Model).Value.Size.ShouldBe("small");
((StateEntry<Widget>)context.Result.Model).Value.Color.ShouldBe("yellow");

context.ValidationState.Count.Should().Be(1);
context.ValidationState[context.Result.Model].SuppressValidation.Should().BeTrue();
context.ValidationState.Count.ShouldBe(1);
context.ValidationState[context.Result.Model].SuppressValidation.ShouldBeTrue();
}

[Fact]
Expand All @@ -122,9 +122,9 @@ public async Task BindAsync_ReturnsNullForNonExistentStateEntry()

await SendResponseWithState<string>(null, request);

context.ModelState.IsValid.Should().BeTrue();
context.Result.IsModelSet.Should().BeFalse();
context.Result.Should().Be(ModelBindingResult.Failed());
context.ModelState.IsValid.ShouldBeTrue();
context.Result.IsModelSet.ShouldBeFalse();
context.Result.ShouldBe(ModelBindingResult.Failed());
}

[Fact]
Expand All @@ -145,9 +145,9 @@ public async Task BindAsync_WithStateEntry_ForNonExistentStateEntry()

await SendResponseWithState<string>(null, request);

context.ModelState.IsValid.Should().BeTrue();
context.Result.IsModelSet.Should().BeTrue();
((StateEntry<Widget>)context.Result.Model).Value.Should().BeNull();
context.ModelState.IsValid.ShouldBeTrue();
context.Result.IsModelSet.ShouldBeTrue();
((StateEntry<Widget>)context.Result.Model).Value.ShouldBeNull();
}

private static ModelBindingContext CreateContext(IServiceProvider services)
Expand Down

0 comments on commit 7183f7f

Please sign in to comment.