private UriTemplateMatch getPathMatches(String pathTemplate, Uri requestPath)
        {
            Uri prefix = new Uri("/", UriKind.Relative);

            UriTemplate template = new UriTemplate(pathTemplate, true);

            UriTemplateMatch results = template.Match(prefix, requestPath);

            return(results);
        }
        public void TestCompoundPathSegmentExpansionCollectionVariableAndPrefixVariableReference()
        {
            string      template    = "{/list*,path:4}";
            UriTemplate uriTemplate = new UriTemplate(template);
            Uri         uri         = uriTemplate.BindByName(Variables);

            Assert.AreEqual("/red/green/blue/%2Ffoo", uri.OriginalString);

            UriTemplateMatch match = uriTemplate.Match(uri, new[] { "list" }, new[] { "keys" });

            Assert.IsNotNull(match);
            CollectionAssert.AreEqual((ICollection)Variables["list"], (ICollection)match.Bindings["list"].Value);
            Assert.AreEqual(((string)Variables["path"]).Substring(0, 4), match.Bindings["path"].Value);

            match = uriTemplate.Match(uri, RequiredVariables, new[] { "list" }, new[] { "keys" });
            Assert.IsNotNull(match);
            CollectionAssert.AreEqual((ICollection)Variables["list"], (ICollection)match.Bindings["list"].Value);
            Assert.AreEqual(((string)Variables["path"]).Substring(0, 4), match.Bindings["path"].Value);
        }
示例#3
0
        public void a_url_matching_result_in_the_query_value_variable_being_set()
        {
            var table = new UriTemplate("/test?query={queryValue}");
            UriTemplateMatch match =
                table.Match(new Uri("http://localhost"), new Uri("http://localhost/test?query=search"));

            match.ShouldNotBeNull();

            match.QueryStringVariables["queryValue"].ShouldBe("search");
        }
        public void TestSimpleExpansionMultipleVariablesInQuery()
        {
            string      template    = "map?{x,y}";
            UriTemplate uriTemplate = new UriTemplate(template);
            Uri         uri         = uriTemplate.BindByName(Variables);

            Assert.AreEqual("map?1024,768", uri.OriginalString);

            UriTemplateMatch match = uriTemplate.Match(uri);

            Assert.IsNotNull(match);
            Assert.AreEqual(Variables["x"], match.Bindings["x"].Value);
            Assert.AreEqual(Variables["y"], match.Bindings["y"].Value);

            match = uriTemplate.Match(uri, RequiredVariables);
            Assert.IsNotNull(match);
            Assert.AreEqual(Variables["x"], match.Bindings["x"].Value);
            Assert.AreEqual(Variables["y"], match.Bindings["y"].Value);
        }
