public ParameterBindingRouteConstraint(ParameterBinder binder)
            : base(binder.ParameterType)
        {
            if (binder == null) throw new ArgumentNullException("binder");

             _Binder = binder;
        }
        void Run(Type controller, BinderPrecedence.BinderPrecedence expected, ParameterBinder globalBinder = null)
        {
            var settings = new CodeRoutingSettings();

             if (globalBinder != null) {
            settings.ParameterBinders.Add(globalBinder);
             }

             routes.Clear();
             routes.MapCodeRoutes(controller, settings);

             var httpContextMock = new Mock<HttpContextBase>();
             httpContextMock.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/index/foo");

             var httpResponseMock = new Mock<HttpResponseBase>();
             httpContextMock.Setup(c => c.Response).Returns(httpResponseMock.Object);

             var routeData = routes.GetRouteData(httpContextMock.Object);

             var controllerInstance = (Controller)Activator.CreateInstance(controller);
             controllerInstance.ValidateRequest = false;

             var requestContext = new RequestContext(httpContextMock.Object, routeData);
             var controllerContext = new ControllerContext(requestContext, controllerInstance);

             controllerInstance.ValueProvider = new ValueProviderCollection(new IValueProvider[] { new RouteDataValueProvider(controllerContext) });

             ((IController)controllerInstance).Execute(requestContext);

             httpResponseMock.Verify(c => c.Write(It.Is<string>(s => s == expected.ToString())), Times.AtLeastOnce());
        }
 public override object CreateParameterBindingRouteConstraint(ParameterBinder binder)
 {
     return new ParameterBindingRouteConstraint(binder);
 }
        public void IntegerCanonicalForm()
        {
            var signedIntegers = new ParameterBinder[] {
            new Int16ParameterBinder(),
            new Int32ParameterBinder(),
            new Int64ParameterBinder(),
            new SByteParameterBinder(),
             };

             var unsignedIntegers = new ParameterBinder[] {
            new ByteParameterBinder(),
            new UInt16ParameterBinder(),
            new UInt32ParameterBinder(),
            new UInt64ParameterBinder()
             };

             IntegerTests(unsignedIntegers, signedIntegers);
        }
        void IntegerTests(ParameterBinder[] unsignedbinders, ParameterBinder[] signedBinders)
        {
            foreach (var binder in signedBinders.Concat(unsignedbinders)) {

            foreach (var item in new[] { "5", "0" }) {

               object result;

               Assert.IsTrue(binder.TryBind(item, CultureInfo.InvariantCulture, out result));
               Assert.AreEqual(item, Convert.ToString(result, CultureInfo.InvariantCulture));
            }

            foreach (var item in new[] { "", " ", null, " 5", "5 ", "00", "05", "+0", "+5" }) {

               object result;

               Assert.IsFalse(binder.TryBind(item, CultureInfo.InvariantCulture, out result));
               Assert.IsNull(result);
            }
             }

             foreach (var binder in signedBinders) {

            foreach (var item in new[] { "-5" }) {

               object result;

               Assert.IsTrue(binder.TryBind(item, CultureInfo.InvariantCulture, out result));
               Assert.AreEqual(item, Convert.ToString(result, CultureInfo.InvariantCulture));
            }

            // TODO: Disallow -0 in v2

            foreach (var item in new[] { /*"-0",*/ "-05" }) {

               object result;

               Assert.IsFalse(binder.TryBind(item, CultureInfo.InvariantCulture, out result));
               Assert.IsNull(result);
            }
             }
        }
        void DecimalTests(ParameterBinder binder)
        {
            foreach (var item in new[] { "-0.5", "0.5" }) {

            object result;

            Assert.IsTrue(binder.TryBind(item, CultureInfo.InvariantCulture, out result));
            Assert.AreEqual(item, Convert.ToString(result, CultureInfo.InvariantCulture));
             }

             // TODO: Disallow trailing 0 in v2

             foreach (var item in new[] { "-.5", ".5", "+.5"/*, "0.50"*/, "0.", "5." }) {

            object result;

            Assert.IsFalse(binder.TryBind(item, CultureInfo.InvariantCulture, out result));
            Assert.IsNull(result);
             }
        }
        void Run(Type controller, BinderPrecedence.BinderPrecedence expected, ParameterBinder globalBinder = null)
        {
            var settings = new CodeRoutingSettings();

             if (globalBinder != null) {
            settings.ParameterBinders.Add(globalBinder);
             }

             config.MapCodeRoutes(controller, settings);

             var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/foo");
             var routeData = routes.GetRouteData(request);

             var controllerInstance = (ApiController)Activator.CreateInstance(controller);

             var controllerContext = new HttpControllerContext(config, routeData, request) {
            ControllerDescriptor = new HttpControllerDescriptor(config, (string)routeData.Values["controller"], controller),
            Controller = controllerInstance
             };

             string value;

             controllerInstance.ExecuteAsync(controllerContext, CancellationToken.None)
            .Result
            .TryGetContentValue(out value);

             Assert.AreEqual(expected.ToString(), value);
        }