Exemple #1
0
    /// <summary>
    /// Produces a <see cref="StatusCodes.Status400BadRequest"/> response
    /// with a <see cref="HttpValidationProblemDetails"/> value.
    /// </summary>
    /// <param name="errors">One or more validation errors.</param>
    /// <param name="detail">The value for <see cref="ProblemDetails.Detail" />.</param>
    /// <param name="instance">The value for <see cref="ProblemDetails.Instance" />.</param>
    /// <param name="statusCode">The status code.</param>
    /// <param name="title">The value for <see cref="ProblemDetails.Title" />. Defaults to "One or more validation errors occurred."</param>
    /// <param name="type">The value for <see cref="ProblemDetails.Type" />.</param>
    /// <param name="extensions">The value for <see cref="ProblemDetails.Extensions" />.</param>
    /// <returns>The created <see cref="IResult"/> for the response.</returns>
    public static IResult ValidationProblem(
        IDictionary <string, string[]> errors,
        string?detail   = null,
        string?instance = null,
        int?statusCode  = null,
        string?title    = null,
        string?type     = null,
        IDictionary <string, object?>?extensions = null)
    {
        // TypedResults.ValidationProblem() does not allow setting the statusCode so we do this manually here
        var problemDetails = new HttpValidationProblemDetails(errors)
        {
            Detail   = detail,
            Instance = instance,
            Type     = type,
            Status   = statusCode,
        };

        problemDetails.Title = title ?? problemDetails.Title;

        if (extensions is not null)
        {
            foreach (var extension in extensions)
            {
                problemDetails.Extensions.Add(extension);
            }
        }

        return(TypedResults.Problem(problemDetails));
    }
Exemple #2
0
 /// <summary>
 /// Produces a <see cref="ProblemDetails"/> response.
 /// </summary>
 /// <param name="statusCode">The value for <see cref="ProblemDetails.Status" />.</param>
 /// <param name="detail">The value for <see cref="ProblemDetails.Detail" />.</param>
 /// <param name="instance">The value for <see cref="ProblemDetails.Instance" />.</param>
 /// <param name="title">The value for <see cref="ProblemDetails.Title" />.</param>
 /// <param name="type">The value for <see cref="ProblemDetails.Type" />.</param>
 /// <param name="extensions">The value for <see cref="ProblemDetails.Extensions" />.</param>
 /// <returns>The created <see cref="IResult"/> for the response.</returns>
 public static IResult Problem(
     string?detail   = null,
     string?instance = null,
     int?statusCode  = null,
     string?title    = null,
     string?type     = null,
     IDictionary <string, object?>?extensions = null)
 => TypedResults.Problem(detail, instance, statusCode, title, type, extensions);
    public void Problem_WithNoArgs_ResultHasCorrectValues()
    {
        /// Act
        var result = TypedResults.Problem();

        // Assert
        Assert.Null(result.ProblemDetails.Detail);
        Assert.Null(result.ProblemDetails.Instance);
        Assert.Equal("application/problem+json", result.ContentType);
        Assert.Equal(StatusCodes.Status500InternalServerError, result.StatusCode);
        Assert.Equal("An error occurred while processing your request.", result.ProblemDetails.Title);
        Assert.Equal("https://tools.ietf.org/html/rfc7231#section-6.6.1", result.ProblemDetails.Type);
        Assert.Empty(result.ProblemDetails.Extensions);
    }
    public void Problem_WithValidationProblemArg_ResultHasCorrectValues()
    {
        // Arrange
        var problem = new HttpValidationProblemDetails {
            Title = "Test title"
        };

        // Act
        var result = TypedResults.Problem(problem);

        // Assert
        Assert.Equal(problem, result.ProblemDetails);
        Assert.Equal("Test title", result.ProblemDetails.Title);
        Assert.Equal("application/problem+json", result.ContentType);
        Assert.Equal(StatusCodes.Status400BadRequest, result.StatusCode);
    }
    public void Problem_WithProblemArg_ResultHasCorrectValues()
    {
        // Arrange
        var problem = new ProblemDetails {
            Title = "Test title"
        };

        // Act
        var result = TypedResults.Problem(problem);

        // Assert
        Assert.Equal(problem, result.ProblemDetails);
        Assert.Equal("Test title", result.ProblemDetails.Title);
        Assert.Equal("application/problem+json", result.ContentType);
        Assert.Equal(StatusCodes.Status500InternalServerError, result.StatusCode);
    }
    public void Problem_WithArgs_ResultHasCorrectValues()
    {
        // Arrange
        var detail     = "test detail";
        var instance   = "test instance";
        var statusCode = StatusCodes.Status409Conflict;
        var title      = "test title";
        var type       = "test type";
        var extensions = new Dictionary <string, object> {
            { "test", "value" }
        };

        // Act
        var result = TypedResults.Problem(detail, instance, statusCode, title, type, extensions);

        // Assert
        Assert.Equal(detail, result.ProblemDetails.Detail);
        Assert.Equal(instance, result.ProblemDetails.Instance);
        Assert.Equal("application/problem+json", result.ContentType);
        Assert.Equal(statusCode, result.StatusCode);
        Assert.Equal(title, result.ProblemDetails.Title);
        Assert.Equal(type, result.ProblemDetails.Type);
        Assert.Equal(extensions, result.ProblemDetails.Extensions);
    }
Exemple #7
0
 /// <summary>
 /// Produces a <see cref="ProblemDetails"/> response.
 /// </summary>
 /// <param name="problemDetails">The <see cref="ProblemDetails"/>  object to produce a response from.</param>
 /// <returns>The created <see cref="IResult"/> for the response.</returns>
 public static IResult Problem(ProblemDetails problemDetails)
 => TypedResults.Problem(problemDetails);
 public void Problem_WithNullProblem_ThrowsArgNullException()
 {
     Assert.Throws <ArgumentNullException>("problemDetails", () => TypedResults.Problem(default(ProblemDetails)));
 }