示例#5
0
        public void MatchWildcard2()
        {
            var t = new UriTemplate("*");
            var m = t.Match(new Uri("http://localhost"), new Uri("http://localhost/hoge/ppp"));

            Assert.IsNotNull(m, "#0");
            Assert.IsEmpty(m.QueryParameters, "#1.0");
            Assert.AreEqual("hoge", m.WildcardPathSegments [0], "#2");
            Assert.AreEqual("ppp", m.WildcardPathSegments [1], "#3");
        }
        public void TestPathParameterExpansionMultipleVariables()
        {
            string      template    = "{;x,y}";
            UriTemplate uriTemplate = new UriTemplate(template);
            Uri         uri         = uriTemplate.BindByName(Variables);

            Assert.AreEqual(";x=1024;y=768", uri.OriginalString);

            UriTemplateMatch match = uriTemplate.Match(uri);

            Assert.IsNotNull(match);
            Assert.AreEqual(Variables["x"], match.Bindings["x"].Value);
            Assert.AreEqual(Variables["y"], match.Bindings["y"].Value);

            match = uriTemplate.Match(uri, RequiredVariables);
            Assert.IsNotNull(match);
            Assert.AreEqual(Variables["x"], match.Bindings["x"].Value);
            Assert.AreEqual(Variables["y"], match.Bindings["y"].Value);
        }
        public void TestFragmentExpansionMultipleVariablesAndLiteral()
        {
            string      template    = "{#path,x}/here";
            UriTemplate uriTemplate = new UriTemplate(template);
            Uri         uri         = uriTemplate.BindByName(Variables);

            Assert.AreEqual("#/foo/bar,1024/here", uri.OriginalString);

            UriTemplateMatch match = uriTemplate.Match(uri);

            Assert.IsNotNull(match);
            Assert.AreEqual(Variables["path"], match.Bindings["path"].Value);
            Assert.AreEqual(Variables["x"], match.Bindings["x"].Value);

            match = uriTemplate.Match(uri, RequiredVariables);
            Assert.IsNotNull(match);
            Assert.AreEqual(Variables["path"], match.Bindings["path"].Value);
            Assert.AreEqual(Variables["x"], match.Bindings["x"].Value);
        }
        public void TestPathSegmentExpansionMultipleVariables()
        {
            string      template    = "{/var,x}/here";
            UriTemplate uriTemplate = new UriTemplate(template);
            Uri         uri         = uriTemplate.BindByName(Variables);

            Assert.AreEqual("/value/1024/here", uri.OriginalString);

            UriTemplateMatch match = uriTemplate.Match(uri);

            Assert.IsNotNull(match);
            Assert.AreEqual(Variables["var"], match.Bindings["var"].Value);
            Assert.AreEqual(Variables["x"], match.Bindings["x"].Value);

            match = uriTemplate.Match(uri, RequiredVariables);
            Assert.IsNotNull(match);
            Assert.AreEqual(Variables["var"], match.Bindings["var"].Value);
            Assert.AreEqual(Variables["x"], match.Bindings["x"].Value);
        }
        public void the_query_parameters_should_be_case_insensitive()
        {
            var template = new UriTemplate("/test?page={page}");

            var match = template.Match(new Uri("http://localhost"), new Uri("http://localhost/test?pAgE=2"));

            match.ShouldNotBeNull();
            match.QueryStringVariables.Count.ShouldBe(1);
            match.QueryStringVariables["PAGE"].ShouldBe("2");
        }
        public void TestQueryExpansionWithMultipleDoubleVariable()
        {
            string      template    = "/loc{?long,lat}";
            UriTemplate uriTemplate = new UriTemplate(template);
            Uri         uri         = uriTemplate.BindByName(Variables1);

            Assert.AreEqual("/loc?long=37.76&lat=-122.427", uri.OriginalString);

            UriTemplateMatch match = uriTemplate.Match(uri, new[] { "fields", "geocode" }, new[] { "assoc_special_chars" });

            Assert.IsNotNull(match);
            Assert.AreEqual(Variables1["long"].ToString(), match.Bindings["long"].Value);
            Assert.AreEqual(Variables1["lat"].ToString(), match.Bindings["lat"].Value);

            match = uriTemplate.Match(uri, RequiredVariables1, new[] { "fields", "geocode" }, new[] { "assoc_special_chars" });
            Assert.IsNotNull(match);
            Assert.AreEqual(Variables1["long"].ToString(), match.Bindings["long"].Value);
            Assert.AreEqual(Variables1["lat"].ToString(), match.Bindings["lat"].Value);
        }
示例#11
0
		public void IgnoreTrailingSlash ()
		{
			var t = new UriTemplate ("/{foo}/{bar}", true);
			var n = new NameValueCollection ();
			Uri baseUri = new Uri ("http://localhost/");
			Assert.IsNotNull (t.Match (baseUri, new Uri ("http://localhost/v1/v2/")), "#1");

			t = new UriTemplate ("/{foo}/{bar}", false);
			Assert.IsNull (t.Match (baseUri, new Uri ("http://localhost/v1/v2/")), "#2");
		}
