private IServiceProvider CreateServices()
        {
            var services = new ServiceCollection();

            services.Add(new ServiceDescriptor(
                             typeof(ILogger <ObjectResult>),
                             new Logger <ObjectResult>(NullLoggerFactory.Instance)));

            var optionsAccessor = new TestOptionsManager <MvcOptions>();

            optionsAccessor.Value.OutputFormatters.Add(new JsonOutputFormatter());
            services.Add(new ServiceDescriptor(typeof(IOptions <MvcOptions>), optionsAccessor));

            var bindingContext = new ActionBindingContext
            {
                OutputFormatters = optionsAccessor.Value.OutputFormatters,
            };
            var bindingContextAccessor = new ActionBindingContextAccessor
            {
                ActionBindingContext = bindingContext,
            };

            services.Add(new ServiceDescriptor(typeof(IActionBindingContextAccessor), bindingContextAccessor));

            return(services.BuildServiceProvider());
        }
Exemple #2
0
        private static ActionContext CreateMockActionContext(
            HttpResponse response             = null,
            string requestAcceptHeader        = "application/*",
            string requestContentType         = "application/json",
            string requestAcceptCharsetHeader = "",
            bool respectBrowserAcceptHeader   = false)
        {
            var httpContext = new Mock <HttpContext>();

            if (response != null)
            {
                httpContext.Setup(o => o.Response).Returns(response);
            }

            var content      = "{name: 'Person Name', Age: 'not-an-age'}";
            var contentBytes = Encoding.UTF8.GetBytes(content);

            var request = new DefaultHttpContext().Request;

            request.Headers["Accept-Charset"] = requestAcceptCharsetHeader;
            request.Headers["Accept"]         = requestAcceptHeader;
            request.ContentType = requestContentType;
            request.Body        = new MemoryStream(contentBytes);

            httpContext.Setup(o => o.Request).Returns(request);
            httpContext.Setup(o => o.RequestServices).Returns(GetServiceProvider());
            var optionsAccessor = new MockMvcOptionsAccessor();

            optionsAccessor.Options.OutputFormatters.Add(new StringOutputFormatter());
            optionsAccessor.Options.OutputFormatters.Add(new JsonOutputFormatter());
            optionsAccessor.Options.RespectBrowserAcceptHeader = respectBrowserAcceptHeader;
            var actionBindingContextAccessor = new ActionBindingContextAccessor()
            {
                ActionBindingContext = new ActionBindingContext()
                {
                    OutputFormatters = optionsAccessor.Options.OutputFormatters
                }
            };

            httpContext.Setup(o => o.RequestServices.GetService(typeof(IActionBindingContextAccessor)))
            .Returns(actionBindingContextAccessor);
            httpContext.Setup(o => o.RequestServices.GetService(typeof(IOptions <MvcOptions>)))
            .Returns(optionsAccessor);
            httpContext.Setup(o => o.RequestServices.GetService(typeof(ILogger <ObjectResult>)))
            .Returns(new Mock <ILogger <ObjectResult> >().Object);

            return(new ActionContext(httpContext.Object, new RouteData(), new ActionDescriptor()));
        }
        private IServiceProvider CreateServices()
        {
            var services = new ServiceCollection();
            services.Add(new ServiceDescriptor(
                typeof(ILogger<ObjectResult>),
                new Logger<ObjectResult>(NullLoggerFactory.Instance)));

            var optionsAccessor = new MockMvcOptionsAccessor();
            optionsAccessor.Options.OutputFormatters.Add(new JsonOutputFormatter());
            services.Add(new ServiceDescriptor(typeof(IOptions<MvcOptions>), optionsAccessor));

            var bindingContext = new ActionBindingContext
            {
                OutputFormatters = optionsAccessor.Options.OutputFormatters,
            };
            var bindingContextAccessor = new ActionBindingContextAccessor
            {
                ActionBindingContext = bindingContext,
            };
            services.Add(new ServiceDescriptor(typeof(IActionBindingContextAccessor), bindingContextAccessor));

            return services.BuildServiceProvider();
        }
        private static ActionContext CreateMockActionContext(
                                                             HttpResponse response = null,
                                                             string requestAcceptHeader = "application/*",
                                                             string requestContentType = "application/json",
                                                             string requestAcceptCharsetHeader = "",
                                                             bool respectBrowserAcceptHeader = false)
        {
            var httpContext = new Mock<HttpContext>();
            if (response != null)
            {
                httpContext.Setup(o => o.Response).Returns(response);
            }

            var content = "{name: 'Person Name', Age: 'not-an-age'}";
            var contentBytes = Encoding.UTF8.GetBytes(content);

            var request = new DefaultHttpContext().Request;
            request.Headers["Accept-Charset"] = requestAcceptCharsetHeader;
            request.Headers["Accept"] = requestAcceptHeader;
            request.ContentType = requestContentType;
            request.Body = new MemoryStream(contentBytes);

            httpContext.Setup(o => o.Request).Returns(request);
            httpContext.Setup(o => o.RequestServices).Returns(GetServiceProvider());
            var optionsAccessor = new MockMvcOptionsAccessor();
            optionsAccessor.Options.OutputFormatters.Add(new StringOutputFormatter());
            optionsAccessor.Options.OutputFormatters.Add(new JsonOutputFormatter());
            optionsAccessor.Options.RespectBrowserAcceptHeader = respectBrowserAcceptHeader;
            var actionBindingContextAccessor = new ActionBindingContextAccessor()
            {
                ActionBindingContext = new ActionBindingContext()
                {
                    OutputFormatters = optionsAccessor.Options.OutputFormatters
                }
            };

            httpContext.Setup(o => o.RequestServices.GetService(typeof(IActionBindingContextAccessor)))
                       .Returns(actionBindingContextAccessor);
            httpContext.Setup(o => o.RequestServices.GetService(typeof(IOptions<MvcOptions>)))
                .Returns(optionsAccessor);
            httpContext.Setup(o => o.RequestServices.GetService(typeof(ILogger<ObjectResult>)))
                .Returns(new Mock<ILogger<ObjectResult>>().Object);

            return new ActionContext(httpContext.Object, new RouteData(), new ActionDescriptor());
        }