Пример #1
0
        public async Task GetReturnsNotFoundWithBadId()
        {
            PopulateDatabaseWithItems();

            var controller = new MedicationsController(CreateDatabase());

            var result = await controller.GetMedication(-1);

            result.Should().BeOfType <NotFoundResult>();
        }
Пример #2
0
        private async Task GetReturnsItem(bool?etag = null, bool?lastModified = null, [CallerMemberName] string name = null)
        {
            var items    = PopulateDatabaseWithItems(name);
            var testItem = items.First();

            var controller = new MedicationsController(CreateDatabase(name))
            {
                ControllerContext = new ControllerContext
                {
                    HttpContext = new DefaultHttpContext()
                }
            };

            if (etag == true)
            {
                controller.Request.Headers["If-None-Match"] = testItem.GetEtag();
            }
            else if (etag == false)
            {
                controller.Request.Headers["If-None-Match"] = _fixture.Create <string>();
            }

            if (lastModified == true)
            {
                controller.Request.Headers["If-Modified-Since"] = testItem.LastModified.ToString(ModifiedDateFormat);
            }
            else if (lastModified == false)
            {
                controller.Request.Headers["If-Modified-Since"] = testItem.LastModified.AddDays(-1).ToString(ModifiedDateFormat);
            }

            var result = await controller.GetMedication(testItem.Id);

            if (etag == true || (lastModified == true && etag == null))
            {
                result.Should().BeOfType <StatusCodeResult>()
                .Which.StatusCode.Should().Be((int)HttpStatusCode.NotModified);
            }
            else
            {
                result.Should().BeOkResultWithValue(testItem);

                controller.Response.Headers.Should().Contain(KeyValuePair.Create("ETag", new StringValues(testItem.GetEtag())));
                controller.Response.Headers.Should().Contain(KeyValuePair.Create("Last-Modified", new StringValues(testItem.LastModified.ToUniversalTime().ToString(ModifiedDateFormat))));
            }
        }