示例#1
0
    public void ProblemDetailsInvalidModelStateResponse_SetsTraceIdFromRequest_IfActivityIsNull()
    {
        // Arrange
        var actionContext = GetActionContext();
        var factory       = GetProblemDetailsFactory();

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

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

        Assert.Equal("42", problemDetails.Extensions["traceId"]);
    }
示例#2
0
    public void ProblemDetailsInvalidModelStateResponse_SetsTraceId()
    {
        // Arrange
        using (new ActivityReplacer())
        {
            var actionContext = GetActionContext();
            var factory       = GetProblemDetailsFactory();

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

            // Assert
            var badRequest     = Assert.IsType <BadRequestObjectResult>(result);
            var problemDetails = Assert.IsType <ValidationProblemDetails>(badRequest.Value);
            Assert.Equal(Activity.Current.Id, problemDetails.Extensions["traceId"]);
        }
    }
示例#3
0
    public void ProblemDetailsInvalidModelStateResponse_ReturnsBadRequestWithProblemDetails()
    {
        // Arrange
        var actionContext = GetActionContext();
        var factory       = GetProblemDetailsFactory();

        // Act
        var result = ApiBehaviorOptionsSetup.ProblemDetailsInvalidModelStateResponse(factory, 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);
        Assert.Equal("One or more validation errors occurred.", problemDetails.Title);
        Assert.Equal("https://tools.ietf.org/html/rfc7231#section-6.5.1", problemDetails.Type);
    }
示例#4
0
    public void ProblemDetailsInvalidModelStateResponse_UsesUserConfiguredLink()
    {
        // Arrange
        var link          = "http://mylink";
        var actionContext = GetActionContext();

        var factory = GetProblemDetailsFactory(options => options.ClientErrorMapping[400].Link = link);

        // Act
        var result = ApiBehaviorOptionsSetup.ProblemDetailsInvalidModelStateResponse(factory, 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);
        Assert.Equal("One or more validation errors occurred.", problemDetails.Title);
        Assert.Equal(link, problemDetails.Type);
    }
示例#5
0
    public void ProblemDetailsInvalidModelStateResponse_UsesProblemDetailsFactory()
    {
        // Arrange
        var actionContext = GetActionContext();
        var factory       = Mock.Of <ProblemDetailsFactory>(m => m.CreateValidationProblemDetails(It.IsAny <HttpContext>(), It.IsAny <ModelStateDictionary>(), null, null, null, null, null) == new ValidationProblemDetails
        {
            Status = 422,
        });

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

        // Assert
        var objectResult = Assert.IsType <ObjectResult>(result);

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

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

        Assert.Equal(422, problemDetails.Status);
        Assert.Equal("One or more validation errors occurred.", problemDetails.Title);
    }