示例#12
0
            public virtual void DeserializeRequest(Message message, object [] parameters)
            {
                if (parameters == null)
                {
                    throw new ArgumentNullException("parameters");
                }
                CheckMessageVersion(message.Version);

                IncomingWebRequestContext iwc = null;

                if (OperationContext.Current != null)
                {
                    OperationContext.Current.Extensions.Add(new WebOperationContext(OperationContext.Current));
                    iwc = WebOperationContext.Current.IncomingRequest;
                }

                var wp  = message.Properties [WebBodyFormatMessageProperty.Name] as WebBodyFormatMessageProperty;
                var fmt = wp != null ? wp.Format : WebContentFormat.Xml;

                Uri to = message.Headers.To;
                UriTemplateMatch match = UriTemplate.Match(Endpoint.Address.Uri, to);

                if (match == null)
                {
                    // not sure if it could happen
                    throw new SystemException(String.Format("INTERNAL ERROR: UriTemplate does not match with the request: {0} / {1}", UriTemplate, to));
                }
                if (iwc != null)
                {
                    iwc.UriTemplateMatch = match;
                }

                MessageDescription md = GetMessageDescription(MessageDirection.Input);

                for (int i = 0; i < parameters.Length; i++)
                {
                    var    p    = md.Body.Parts [i];
                    string name = p.Name.ToUpperInvariant();
                    var    str  = match.BoundVariables [name];
                    if (str != null)
                    {
                        parameters [i] = Converter.ConvertStringToValue(str, p.Type);
                    }
                    else if (fmt == WebContentFormat.Raw && p.Type == typeof(Stream))
                    {
                        var rmsg = (RawMessage)message;
                        parameters [i] = rmsg.Stream;
                    }
                    else
                    {
                        var serializer = GetSerializer(fmt, IsRequestBodyWrapped, p);
                        parameters [i] = DeserializeObject(serializer, message, md, IsRequestBodyWrapped, fmt);
                    }
                }
            }
        public void a_parameter_different_by_last_letter_to_query_parameters_should_not_match()
        {
            var template = new UriTemplate("/test?query1={test}&query2={test2}");
            var match    = template.Match(new Uri("http://localhost"),
                                          new Uri("http://localhost/test?query1=test1&query3=test2"));

            match.ShouldNotBeNull();
            match.PathSegmentVariables.Count.ShouldBe(0);
            match.QueryStringVariables.Count.ShouldBe(1);
            match.QueryParameters.Count.ShouldBe(2);
        }
        public void a_url_matching_three_query_string_parameters_will_match()
        {
            var table = new UriTemplate("/test?q={searchTerm}&p={pageNumber}&s={pageSize}");
            UriTemplateMatch match =
                table.Match(new Uri("http://localhost"), new Uri("http://localhost/test?q=&p=1&s=10"));

            match.ShouldNotBeNull();
            match.QueryStringVariables["searchTerm"].ShouldBe(string.Empty);
            match.QueryStringVariables["pageNumber"].ShouldBe("1");
            match.QueryStringVariables["pageSize"].ShouldBe("10");
        }
 public void Experiment()
 {
     var template = new UriTemplate("devices/{deviceId}/messages/outbound/{*subTopic}");
     var baseUri  = new Uri("http://whatever");
     Uri bound    = template.BindByName(baseUri, new Dictionary <string, string>
     {
         { "deviceId", "VINno" },
         { "SubTopic", "toptop/toptoptop" },
     });
     var t2 = new UriTemplate("devices/{deviceId}/messages/log/{level=info}/{subject=n%2Fa}", true);
     UriTemplateMatch match = t2.Match(baseUri, new Uri("http://whatever/devices/VINno/messages/log", UriKind.Absolute));
 }
