Exemplo n.º 1
0
        public void BuildRouteParameters_Can_Parse_Parameter_From_Form_Body(string methodName, string parameterName, string body, string culture, object rawExpected)
        {
            using (new CultureSwap(culture)) {
                var route = Route_Tests.CreateRoute(typeof(PostFormController), methodName);
                _RouteMapper.Initialise(new Route[] { route });
                _Environment.SetRequestBody(body, contentType: "application/x-www-form-urlencoded");
                _Environment.SetRequestPath(route.PathParts[0].Part);

                var parameters = _RouteMapper.BuildRouteParameters(route, _Environment.Environment);

                var expected = rawExpected;

                Assert.IsTrue(parameters.IsValid);
                Assert.AreEqual(1, parameters.Parameters.Length);
                var param = parameters.Parameters[0];

                if (expected is IList expectedList)
                {
                    var actualList = (IList)param;
                    Assert.AreEqual(expectedList.Count, actualList.Count);
                    for (var i = 0; i < expectedList.Count; ++i)
                    {
                        Assert.AreEqual(expectedList[i], actualList[i]);
                    }
                }
                else
                {
                    Assert.AreEqual(expected, param);
                }
            }
        }
Exemplo n.º 2
0
        public void BuildRouteParameters_Uses_Case_Sensitivity_Flag_When_Building_Model_From_Form_Body(bool caseSensitiveKeys)
        {
            _RouteMapper.AreFormNamesCaseSensitive = caseSensitiveKeys;
            var route = Route_Tests.CreateRoute(typeof(FormBodyModelController), nameof(FormBodyModelController.PostStringModel));

            _RouteMapper.Initialise(new Route[] { route });
            _Environment.RequestMethod = "POST";
            _Environment.SetRequestPath(route.PathParts[0].Part);
            _Environment.SetRequestBody("STRINGVALUE=Ab", contentType: "application/x-www-form-urlencoded");

            var parameters = _RouteMapper.BuildRouteParameters(route, _Environment.Environment);

            Assert.AreEqual(1, parameters.Parameters.Length);
            var model = parameters.Parameters[0] as StringModel;

            if (caseSensitiveKeys)
            {
                Assert.IsTrue(parameters.IsValid);
                Assert.IsNull(model.StringValue);
            }
            else
            {
                Assert.IsTrue(parameters.IsValid);
                Assert.AreEqual("Ab", model.StringValue);
            }
        }
Exemplo n.º 3
0
        public void BuildRouteParameters_Parses_Different_Types_From_Path_Parts(string methodName, string pathPart, string culture, object rawExpected)
        {
            using (new CultureSwap(culture)) {
                var route = Route_Tests.CreateRoute(typeof(PathPartTypeController), methodName);
                _RouteMapper.Initialise(new Route[] { route });

                _Environment.SetRequestPath(new string[] { route.PathParts[0].Part, pathPart });
                var parameters = _RouteMapper.BuildRouteParameters(route, _Environment.Environment);

                var expected = rawExpected;
                switch (methodName)
                {
                case nameof(PathPartTypeController.DateTimePP):         expected = DataRowParser.DateTime((string)expected); break;

                case nameof(PathPartTypeController.DateTimeOffsetPP):   expected = DataRowParser.DateTimeOffset((string)expected); break;
                }

                Assert.IsTrue(parameters.IsValid);
                Assert.AreEqual(1, parameters.Parameters.Length);
                var actual = parameters.Parameters[0];
                if (actual is byte[] byteArray)
                {
                    var expectedByteArray = (byte[])expected;
                    Assert.IsTrue(expectedByteArray.SequenceEqual(byteArray));
                }
                else
                {
                    Assert.AreEqual(expected, parameters.Parameters[0]);
                }
            }
        }
