public void GetTypeName_verify_error_on_unrecognized_type()
        {
            DerivedTypeModelBinderCache.Reset();
            DerivedTypeModelBinderCache.RegisterDerivedTypes(typeof(ITestClass), new[] { typeof(TestClass) });

            Assert.Throws <KeyNotFoundException>(() => DerivedTypeModelBinderCache.GetTypeName(typeof(DerivedTypeModelBinderCacheTests)));
        }
Exemplo n.º 2
0
        public void ValidateBehaviorWhenValidDerivedTypeIsFound()
        {
            DerivedTypeModelBinderCache.Reset();

            DerivedTypeModelBinderCache.RegisterDerivedTypes(typeof(DerivedTypeModelBinderTests),
                                                             new[] { typeof(StubClass) });

            var valueProvider     = MockRepository.GenerateMock <IValueProvider>();
            var typeStampOperator = MockRepository.GenerateMock <ITypeStampOperator>();

            valueProvider.Expect(b => b.ContainsPrefix("test"))
            .Return(true);

            typeStampOperator.Expect(a => a.DetectTypeStamp(null, null)).IgnoreArguments().Return(DerivedTypeModelBinderCache.GetTypeName(typeof(StubClass)));

            var binder = new DerivedTypeModelBinder(typeStampOperator);

            var bindingContext = new ModelBindingContext
            {
                ModelMetadata =
                    new ModelMetadata(new EmptyModelMetadataProvider(), typeof(DerivedTypeModelBinder),
                                      null, typeof(DerivedTypeModelBinderTests), "propertyName"
                                      ),
                ModelName     = "test",
                ValueProvider = valueProvider
            };


            object model = binder.BindModel(new ControllerContext(), bindingContext);

            Assert.That(model, Is.TypeOf(typeof(StubClass)));
            Assert.That(bindingContext.ModelType.FullName, Is.EqualTo(typeof(StubClass).FullName));

            DerivedTypeModelBinderCache.Reset();
        }
Exemplo n.º 3
0
        protected void Application_Start()
        {
            // Register the default hubs route: ~/signalr
            RouteTable.Routes.MapHubs(new HubConfiguration {
                EnableDetailedErrors = true
            });

            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();

            Database.SetInitializer(new RegisterKeeperDbInitialiser());

            ModelBinders.Binders.Add(typeof(Distance), new EnumFlagsModelBinder());

            BundleMobileConfig.RegisterBundles(BundleTable.Bundles);

            DerivedTypeModelBinderCache.RegisterDerivedTypes(
                typeof(Shot),
                new[]
            {
                typeof(SightingShot),
                typeof(ScoringShot)
            });
        }
Exemplo n.º 4
0
        public void TypeStamp_throws_error_when_requested_class_is_not_registered()
        {
            DerivedTypeModelBinderCache.Reset();

            var html = CreateHtmlHelper <HtmlExtensionTests>();

            Assert.Throws <KeyNotFoundException>(() => html.TypeStamp().ToHtmlString());
        }
        public void GetTypeName_verify_encryption()
        {
            DerivedTypeModelBinderCache.Reset();
            DerivedTypeModelBinderCache.RegisterDerivedTypes(typeof(ITestClass), new[] { typeof(TestClass) });

            var typeName = DerivedTypeModelBinderCache.GetTypeName(typeof(TestClass));

            Assert.That(typeName, Is.StringStarting("VFxCac+"));
        }
Exemplo n.º 6
0
        public void validate_attribute_scan_on_getDerivedTypes_call()
        {
            DerivedTypeModelBinderCache.Reset();

            Assert.That((from p in DerivedTypeModelBinderCache.GetDerivedTypes(typeof(DerivedTypeModelBinderCacheTests))
                         where p.Name == typeof(int).Name
                         select p).FirstOrDefault(), Is.Not.Null);

            DerivedTypeModelBinderCache.Reset();
        }
        public void validate_declarative_registration_of_derived_types()
        {
            DerivedTypeModelBinderCache.RegisterDerivedTypes(typeof(DerivedTypeModelBinderCacheTests),
                                                             new[] { typeof(string) });

            Assert.That(DerivedTypeModelBinderCache.GetTypeName(typeof(string)), Is.Not.Empty);

            DerivedTypeModelBinderCache.Reset();

            // next, let's validate that the cache was cleared by reset
            Assert.Throws <KeyNotFoundException>(() => DerivedTypeModelBinderCache.GetTypeName(typeof(string)));
        }
