Exemplo n.º 1
0
        public void GetMatchingMethod_ListParam_Match_Snake_Case(string parameterNameCase)
        {
            DefaultRequestMatcher matcher = this.GetMatcher();

            IEnumerable <KeyValuePair <string, RpcParameterType> > parameters = new[]
            {
                new KeyValuePair <string, RpcParameterType>(parameterNameCase, RpcParameterType.String)
            };

            string        methodName       = nameof(MethodMatcherController.SnakeCaseParams);
            var           requestSignature = RpcRequestSignature.Create(methodName, parameters);
            RpcMethodInfo methodInfo       = matcher.GetMatchingMethod(requestSignature);


            Assert.NotNull(methodInfo);
            MethodInfo expectedMethodInfo = typeof(MethodMatcherController).GetMethod(methodName) !;

            Assert.Equal(expectedMethodInfo, methodInfo.MethodInfo);
            Assert.Single(methodInfo.Parameters);

            Assert.False(methodInfo.Parameters[0].IsOptional);
            Assert.Equal(typeof(string), methodInfo.Parameters[0].RawType);
            Assert.Equal(RpcParameterType.String, methodInfo.Parameters[0].Type);
            Assert.True(RpcUtil.NamesMatch(methodInfo.Parameters[0].Name, parameterNameCase));
        }
Exemplo n.º 2
0
        public void CompareBody(string recordedEncoding, string recordedBody, string encoding, string body, bool compare, bool found)
        {
            //arrange
            var matcher = new DefaultRequestMatcher()
            {
                CompareBody = compare
            };
            var recordedRequest = new HttpRequest {
                Uri    = "https://example.com",
                Method = "POST",
                Body   = new HttpBody {
                    Encoding = recordedEncoding,
                    String   = recordedBody
                }
            };
            var request = new HttpRequest {
                Uri    = "https://example.com",
                Method = "POST",
                Body   = new HttpBody {
                    Encoding = encoding,
                    String   = body
                }
            };

            //act
            var result = matcher.FindMatch(new[] { new HttpInteraction {
                                                       Request = recordedRequest
                                                   } }, request);

            //assert
            Assert.Equal(found, result != null);
        }
Exemplo n.º 3
0
        public void CompareHeaders(Dictionary <string, List <string> > recordedHeaders, Dictionary <string, List <string> > headers, bool compare, bool found)
        {
            //arrange
            var matcher = new DefaultRequestMatcher()
            {
                CompareHeaders = compare
            };
            var recordedRequest = new HttpRequest
            {
                Uri     = "https://example.com",
                Method  = "GET",
                Headers = recordedHeaders
            };
            var request = new HttpRequest
            {
                Uri     = "https://example.com",
                Method  = "GET",
                Headers = headers
            };

            //act
            var result = matcher.FindMatch(new[] { new HttpInteraction {
                                                       Request = recordedRequest
                                                   } }, request);

            //assert
            Assert.Equal(found, result != null);
        }
Exemplo n.º 4
0
        public void GetMatchingMethod_SimpleMulitParam_DictMatch()
        {
            DefaultRequestMatcher matcher = this.GetMatcher();

            var parameters = new Dictionary <string, RpcParameterType>
            {
                { "a", RpcParameterType.Number },
                { "b", RpcParameterType.Boolean },
                { "c", RpcParameterType.String },
                { "d", RpcParameterType.Object },
                { "e", RpcParameterType.Null }
            };
            string        methodName       = nameof(MethodMatcherController.SimpleMulitParam);
            var           requestSignature = RpcRequestSignature.Create(methodName, parameters);
            RpcMethodInfo methodInfo       = matcher.GetMatchingMethod(requestSignature);


            Assert.NotNull(methodInfo);
            MethodInfo expectedMethodInfo = typeof(MethodMatcherController).GetMethod(methodName) !;

            Assert.Equal(expectedMethodInfo, methodInfo.MethodInfo);
            Assert.Equal(5, methodInfo.Parameters.Length);

            Assert.False(methodInfo.Parameters[0].IsOptional);
            Assert.Equal(typeof(int), methodInfo.Parameters[0].RawType);
            Assert.Equal(RpcParameterType.Number, methodInfo.Parameters[0].Type);
            Assert.Equal("a", methodInfo.Parameters[0].Name);

            Assert.False(methodInfo.Parameters[1].IsOptional);
            Assert.Equal(typeof(bool), methodInfo.Parameters[1].RawType);
            Assert.Equal(RpcParameterType.Boolean, methodInfo.Parameters[1].Type);
            Assert.Equal("b", methodInfo.Parameters[1].Name);

            Assert.False(methodInfo.Parameters[2].IsOptional);
            Assert.Equal(typeof(string), methodInfo.Parameters[2].RawType);
            Assert.Equal(RpcParameterType.String, methodInfo.Parameters[2].Type);
            Assert.Equal("c", methodInfo.Parameters[2].Name);

            Assert.False(methodInfo.Parameters[3].IsOptional);
            Assert.Equal(typeof(object), methodInfo.Parameters[3].RawType);
            Assert.Equal(RpcParameterType.Object, methodInfo.Parameters[3].Type);
            Assert.Equal("d", methodInfo.Parameters[3].Name);

            Assert.True(methodInfo.Parameters[4].IsOptional);
            Assert.Equal(typeof(int?), methodInfo.Parameters[4].RawType);
            Assert.Equal(RpcParameterType.Object, methodInfo.Parameters[4].Type);
            Assert.Equal("e", methodInfo.Parameters[4].Name);
        }