Exemplo n.º 4
0
        public void CallRoute_Throws_If_Environment_Is_Null()
        {
            var route           = Route_Tests.CreateRoute <Controller>(nameof(Controller.Route_1));
            var routeParameters = Route_Tests.CreateRouteParameters(41);

            _RouteCaller.CallRoute(null, route, routeParameters);
        }
Exemplo n.º 5
0
        public void BuildRouteParameters_Throws_If_Environment_Is_Null()
        {
            var route = Route_Tests.CreateRoute(typeof(ApiEntityWithIDController), nameof(ApiEntityWithIDController.Method));

            _RouteMapper.Initialise(new Route[] { route });

            _RouteMapper.BuildRouteParameters(route, null);
        }
Exemplo n.º 6
0
        public void CanCallRoute_Returns_True_If_Route_Has_No_Authorisation_Filter()
        {
            var route = Route_Tests.CreateRoute <Controller>(nameof(Controller.NoAuthFilter));

            var actual = _RouteFilter.CanCallRoute(route, _Environment.Environment);

            Assert.IsTrue(actual);
        }
Exemplo n.º 7
0
        public void BuildRouteParameters_Throws_If_Route_Is_Null()
        {
            var route = Route_Tests.CreateRoute(typeof(ApiEntityWithIDController), nameof(ApiEntityWithIDController.Method));

            _RouteMapper.Initialise(new Route[] { route });

            _Environment.SetRequestPath("/api/entity/1");
            _RouteMapper.BuildRouteParameters(null, _Environment.Environment);
        }
Exemplo n.º 8
0
        public void CallRoute_Disposes_Of_Disposable_Controllers()
        {
            var route           = Route_Tests.CreateRoute <DisposableController>(nameof(DisposableController.Route_D1));
            var routeParameters = Route_Tests.CreateRouteParameters(99);

            _RouteCaller.CallRoute(_Environment.Environment, route, routeParameters);

            Assert.IsTrue(_DisposableControllerDisposed);
        }
Exemplo n.º 9
0
        public void Middleware_Does_Not_Call_WebApiResponder_For_Void_Routes()
        {
            _RouteOutcome = null;
            _FoundRoute   = Route_Tests.CreateRoute <Controller>(nameof(Controller.VoidMethod));

            _Pipeline.BuildAndCallMiddleware(_WebApi.AppFuncBuilder, _Environment.Environment);

            _Responder.Verify(r => r.ReturnJsonObject(_Environment.Environment, _RouteOutcome), Times.Never());
        }
Exemplo n.º 10
0
        public void CanCallRoute_Passes_Environment_To_Other_Filter()
        {
            var route       = Route_Tests.CreateRoute <Controller>(nameof(Controller.OneOtherFilter));
            var otherFilter = ConfigureBaseFilter(route, "", true);

            _RouteFilter.CanCallRoute(route, _Environment.Environment);

            Assert.AreSame(_Environment.Environment, otherFilter.OwinEnvironment);
        }
Exemplo n.º 11
0
        public void CanCallRoute_Does_Not_Skip_Authorisation_Filters_If_AllowAnonymous_Used_And_Request_Is_Not_Anonymous()
        {
            var route = Route_Tests.CreateRoute <Controller>(nameof(Controller.OneAuthFilterAllowAnonymous));

            ConfigureBaseFilter(route, "", false);

            var actual = _RouteFilter.CanCallRoute(route, _Environment.Environment);

            Assert.IsFalse(actual);
        }
Exemplo n.º 12
0
        public void CanCallRoute_Returns_True_If_Singleton_Auth_Filter_Returns_True()
        {
            var route = Route_Tests.CreateRoute <Controller>(nameof(Controller.OneAuthFilter));

            ConfigureBaseFilter(route, "", true);

            var actual = _RouteFilter.CanCallRoute(route, _Environment.Environment);

            Assert.IsTrue(actual);
        }
