예제 #1
0
        private IServiceCollection CreateServices(object diagnosticListener, HttpContext context, params ViewComponentDescriptor[] descriptors)
        {
            var httpContext = new HttpContextAccessor()
            {
                HttpContext = context
            };
            var tempDataProvider = new SessionStateTempDataProvider();
            var diagnosticSource = new DiagnosticListener("Microsoft.AspNet");

            if (diagnosticListener != null)
            {
                diagnosticSource.SubscribeWithAdapter(diagnosticListener);
            }

            var services = new ServiceCollection();

            services.AddInstance <DiagnosticSource>(diagnosticSource);
            services.AddSingleton <IOptions <MvcViewOptions>, TestOptionsManager <MvcViewOptions> >();
            services.AddTransient <IViewComponentHelper, DefaultViewComponentHelper>();
            services.AddSingleton <IViewComponentSelector, DefaultViewComponentSelector>();
            services.AddSingleton <IViewComponentDescriptorCollectionProvider, DefaultViewComponentDescriptorCollectionProvider>();
            services.AddSingleton <IViewComponentInvokerFactory, DefaultViewComponentInvokerFactory>();
            services.AddSingleton <ITypeActivatorCache, DefaultTypeActivatorCache>();
            services.AddSingleton <IViewComponentActivator, DefaultViewComponentActivator>();
            services.AddInstance <IViewComponentDescriptorProvider>(new FixedSetViewComponentDescriptorProvider(descriptors));
            services.AddSingleton <IModelMetadataProvider, EmptyModelMetadataProvider>();
            services.AddInstance <ILoggerFactory>(NullLoggerFactory.Instance);
            services.AddInstance <ITempDataDictionary>(new TempDataDictionary(httpContext, tempDataProvider));
            services.AddTransient <IHttpContextAccessor, HttpContextAccessor>();

            return(services);
        }
        public void Save_NullSession_NullDictionary_DoesNotThrow()
        {
            // Arrange
            var testProvider = new SessionStateTempDataProvider();

            // Act & Assert (does not throw)
            testProvider.SaveTempData(GetHttpContext(session: null, sessionEnabled: false), null);
        }
        public void EnsureObjectCanBeSerialized_ValidType_DoesNotThrow(object value)
        {
            // Arrange
            var testProvider = new SessionStateTempDataProvider();

            // Act & Assert (Does not throw)
            testProvider.EnsureObjectCanBeSerialized(value);
        }
예제 #4
0
        public void Save_NullSession_NullDictionary_DoesNotThrow()
        {
            // Arrange
            var testProvider = new SessionStateTempDataProvider();

            // Act & Assert (does not throw)
            testProvider.SaveTempData(GetHttpContext(session: null, sessionEnabled: false), null);
        }
        public void Load_NullSession_ReturnsEmptyDictionary()
        {
            // Arrange
            var testProvider = new SessionStateTempDataProvider();

            // Act
            var tempDataDictionary = testProvider.LoadTempData(
                GetHttpContext(session: null, sessionEnabled: true));

            // Assert
            Assert.Empty(tempDataDictionary);
        }
        public void Load_NonNullSession_NoSessionData_ReturnsEmptyDictionary()
        {
            // Arrange
            var testProvider = new SessionStateTempDataProvider();

            // Act
            var tempDataDictionary = testProvider.LoadTempData(
                GetHttpContext(Mock.Of<ISessionCollection>()));

            // Assert
            Assert.Empty(tempDataDictionary);
        }
예제 #7
0
        public void Load_NullSession_ReturnsEmptyDictionary()
        {
            // Arrange
            var testProvider = new SessionStateTempDataProvider();

            // Act
            var tempDataDictionary = testProvider.LoadTempData(
                GetHttpContext(session: null, sessionEnabled: true));

            // Assert
            Assert.Empty(tempDataDictionary);
        }
예제 #8
0
        public void Load_NonNullSession_NoSessionData_ReturnsEmptyDictionary()
        {
            // Arrange
            var testProvider = new SessionStateTempDataProvider();

            // Act
            var tempDataDictionary = testProvider.LoadTempData(
                GetHttpContext(Mock.Of <ISessionCollection>()));

            // Assert
            Assert.Empty(tempDataDictionary);
        }
        public void EnsureObjectCanBeSerialized_InvalidDictionaryType_Throws(object value, Type type)
        {
            // Arrange
            var testProvider = new SessionStateTempDataProvider();

            // Act & Assert
            var exception = Assert.Throws <InvalidOperationException>(() =>
            {
                testProvider.EnsureObjectCanBeSerialized(value);
            });

            Assert.Equal($"The '{typeof(SessionStateTempDataProvider).FullName}' cannot serialize a dictionary with a key of type '{type}' to session state.",
                         exception.Message);
        }
        public void Save_NullSession_NonEmptyDictionary_Throws()
        {
            // Arrange
            var testProvider = new SessionStateTempDataProvider();

            // Act & Assert
            Assert.Throws<InvalidOperationException>(() =>
            {
                testProvider.SaveTempData(
                    GetHttpContext(session: null, sessionEnabled: false),
                    new Dictionary<string, object> { { "foo", "bar" } }
                );
            });
        }