Exemplo n.º 5
0
        public void GetMatchingMethod_GuidParameter_Match()
        {
            string methodName = nameof(MethodMatcherController.GuidTypeMethod);

            DefaultRequestMatcher matcher   = this.GetMatcher(path: typeof(MethodMatcherController).GetTypeInfo().Name);
            var            requestSignature = RpcRequestSignature.Create(methodName, new[] { RpcParameterType.String });
            IRpcMethodInfo methodInfo       = matcher.GetMatchingMethod(requestSignature);

            Assert.NotNull(methodInfo);
            Assert.Equal(methodName, methodInfo.Name);
            Assert.Single(methodInfo.Parameters);
            Assert.False(methodInfo.Parameters[0].IsOptional);
            Assert.Equal(typeof(Guid), methodInfo.Parameters[0].RawType);
            Assert.Equal(RpcParameterType.Object, methodInfo.Parameters[0].Type);
            Assert.Equal("guid", methodInfo.Parameters[0].Name);
        }
Exemplo n.º 6
0
        public void GetMatchingMethod_WithRpcRoute()
        {
            string methodName = nameof(MethodMatcherController.GuidTypeMethod);
            RpcRequestSignature requestSignature = RpcRequestSignature.Create(methodName, new[] { RpcParameterType.String });

            DefaultRequestMatcher path1Matcher = this.GetMatcher(path: typeof(MethodMatcherController).GetTypeInfo().Name);
            IRpcMethodInfo        path1Match   = path1Matcher.GetMatchingMethod(requestSignature);

            Assert.NotNull(path1Match);


            DefaultRequestMatcher path2Matcher = this.GetMatcher(path: typeof(MethodMatcherDuplicatesController).GetTypeInfo().Name);
            IRpcMethodInfo        path2Match   = path2Matcher.GetMatchingMethod(requestSignature);

            Assert.NotNull(path2Match);
            Assert.NotSame(path1Match, path2Match);
        }
Exemplo n.º 7
0
        public void IgnoreHeaders()
        {
            //arrange
            var matcher = new DefaultRequestMatcher {
                CompareHeaders = true
            };

            matcher.IgnoreHeaders.Add("Authorization");

            var recordedRequest = new HttpRequest
            {
                Uri     = "https://example.com",
                Method  = "GET",
                Headers = new Dictionary <string, List <string> > {
                    ["Authorization"] = new List <string> {
                        "Bearer Token1"
                    },
                    ["Accept"] = new List <string> {
                        "text/plain"
                    }
                }
            };
            var request = new HttpRequest
            {
                Uri     = "https://example.com",
                Method  = "GET",
                Headers = new Dictionary <string, List <string> >
                {
                    ["Authorization"] = new List <string> {
                        "Bearer Token2"
                    },
                    ["Accept"] = new List <string> {
                        "text/plain"
                    }
                }
            };

            //act
            var result = matcher.FindMatch(new[] { new HttpInteraction {
                                                       Request = recordedRequest
                                                   } }, request);

            //assert
            Assert.NotNull(result);
        }