示例#16
0
        public void bound_query_params_should_be_escaped()
        {
            const string searchTerm    = "test $&+,/ test";
            var          encodedString = HttpUtility.UrlEncode(searchTerm);
            var          template      = new UriTemplate("/test?q={searchTerm}");

            var match = template.Match(new Uri("http://localhost"), new Uri("http://localhost/test?q=" + encodedString));

            match.ShouldNotBeNull();
            match.BoundQueryParameters.Count.ShouldBe(1);
            match.BoundQueryParameters["searchTerm"].ShouldBe(searchTerm);
        }
        public void TestQueryContinuationExpansionMultipleVariables()
        {
            string      template    = "{&x,y,empty}";
            UriTemplate uriTemplate = new UriTemplate(template);
            Uri         uri         = uriTemplate.BindByName(Variables);

            Assert.AreEqual("&x=1024&y=768&empty=", uri.OriginalString);

            UriTemplateMatch match = uriTemplate.Match(uri);

            Assert.IsNotNull(match);
            Assert.AreEqual(Variables["x"], match.Bindings["x"].Value);
            Assert.AreEqual(Variables["y"], match.Bindings["y"].Value);
            Assert.AreEqual(Variables["empty"], match.Bindings["empty"].Value);

            match = uriTemplate.Match(uri, RequiredVariables);
            Assert.IsNotNull(match);
            Assert.AreEqual(Variables["x"], match.Bindings["x"].Value);
            Assert.AreEqual(Variables["y"], match.Bindings["y"].Value);
            Assert.AreEqual(Variables["empty"], match.Bindings["empty"].Value);
        }
        protected virtual string GetODataPath()
        {
            var routedata = Request.GetRouteData();

            var uriTemplate = new UriTemplate(routedata.Route.RouteTemplate);
            var baseUri = new Uri(Request.RequestUri.Scheme + "://" +
                                  Request.RequestUri.Authority +
                                  Request.GetRequestContext().VirtualPathRoot.TrimEnd('/') + "/");
            var match = uriTemplate.Match(baseUri, Request.RequestUri);
            var path = "/" + String.Join("/", match.WildcardPathSegments);
            return path;
        }
        public void TestReservedExpansionMultipleVariables()
        {
            string      template    = "{+x,hello,y}";
            UriTemplate uriTemplate = new UriTemplate(template);
            Uri         uri         = uriTemplate.BindByName(Variables);

            Assert.AreEqual("1024,Hello%20World!,768", uri.OriginalString);

            UriTemplateMatch match = uriTemplate.Match(uri);

            Assert.IsNotNull(match);
            Assert.AreEqual(Variables["x"], match.Bindings["x"].Value);
            Assert.AreEqual(Variables["hello"], match.Bindings["hello"].Value);
            Assert.AreEqual(Variables["y"], match.Bindings["y"].Value);

            match = uriTemplate.Match(uri, RequiredVariables);
            Assert.IsNotNull(match);
            Assert.AreEqual(Variables["x"], match.Bindings["x"].Value);
            Assert.AreEqual(Variables["hello"], match.Bindings["hello"].Value);
            Assert.AreEqual(Variables["y"], match.Bindings["y"].Value);
        }
