forked from ardalis/Result
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Extensions to Translate Ardalis.Result to Microsoft.AspNetCore.Ht…
…tp.IResult (ardalis#103) * Add Microsoft.AspNetCore.Http.IResult Extensions. Add minimal API sample project. * Code cleanup * Whitespace * Add minimal API sample * Add badrequest to weatherservice, change webmarker to interface * Rename ToMinimalApiResult method * Update README sample usage, rename extension class * Consolidate package
- Loading branch information
1 parent
860263c
commit 247e8df
Showing
14 changed files
with
347 additions
and
51 deletions.
There are no files selected for viewing
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
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
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
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
26 changes: 26 additions & 0 deletions
26
...t.SampleMinimalApi.FunctionalTests/Ardalis.Result.SampleMinimalApi.FunctionalTests.csproj
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,26 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.9" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" /> | ||
<PackageReference Include="xunit" Version="2.4.2" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="3.1.2"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Ardalis.Result.SampleMinimalApi\Ardalis.Result.SampleMinimalApi.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
70 changes: 70 additions & 0 deletions
70
sample/Ardalis.Result.SampleMinimalApi.FunctionalTests/NewWeatherForecast.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,70 @@ | ||
using Ardalis.Result.Sample.Core.DTOs; | ||
using Ardalis.Result.Sample.Core.Model; | ||
using Microsoft.AspNetCore.Mvc.Testing; | ||
using Newtonsoft.Json; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Ardalis.Result.SampleMinimalApi.FunctionalTests; | ||
public class NewWeatherForecast : IClassFixture<WebApplicationFactory<IWebMarker>> | ||
{ | ||
private const string ENDPOINT_POST_ROUTE = "/forecast/new"; | ||
private readonly HttpClient _client; | ||
|
||
public NewWeatherForecast(WebApplicationFactory<IWebMarker> factory) | ||
{ | ||
_client = factory.CreateClient(); | ||
} | ||
|
||
[Fact] | ||
public async Task ReturnsOkWithValueGivenValidPostalCode() | ||
{ | ||
var requestDto = new ForecastRequestDto() { PostalCode = "55555" }; | ||
|
||
var jsonContent = new StringContent(JsonConvert.SerializeObject(requestDto), Encoding.Default, "application/json"); | ||
var response = await _client.PostAsync(ENDPOINT_POST_ROUTE, jsonContent); | ||
response.EnsureSuccessStatusCode(); | ||
|
||
var stringResponse = await response.Content.ReadAsStringAsync(); | ||
var forecasts = JsonConvert.DeserializeObject<List<WeatherForecast>>(stringResponse); | ||
|
||
Assert.Equal("Freezing", forecasts.First().Summary); | ||
} | ||
|
||
[Fact] | ||
public async Task ReturnsBadRequestGivenNoPostalCode() | ||
{ | ||
var requestDto = new ForecastRequestDto() { PostalCode = "" }; | ||
var jsonContent = new StringContent(JsonConvert.SerializeObject(requestDto), Encoding.Default, "application/json"); | ||
var response = await _client.PostAsync(ENDPOINT_POST_ROUTE, jsonContent); | ||
|
||
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); | ||
} | ||
|
||
[Fact] | ||
public async Task ReturnsNotFoundGivenNonExistentPostalCode() | ||
{ | ||
var requestDto = new ForecastRequestDto() { PostalCode = "NotFound" }; | ||
var jsonContent = new StringContent(JsonConvert.SerializeObject(requestDto), Encoding.Default, "application/json"); | ||
var response = await _client.PostAsync(ENDPOINT_POST_ROUTE, jsonContent); | ||
|
||
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); | ||
} | ||
|
||
[Fact] | ||
public async Task ReturnsBadRequestGivenPostalCodeTooLong() | ||
{ | ||
var requestDto = new ForecastRequestDto() { PostalCode = "01234567890" }; | ||
var jsonContent = new StringContent(JsonConvert.SerializeObject(requestDto), Encoding.Default, "application/json"); | ||
var response = await _client.PostAsync(ENDPOINT_POST_ROUTE, jsonContent); | ||
|
||
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); | ||
var stringResponse = await response.Content.ReadAsStringAsync(); | ||
Assert.Contains("PostalCode cannot exceed 10 characters.", stringResponse); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
sample/Ardalis.Result.SampleMinimalApi/Ardalis.Result.SampleMinimalApi.csproj
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,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Ardalis.Result.AspNetCore\Ardalis.Result.AspNetCore.csproj"> | ||
<SetTargetFramework>TargetFramework=net6.0</SetTargetFramework> | ||
</ProjectReference> | ||
<ProjectReference Include="..\Ardalis.Result.Sample.Core\Ardalis.Result.Sample.Core.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,8 @@ | ||
namespace Ardalis.Result.SampleMinimalApi; | ||
|
||
/// <summary> | ||
/// This is a simple marker type that is used by the integration tests to reference the correct assembly for host building | ||
/// </summary> | ||
public interface IWebMarker | ||
{ | ||
} |
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,44 @@ | ||
using Ardalis.Result.AspNetCore; | ||
using Ardalis.Result.Sample.Core.DTOs; | ||
using Ardalis.Result.Sample.Core.Services; | ||
using Microsoft.AspNetCore.Localization; | ||
using System.Globalization; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
// Add services to the container. | ||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle | ||
builder.Services.AddEndpointsApiExplorer(); | ||
builder.Services.AddSwaggerGen(); | ||
builder.Services.AddScoped<WeatherService>(); | ||
builder.Services.AddLocalization(opt => { opt.ResourcesPath = "Resources"; }); | ||
builder.Services.Configure<RequestLocalizationOptions>(options => | ||
{ | ||
var supportedCultures = new List<CultureInfo> | ||
{ | ||
new CultureInfo("en-US"), | ||
new CultureInfo("de-DE"), | ||
}; | ||
options.DefaultRequestCulture = new RequestCulture("en-US"); | ||
options.SupportedCultures = supportedCultures; | ||
options.SupportedUICultures = supportedCultures; | ||
}); | ||
|
||
var app = builder.Build(); | ||
|
||
// Configure the HTTP request pipeline. | ||
if (app.Environment.IsDevelopment()) | ||
{ | ||
app.UseSwagger(); | ||
app.UseSwaggerUI(); | ||
} | ||
|
||
app.UseHttpsRedirection(); | ||
|
||
app.MapPost("/Forecast/New", (ForecastRequestDto request, WeatherService weatherService) => | ||
{ | ||
return weatherService.GetForecast(request).ToMinimalApiResult(); | ||
}) | ||
.WithName("NewWeatherForecast"); | ||
|
||
app.Run(); |
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,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
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
Oops, something went wrong.