コード例 #1
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 TestOptionsManager <MvcOptions>();

            optionsAccessor.Value.OutputFormatters.Add(new StringOutputFormatter());
            optionsAccessor.Value.OutputFormatters.Add(new JsonOutputFormatter());
            optionsAccessor.Value.RespectBrowserAcceptHeader = respectBrowserAcceptHeader;
            var actionBindingContextAccessor = new ActionBindingContextAccessor()
            {
                ActionBindingContext = new ActionBindingContext()
                {
                    OutputFormatters = optionsAccessor.Value.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()));
        }
コード例 #2
0
        private static IServiceProvider CreateServices()
        {
            var options = new TestOptionsManager <MvcOptions>();

            options.Value.OutputFormatters.Add(new StringOutputFormatter());
            options.Value.OutputFormatters.Add(new JsonOutputFormatter());

            var services = new ServiceCollection();

            services.AddSingleton(new ObjectResultExecutor(
                                      options,
                                      new TestHttpResponseStreamWriterFactory(),
                                      NullLoggerFactory.Instance));

            return(services.BuildServiceProvider());
        }
コード例 #3
0
        private IServiceProvider GetServiceProvider(Dictionary <string, CacheProfile> cacheProfiles)
        {
            var serviceProvider = new Mock <IServiceProvider>();
            var optionsAccessor = new TestOptionsManager <MvcOptions>();

            if (cacheProfiles != null)
            {
                foreach (var p in cacheProfiles)
                {
                    optionsAccessor.Value.CacheProfiles.Add(p.Key, p.Value);
                }
            }

            serviceProvider
            .Setup(s => s.GetService(typeof(IOptions <MvcOptions>)))
            .Returns(optionsAccessor);

            return(serviceProvider.Object);
        }
コード例 #4
0
ファイル: PartialViewResultTest.cs プロジェクト: zyonet/Mvc
        private HttpContext GetHttpContext()
        {
            var options = new TestOptionsManager <MvcViewOptions>();

            var viewExecutor = new PartialViewResultExecutor(
                options,
                new TestHttpResponseStreamWriterFactory(),
                new CompositeViewEngine(options),
                new DiagnosticListener("Microsoft.AspNet"),
                NullLoggerFactory.Instance);

            var services = new ServiceCollection();

            services.AddInstance(viewExecutor);

            var httpContext = new DefaultHttpContext();

            httpContext.RequestServices = services.BuildServiceProvider();
            return(httpContext);
        }
コード例 #5
0
        private static HttpContext GetHttpContext()
        {
            var httpContext = new Mock <HttpContext>();
            var realContext = new DefaultHttpContext();
            var request     = realContext.Request;

            request.PathBase = new PathString("");
            var response = realContext.Response;

            response.Body = new MemoryStream();

            httpContext.Setup(o => o.Request)
            .Returns(request);
            httpContext.Setup(o => o.Response)
            .Returns(response);
            var optionsAccessor = new TestOptionsManager <MvcOptions>();

            optionsAccessor.Value.OutputFormatters.Add(new StringOutputFormatter());
            optionsAccessor.Value.OutputFormatters.Add(new JsonOutputFormatter());
            httpContext
            .Setup(p => p.RequestServices.GetService(typeof(IOptions <MvcOptions>)))
            .Returns(optionsAccessor);
            httpContext
            .Setup(p => p.RequestServices.GetService(typeof(ILogger <ObjectResult>)))
            .Returns(new Mock <ILogger <ObjectResult> >().Object);

            var actionBindingContext = new ActionBindingContext()
            {
                OutputFormatters = optionsAccessor.Value.OutputFormatters
            };

            httpContext
            .Setup(o => o.RequestServices.GetService(typeof(IActionBindingContextAccessor)))
            .Returns(new ActionBindingContextAccessor()
            {
                ActionBindingContext = actionBindingContext
            });

            return(httpContext.Object);
        }