-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ae73791
commit 042db94
Showing
3 changed files
with
255 additions
and
0 deletions.
There are no files selected for viewing
211 changes: 211 additions & 0 deletions
211
source/TestUtils/PeanutButter.TestUtils.AspNetCore.Tests/Fakes/TestJsonResultExecutor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
using System; | ||
using System.Net; | ||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Infrastructure; | ||
using Microsoft.AspNetCore.Mvc.ViewEngines; | ||
using PeanutButter.TestUtils.AspNetCore.Builders; | ||
using PeanutButter.TestUtils.AspNetCore.Fakes; | ||
using PeanutButter.Utils; | ||
using JsonResult = Microsoft.AspNetCore.Mvc.JsonResult; | ||
using TempDataDictionary = Microsoft.AspNetCore.Mvc.ViewFeatures.TempDataDictionary; | ||
using ViewResult = Microsoft.AspNetCore.Mvc.ViewResult; | ||
|
||
namespace PeanutButter.TestUtils.AspNetCore.Tests.Fakes; | ||
|
||
[TestFixture] | ||
public class TestJsonResultExecutor | ||
{ | ||
[Test] | ||
public async Task ShouldRenderJsonToTheContext() | ||
{ | ||
// Arrange | ||
var actionContext = ActionContextBuilder.BuildDefault(); | ||
var data = new | ||
{ | ||
id = 1, | ||
name = GetRandomString() | ||
}; | ||
var expected = JsonSerializer.Serialize(data); | ||
var actionResult = new JsonResult(data); | ||
var sut = Create(); | ||
// Act | ||
await sut.ExecuteAsync( | ||
actionContext, | ||
actionResult | ||
); | ||
// Assert | ||
var result = await actionContext.HttpContext.Response.Body.ReadAllTextAsync(); | ||
Expect(result) | ||
.To.Equal(expected); | ||
} | ||
|
||
|
||
private static IActionResultExecutor<JsonResult> Create() | ||
{ | ||
return new FakeJsonResultExecutor(); | ||
} | ||
} | ||
|
||
[TestFixture] | ||
public class TestFakeObjectResultExecutor | ||
{ | ||
[Test] | ||
public async Task ShouldRenderJsonToTheContext() | ||
{ | ||
// Arrange | ||
var actionContext = ActionContextBuilder.BuildDefault(); | ||
var data = new | ||
{ | ||
id = 1, | ||
name = GetRandomString() | ||
}; | ||
var expected = JsonSerializer.Serialize(data); | ||
var actionResult = new ObjectResult(data); | ||
var sut = Create(); | ||
// Act | ||
await sut.ExecuteAsync( | ||
actionContext, | ||
actionResult | ||
); | ||
// Assert | ||
var result = await actionContext.HttpContext.Response.Body.ReadAllTextAsync(); | ||
Expect(result) | ||
.To.Equal(expected); | ||
} | ||
|
||
|
||
private static IActionResultExecutor<ObjectResult> Create() | ||
{ | ||
return new FakeObjectResultExecutor(); | ||
} | ||
} | ||
|
||
[TestFixture] | ||
public class TestFakeViewResultExecutor | ||
{ | ||
[TestFixture] | ||
public class ActingOnViewResult | ||
{ | ||
[Test] | ||
public async Task ShouldRenderSomethingToTheContext() | ||
{ | ||
// Arrange | ||
var actionContext = ActionContextBuilder.BuildDefault(); | ||
var data = new | ||
{ | ||
id = 1, | ||
value = false | ||
}; | ||
var viewName = GetRandomString(); | ||
var viewDataDictionary = ViewDataDictionaryBuilder.Create() | ||
.WithModel(data) | ||
.Build(); | ||
var httpStatusCode = GetRandom<HttpStatusCode>(); | ||
var contentType = GetRandomFrom(["text/html", "application/json", "text/plain", "text/xml"]); | ||
var actionResult = new ViewResult() | ||
{ | ||
ContentType = contentType, | ||
StatusCode = (int)httpStatusCode, | ||
TempData = new TempDataDictionary(actionContext.HttpContext, new FakeTempDataProvider()), | ||
ViewData = viewDataDictionary, | ||
ViewEngine = Substitute.For<IViewEngine>(), | ||
ViewName = viewName | ||
}; | ||
var sut = Create(); | ||
// Act | ||
await sut.ExecuteAsync(actionContext, actionResult); | ||
// Assert | ||
var result = await actionContext.HttpContext.Response.Body.ReadAllTextAsync(); | ||
var opts = new JsonSerializerOptions() | ||
{ | ||
WriteIndented = true | ||
}; | ||
Expect(result) | ||
.To.Contain($"Status: {actionResult.StatusCode}") | ||
.Then($"Content Type: {contentType}") | ||
.Then($"View: {viewName}") | ||
.Then("Model:") | ||
.Then(JsonSerializer.Serialize(data, opts)) | ||
.Then("ViewData:") | ||
.Then(JsonSerializer.Serialize(viewDataDictionary, opts)); | ||
var httpContext = actionContext.HttpContext; | ||
Expect(httpContext.Items["Model"]) | ||
.To.Be(data); | ||
Expect(httpContext.Items["ViewData"]) | ||
.To.Be(viewDataDictionary); | ||
Expect(httpContext.Response.StatusCode) | ||
.To.Equal(httpStatusCode); | ||
Expect(httpContext.Response.ContentType) | ||
.To.Equal(contentType); | ||
} | ||
|
||
private static IActionResultExecutor<ViewResult> Create() | ||
{ | ||
return new FakeViewResultExecutor(); | ||
} | ||
} | ||
|
||
[TestFixture] | ||
public class ActingOnPartialViewResult | ||
{ | ||
[Test] | ||
public async Task ShouldRenderSomethingToTheContext() | ||
{ | ||
// Arrange | ||
var actionContext = ActionContextBuilder.BuildDefault(); | ||
var data = new | ||
{ | ||
id = 1, | ||
value = false | ||
}; | ||
var viewName = GetRandomString(); | ||
var viewDataDictionary = ViewDataDictionaryBuilder.Create() | ||
.WithModel(data) | ||
.Build(); | ||
var httpStatusCode = GetRandom<HttpStatusCode>(); | ||
var contentType = GetRandomFrom(["text/html", "application/json", "text/plain", "text/xml"]); | ||
var actionResult = new PartialViewResult() | ||
{ | ||
ContentType = contentType, | ||
StatusCode = (int)httpStatusCode, | ||
TempData = new TempDataDictionary(actionContext.HttpContext, new FakeTempDataProvider()), | ||
ViewData = viewDataDictionary, | ||
ViewEngine = Substitute.For<IViewEngine>(), | ||
ViewName = viewName | ||
}; | ||
var sut = Create(); | ||
// Act | ||
await sut.ExecuteAsync(actionContext, actionResult); | ||
// Assert | ||
var result = await actionContext.HttpContext.Response.Body.ReadAllTextAsync(); | ||
var opts = new JsonSerializerOptions() | ||
{ | ||
WriteIndented = true | ||
}; | ||
Expect(result) | ||
.To.Contain($"Status: {actionResult.StatusCode}") | ||
.Then($"Content Type: {contentType}") | ||
.Then($"View: {viewName}") | ||
.Then("Model:") | ||
.Then(JsonSerializer.Serialize(data, opts)) | ||
.Then("ViewData:") | ||
.Then(JsonSerializer.Serialize(viewDataDictionary, opts)); | ||
var httpContext = actionContext.HttpContext; | ||
Expect(httpContext.Items["Model"]) | ||
.To.Be(data); | ||
Expect(httpContext.Items["ViewData"]) | ||
.To.Be(viewDataDictionary); | ||
Expect(httpContext.Response.StatusCode) | ||
.To.Equal(httpStatusCode); | ||
Expect(httpContext.Response.ContentType) | ||
.To.Equal(contentType); | ||
} | ||
|
||
private static IActionResultExecutor<PartialViewResult> Create() | ||
{ | ||
return new FakeViewResultExecutor(); | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
source/TestUtils/PeanutButter.TestUtils.AspNetCore/Fakes/FakeJsonResultExecutor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System.Net; | ||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Infrastructure; | ||
|
||
#if BUILD_PEANUTBUTTER_INTERNAL | ||
namespace Imported.PeanutButter.TestUtils.AspNetCore.Fakes; | ||
#else | ||
namespace PeanutButter.TestUtils.AspNetCore.Fakes; | ||
#endif | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
#if BUILD_PEANUTBUTTER_INTERNAL | ||
internal | ||
#else | ||
public | ||
#endif | ||
class FakeJsonResultExecutor | ||
: IActionResultExecutor<JsonResult> | ||
{ | ||
/// <inheritdoc /> | ||
public async Task ExecuteAsync( | ||
ActionContext context, | ||
JsonResult result | ||
) | ||
{ | ||
var json = result.Value is null | ||
? "" | ||
: JsonSerializer.Serialize(result.Value); | ||
await context.HttpContext.Response.WriteAsync( | ||
json | ||
); | ||
context.HttpContext.Response.StatusCode = | ||
result.StatusCode ?? (int)HttpStatusCode.OK; | ||
context.HttpContext.Response.ContentType = | ||
result.ContentType ?? "application/json"; | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
...ls/PeanutButter.TestUtils.AspNetCore/PeanutButter.TestUtils.AspNetCore.csproj.DotSettings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> | ||
<s:String x:Key="/Default/CodeInspection/Daemon/ConfigureAwaitAnalysisMode/@EntryValue">Library</s:String></wpf:ResourceDictionary> |