コード例 #1
0
        public async void ReturnsTrueAndDoesWritesUsingMatchedFormatter()
        {
            var formatter   = SetupJsonFormatterMock(null, new string[] { "application/json" });
            var mockFactory = SetupFormatterFactory(formatter.Object);

            var context = new ApiRequestContext
            {
                RequestAborted = new System.Threading.CancellationToken(false),
                Response       = new ApiResponseInfo
                {
                    StatusCode     = 201,
                    ResponseObject = "test"
                },
                Request = new ApiRequestInfo
                {
                    Accept = "application/json"
                }
            };

            var processed = await context.ProcessHttpResponseBodyWriting(new ApiRequestContextResolver(), mockFactory.Object).ConfigureAwait(false);

            processed.Should().BeTrue();

            context.Response.ResponseObject.Should().NotBeNull();
            context.Response.StatusCode.Should().Be(201);
            context.Response.Headers.Should().NotBeNull();
            context.Response.ContentType.Should().NotBeNull();
            context.Response.ContentType.Should().Be("application/json");
            context.Response.ContentLength.Should().Be(0);
        }
コード例 #2
0
        public async void ReturnsTrueForCancelledRequest()
        {
            var context = new ApiRequestContext
            {
                RequestAborted = new System.Threading.CancellationToken(true),
                Response       = null
            };

            var processed = await context.ProcessHttpResponseBodyWriting(new ApiRequestContextResolver(), null).ConfigureAwait(false);

            processed.Should().BeTrue();

            context.Response.Should().BeNull();
        }
コード例 #3
0
        public async void ReturnsTrueAndDoesWritesWithMatchedEtagButUnMatchedIfModifiedSince()
        {
            var            formatter   = SetupJsonFormatterMock(null, new string[] { "application/json", "text/json" });
            var            mockFactory = SetupFormatterFactory(formatter.Object);
            var            etag        = "TEST-IF-MATCH";
            DateTimeOffset lastModifed = DateTimeOffset.UtcNow;


            var context = new ApiRequestContext
            {
                RequestAborted = new System.Threading.CancellationToken(false),
                Response       = new ApiResponseInfo
                {
                    StatusCode     = 201,
                    ResponseObject = "test"
                },
                Request = new ApiRequestInfo
                {
                    Method          = "GET",
                    Accept          = "application/json",
                    IfMatch         = etag,
                    IfModifiedSince = lastModifed.AddSeconds(1)
                }
            };

            var contextResolver = new ApiRequestContextResolver();

            contextResolver.SetContext(context);

            context.Response.Headers.Add(new ApiHeader("ETag", etag));
            context.Response.Headers.Add(new ApiHeader("Last-Modified", lastModifed.ToString("r")));

            var processed = await context.ProcessHttpResponseBodyWriting(contextResolver, mockFactory.Object).ConfigureAwait(false);

            processed.Should().BeTrue();

            context.Response.ResponseObject.Should().NotBeNull();
            context.Response.StatusCode.Should().Be(201);
            context.Response.Headers.Should().NotBeNull();
            context.Response.Headers.Should().HaveCount(2);
            context.Response.ContentType.Should().NotBeNull();
            context.Response.ContentType.Should().Be("application/json");
            context.Response.ContentLength.Should().Be(0);
        }
コード例 #4
0
        public async void ReturnsTrueAndDoesNotWriteForNullFormatter()
        {
            var mockFactory = SetupFormatterFactory(new IDeepSleepMediaSerializer[] { });

            var context = new ApiRequestContext
            {
                RequestAborted = new System.Threading.CancellationToken(false),
                Response       = new ApiResponseInfo
                {
                    ResponseObject = "test"
                }
            };

            var processed = await context.ProcessHttpResponseBodyWriting(new ApiRequestContextResolver(), mockFactory.Object).ConfigureAwait(false);

            processed.Should().BeTrue();

            context.Response.ResponseObject.Should().NotBeNull();
            context.Response.StatusCode.Should().Be(204);
            context.Response.ContentLength.Should().Be(0);
        }
コード例 #5
0
        public async void ReturnsTrueAndDoesNotWriteForNullFormatterFactory()
        {
            var mockRequestInfo = new Mock <ApiRequestInfo>();

            mockRequestInfo.Setup(m => m.Accept).Throws(new Exception("test"));

            var context = new ApiRequestContext
            {
                RequestAborted = new System.Threading.CancellationToken(false),
                Response       = new ApiResponseInfo
                {
                    ResponseObject = "test"
                },
                Request = mockRequestInfo.Object
            };

            var processed = await context.ProcessHttpResponseBodyWriting(new ApiRequestContextResolver(), null).ConfigureAwait(false);

            processed.Should().BeTrue();

            context.Response.ResponseObject.Should().NotBeNull();
            context.Response.StatusCode.Should().Be(204);
            context.Response.ContentLength.Should().Be(0);
        }