-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #168 from ServiceComposer/custom-status-code
Support custom HTTP status code
- Loading branch information
Showing
10 changed files
with
262 additions
and
24 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
62 changes: 62 additions & 0 deletions
62
src/ServiceComposer.AspNetCore.Endpoints.Tests/When_setting_custom_http_status_code.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,62 @@ | ||
using System; | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using ServiceComposer.AspNetCore.Testing; | ||
using Xunit; | ||
|
||
namespace ServiceComposer.AspNetCore.Endpoints.Tests | ||
{ | ||
public class When_setting_custom_http_status_code | ||
{ | ||
public class CustomStatusCodeHandler : ICompositionRequestsHandler | ||
{ | ||
[HttpGet("/sample/{id}")] | ||
public Task Handle(HttpRequest request) | ||
{ | ||
var response = request.HttpContext.Response; | ||
response.StatusCode = (int)HttpStatusCode.Forbidden; | ||
|
||
return Task.CompletedTask; | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task Default_status_code_should_be_overwritten() | ||
{ | ||
// Arrange | ||
var expectedStatusCode = HttpStatusCode.Forbidden; | ||
var client = new SelfContainedWebApplicationFactoryWithWebHost<When_setting_custom_http_status_code> | ||
( | ||
configureServices: services => | ||
{ | ||
services.AddViewModelComposition(options => | ||
{ | ||
options.AssemblyScanner.Disable(); | ||
options.RegisterCompositionHandler<CustomStatusCodeHandler>(); | ||
}); | ||
services.AddControllers(); | ||
services.AddRouting(); | ||
}, | ||
configure: app => | ||
{ | ||
app.UseRouting(); | ||
app.UseEndpoints(builder => | ||
{ | ||
builder.MapCompositionHandlers(); | ||
builder.MapControllers(); | ||
}); | ||
} | ||
).CreateClient(); | ||
|
||
// Act | ||
var composedResponse = await client.GetAsync("/sample/1"); | ||
|
||
// Assert | ||
Assert.Equal(expectedStatusCode, composedResponse.StatusCode); | ||
} | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/ServiceComposer.AspNetCore.Tests/When_setting_custom_http_status_code.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,60 @@ | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Routing; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using ServiceComposer.AspNetCore.Gateway; | ||
using ServiceComposer.AspNetCore.Testing; | ||
using Xunit; | ||
|
||
namespace ServiceComposer.AspNetCore.Tests | ||
{ | ||
public class When_setting_custom_http_status_code | ||
{ | ||
class CustomStatusCodeHandler : IHandleRequests | ||
{ | ||
public Task Handle(string requestId, dynamic vm, RouteData routeData, HttpRequest request) | ||
{ | ||
var response = request.HttpContext.Response; | ||
response.StatusCode = (int)HttpStatusCode.Forbidden; | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
public bool Matches(RouteData routeData, string httpVerb, HttpRequest request) | ||
{ | ||
var controller = routeData.Values["controller"]?.ToString(); | ||
return controller?.ToLowerInvariant() == "custom-status-code"; | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task Default_status_code_should_be_overwritten() | ||
{ | ||
// Arrange | ||
var expectedStatusCode = HttpStatusCode.Forbidden; | ||
var client = new SelfContainedWebApplicationFactoryWithWebHost<When_setting_custom_http_status_code> | ||
( | ||
configureServices: services => | ||
{ | ||
services.AddViewModelComposition(options => | ||
{ | ||
options.AssemblyScanner.Disable(); | ||
options.RegisterRequestsHandler<CustomStatusCodeHandler>(); | ||
}); | ||
services.AddRouting(); | ||
}, | ||
configure: app => | ||
{ | ||
app.RunCompositionGatewayWithDefaultRoutes(); | ||
} | ||
).CreateClient(); | ||
|
||
// Act | ||
var response = await client.GetAsync("/custom-status-code/1"); | ||
|
||
// Assert | ||
Assert.Equal(expectedStatusCode, response.StatusCode); | ||
} | ||
} | ||
} |
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
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 @@ | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Routing; | ||
using ServiceComposer.AspNetCore; | ||
|
||
namespace Snippets.NetCore2x.SampleHandler | ||
{ | ||
// begin-snippet: net-core-2x-sample-handler-with-custom-status-code | ||
public class SampleHandlerWithCustomStatusCode : IHandleRequests | ||
{ | ||
public bool Matches(RouteData routeData, string httpVerb, HttpRequest request) | ||
{ | ||
return true; | ||
} | ||
|
||
public Task Handle(string requestId, dynamic vm, RouteData routeData, HttpRequest request) | ||
{ | ||
var response = request.HttpContext.Response; | ||
response.StatusCode = (int)HttpStatusCode.Forbidden; | ||
|
||
return Task.CompletedTask; | ||
} | ||
} | ||
// end-snippet | ||
} |
Oops, something went wrong.