Exemplo n.º 8
0
        public static void RegisterRoutes(RouteCollection routes)
        {
            DerivedTypeModelBinderCache.RegisterDerivedTypes(typeof(IContent), new[] { typeof(AddressInfo), typeof(User) });

            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default",                                                              // Route name
                "{controller}/{action}/{id}",                                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }                  // Parameter defaults
                );
        }
Exemplo n.º 9
0
        public void TypeStamp_returns_proper_encoded_identifier()
        {
            DerivedTypeModelBinderCache.Reset();

            DerivedTypeModelBinderCache.RegisterDerivedTypes(typeof(ITestClass), new[] { typeof(TestClass) });

            var html = CreateHtmlHelper <TestClass>();

            var output = html.TypeStamp();

            Assert.That(output.ToHtmlString(), Is.StringContaining("name=\"_xTypeStampx_\""));
            Assert.That(output.ToHtmlString(), Is.StringContaining("value=\""));
            Assert.That(output.ToHtmlString(), Is.StringContaining("type=\"hidden\""));
        }
Exemplo n.º 10
0
        public void validate_declarative_registration_of_derived_types()
        {
            DerivedTypeModelBinderCache.RegisterDerivedTypes(typeof(DerivedTypeModelBinderCacheTests),
                                                             new[] { typeof(string) });

            Assert.That((from p in DerivedTypeModelBinderCache.GetDerivedTypes(typeof(DerivedTypeModelBinderCacheTests))
                         where p.Name == typeof(string).Name
                         select p).FirstOrDefault(), Is.Not.Null);

            DerivedTypeModelBinderCache.Reset();

            // next, let's validate that the cache was cleared by reset
            Assert.That((from p in DerivedTypeModelBinderCache.GetDerivedTypes(typeof(DerivedTypeModelBinderCacheTests))
                         where p.Name == typeof(string).Name
                         select p).FirstOrDefault(), Is.Null);
        }
Exemplo n.º 11
0
        public void TypeStamp_uses_type_stamp_name()
        {
            DerivedTypeModelBinderCache.Reset();

            DerivedTypeModelBinderCache.RegisterDerivedTypes(typeof(ITestClass), new[] { typeof(TestClass) });

            var originalField = DerivedTypeModelBinderCache.TypeStampFieldName;

            DerivedTypeModelBinderCache.TypeStampFieldName = "test";

            var html = CreateHtmlHelper <TestClass>();

            var output = html.TypeStamp();

            DerivedTypeModelBinderCache.TypeStampFieldName = originalField;

            Assert.That(output.ToHtmlString(), Is.StringContaining("name=\"test\""));
        }
        public void TypeStamp_verify_encryption_set_and_reset_behaviors()
        {
            DerivedTypeModelBinderCache.Reset();
            DerivedTypeModelBinderCache.RegisterDerivedTypes(typeof(ITestClass), new[] { typeof(TestClass) });

            var standardTypeStamp = DerivedTypeModelBinderCache.GetTypeName(typeof(TestClass));


            DerivedTypeModelBinderCache.Reset();
            DerivedTypeModelBinderCache.SetTypeStampSaltValue(Guid.NewGuid());
            DerivedTypeModelBinderCache.RegisterDerivedTypes(typeof(ITestClass), new[] { typeof(TestClass) });

            var alteredTypeStamp = DerivedTypeModelBinderCache.GetTypeName(typeof(TestClass));


            DerivedTypeModelBinderCache.Reset();
            DerivedTypeModelBinderCache.RegisterDerivedTypes(typeof(ITestClass), new[] { typeof(TestClass) });

            var resetTypeStamp = DerivedTypeModelBinderCache.GetTypeName(typeof(TestClass));


            Assert.That(standardTypeStamp, Is.Not.EqualTo(alteredTypeStamp));
            Assert.That(standardTypeStamp, Is.EqualTo(resetTypeStamp));
        }
 /// <summary>
 /// Renders metadata used by the derived type model binder to determine
 /// the concrete type to instantiate.
 /// </summary>
 /// <typeparam name="TModel">The type of the model.</typeparam>
 /// <param name="htmlHelper">The HTML helper.</param>
 /// <returns>a hidden field rendering that holds the type metadata</returns>
 public static MvcHtmlString TypeStamp <TModel>(this HtmlHelper <TModel> htmlHelper)
 {
     return(htmlHelper.Hidden(DerivedTypeModelBinderCache.TypeStampFieldName, DerivedTypeModelBinderCache.GetTypeName(typeof(TModel))));
 }