public void Configuration(IAppBuilder app) { var customFactory = new CustomProblemDetailsFactory(); app.UseProblemDetails(new IntelligentPlant.ProblemDetails.Owin.ProblemDetailsMiddlewareOptions() { IncludePaths = new[] { new PathString("/") }, Factory = customFactory, ExceptionHandler = (context, error, factory) => { if (error is HttpResponseException httpError) { return(factory.CreateProblemDetails(context, (int)httpError.Response.StatusCode, detail: httpError.Message)); } if (error is System.Security.SecurityException) { return(factory.CreateProblemDetails(context, 403)); } if (error is ArgumentException) { return(factory.CreateProblemDetails(context, 400, detail: error.Message)); } if (error is OperationCanceledException) { return(factory.CreateProblemDetails(context, 0, detail: error.Message)); } return(factory.CreateServerErrorProblemDetails(context, error, detail: error.Message)); } });
public void When_CreatingValidationProblemDetailsWithActiveSpan_Expect_ValidationProblemDetailsWithTraceId() { // Arrange ServiceCollection services = new ServiceCollection(); services.AddScoped <ITracer, MockTracer>(); IServiceProvider serviceProvider = services.BuildServiceProvider(); ITracer tracer = serviceProvider.GetRequiredService <ITracer>(); IOptions <ApiBehaviorOptions> options = Options.Create(new ApiBehaviorOptions()); ProblemDetailsFactory factory = new CustomProblemDetailsFactory(options); ValidationProblemDetails result; // Act using (tracer.BuildSpan("Unit Test").StartActive()) { result = factory.CreateValidationProblemDetails( new DefaultHttpContext { RequestServices = serviceProvider }, new ModelStateDictionary()); } // Assert MockTracer mockTracer = Assert.IsType <MockTracer>(tracer); MockSpan mockSpan = Assert.Single(mockTracer.FinishedSpans()); Assert.Equal(mockSpan.Context.TraceId, result.Extensions["traceId"]); }
public void When_CreatingValidationProblemDetailsWithClientErrorMapping_Expect_ValidationProblemDetailsWithClientErrorNotMapped() { // Arrange ApiBehaviorOptions apiBehaviorOptions = new ApiBehaviorOptions(); int status = new Random().Next(int.MaxValue); apiBehaviorOptions.ClientErrorMapping[status] = new ClientErrorData { Title = Guid.NewGuid().ToString(), Link = Guid.NewGuid().ToString(), }; IOptions <ApiBehaviorOptions> options = Options.Create(apiBehaviorOptions); ProblemDetailsFactory factory = new CustomProblemDetailsFactory(options); string title = Guid.NewGuid().ToString(); string type = Guid.NewGuid().ToString(); // Act ValidationProblemDetails result = factory.CreateValidationProblemDetails(new DefaultHttpContext(), new ModelStateDictionary(), status, title, type); // Assert Assert.NotEqual(apiBehaviorOptions.ClientErrorMapping[status].Title, title); Assert.NotEqual(apiBehaviorOptions.ClientErrorMapping[status].Link, type); Assert.Equal(status, result.Status); Assert.Equal(title, result.Title); Assert.Equal(type, result.Type); }
public void When_CreatingProblemDetailsWithNullHttpContext_Expect_ArgumentNullException() { // Arrange IOptions <ApiBehaviorOptions> options = Options.Create(new ApiBehaviorOptions()); ProblemDetailsFactory factory = new CustomProblemDetailsFactory(options); // Act Exception result = Record.Exception(() => factory.CreateProblemDetails(null !)); // Assert Assert.NotNull(result); ArgumentNullException argumentNullException = Assert.IsType <ArgumentNullException>(result); Assert.Equal("httpContext", argumentNullException.ParamName); }
public void When_CreatingProblemDetails_Expect_ProblemDetails() { // Arrange IOptions <ApiBehaviorOptions> options = Options.Create(new ApiBehaviorOptions()); ProblemDetailsFactory factory = new CustomProblemDetailsFactory(options); // Act ProblemDetails result = factory.CreateProblemDetails(new DefaultHttpContext()); // Assert Assert.Equal(500, result.Status); Assert.Null(result.Title); Assert.Null(result.Type); Assert.Null(result.Detail); Assert.Null(result.Instance); Assert.Empty(result.Extensions); }
public void When_CreatingValidationProblemDetails_Expect_ValidationProblemDetails() { // Arrange IOptions <ApiBehaviorOptions> options = Options.Create(new ApiBehaviorOptions()); ProblemDetailsFactory factory = new CustomProblemDetailsFactory(options); // Act ValidationProblemDetails result = factory.CreateValidationProblemDetails(new DefaultHttpContext(), new ModelStateDictionary()); // Assert Assert.Equal(400, result.Status); Assert.Equal("One or more validation errors occurred.", result.Title); Assert.Null(result.Type); Assert.Null(result.Detail); Assert.Null(result.Instance); Assert.Empty(result.Extensions); }
public static IServiceCollection AddExceptionInterceptor(this IServiceCollection services) { services.AddTransient <ProblemDetailsFactory, CustomProblemDetailsFactory>(provider => { var options = provider.GetRequiredService <IOptions <ApiBehaviorOptions> >(); var problemDetailsFactory = new CustomProblemDetailsFactory(options); problemDetailsFactory.Map <BusinessRuleValidationException>((ex, context) => new ProblemDetails() { Title = ex.Message, Detail = ex.Details, Status = StatusCodes.Status409Conflict, }); return(problemDetailsFactory); }); return(services); }
public void When_CreatingProblemDetailsWithValues_Expect_ProblemDetailsWithValues() { // Arrange IOptions <ApiBehaviorOptions> options = Options.Create(new ApiBehaviorOptions()); ProblemDetailsFactory factory = new CustomProblemDetailsFactory(options); int status = new Random().Next(int.MaxValue); string title = Guid.NewGuid().ToString(); string type = Guid.NewGuid().ToString(); string detail = Guid.NewGuid().ToString(); string instance = Guid.NewGuid().ToString(); // Act ProblemDetails result = factory.CreateProblemDetails(new DefaultHttpContext(), status, title, type, detail, instance); // Assert Assert.Equal(status, result.Status); Assert.Equal(title, result.Title); Assert.Equal(type, result.Type); Assert.Equal(detail, result.Detail); Assert.Equal(instance, result.Instance); Assert.Empty(result.Extensions); }