Exemplo n.º 13
0
        public void CanCallRoute_Returns_False_If_Other_Filter_Returns_False()
        {
            var route = Route_Tests.CreateRoute <Controller>(nameof(Controller.OneOtherFilter));

            ConfigureBaseFilter(route, "", false);

            var actual = _RouteFilter.CanCallRoute(route, _Environment.Environment);

            Assert.IsFalse(actual);
        }
Exemplo n.º 14
0
        public void BuildRouteParameters_Clears_IsValid_When_Parameter_Cannot_Be_Parsed()
        {
            var route = Route_Tests.CreateRoute(typeof(ApiEntityWithIDController), nameof(ApiEntityWithIDController.Method));

            _RouteMapper.Initialise(new Route[] { route });

            _Environment.SetRequestPath("/api/entity/not-an-int");
            var parameters = _RouteMapper.BuildRouteParameters(route, _Environment.Environment);

            Assert.IsFalse(parameters.IsValid);
        }
Exemplo n.º 15
0
        public void BuildRouteParameters_Does_Not_Inject_Owin_Environment_To_Instance_Route_Method()
        {
            var route = Route_Tests.CreateRoute(typeof(OwinEnvController), nameof(OwinEnvController.NotStatic));

            _RouteMapper.Initialise(new Route[] { route });

            _Environment.SetRequestPath(route.PathParts[0].Part);
            var parameters = _RouteMapper.BuildRouteParameters(route, _Environment.Environment);

            Assert.IsFalse(parameters.IsValid);
        }
