-
Notifications
You must be signed in to change notification settings - Fork 8
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
Showing
12 changed files
with
203 additions
and
3 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
23 changes: 23 additions & 0 deletions
23
...les/Opw.HttpExceptions.AspNetCore.Sample/CustomErrors/ProblemDetailsAttributeException.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,23 @@ | ||
using System; | ||
using System.Net; | ||
using System.Runtime.Serialization; | ||
|
||
namespace Opw.HttpExceptions.AspNetCore.Sample.CustomErrors | ||
{ | ||
public class ProblemDetailsAttributeException : HttpException | ||
{ | ||
public ProblemDetailsAttributeException(string message) : base(HttpStatusCode.Ambiguous, message) { } | ||
|
||
public ProblemDetailsAttributeException(string message, Exception innerException) : base(message, innerException) { } | ||
|
||
public ProblemDetailsAttributeException(SerializationInfo info, StreamingContext context) : base(info, context) { } | ||
|
||
[ProblemDetails] | ||
public string PropertyA { get; set; } | ||
|
||
[ProblemDetails] | ||
public int PropertyB { get; set; } | ||
|
||
public long PropertyC { get; set; } | ||
} | ||
} |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
using System.Runtime.CompilerServices; | ||
[assembly: InternalsVisibleTo("Opw.HttpExceptions.AspNetCore.Tests")] | ||
[assembly: InternalsVisibleTo("Opw.HttpExceptions.AspNetCore.Sample.Tests")] |
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,12 @@ | ||
using System; | ||
|
||
namespace Opw.HttpExceptions | ||
{ | ||
/// <summary> | ||
/// Include the property in the problem details in exception details. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)] | ||
public class ProblemDetailsAttribute : Attribute | ||
{ | ||
} | ||
} |
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
23 changes: 23 additions & 0 deletions
23
tests/Opw.HttpExceptions.AspNetCore.Tests/TestData/ProblemDetailsAttributeException.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,23 @@ | ||
using System; | ||
using System.Net; | ||
using System.Runtime.Serialization; | ||
|
||
namespace Opw.HttpExceptions.AspNetCore.Tests.TestData | ||
{ | ||
public class ProblemDetailsAttributeException : HttpException | ||
{ | ||
public ProblemDetailsAttributeException(string message) : base(HttpStatusCode.Ambiguous, message) { } | ||
|
||
public ProblemDetailsAttributeException(string message, Exception innerException) : base(message, innerException) { } | ||
|
||
public ProblemDetailsAttributeException(SerializationInfo info, StreamingContext context) : base(info, context) { } | ||
|
||
[ProblemDetails] | ||
public string PropertyA { get; set; } | ||
|
||
[ProblemDetails] | ||
public int PropertyB { get; set; } | ||
|
||
public long PropertyC { get; set; } | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
tests/Opw.HttpExceptions.Tests/ProblemDetailAttributeTests.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 FluentAssertions; | ||
using Xunit; | ||
|
||
namespace Opw.HttpExceptions | ||
{ | ||
public class ProblemDetailAttributeTests | ||
{ | ||
[Fact] | ||
public void Property_Should_HaveAttribute() | ||
{ | ||
new Person().GetType().GetProperty(nameof(Person.Id)) | ||
.GetCustomAttributes(false).Should() | ||
.Contain(new ProblemDetailsAttribute()); | ||
} | ||
|
||
[Fact] | ||
public void Customer_BaseProperty_Should_HaveAttribute() | ||
{ | ||
new Customer().GetType().GetProperty(nameof(Person.Name)) | ||
.GetCustomAttributes(false).Should() | ||
.Contain(new ProblemDetailsAttribute()); | ||
} | ||
|
||
[Fact] | ||
public void Customer_OverriddenBaseProperty_Should_HaveAttribute() | ||
{ | ||
new Customer().GetType().GetProperty(nameof(Customer.Id)) | ||
.GetCustomAttributes(false).Should() | ||
.Contain(new ProblemDetailsAttribute()); | ||
} | ||
|
||
[Fact] | ||
public void Customer_Property_Should_NotHaveAttribute() | ||
{ | ||
new Customer().GetType().GetProperty(nameof(Customer.Code)) | ||
.GetCustomAttributes(false).Should() | ||
.NotContain(new ProblemDetailsAttribute()); | ||
} | ||
|
||
private class Person | ||
{ | ||
[ProblemDetails] | ||
public virtual int Id { get; set; } | ||
|
||
[ProblemDetails] | ||
public string Name { get; set; } | ||
} | ||
|
||
private class Customer : Person | ||
{ | ||
[ProblemDetails] | ||
public override int Id { get; set; } | ||
|
||
public string Code { get; set; } | ||
} | ||
} | ||
} |