示例#1
0
    public void ExecuteAsync_ThrowsArgumentNullException_WhenHttpContextIsNull()
    {
        // Arrange
        var         result      = new CreatedAtRoute(null);
        HttpContext httpContext = null;

        // Act & Assert
        Assert.ThrowsAsync <ArgumentNullException>("httpContext", () => result.ExecuteAsync(httpContext));
    }
示例#2
0
    public void CreatedAtRouteResult_Implements_IValueHttpResult_Correctly()
    {
        // Arrange & Act
        var rawResult = new CreatedAtRoute(
            routeName: null,
            routeValues: new Dictionary <string, object>());

        // Assert
        var result = Assert.IsAssignableFrom <IStatusCodeHttpResult>(rawResult);

        Assert.Equal(StatusCodes.Status201Created, result.StatusCode);
    }
示例#3
0
    public async Task CreatedAtRouteResult_ThrowsOnNullUrl()
    {
        // Arrange
        var httpContext = GetHttpContext(expectedUrl: null);

        var result = new CreatedAtRoute(
            routeName: null,
            routeValues: new Dictionary <string, object>());

        // Act & Assert
        await ExceptionAssert.ThrowsAsync <InvalidOperationException>(
            async() => await result.ExecuteAsync(httpContext),
            "No route matches the supplied values.");
    }
示例#4
0
    public async Task CreatedAtRouteResult_ReturnsStatusCode_SetsLocationHeader(object values)
    {
        // Arrange
        var expectedUrl = "testAction";
        var httpContext = GetHttpContext(expectedUrl);

        // Act
        var result = new CreatedAtRoute(routeName: null, routeValues: values);
        await result.ExecuteAsync(httpContext);

        // Assert
        Assert.Equal(StatusCodes.Status201Created, httpContext.Response.StatusCode);
        Assert.Equal(expectedUrl, httpContext.Response.Headers["Location"]);
    }
示例#5
0
    public void CreatedAtRouteResult_Implements_IValueHttpResultOfT_Correctly()
    {
        // Arrange & Act
        var value     = "Foo";
        var rawResult = new CreatedAtRoute <string>(
            routeName: null,
            routeValues: new Dictionary <string, object>(),
            value: value);

        // Assert
        var result = Assert.IsAssignableFrom <IValueHttpResult <string> >(rawResult);

        Assert.IsType <string>(result.Value);
        Assert.Equal(value, result.Value);
    }
示例#6
0
    public void CreatedAtRouteResult_ProblemDetails_SetsStatusCodeAndValue()
    {
        // Arrange & Act
        var routeValues = new RouteValueDictionary(new Dictionary <string, string>()
        {
            { "test", "case" },
            { "sample", "route" }
        });
        var obj    = new HttpValidationProblemDetails();
        var result = new CreatedAtRoute <HttpValidationProblemDetails>(routeValues, obj);

        // Assert
        Assert.Equal(StatusCodes.Status201Created, result.StatusCode);
        Assert.Equal(StatusCodes.Status201Created, obj.Status);
        Assert.Equal(obj, result.Value);
    }
示例#7
0
        public IActionResult UpdateBookForAuthor(Guid authorId, Guid id,
                                                 [FromBody] BookForUpdateDto book)
        {
            if (book == null)
            {
                return(BadRequest());
            }

            if (book.Description == book.Title)
            {
                ModelState.AddModelError(nameof(BookForUpdateDto),
                                         "The provided description should be different from the title.");
            }

            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            if (!_libraryRepository.AuthorExists(authorId))
            {
                return(NotFound());
            }

            var bookForAuthorFromRepo = _libraryRepository.GetBookForAuthor(authorId, id);

            if (bookForAuthorFromRepo == null)
            {
                //return NotFound();

                var bookToAdd = _mapper.Map <Book>(book);
                bookToAdd.Id = id;

                _libraryRepository.AddBookForAuthor(authorId, bookToAdd);

                if (!_libraryRepository.Save())
                {
                    throw new Exception($"Upserting book {id} for author {authorId} failed on save.");
                }

                var bookToReturn = _mapper.Map <BookDto>(bookToAdd);

                return(CreatedAtRoute("GetBookForAuthor",
                                      (authorId: authorId, id: bookToReturn.Id),
                                      bookToReturn));
            }

            // map

            // apply update

            // map back to entity
            _mapper.Map(book, bookForAuthorFromRepo);

            _libraryRepository.UpdateBookForAuthor(bookForAuthorFromRepo);

            if (!_libraryRepository.Save())
            {
                throw new Exception($"Updating book {id} for author {authorId} failed on save.");
            }

            return(NoContent());
        }