Exemplo n.º 16
0
        public void CanCallRoute_Returns_Correct_Result_When_More_Than_One_Auth_Filter_Used(bool auth1Result, bool auth2Result, bool expected)
        {
            var route = Route_Tests.CreateRoute <Controller>(nameof(Controller.TwoAuthFilters));

            ConfigureBaseFilter(route, "1", auth1Result);
            ConfigureBaseFilter(route, "2", auth2Result);

            var actual = _RouteFilter.CanCallRoute(route, _Environment.Environment);

            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 17
0
        public void CanCallRoute_Skips_Authorisation_Filters_If_Anonymous_And_AllowAnonymous_Used()
        {
            var route      = Route_Tests.CreateRoute <Controller>(nameof(Controller.OneAuthFilterAllowAnonymous));
            var authFilter = ConfigureBaseFilter(route, "", false);

            _Environment.SetRequestPrincipal(null);

            var actual = _RouteFilter.CanCallRoute(route, _Environment.Environment);

            Assert.IsTrue(actual);
            Assert.IsNull(authFilter.OwinEnvironment);
        }
Exemplo n.º 18
0
        public void CallRoute_Can_Call_Void_Routes()
        {
            var route           = Route_Tests.CreateRoute <Controller>(nameof(Controller.Route_3));
            var routeParameters = Route_Tests.CreateRouteParameters(41);

            var outcome = _RouteCaller.CallRoute(_Environment.Environment, route, routeParameters);

            Assert.IsNull(outcome);
            Assert.IsNotNull(_ControllerInstance);
            Assert.AreEqual(1, _ControllerInstance.Route_3_CallCount);
            Assert.AreEqual(41, _ControllerInstance.Route_3_LastInput);
        }
Exemplo n.º 19
0
        public void BuildRouteParameters_Rejects_Call_If_Route_Was_Not_Initialised()
        {
            var initialisedRoute   = Route_Tests.CreateRoute(typeof(ApiEntityWithIDController), nameof(ApiEntityWithIDController.Method));
            var uninitialisedRoute = Route_Tests.CreateRoute(typeof(ApiEntityWithOptionalIDController), nameof(ApiEntityWithOptionalIDController.Method));

            _RouteMapper.Initialise(new Route[] { initialisedRoute });

            _Environment.SetRequestPath("/api/entity/1");
            var parameters = _RouteMapper.BuildRouteParameters(uninitialisedRoute, _Environment.Environment);

            Assert.IsFalse(parameters.IsValid);
        }
Exemplo n.º 20
0
        public void CallRoute_Creates_Instance_Of_Controller_And_Calls_Route()
        {
            var route           = Route_Tests.CreateRoute <Controller>(nameof(Controller.Route_1));
            var routeParameters = Route_Tests.CreateRouteParameters(41);

            var outcome = _RouteCaller.CallRoute(_Environment.Environment, route, routeParameters);

            Assert.IsNotNull(_ControllerInstance);
            Assert.AreEqual(1, _ControllerInstance.Route_1_CallCount);
            Assert.AreEqual(41, _ControllerInstance.Route_1_LastInput);
            Assert.AreEqual(42, outcome);
        }
Exemplo n.º 21
0
        public void CallRoute_Can_Call_Static_Routes()
        {
            var route           = Route_Tests.CreateRoute <Controller>(nameof(Controller.Route_2));
            var routeParameters = Route_Tests.CreateRouteParameters(_Environment.Environment, 32);

            var outcome = _RouteCaller.CallRoute(_Environment.Environment, route, routeParameters);

            Assert.IsNull(_ControllerInstance);
            Assert.AreEqual(1, Controller.Route_2_CallCount);
            Assert.AreEqual(32, Controller.Route_2_LastInput);
            Assert.AreEqual(64, outcome);
        }
Exemplo n.º 22
0
        public void BuildRouteParameters_Rejects_Attempt_To_Build_From_Form_Body_With_Bad_Encoding()
        {
            var route = Route_Tests.CreateRoute(typeof(FormBodyModelController), nameof(FormBodyModelController.PostStringModel));

            _RouteMapper.Initialise(new Route[] { route });
            _Environment.RequestMethod = "POST";
            _Environment.SetRequestPath(route.PathParts[0].Part);
            _Environment.SetRequestBody("StringValue=Ab", contentType: "application/x-www-form-urlencoded; charset=who-knows");

            var parameters = _RouteMapper.BuildRouteParameters(route, _Environment.Environment);

            Assert.IsFalse(parameters.IsValid);
        }
Exemplo n.º 23
0
        public void BuildRouteParameters_Can_Inject_Owin_Environment_To_Static_Route_Method()
        {
            var route = Route_Tests.CreateRoute(typeof(OwinEnvController), nameof(OwinEnvController.JustEnvironment));

            _RouteMapper.Initialise(new Route[] { route });

            _Environment.SetRequestPath(route.PathParts[0].Part);
            var parameters = _RouteMapper.BuildRouteParameters(route, _Environment.Environment);

            Assert.IsTrue(parameters.IsValid);
            Assert.AreEqual(1, parameters.Parameters.Length);
            Assert.AreSame(_Environment.Environment, parameters.Parameters[0]);
        }
Exemplo n.º 24
0
        public void BuildRouteParameters_Will_Use_Defaults_If_No_Value_Is_Supplied()
        {
            var route = Route_Tests.CreateRoute(typeof(DefaultsController), nameof(DefaultsController.DefaultedRoute));

            _RouteMapper.Initialise(new Route[] { route });

            _Environment.SetRequestPath(route.PathParts[0].Part);
            var parameters = _RouteMapper.BuildRouteParameters(route, _Environment.Environment);

            Assert.IsTrue(parameters.IsValid);
            Assert.AreEqual(1, parameters.Parameters.Length);
            Assert.AreEqual(-1, parameters.Parameters[0]);
        }
Exemplo n.º 25
0
        public void ReturnJsonObject_Will_Write_Body_For_Void_Routes()
        {
            var route = Route_Tests.CreateRoute <Controller>(nameof(Controller.VoidMethod));

            _Environment.Environment[WebApiEnvironmentKey.Route] = route;
            var value = 7;

            _Responder.ReturnJsonObject(_Environment.Environment, value);

            Assert.AreEqual("application/json; charset=utf-8", _Environment.ResponseHeadersDictionary["Content-Type"]);
            Assert.AreEqual("1", _Environment.ResponseHeadersDictionary["Content-Length"]);
            Assert.AreEqual("7", _Environment.ResponseBodyText);
        }
Exemplo n.º 26
0
        public void BuildRouteParameters_Uses_Default_If_Optional_Path_Part_Is_Missing()
        {
            var route = Route_Tests.CreateRoute(typeof(ApiEntityWithOptionalIDController), nameof(ApiEntityWithOptionalIDController.Method));

            _RouteMapper.Initialise(new Route[] { route });

            _Environment.SetRequestPath("/api/entity");
            var parameters = _RouteMapper.BuildRouteParameters(route, _Environment.Environment);

            Assert.IsTrue(parameters.IsValid);
            Assert.AreEqual(1, parameters.Parameters.Length);
            Assert.IsInstanceOfType(parameters.Parameters[0], typeof(int));
            Assert.AreEqual(-1, (int)parameters.Parameters[0]);
        }
Exemplo n.º 27
0
        public void ReturnJsonObject_Uses_Formatter_From_Route()
        {
            var resolver = TypeFormatterResolverCache.Find(new Guid_UpperNoHyphens_Formatter());
            var route    = Route_Tests.CreateRoute <Controller>(nameof(Controller.IntMethod), formatterResolver: resolver);

            _Environment.Environment[WebApiEnvironmentKey.Route] = route;
            var guid = Guid.Parse("c151f1a8-d235-4f28-8c0d-5521a768be9e");

            _Responder.ReturnJsonObject(_Environment.Environment, guid);

            var actual = _Environment.ResponseBodyText;

            Assert.AreEqual("\"C151F1A8D2354F288C0D5521A768BE9E\"", actual);
        }
Exemplo n.º 28
0
        public void BuildRouteParameters_Extracts_Values_From_Path_Parts()
        {
            var route = Route_Tests.CreateRoute(typeof(ApiEntityWithIDController), nameof(ApiEntityWithIDController.Method));

            _RouteMapper.Initialise(new Route[] { route });

            _Environment.SetRequestPath("/api/entity/12");
            var parameters = _RouteMapper.BuildRouteParameters(route, _Environment.Environment);

            Assert.IsTrue(parameters.IsValid);
            Assert.AreEqual(1, parameters.Parameters.Length);
            Assert.IsInstanceOfType(parameters.Parameters[0], typeof(int));
            Assert.AreEqual(12, (int)parameters.Parameters[0]);
        }
Exemplo n.º 29
0
        public void BuildRouteParameters_Can_Fill_Multiple_Path_Part_Parameters()
        {
            var route = Route_Tests.CreateRoute(typeof(MultipleParameterController), nameof(MultipleParameterController.Method));

            _RouteMapper.Initialise(new Route[] { route });

            _Environment.SetRequestPath(new string[] { route.PathParts[0].Part, "string-value", "55" });
            var parameters = _RouteMapper.BuildRouteParameters(route, _Environment.Environment);

            Assert.IsTrue(parameters.IsValid);
            Assert.AreEqual(2, parameters.Parameters.Length);
            Assert.AreEqual("string-value", parameters.Parameters[0]);
            Assert.AreEqual(55, parameters.Parameters[1]);
        }
Exemplo n.º 30
0
        public void CanCallRoute_Always_Calls_Other_Filter_If_Authentication_Skipped()
        {
            var route       = Route_Tests.CreateRoute <Controller>(nameof(Controller.OneAuthOneOtherFilterAllowAnonymous));
            var authFilter  = ConfigureBaseFilter(route, "A1", false);
            var otherFilter = ConfigureBaseFilter(route, "O1", false);

            _Environment.SetRequestPrincipal(null);

            var actual = _RouteFilter.CanCallRoute(route, _Environment.Environment);

            Assert.IsFalse(actual);
            Assert.IsNull(authFilter.OwinEnvironment);
            Assert.IsNotNull(otherFilter.OwinEnvironment);
        }