예제 #11
0
        public void Save_NullSession_NonEmptyDictionary_Throws()
        {
            // Arrange
            var testProvider = new SessionStateTempDataProvider();

            // Act & Assert
            Assert.Throws <InvalidOperationException>(() =>
            {
                testProvider.SaveTempData(
                    GetHttpContext(session: null, sessionEnabled: false),
                    new Dictionary <string, object> {
                    { "foo", "bar" }
                }
                    );
            });
        }
        public void SaveAndLoad_IntCanBeStoredAndLoaded()
        {
            // Arrange
            var testProvider = new SessionStateTempDataProvider();
            var input        = new Dictionary <string, object>
            {
                { "int", 10 }
            };
            var context = GetHttpContext(new TestSession(), true);

            // Act
            testProvider.SaveTempData(context, input);
            var TempData = testProvider.LoadTempData(context);

            // Assert
            var intVal = Convert.ToInt32(TempData["int"]);

            Assert.Equal(10, intVal);
        }
        public void SaveAndLoad_BoolCanBeStoredAndLoaded(bool value)
        {
            // Arrange
            var testProvider = new SessionStateTempDataProvider();
            var input        = new Dictionary <string, object>
            {
                { "bool", value }
            };
            var context = GetHttpContext(new TestSession(), true);

            // Act
            testProvider.SaveTempData(context, input);
            var TempData = testProvider.LoadTempData(context);

            // Assert
            var boolVal = Assert.IsType <bool>(TempData["bool"]);

            Assert.Equal(value, boolVal);
        }
        public void SaveAndLoad_StringCanBeStoredAndLoaded()
        {
            // Arrange
            var testProvider = new SessionStateTempDataProvider();
            var input        = new Dictionary <string, object>
            {
                { "string", "value" }
            };
            var context = GetHttpContext(new TestSession(), true);

            // Act
            testProvider.SaveTempData(context, input);
            var TempData = testProvider.LoadTempData(context);

            // Assert
            var stringVal = Assert.IsType <string>(TempData["string"]);

            Assert.Equal("value", stringVal);
        }
        public void SaveAndLoad_EmptyDictionary_RoundTripsAsNull()
        {
            // Arrange
            var testProvider = new SessionStateTempDataProvider();
            var input        = new Dictionary <string, object>
            {
                { "EmptyDictionary", new Dictionary <string, int>() }
            };
            var context = GetHttpContext(new TestSession(), true);

            // Act
            testProvider.SaveTempData(context, input);
            var TempData = testProvider.LoadTempData(context);

            // Assert
            var emptyDictionary = (IDictionary <string, int>)TempData["EmptyDictionary"];

            Assert.Null(emptyDictionary);
        }
        public void SaveAndLoad_GuidCanBeStoredAndLoaded()
        {
            // Arrange
            var testProvider = new SessionStateTempDataProvider();
            var inputGuid    = Guid.NewGuid();
            var input        = new Dictionary <string, object>
            {
                { "Guid", inputGuid }
            };
            var context = GetHttpContext(new TestSession(), true);

            // Act
            testProvider.SaveTempData(context, input);
            var TempData = testProvider.LoadTempData(context);

            // Assert
            var guidVal = Assert.IsType <Guid>(TempData["Guid"]);

            Assert.Equal(inputGuid, guidVal);
        }
        public void SaveAndLoad_DateTimeCanBeStoredAndLoaded()
        {
            // Arrange
            var testProvider  = new SessionStateTempDataProvider();
            var inputDatetime = new DateTime(2010, 12, 12, 1, 2, 3, DateTimeKind.Local);
            var input         = new Dictionary <string, object>
            {
                { "DateTime", inputDatetime }
            };
            var context = GetHttpContext(new TestSession(), true);

            // Act
            testProvider.SaveTempData(context, input);
            var TempData = testProvider.LoadTempData(context);

            // Assert
            var datetime = Assert.IsType <DateTime>(TempData["DateTime"]);

            Assert.Equal(inputDatetime, datetime);
        }
        public void SaveAndLoad_DictionaryCanBeStoredAndLoaded()
        {
            // Arrange
            var testProvider    = new SessionStateTempDataProvider();
            var inputDictionary = new Dictionary <string, string>
            {
                { "Hello", "World" },
            };
            var input = new Dictionary <string, object>
            {
                { "Dictionary", inputDictionary }
            };
            var context = GetHttpContext(new TestSession(), true);

            // Act
            testProvider.SaveTempData(context, input);
            var TempData = testProvider.LoadTempData(context);

            // Assert
            var dictionary = Assert.IsType <Dictionary <string, string> >(TempData["Dictionary"]);

            Assert.Equal("World", dictionary["Hello"]);
        }
        public void SaveAndLoad_ListCanBeStoredAndLoaded()
        {
            // Arrange
            var testProvider = new SessionStateTempDataProvider();
            var input        = new Dictionary <string, object>
            {
                { "List`string", new List <string> {
                      "one", "two"
                  } }
            };
            var context = GetHttpContext(new TestSession(), true);

            // Act
            testProvider.SaveTempData(context, input);
            var TempData = testProvider.LoadTempData(context);

            // Assert
            var list = (IList <string>)TempData["List`string"];

            Assert.Equal(2, list.Count);
            Assert.Equal("one", list[0]);
            Assert.Equal("two", list[1]);
        }
        public void SaveAndLoad_IntCanBeStoredAndLoaded()
        {
            // Arrange
            var testProvider = new SessionStateTempDataProvider();
            var input = new Dictionary<string, object>
            {
                { "int", 10 }
            };
            var context = GetHttpContext(new TestSessionCollection(), true);

            // Act
            testProvider.SaveTempData(context, input);
            var TempData = testProvider.LoadTempData(context);

            // Assert
            var intVal = Convert.ToInt32(TempData["int"]);
            Assert.Equal(10, intVal);
        }
        public void SaveAndLoad_BoolCanBeStoredAndLoaded(bool value)
        {
            // Arrange
            var testProvider = new SessionStateTempDataProvider();
            var input = new Dictionary<string, object>
            {
                { "bool", value }
            };
            var context = GetHttpContext(new TestSessionCollection(), true);

            // Act
            testProvider.SaveTempData(context, input);
            var TempData = testProvider.LoadTempData(context);

            // Assert
            var boolVal = Assert.IsType<bool>(TempData["bool"]);
            Assert.Equal(value, boolVal);
        }
        public void SaveAndLoad_EmptyDictionary_RoundTripsAsNull()
        {
            // Arrange
            var testProvider = new SessionStateTempDataProvider();
            var input = new Dictionary<string, object>
            {
                { "EmptyDictionary", new Dictionary<string, int>() }
            };
            var context = GetHttpContext(new TestSessionCollection(), true);

            // Act
            testProvider.SaveTempData(context, input);
            var TempData = testProvider.LoadTempData(context);

            // Assert
            var emptyDictionary = (IDictionary<string, int>)TempData["EmptyDictionary"];
            Assert.Null(emptyDictionary);
        }
        public void SaveAndLoad_StringCanBeStoredAndLoaded()
        {
            // Arrange
            var testProvider = new SessionStateTempDataProvider();
            var input = new Dictionary<string, object>
            {
                { "string", "value" }
            };
            var context = GetHttpContext(new TestSessionCollection(), true);

            // Act
            testProvider.SaveTempData(context, input);
            var TempData = testProvider.LoadTempData(context);

            // Assert
            var stringVal = Assert.IsType<string>(TempData["string"]);
            Assert.Equal("value", stringVal);
        }
        public void SaveAndLoad_DateTimeCanBeStoredAndLoaded()
        {
            // Arrange
            var testProvider = new SessionStateTempDataProvider();
            var inputDatetime = new DateTime(2010, 12, 12, 1, 2, 3, DateTimeKind.Local);
            var input = new Dictionary<string, object>
            {
                { "DateTime", inputDatetime }
            };
            var context = GetHttpContext(new TestSessionCollection(), true);

            // Act
            testProvider.SaveTempData(context, input);
            var TempData = testProvider.LoadTempData(context);

            // Assert
            var datetime = Assert.IsType<DateTime>(TempData["DateTime"]);
            Assert.Equal(inputDatetime, datetime);
        }
        public void SaveAndLoad_GuidCanBeStoredAndLoaded()
        {
            // Arrange
            var testProvider = new SessionStateTempDataProvider();
            var inputGuid = Guid.NewGuid();
            var input = new Dictionary<string, object>
            {
                { "Guid", inputGuid }
            };
            var context = GetHttpContext(new TestSessionCollection(), true);

            // Act
            testProvider.SaveTempData(context, input);
            var TempData = testProvider.LoadTempData(context);

            // Assert
            var guidVal = Assert.IsType<Guid>(TempData["Guid"]);
            Assert.Equal(inputGuid, guidVal);
        }
        public void EnsureObjectCanBeSerialized_ValidType_DoesNotThrow(object value)
        {
            // Arrange
            var testProvider = new SessionStateTempDataProvider();

            // Act & Assert (Does not throw)
            testProvider.EnsureObjectCanBeSerialized(value);
        }
        public void SaveAndLoad_ListCanBeStoredAndLoaded()
        {
            // Arrange
            var testProvider = new SessionStateTempDataProvider();
            var input = new Dictionary<string, object>
            {
                { "List`string", new List<string> { "one", "two" } }
            };
            var context = GetHttpContext(new TestSessionCollection(), true);

            // Act
            testProvider.SaveTempData(context, input);
            var TempData = testProvider.LoadTempData(context);

            // Assert
            var list = (IList<string>)TempData["List`string"];
            Assert.Equal(2, list.Count);
            Assert.Equal("one", list[0]);
            Assert.Equal("two", list[1]);
        }
        public void SaveAndLoad_DictionaryCanBeStoredAndLoaded()
        {
            // Arrange
            var testProvider = new SessionStateTempDataProvider();
            var inputDictionary = new Dictionary<string, string>
            {
                { "Hello", "World" },
            };
            var input = new Dictionary<string, object>
            {
                { "Dictionary", inputDictionary }
            };
            var context = GetHttpContext(new TestSessionCollection(), true);

            // Act
            testProvider.SaveTempData(context, input);
            var TempData = testProvider.LoadTempData(context);

            // Assert
            var dictionary = Assert.IsType<Dictionary<string, string>>(TempData["Dictionary"]);
            Assert.Equal("World", dictionary["Hello"]);
        }
