-
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.
Merge pull request #25 from JosephWoodward/HttpStatusOverload
Add HttpStatus overloads, tidy up overloads and update test names
- Loading branch information
Showing
11 changed files
with
169 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
24 changes: 24 additions & 0 deletions
24
src/GlobalExceptionHandler.Tests/Exceptions/HttpNotFoundException.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,24 @@ | ||
using System; | ||
using System.Net; | ||
|
||
namespace GlobalExceptionHandler.Tests.Exceptions | ||
{ | ||
public class HttpNotFoundException : Exception | ||
{ | ||
public HttpNotFoundException() | ||
{ | ||
} | ||
|
||
public HttpNotFoundException(string message) : base(message) | ||
{ | ||
} | ||
|
||
public HttpNotFoundException(string message, Exception inner) : base(message, inner) | ||
{ | ||
} | ||
|
||
public HttpStatusCode StatusCodeEnum => HttpStatusCode.NotFound; | ||
|
||
public int StatusCodeInt => 404; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
src/GlobalExceptionHandler.Tests/Exceptions/RecordNotFoundException.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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
using System; | ||
using System.Net; | ||
|
||
namespace GlobalExceptionHandler.Tests.Exceptions | ||
{ | ||
|
57 changes: 57 additions & 0 deletions
57
src/GlobalExceptionHandler.Tests/WebApi/StatusCodeTests/StatusCodeAtRunTimeWithEnum.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,57 @@ | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using GlobalExceptionHandler.Tests.Exceptions; | ||
using GlobalExceptionHandler.Tests.Fixtures; | ||
using GlobalExceptionHandler.WebApi; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.TestHost; | ||
using Newtonsoft.Json; | ||
using Shouldly; | ||
using Xunit; | ||
|
||
namespace GlobalExceptionHandler.Tests.WebApi.StatusCodeTests | ||
{ | ||
public class StatusCodeAtRunTimeWithEnum : IClassFixture<WebApiServerFixture> | ||
{ | ||
private const string ApiProductNotFound = "/api/productnotfound"; | ||
private readonly HttpClient _client; | ||
|
||
public StatusCodeAtRunTimeWithEnum(WebApiServerFixture fixture) | ||
{ | ||
// Arrange | ||
var webHost = fixture.CreateWebHostWithMvc(); | ||
webHost.Configure(app => | ||
{ | ||
app.UseGlobalExceptionHandler(x => | ||
{ | ||
x.ContentType = "application/json"; | ||
x.Map<HttpNotFoundException>().ToStatusCode(ex => ex.StatusCodeEnum); | ||
x.ResponseBody(c => JsonConvert.SerializeObject(new TestResponse | ||
{ | ||
Message = c.Message | ||
})); | ||
}); | ||
app.Map(ApiProductNotFound, config => { config.Run(context => throw new HttpNotFoundException("Record not found")); }); | ||
}); | ||
|
||
_client = new TestServer(webHost).CreateClient(); | ||
} | ||
|
||
[Fact] | ||
public async Task Returns_correct_response_type() | ||
{ | ||
var response = await _client.SendAsync(new HttpRequestMessage(new HttpMethod("GET"), ApiProductNotFound)); | ||
response.Content.Headers.ContentType.MediaType.ShouldBe("application/json"); | ||
} | ||
|
||
[Fact] | ||
public async Task Returns_correct_status_code() | ||
{ | ||
var response = await _client.SendAsync(new HttpRequestMessage(new HttpMethod("GET"), ApiProductNotFound)); | ||
response.StatusCode.ShouldBe(HttpStatusCode.NotFound); | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/GlobalExceptionHandler.Tests/WebApi/StatusCodeTests/StatusCodeAtRunTimeWithInt.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,57 @@ | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using GlobalExceptionHandler.Tests.Exceptions; | ||
using GlobalExceptionHandler.Tests.Fixtures; | ||
using GlobalExceptionHandler.WebApi; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.TestHost; | ||
using Newtonsoft.Json; | ||
using Shouldly; | ||
using Xunit; | ||
|
||
namespace GlobalExceptionHandler.Tests.WebApi.StatusCodeTests | ||
{ | ||
public class StatusCodeAtRunTimeWithInt : IClassFixture<WebApiServerFixture> | ||
{ | ||
private const string ApiProductNotFound = "/api/productnotfound"; | ||
private readonly HttpClient _client; | ||
|
||
public StatusCodeAtRunTimeWithInt(WebApiServerFixture fixture) | ||
{ | ||
// Arrange | ||
var webHost = fixture.CreateWebHostWithMvc(); | ||
webHost.Configure(app => | ||
{ | ||
app.UseGlobalExceptionHandler(x => | ||
{ | ||
x.ContentType = "application/json"; | ||
x.Map<HttpNotFoundException>().ToStatusCode(ex => ex.StatusCodeInt); | ||
x.ResponseBody(c => JsonConvert.SerializeObject(new TestResponse | ||
{ | ||
Message = c.Message | ||
})); | ||
}); | ||
app.Map(ApiProductNotFound, config => { config.Run(context => throw new HttpNotFoundException("Record not found")); }); | ||
}); | ||
|
||
_client = new TestServer(webHost).CreateClient(); | ||
} | ||
|
||
[Fact] | ||
public async Task Returns_correct_response_type() | ||
{ | ||
var response = await _client.SendAsync(new HttpRequestMessage(new HttpMethod("GET"), ApiProductNotFound)); | ||
response.Content.Headers.ContentType.MediaType.ShouldBe("application/json"); | ||
} | ||
|
||
[Fact] | ||
public async Task Returns_correct_status_code() | ||
{ | ||
var response = await _client.SendAsync(new HttpRequestMessage(new HttpMethod("GET"), ApiProductNotFound)); | ||
response.StatusCode.ShouldBe(HttpStatusCode.NotFound); | ||
} | ||
} | ||
} |
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