Exemplo n.º 1
0
        public async void ReturnsAllFormatterTypesAcceptAllowWhenFormatterNotFound()
        {
            var context = new ApiRequestContext
            {
                RequestAborted = new System.Threading.CancellationToken(false),
                Routing        = new ApiRoutingInfo
                {
                    Route = new ApiRoutingItem
                    {
                        Location = new ApiEndpointLocation(
                            controller: typeof(TestController),
                            methodInfo: typeof(TestController).GetMethod(nameof(TestController.Get)),
                            httpMethod: "GET")
                    }
                },
                Request = new ApiRequestInfo()
            };

            var contextResolver = new ApiRequestContextResolver();

            contextResolver.SetContext(context);

            var mockFormatterFactory = new Mock <IDeepSleepMediaSerializerFactory>();

            mockFormatterFactory
            .Setup(m => m.GetWriteableTypes(It.IsAny <Type>(), It.IsAny <IList <IDeepSleepMediaSerializer> >()))
            .Returns(new string[] { "application/json", "text/xml", "text/plain" });

            var processed = await context.ProcessHttpRequestAccept(contextResolver, mockFormatterFactory.Object).ConfigureAwait(false);

            processed.Should().BeFalse();
            context.Response.Should().NotBeNull();
            context.Response.ResponseObject.Should().BeNull();
            context.Response.StatusCode.Should().Be(406);
            context.Response.Headers.Should().NotBeNull();
            context.Response.Headers.Should().HaveCount(1);
            context.Response.Headers[0].Name.Should().Be("X-Allow-Accept");
            context.Response.Headers[0].Value.Should().Be("application/json, text/xml, text/plain");
        }
Exemplo n.º 2
0
        public async void ReturnsTrueForFoundFormatter()
        {
            var context = new ApiRequestContext
            {
                RequestAborted = new System.Threading.CancellationToken(false),
                Routing        = new ApiRoutingInfo
                {
                    Route = new ApiRoutingItem
                    {
                        Location = new ApiEndpointLocation(
                            controller: typeof(TestController),
                            methodInfo: typeof(TestController).GetMethod(nameof(TestController.Get)),
                            httpMethod: "GET")
                    }
                },
                Request = new ApiRequestInfo()
            };

            var contextResolver = new ApiRequestContextResolver();

            contextResolver.SetContext(context);

            string formatterType;
            var    mockFormatter = new Mock <IDeepSleepMediaSerializer>();

            var mockFormatterFactory = new Mock <IDeepSleepMediaSerializerFactory>();

            mockFormatterFactory
            .Setup(m => m.GetAcceptableFormatter(It.IsAny <AcceptHeader>(), It.IsAny <Type>(), out formatterType, It.IsAny <IList <IDeepSleepMediaSerializer> >(), It.IsAny <IList <string> >()))
            .Returns(Task.FromResult(mockFormatter.Object));

            var processed = await context.ProcessHttpRequestAccept(contextResolver, mockFormatterFactory.Object).ConfigureAwait(false);

            processed.Should().BeTrue();
            context.Response.Should().NotBeNull();
            context.Response.ResponseObject.Should().BeNull();
            context.Response.Headers.Should().NotBeNull();
            context.Response.Headers.Should().HaveCount(0);
        }
Exemplo n.º 3
0
        public async void ReturnsTrueForFoundImplmentedFormatter(string requestAccept)
        {
            var context = new ApiRequestContext
            {
                RequestAborted = new System.Threading.CancellationToken(false),
                Routing        = new ApiRoutingInfo
                {
                    Route = new ApiRoutingItem
                    {
                        Location = new ApiEndpointLocation(
                            controller: typeof(TestController),
                            methodInfo: typeof(TestController).GetMethod(nameof(TestController.Get)),
                            httpMethod: "GET")
                    }
                },
                Request = new ApiRequestInfo
                {
                    Accept = new AcceptHeader(requestAccept)
                }
            };

            var contextResolver = new ApiRequestContextResolver();

            contextResolver.SetContext(context);

            var formatter   = SetupJsonFormatterMock(null, new string[] { "application/json" });
            var mockFactory = SetupFormatterFactory(formatter.Object);


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

            processed.Should().BeTrue();
            context.Response.Should().NotBeNull();
            context.Response.ResponseObject.Should().BeNull();
            context.Response.Headers.Should().NotBeNull();
            context.Response.Headers.Should().HaveCount(0);
        }