예제 #29
0
        private IServiceCollection CreateServices(object diagnosticListener, HttpContext context, params ViewComponentDescriptor[] descriptors)
        { 
            var httpContext = new HttpContextAccessor() { HttpContext = context };
            var tempDataProvider = new SessionStateTempDataProvider();
            var diagnosticSource = new DiagnosticListener("Microsoft.AspNet");
            if (diagnosticListener != null)
            {
                diagnosticSource.SubscribeWithAdapter(diagnosticListener);
            }

            var services = new ServiceCollection();
            services.AddInstance<DiagnosticSource>(diagnosticSource);
            services.AddSingleton<IOptions<MvcViewOptions>, TestOptionsManager<MvcViewOptions>>();
            services.AddTransient<IViewComponentHelper, DefaultViewComponentHelper>();
            services.AddSingleton<IViewComponentSelector, DefaultViewComponentSelector>();
            services.AddSingleton<IViewComponentDescriptorCollectionProvider, DefaultViewComponentDescriptorCollectionProvider>();
            services.AddSingleton<IViewComponentInvokerFactory, DefaultViewComponentInvokerFactory>();
            services.AddSingleton<ITypeActivatorCache, DefaultTypeActivatorCache>();
            services.AddSingleton<IViewComponentActivator, DefaultViewComponentActivator>();
            services.AddInstance<IViewComponentDescriptorProvider>(new FixedSetViewComponentDescriptorProvider(descriptors));
            services.AddSingleton<IModelMetadataProvider, EmptyModelMetadataProvider>();
            services.AddInstance<ILoggerFactory>(NullLoggerFactory.Instance);
            services.AddInstance<ITempDataDictionary>(new TempDataDictionary(httpContext, tempDataProvider));
            services.AddTransient<IHttpContextAccessor, HttpContextAccessor>();

            return services;
        }
        public void EnsureObjectCanBeSerialized_InvalidType_Throws(object value, Type type)
        {
            // Arrange
            var testProvider = new SessionStateTempDataProvider();

            // Act & Assert
            var exception = Assert.Throws<InvalidOperationException>(() =>
            {
                testProvider.EnsureObjectCanBeSerialized(value);
            });
            Assert.Equal($"The '{typeof(SessionStateTempDataProvider).FullName}' cannot serialize an object of type '{type}' to session state.",
                exception.Message);
        }