Esempio n. 1
0
    public async Task ExecuteAsync_UsesDefaults_ForValidationProblemDetails()
    {
        // Arrange
        var details = new HttpValidationProblemDetails();

        var result      = new JsonHttpResult <HttpValidationProblemDetails>(details, jsonSerializerOptions: null);
        var stream      = new MemoryStream();
        var httpContext = new DefaultHttpContext()
        {
            RequestServices = CreateServices(),
            Response        =
            {
                Body = stream,
            },
        };

        // Act
        await result.ExecuteAsync(httpContext);

        // Assert
        Assert.Equal(StatusCodes.Status400BadRequest, httpContext.Response.StatusCode);
        stream.Position = 0;
        var responseDetails = JsonSerializer.Deserialize <HttpValidationProblemDetails>(stream);

        Assert.Equal("https://tools.ietf.org/html/rfc7231#section-6.5.1", responseDetails.Type);
        Assert.Equal("One or more validation errors occurred.", responseDetails.Title);
        Assert.Equal(StatusCodes.Status400BadRequest, responseDetails.Status);
    }
Esempio n. 2
0
    public async Task ExecuteAsync_UsesDefaults_ForProblemDetails()
    {
        // Arrange
        var details = new ProblemDetails();

        var result      = new JsonHttpResult <ProblemDetails>(details, jsonSerializerOptions: null);
        var stream      = new MemoryStream();
        var httpContext = new DefaultHttpContext()
        {
            RequestServices = CreateServices(),
            Response        =
            {
                Body = stream,
            },
        };

        // Act
        await result.ExecuteAsync(httpContext);

        // Assert
        Assert.Equal(StatusCodes.Status500InternalServerError, httpContext.Response.StatusCode);
        stream.Position = 0;
        var responseDetails = JsonSerializer.Deserialize <ProblemDetails>(stream);

        Assert.Equal("https://tools.ietf.org/html/rfc7231#section-6.6.1", responseDetails.Type);
        Assert.Equal("An error occurred while processing your request.", responseDetails.Title);
        Assert.Equal(StatusCodes.Status500InternalServerError, responseDetails.Status);
    }
Esempio n. 3
0
    public void ExecuteAsync_ThrowsArgumentNullException_WhenHttpContextIsNull()
    {
        // Arrange
        var         result      = new JsonHttpResult <object>(null, null);
        HttpContext httpContext = null;

        // Act & Assert
        Assert.ThrowsAsync <ArgumentNullException>("httpContext", () => result.ExecuteAsync(httpContext));
    }
Esempio n. 4
0
    public async Task JsonResult_ExecuteAsync_SetsStatusCode()
    {
        // Arrange
        var result = new JsonHttpResult <object>(value: null, statusCode: 407, jsonSerializerOptions: null);

        var httpContext = new DefaultHttpContext()
        {
            RequestServices = CreateServices(),
        };

        // Act
        await result.ExecuteAsync(httpContext);

        // Assert
        Assert.Equal(407, httpContext.Response.StatusCode);
    }
Esempio n. 5
0
    public async Task ExecuteAsync_SetsProblemDetailsStatus_ForValidationProblemDetails()
    {
        // Arrange
        var details = new HttpValidationProblemDetails();

        var result      = new JsonHttpResult <HttpValidationProblemDetails>(details, StatusCodes.Status422UnprocessableEntity, jsonSerializerOptions: null);
        var httpContext = new DefaultHttpContext()
        {
            RequestServices = CreateServices(),
        };

        // Act
        await result.ExecuteAsync(httpContext);

        // Assert
        Assert.Equal(StatusCodes.Status422UnprocessableEntity, details.Status.Value);
    }
Esempio n. 6
0
    public async Task JsonResult_ExecuteAsync_JsonSerializesBody()
    {
        // Arrange
        var result      = new JsonHttpResult <string>(value: "Hello", statusCode: 407, jsonSerializerOptions: null);
        var stream      = new MemoryStream();
        var httpContext = new DefaultHttpContext()
        {
            RequestServices = CreateServices(),
            Response        =
            {
                Body = stream,
            },
        };

        // Act
        await result.ExecuteAsync(httpContext);

        // Assert
        Assert.Equal("\"Hello\"", Encoding.UTF8.GetString(stream.ToArray()));
    }
Esempio n. 7
0
    public async Task JsonResult_ExecuteAsync_JsonSerializesBody_WithOptions()
    {
        // Arrange
        var jsonOptions = new JsonSerializerOptions()
        {
            WriteIndented          = true,
            DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull
        };
        var value = new Todo(10, "MyName")
        {
            Description = null
        };
        var result      = new JsonHttpResult <object>(value, jsonSerializerOptions: jsonOptions);
        var stream      = new MemoryStream();
        var httpContext = new DefaultHttpContext()
        {
            RequestServices = CreateServices(),
            Response        =
            {
                Body = stream,
            },
        };

        // Act
        await result.ExecuteAsync(httpContext);

        // Assert
        Assert.Equal(StatusCodes.Status200OK, httpContext.Response.StatusCode);

        stream.Position = 0;
        var responseDetails = JsonSerializer.Deserialize <Todo>(stream);

        Assert.Equal(value.Id, responseDetails.Id);
        Assert.Equal(value.Title, responseDetails.Title);
        Assert.Equal(value.Description, responseDetails.Description);

        stream.Position = 0;
        Assert.Equal(JsonSerializer.Serialize(value, options: jsonOptions), Encoding.UTF8.GetString(stream.ToArray()));
    }
Esempio n. 8
0
    public async Task ExecuteAsync_GetsStatusCodeFromProblemDetails()
    {
        // Arrange
        var details = new ProblemDetails {
            Status = StatusCodes.Status413RequestEntityTooLarge,
        };

        var result = new JsonHttpResult <ProblemDetails>(details, jsonSerializerOptions: null);

        var httpContext = new DefaultHttpContext()
        {
            RequestServices = CreateServices(),
        };

        // Act
        await result.ExecuteAsync(httpContext);

        // Assert
        Assert.Equal(StatusCodes.Status413RequestEntityTooLarge, details.Status.Value);
        Assert.Equal(StatusCodes.Status413RequestEntityTooLarge, result.StatusCode);
        Assert.Equal(StatusCodes.Status413RequestEntityTooLarge, httpContext.Response.StatusCode);
    }