示例#20
0
        public void EscapedUriCandidate()
        {
            var candidateUri = new Uri(@"https://*****:*****@"/path1/path2/path3/endprefix");

            var template = new UriTemplate(@"tpath1/{guid}/tpath2/{encodedGuidString}/tpath3");
            var match    = template.Match(matchUri, candidateUri);

            Assert.IsNotNull(match);
            Assert.That(match.BoundVariables ["GUID"] == "guid1");
            Assert.That(match.BoundVariables ["ENCODEDGUIDSTRING"] == "~|~~|~?~|~Path{guid2}~|~");
        }
示例#21
0
		public void MatchWildcard ()
		{
			var t = new UriTemplate ("/hoge/*?p1={foo}");
			var m = t.Match (new Uri ("http://localhost"), new Uri ("http://localhost/hoge/ppp/qqq?p1=v1"));
			Assert.IsNotNull (m, "#0");
			Assert.IsNotNull (m.QueryParameters, "#1.0");
			Assert.AreEqual ("v1", m.QueryParameters ["p1"], "#1");
			Assert.IsNotNull (m.WildcardPathSegments, "#2.0");
			Assert.AreEqual (2, m.WildcardPathSegments.Count, "#2");
			Assert.AreEqual ("ppp", m.WildcardPathSegments [0], "#3");
			Assert.AreEqual ("qqq", m.WildcardPathSegments [1], "#4");
		}
示例#22
0
        static void Main(string[] args)
        {
            // <Snippet0>
            UriTemplate template    = new UriTemplate("weather/{state}/{city}?forecast=today");
            Uri         baseAddress = new Uri("http://localhost");
            Uri         fullUri     = new Uri("http://localhost/weather/WA/Seattle?forecast=today");

            Console.WriteLine("Matching {0} to {1}", template.ToString(), fullUri.ToString());

            // Match a URI to a template
            UriTemplateMatch results = template.Match(baseAddress, fullUri);

            if (results != null)
            {
                // BaseUri
                Console.WriteLine("BaseUri: {0}", results.BaseUri);

                Console.WriteLine("BoundVariables:");
                foreach (string variableName in results.BoundVariables.Keys)
                {
                    Console.WriteLine("    {0}: {1}", variableName, results.BoundVariables[variableName]);
                }

                Console.WriteLine("QueryParameters:");
                foreach (string queryName in results.QueryParameters.Keys)
                {
                    Console.WriteLine("    {0} : {1}", queryName, results.QueryParameters[queryName]);
                }
                Console.WriteLine();

                Console.WriteLine("RelativePathSegments:");
                foreach (string segment in results.RelativePathSegments)
                {
                    Console.WriteLine("     {0}", segment);
                }
                Console.WriteLine();

                Console.WriteLine("RequestUri:");
                Console.WriteLine(results.RequestUri);

                Console.WriteLine("Template:");
                Console.WriteLine(results.Template);

                Console.WriteLine("WildcardPathSegments:");
                foreach (string segment in results.WildcardPathSegments)
                {
                    Console.WriteLine("     {0}", segment);
                }
                Console.WriteLine();
            }
            // </Snippet0>
        }
示例#23
0
        public static void Main()
        {
            Uri prefix = new Uri("http://localhost/");

            //A UriTemplate is a "URI with holes". It describes a set of URI's that
            //are structurally similar. This UriTemplate might be used for organizing
            //weather reports:
            UriTemplate template = new UriTemplate("weather/{state}/{city}");

            //You can convert a UriTemplate into a Uri by filling
            //the holes in the template with parameters.

            //BindByPosition moves left-to-right across the template
            Uri positionalUri = template.BindByPosition(prefix, "Washington", "Redmond");

            Console.WriteLine("Calling BindByPosition...");
            Console.WriteLine(positionalUri);
            Console.WriteLine();

            //BindByName takes a NameValueCollection of parameters.
            //Each parameter gets substituted into the UriTemplate "hole"
            //that has the same name as the parameter.
            NameValueCollection parameters = new NameValueCollection();

            parameters.Add("state", "Washington");
            parameters.Add("city", "Redmond");

            Uri namedUri = template.BindByName(prefix, parameters);

            Console.WriteLine("Calling BindByName...");
            Console.WriteLine(namedUri);
            Console.WriteLine();


            //The inverse operation of Bind is Match(), which extrudes a URI
            //through the template to produce a set of name/value pairs.
            Uri fullUri = new Uri("http://localhost/weather/Washington/Redmond");
            UriTemplateMatch results = template.Match(prefix, fullUri);

            Console.WriteLine(String.Format("Matching {0} to {1}", template.ToString(), fullUri.ToString()));

            if (results != null)
            {
                foreach (string variableName in results.BoundVariables.Keys)
                {
                    Console.WriteLine(String.Format("   {0}: {1}", variableName, results.BoundVariables[variableName]));
                }
            }

            Console.WriteLine("Press any key to terminate");
            Console.ReadLine();
        }
示例#24
0
        public void TestEmptyTemplate()
        {
            string      template    = string.Empty;
            UriTemplate uriTemplate = new UriTemplate(template);
            Uri         uri         = uriTemplate.BindByName(Variables);

            Assert.AreEqual(string.Empty, uri.OriginalString);

            UriTemplateMatch match = uriTemplate.Match(uri);

            Assert.IsNotNull(match);
            Assert.AreEqual(0, match.Bindings.Count);
        }
        public void TestCompoundPathSegmentExpansionCollectionVariable()
        {
            string      template    = "{/id*}";
            UriTemplate uriTemplate = new UriTemplate(template);
            Uri         uri         = uriTemplate.BindByName(Variables2);

            string[] allowed =
            {
                "/person/albums",
                "/albums/person"
            };

            CollectionAssert.Contains(allowed, uri.OriginalString);

            UriTemplateMatch match = uriTemplate.Match(uri, new[] { "id", "fields", "geocode" }, new string[0]);

            Assert.IsNotNull(match);
            CollectionAssert.AreEqual((ICollection)Variables2["id"], (ICollection)match.Bindings["id"].Value);

            match = uriTemplate.Match(uri, RequiredVariables2, new[] { "id", "fields", "geocode" }, new string[0]);
            Assert.IsNotNull(match);
            CollectionAssert.AreEqual((ICollection)Variables2["id"], (ICollection)match.Bindings["id"].Value);
        }
        public void TestCompoundQueryExpansionNumericKeyMapVariable()
        {
            string      template    = "{?german*}";
            UriTemplate uriTemplate = new UriTemplate(template);
            Uri         uri         = uriTemplate.BindByName(Variables4);

            string[] allowed =
            {
                "?11=elf&12=zw%C3%B6lf",
                "?12=zw%C3%B6lf&11=elf"
            };

            CollectionAssert.Contains(allowed, uri.OriginalString);

            UriTemplateMatch match = uriTemplate.Match(uri, new[] { "1337" }, new[] { "german" });

            Assert.IsNotNull(match);
            CollectionAssert.AreEqual((ICollection)Variables4["german"], (ICollection)match.Bindings["german"].Value);

            match = uriTemplate.Match(uri, RequiredVariables4, new[] { "1337" }, new[] { "german" });
            Assert.IsNotNull(match);
            CollectionAssert.AreEqual((ICollection)Variables4["german"], (ICollection)match.Bindings["german"].Value);
        }
示例#27
0
        static void Main(string[] args)
        {
            /*
             * UriTeplate标识统一资源标识符(URI)模版的类,它继承自System.Object
             * 其位于System.ServiceModel程序集中(System.ServiceModel.dll)
             *
             * URI模版,可以定义一组结构相似的URI。模版由两部分组成,即路径和查询。路径由一些列反斜杠(/)分隔的段组成。
             * 每个段可都可以具有文本值,变量值(书写在大括号中({})且被选址为仅与一个段的内容匹配)或必须出现在路径末端的通配符(书写为型号*,与"路径的其余部分"匹配)。
             * 查询表达式可以完全忽略,如果出现表达式,则它指定一组无序的名称/值对,查询表达式的元素可以是文本对(?x=2)
             * 也可以是变量对(?x={val})。不允许使用不成对的值
             */

            UriTemplate template = new UriTemplate("weather/{state}/{city}?forecast={day}");
            Uri         prefix   = new Uri("http://localhost");

            Console.WriteLine("路径片段所有的变量名称:");
            foreach (string name in template.PathSegmentVariableNames)
            {
                Console.WriteLine("     {0}", name);
            }

            Console.WriteLine("查询表达式中的变量名称:");
            foreach (string name in template.QueryValueVariableNames)
            {
                Console.WriteLine("     {0}", name);
            }

            Uri positionaUri = template.BindByPosition(prefix, "Washington", "Redmond", "Today");
            NameValueCollection parameters = new NameValueCollection();

            parameters.Add("state", "Washington");
            parameters.Add("city", "Redmond");
            parameters.Add("day", "Today");
            Uri nameUri = template.BindByName(prefix, parameters);

            Uri fullUri             = new Uri("http://localhost/weather/Washington/Redmond?forecast=today");
            UriTemplateMatch result = template.Match(prefix, fullUri);

            Console.WriteLine("Matching {0} to {1}", template.ToString(), fullUri.ToString());

            if (result != null)
            {
                foreach (string variableName in result.BoundVariables.Keys)
                {
                    Console.WriteLine("     {0}:{1}", variableName, result.BoundVariables[variableName]);
                }
            }

            Console.Read();
        }
示例#28
0
        /// <summary>
        /// Extracts the Guid from the request URI.
        /// </summary>
        /// <param name="baseUri">The base URI.</param>
        /// <param name="requestUri">The request URI.</param>
        /// <returns>The extracted Guid.</returns>
        internal static Guid ExtractGuidFromRequestUri(Uri baseUri, Uri requestUri)
        {
            UriTemplate      template = new UriTemplate("{guid}.rdf");
            UriTemplateMatch results  = template.Match(baseUri, requestUri);

            if (results != null)
            {
                return(new Guid(results.BoundVariables["guid"]));
            }
            else
            {
                return(Guid.Empty);
            }
        }
        public void TestCompoundPathSegmentExpansionWithQueryStringCollectionVariable()
        {
            string      template    = "{/id*}{?fields,token}";
            UriTemplate uriTemplate = new UriTemplate(template);
            Uri         uri         = uriTemplate.BindByName(Variables2);

            string[] allowed =
            {
                "/person/albums?fields=id,name,picture&token=12345",
                "/person/albums?fields=id,picture,name&token=12345",
                "/person/albums?fields=picture,name,id&token=12345",
                "/person/albums?fields=picture,id,name&token=12345",
                "/person/albums?fields=name,picture,id&token=12345",
                "/person/albums?fields=name,id,picture&token=12345",
                "/albums/person?fields=id,name,picture&token=12345",
                "/albums/person?fields=id,picture,name&token=12345",
                "/albums/person?fields=picture,name,id&token=12345",
                "/albums/person?fields=picture,id,name&token=12345",
                "/albums/person?fields=name,picture,id&token=12345",
                "/albums/person?fields=name,id,picture&token=12345"
            };

            CollectionAssert.Contains(allowed, uri.OriginalString);

            UriTemplateMatch match = uriTemplate.Match(uri, new[] { "id", "fields", "geocode" }, new string[0]);

            Assert.IsNotNull(match);
            CollectionAssert.AreEqual((ICollection)Variables2["id"], (ICollection)match.Bindings["id"].Value);
            CollectionAssert.AreEqual((ICollection)Variables2["fields"], (ICollection)match.Bindings["fields"].Value);
            Assert.AreEqual(Variables2["token"], match.Bindings["token"].Value);

            match = uriTemplate.Match(uri, RequiredVariables2, new[] { "id", "fields", "geocode" }, new string[0]);
            Assert.IsNotNull(match);
            CollectionAssert.AreEqual((ICollection)Variables2["id"], (ICollection)match.Bindings["id"].Value);
            CollectionAssert.AreEqual((ICollection)Variables2["fields"], (ICollection)match.Bindings["fields"].Value);
            Assert.AreEqual(Variables2["token"], match.Bindings["token"].Value);
        }
示例#30
0
        public (UriAction action, string parameter) Parse(Uri uri)
        {
            var matched = UriTemplate.Match(URI_PREFIX, uri);

            if (matched != null)
            {
                var actionStr = matched.BoundVariables[ActionKey];
                Enum.TryParse <UriAction>(actionStr, true, out var action);
                var parameter = matched.BoundVariables[ParameterKey];
                return(action, parameter);
            }
            else
            {
                return(UriAction.Unknown, string.Empty);
            }
        }
        public void the_template_matches_when_query_strings_are_present()
        {
            var template = new UriTemplate("/temperature?unit={unit}");
            var match = template.Match(new Uri("http://localhost"), new Uri("http://localhost/temperature"));

            match.ShouldNotBeNull();
            match.PathSegmentVariables.Count.ShouldBe(0);
            match.QueryParameters.Count.ShouldBe(1);
        }
        public void bound_query_params_should_be_escaped()
        {
            const string searchTerm = "test $&+,/ test";
            var encodedString = HttpUtility.UrlEncode(searchTerm);
            var template = new UriTemplate("/test?q={searchTerm}");

            var match = template.Match(new Uri("http://localhost"), new Uri("http://localhost/test?q=" + encodedString));

            match.ShouldNotBeNull();
            match.BoundQueryParameters.Count.ShouldBe(1);
            match.BoundQueryParameters["searchTerm"].ShouldBe(searchTerm);
        }
        public void the_query_parameters_should_be_case_insensitive()
        {
            var template = new UriTemplate("/test?page={page}");

            var match = template.Match(new Uri("http://localhost"), new Uri("http://localhost/test?pAgE=2"));

            match.ShouldNotBeNull();
            match.QueryStringVariables.Count.ShouldBe(1);
            match.QueryStringVariables["PAGE"].ShouldBe("2");
        }
        public void a_url_matching_result_in_the_query_value_variable_being_set()
        {
            var table = new UriTemplate("/test?query={queryValue}");
            UriTemplateMatch match = table.Match(new Uri("http://localhost"), new Uri("http://localhost/test?query=search"));

            match.ShouldNotBeNull();

            match.QueryParameters["queryValue"].ShouldBe("search");
        }
 public void matching_urls_with_different_host_names_returns_no_match()
 {
     var table = new UriTemplate("/temp");
     table.Match(new Uri("http://localhost"), new Uri("http://notlocalhost/temp")).ShouldBeNull();
 }
 public void a_url_not_matching_a_literal_query_string_will_not_match()
 {
     var table = new UriTemplate("/test?query=literal");
     UriTemplateMatch match = table.Match(new Uri("http://localhost"), new Uri("http://localhost/test?query=notliteral"));
     match.ShouldBeNull();
 }
 public void a_url_matching_multiple_query_parameters_should_match()  
 {  
    var template = new UriTemplate("/test?query1={test}&query2={test2}");  
    var match = template.Match(new Uri("http://localhost"), new Uri("http://localhost/test?query1=test1&query2=test2"));  
    match.ShouldNotBeNull();  
 }  
 public void a_url_different_by_last_letter_to_query_parameters_should_not_match()  
 {  
    var template = new UriTemplate("/test?query1={test}&query2={test2}");  
    var match = template.Match(new Uri("http://localhost"), new Uri("http://localhost/test?query1=test1&query3=test2"));  
    match.ShouldBeNull();  
 }  
 public void a_url_matching_three_query_string_parameters_will_match()  
 {  
    var table = new UriTemplate("/test?q={searchTerm}&p={pageNumber}&s={pageSize}");  
    UriTemplateMatch match = table.Match(new Uri("http://localhost"), new Uri("http://localhost/test?q=&p=1&s=10"));  
    match.ShouldNotBeNull();  
    match.QueryParameters["searchTerm"].ShouldBe(string.Empty);  
    match.QueryParameters["pageNumber"].ShouldBe("1");  
    match.QueryParameters["pageSize"].ShouldBe("10");  
 }  
 public void a_url_with_extra_query_string_parameters_will_match()  
 {  
    var template = new UriTemplate("/test?q={searchTerm}&p={pageNumber}&s={pageSize}");  
    UriTemplateMatch match = template.Match(new Uri("http://localhost/"), new Uri("http://localhost/test?q=test&p=1&s=10&contentType=json"));  
    match.ShouldNotBeNull();  
 }