Esempio n. 1
0
        public void Configure_AssignsInvalidModelStateResponseFactory()
        {
            // Arrange
            var optionsSetup = new ApiBehaviorOptionsSetup(
                NullLoggerFactory.Instance,
                Options.Create(new MvcCompatibilityOptions()));
            var options = new ApiBehaviorOptions();

            // Act
            optionsSetup.Configure(options);

            // Assert
            Assert.Same(ApiBehaviorOptionsSetup.DefaultFactory, options.InvalidModelStateResponseFactory);
        }
Esempio n. 2
0
        public void Configure_AddsClientErrorFactories()
        {
            // Arrange
            var expected     = new[] { 400, 401, 403, 404, 406, 409, 415, 422, };
            var optionsSetup = new ApiBehaviorOptionsSetup(
                NullLoggerFactory.Instance,
                Options.Create(new MvcCompatibilityOptions()));
            var options = new ApiBehaviorOptions();

            // Act
            optionsSetup.Configure(options);

            // Assert
            Assert.Equal(expected, options.ClientErrorFactory.Keys);
        }
Esempio n. 3
0
        public void PostConfigure_DoesNotSetProblemDetailsFactoryWithLegacyCompatBehavior()
        {
            // Arrange
            var optionsSetup = new ApiBehaviorOptionsSetup(
                NullLoggerFactory.Instance,
                Options.Create(new MvcCompatibilityOptions {
                CompatibilityVersion = CompatibilityVersion.Version_2_1
            }));
            var options = new ApiBehaviorOptions();

            // Act
            optionsSetup.Configure(options);
            optionsSetup.PostConfigure(string.Empty, options);

            // Assert
            Assert.Same(ApiBehaviorOptionsSetup.DefaultFactory, options.InvalidModelStateResponseFactory);
        }
        public void ProblemDetailsInvalidModelStateResponse_SetsTraceIdFromRequest_IfActivityIsNull()
        {
            // Arrange
            var actionContext = new ActionContext
            {
                HttpContext = new DefaultHttpContext {
                    TraceIdentifier = "42"
                },
            };

            // Act
            var result = ApiBehaviorOptionsSetup.ProblemDetailsInvalidModelStateResponse(actionContext);

            // Assert
            var badRequest     = Assert.IsType <BadRequestObjectResult>(result);
            var problemDetails = Assert.IsType <ValidationProblemDetails>(badRequest.Value);

            Assert.Equal("42", problemDetails.Extensions["traceId"]);
        }
Esempio n. 5
0
        public void PostConfigure_DoesNotSetProblemDetailsFactory_IfValueWasModified()
        {
            // Arrange
            var optionsSetup = new ApiBehaviorOptionsSetup(
                NullLoggerFactory.Instance,
                Options.Create(new MvcCompatibilityOptions {
                CompatibilityVersion = CompatibilityVersion.Latest
            }));
            var options = new ApiBehaviorOptions();
            Func <ActionContext, IActionResult> expected = _ => null;

            // Act
            optionsSetup.Configure(options);
            // This is equivalent to user code updating the value via ConfigureOptions
            options.InvalidModelStateResponseFactory = expected;
            optionsSetup.PostConfigure(string.Empty, options);

            // Assert
            Assert.Same(expected, options.InvalidModelStateResponseFactory);
        }
        public void ProblemDetailsInvalidModelStateResponse_ReturnsBadRequestWithProblemDetails()
        {
            // Arrange
            var actionContext = new ActionContext
            {
                HttpContext = new DefaultHttpContext {
                    TraceIdentifier = "42"
                },
            };

            // Act
            var result = ApiBehaviorOptionsSetup.ProblemDetailsInvalidModelStateResponse(actionContext);

            // Assert
            var badRequest = Assert.IsType <BadRequestObjectResult>(result);

            Assert.Equal(new[] { "application/problem+json", "application/problem+xml" }, badRequest.ContentTypes.OrderBy(c => c));

            var problemDetails = Assert.IsType <ValidationProblemDetails>(badRequest.Value);

            Assert.Equal(400, problemDetails.Status);
        }