Exemplo n.º 8
0
        private DefaultRpcInvoker GetInvoker()
        {
            var authorizationService = new Mock <IAuthorizationService>();
            var policyProvider       = new Mock <IAuthorizationPolicyProvider>();
            var logger  = new Mock <ILogger <DefaultRpcInvoker> >();
            var options = new Mock <IOptions <RpcServerConfiguration> >();
            var logger2 = new Mock <ILogger <DefaultRequestMatcher> >();
            //TODO mock and make other tests for this
            var rpcRequestMatcher = new DefaultRequestMatcher(logger2.Object, options.Object);
            var config            = new RpcServerConfiguration();

            config.ShowServerExceptions = true;
            options
            .SetupGet(o => o.Value)
            .Returns(config);

            return(new DefaultRpcInvoker(authorizationService.Object, policyProvider.Object, logger.Object, options.Object, rpcRequestMatcher));
        }
Exemplo n.º 9
0
        public void GetMatchingMethod_ListParam_Match()
        {
            DefaultRequestMatcher matcher = this.GetMatcher(path: typeof(MethodMatcherController).GetTypeInfo().Name);

            RpcParameterType[] parameters   = new[] { RpcParameterType.Object };
            string             methodName   = nameof(MethodMatcherController.List);
            var            requestSignature = RpcRequestSignature.Create(methodName, parameters);
            IRpcMethodInfo methodInfo       = matcher.GetMatchingMethod(requestSignature);


            Assert.NotNull(methodInfo);
            Assert.Equal(methodName, methodInfo.Name);
            Assert.Single(methodInfo.Parameters);

            Assert.False(methodInfo.Parameters[0].IsOptional);
            Assert.Equal(typeof(List <string>), methodInfo.Parameters[0].RawType);
            Assert.Equal(RpcParameterType.Object, methodInfo.Parameters[0].Type);
            Assert.Equal("values", methodInfo.Parameters[0].Name);
        }
Exemplo n.º 10
0
        public void CompareHttpMethod(string recordedMethod, string method, bool found)
        {
            //arrange
            var matcher         = new DefaultRequestMatcher();
            var recordedRequest = new HttpRequest {
                Uri = "https://example.com", Method = recordedMethod
            };
            var request = new HttpRequest {
                Uri = "https://example.com", Method = method
            };

            //act
            var result = matcher.FindMatch(new[] { new HttpInteraction {
                                                       Request = recordedRequest
                                                   } }, request);

            //assert
            Assert.Equal(found, result != null);
        }
Exemplo n.º 11
0
        public void CompareUri(string recordedUri, string uri, bool found)
        {
            //arrange
            var matcher         = new DefaultRequestMatcher();
            var recordedRequest = new HttpRequest {
                Uri = recordedUri
            };
            var request = new HttpRequest {
                Uri = uri
            };

            //act
            var result = matcher.FindMatch(new[] { new HttpInteraction {
                                                       Request = recordedRequest
                                                   } }, request);

            //assert
            Assert.Equal(found, result != null);
        }
Exemplo n.º 12
0
        public void GetMatchingMethod_CulturallyInvariantComparison()
        {
            DefaultRequestMatcher matcher = this.GetMatcher(path: typeof(MethodMatcherController).GetTypeInfo().Name);

            RpcParameterType[] parameters = Array.Empty <RpcParameterType>();
            string             methodName = nameof(MethodMatcherController.IsLunchTime);
            // Use lowercase version of method name when making request.
            var methodNameLower  = methodName.ToLowerInvariant();
            var requestSignature = RpcRequestSignature.Create(methodNameLower, parameters);
            var previousCulture  = System.Globalization.CultureInfo.CurrentCulture;

            // Switch to a culture that would result in lowercasing 'I' to
            // U+0131, if not done with invariant culture.
            System.Globalization.CultureInfo.CurrentCulture = new System.Globalization.CultureInfo("az");
            IRpcMethodInfo methodInfo = matcher.GetMatchingMethod(requestSignature);

            Assert.NotNull(methodInfo);
            Assert.Equal(methodName, methodInfo.Name);
            System.Globalization.CultureInfo.CurrentCulture = previousCulture;
        }
Exemplo n.º 13
0
        public void GetMatchingMethod_SimpleMulitParam_ListMatch()
        {
            DefaultRequestMatcher matcher = this.GetMatcher(path: typeof(MethodMatcherController).GetTypeInfo().Name);

            RpcParameterType[] parameters   = new[] { RpcParameterType.Number, RpcParameterType.Boolean, RpcParameterType.String, RpcParameterType.Object, RpcParameterType.Null };
            string             methodName   = nameof(MethodMatcherController.SimpleMulitParam);
            var            requestSignature = RpcRequestSignature.Create(methodName, parameters);
            IRpcMethodInfo methodInfo       = matcher.GetMatchingMethod(requestSignature);


            Assert.NotNull(methodInfo);
            Assert.Equal(methodName, methodInfo.Name);
            Assert.Equal(5, methodInfo.Parameters.Count);

            Assert.False(methodInfo.Parameters[0].IsOptional);
            Assert.Equal(typeof(int), methodInfo.Parameters[0].RawType);
            Assert.Equal(RpcParameterType.Number, methodInfo.Parameters[0].Type);
            Assert.Equal("a", methodInfo.Parameters[0].Name);

            Assert.False(methodInfo.Parameters[1].IsOptional);
            Assert.Equal(typeof(bool), methodInfo.Parameters[1].RawType);
            Assert.Equal(RpcParameterType.Boolean, methodInfo.Parameters[1].Type);
            Assert.Equal("b", methodInfo.Parameters[1].Name);

            Assert.False(methodInfo.Parameters[2].IsOptional);
            Assert.Equal(typeof(string), methodInfo.Parameters[2].RawType);
            Assert.Equal(RpcParameterType.String, methodInfo.Parameters[2].Type);
            Assert.Equal("c", methodInfo.Parameters[2].Name);

            Assert.False(methodInfo.Parameters[3].IsOptional);
            Assert.Equal(typeof(object), methodInfo.Parameters[3].RawType);
            Assert.Equal(RpcParameterType.Object, methodInfo.Parameters[3].Type);
            Assert.Equal("d", methodInfo.Parameters[3].Name);

            Assert.True(methodInfo.Parameters[4].IsOptional);
            Assert.Equal(typeof(int?), methodInfo.Parameters[4].RawType);
            Assert.Equal(RpcParameterType.Object, methodInfo.Parameters[4].Type);
            Assert.Equal("e", methodInfo.Parameters[4].Name);
        }
Exemplo n.º 14
0
        private DefaultRpcInvoker GetInvoker(MethodInfo?methodInfo, RpcPath?path = null)

        {
            var         logger         = new Mock <ILogger <DefaultRpcInvoker> >(MockBehavior.Loose);
            var         options        = new Mock <IOptions <RpcServerConfiguration> >(MockBehavior.Strict);
            var         matcher        = new Mock <IRpcRequestMatcher>(MockBehavior.Strict);
            var         accessor       = new Mock <IRpcContextAccessor>(MockBehavior.Strict);
            IRpcContext requestContext = this.GetRouteContext(path);

            accessor
            .SetupGet(a => a.Value)
            .Returns(requestContext);
            Moq.Language.Flow.ISetup <IRpcRequestMatcher, RpcMethodInfo> matcherSetup = matcher
                                                                                        .Setup(m => m.GetMatchingMethod(It.IsAny <RpcRequestSignature>()));
            if (methodInfo != null)
            {
                //TODO better way of getting this for unit tests?
                RpcMethodInfo method = DefaultRequestMatcher.BuildMethodInfo(methodInfo);
                matcherSetup.Returns(method);
            }
            else
            {
                matcherSetup.Throws(new RpcException(RpcErrorCode.MethodNotFound, "Method not found"));
            }
            var config = new RpcServerConfiguration();

            config.ShowServerExceptions = true;
            options
            .SetupGet(o => o.Value)
            .Returns(config);
            var authHandler = new Mock <IRpcAuthorizationHandler>(MockBehavior.Strict);

            authHandler
            .Setup(h => h.IsAuthorizedAsync(It.IsAny <MethodInfo>()))
            .Returns(Task.FromResult(true));

            return(new DefaultRpcInvoker(logger.Object, options.Object, matcher.Object, accessor.Object, authHandler.Object));
        }