Match() public method

public Match ( Uri baseAddress, Uri candidate ) : System.UriTemplateMatch
baseAddress Uri
candidate Uri
return System.UriTemplateMatch
        public void TestCompoundFragmentExpansionAssociativeMapVariable()
        {
            string template = "{#keys*}";
            UriTemplate uriTemplate = new UriTemplate(template);
            Uri uri = uriTemplate.BindByName(variables);
            string[] allowed =
                {
                    "#comma=,,dot=.,semi=;",
                    "#comma=,,semi=;,dot=.",
                    "#dot=.,comma=,,semi=;",
                    "#dot=.,semi=;,comma=,",
                    "#semi=;,comma=,,dot=.",
                    "#semi=;,dot=.,comma=,"
                };

            CollectionAssert.Contains(allowed, uri.ToString());

            UriTemplateMatch match = uriTemplate.Match(uri, new[] { "list" }, new[] { "keys" });
            Assert.IsNotNull(match);
            CollectionAssert.AreEqual((ICollection)variables["keys"], (ICollection)match.Bindings["keys"].Value);

            match = uriTemplate.Match(uri, requiredVariables, new[] { "list" }, new[] { "keys" });
            Assert.IsNotNull(match);
            CollectionAssert.AreEqual((ICollection)variables["keys"], (ICollection)match.Bindings["keys"].Value);
        }
Exemplo n.º 2
0
        public void TestReservedExpansionEscaping()
        {
            string template = "{+hello}";
            UriTemplate uriTemplate = new UriTemplate(template);
            Uri uri = uriTemplate.BindByName(Variables);
            Assert.AreEqual("Hello%20World!", uri.OriginalString);

            UriTemplateMatch match = uriTemplate.Match(uri);
            Assert.IsNotNull(match);
            Assert.AreEqual(Variables["hello"], match.Bindings["hello"].Value);

            match = uriTemplate.Match(uri, RequiredVariables);
            Assert.IsNotNull(match);
            Assert.AreEqual(Variables["hello"], match.Bindings["hello"].Value);
        }
        public void TestReservedExpansionReservedCharacters()
        {
            string template = "{+path}/here";
            UriTemplate uriTemplate = new UriTemplate(template);
            Uri uri = uriTemplate.BindByName(variables);
            Assert.AreEqual("/foo/bar/here", uri.ToString());

            UriTemplateMatch match = uriTemplate.Match(uri);
            Assert.IsNotNull(match);
            Assert.AreEqual(variables["path"], match.Bindings["path"].Value);

            match = uriTemplate.Match(uri, requiredVariables);
            Assert.IsNotNull(match);
            Assert.AreEqual(variables["path"], match.Bindings["path"].Value);
        }
        public void TestCompoundFragmentExpansionCollectionVariable()
        {
            string template = "{#list*}";
            UriTemplate uriTemplate = new UriTemplate(template);
            Uri uri = uriTemplate.BindByName(variables);
            Assert.AreEqual("#red,green,blue", uri.ToString());

            UriTemplateMatch match = uriTemplate.Match(uri, new[] { "list" }, new[] { "keys" });
            Assert.IsNotNull(match);
            CollectionAssert.AreEqual((ICollection)variables["list"], (ICollection)match.Bindings["list"].Value);

            match = uriTemplate.Match(uri, requiredVariables, new[] { "list" }, new[] { "keys" });
            Assert.IsNotNull(match);
            CollectionAssert.AreEqual((ICollection)variables["list"], (ICollection)match.Bindings["list"].Value);
        }
        public void TestSimpleExpansion()
        {
            string template = "{var}";
            UriTemplate uriTemplate = new UriTemplate(template);
            Uri uri = uriTemplate.BindByName(variables);
            Assert.AreEqual("value", uri.ToString());

            UriTemplateMatch match = uriTemplate.Match(uri);
            Assert.IsNotNull(match);
            Assert.AreEqual(variables["var"], match.Bindings["var"].Value);

            match = uriTemplate.Match(uri, requiredVariables);
            Assert.IsNotNull(match);
            Assert.AreEqual(variables["var"], match.Bindings["var"].Value);
        }
Exemplo n.º 6
0
        public void TestReservedExpansion()
        {
            string template = "{+var}";
            UriTemplate uriTemplate = new UriTemplate(template);
            Uri uri = uriTemplate.BindByName(Variables);
            Assert.AreEqual("value", uri.OriginalString);

            UriTemplateMatch match = uriTemplate.Match(uri);
            Assert.IsNotNull(match);
            Assert.AreEqual(Variables["var"], match.Bindings["var"].Value);

            match = uriTemplate.Match(uri, RequiredVariables);
            Assert.IsNotNull(match);
            Assert.AreEqual(Variables["var"], match.Bindings["var"].Value);
        }
        public void TestSimpleExpansionEscaping()
        {
            string template = "{hello}";
            UriTemplate uriTemplate = new UriTemplate(template);
            Uri uri = uriTemplate.BindByName(variables);
            Assert.AreEqual("Hello%20World%21", uri.ToString());

            UriTemplateMatch match = uriTemplate.Match(uri);
            Assert.IsNotNull(match);
            Assert.AreEqual(variables["hello"], match.Bindings["hello"].Value);

            match = uriTemplate.Match(uri, requiredVariables);
            Assert.IsNotNull(match);
            Assert.AreEqual(variables["hello"], match.Bindings["hello"].Value);
        }
Exemplo n.º 8
0
 private bool TryUriTemplateMatch(string uri, out UriTemplateMatch uriTemplateMatch)
 {
     var uriTemplate = new UriTemplate(uri);
     var serverPath = Request.Url.GetServerBaseUri();
     uriTemplateMatch = uriTemplate.Match(new Uri(serverPath), Request.Url);
     return uriTemplateMatch != null;
 }
 public void when_validating_a_uri_template_with_url_encoded_chars()
 {
     var template = new UriTemplate("/streams/$all?embed={embed}");
     var uri = new Uri("http://127.0.0.1/streams/$all");
     var baseaddress = new Uri("http://127.0.0.1");
     Assert.IsTrue(template.Match(baseaddress, uri) != null);
 }        
Exemplo n.º 10
0
        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.ToString());

            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);
        }
Exemplo n.º 11
0
        public void TestFragmentExpansionMultipleVariables()
        {
            string template = "{#x,hello,y}";
            UriTemplate uriTemplate = new UriTemplate(template);
            Uri uri = uriTemplate.BindByName(variables);
            Assert.AreEqual("#1024,Hello%20World!,768", uri.ToString());

            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);
        }
Exemplo n.º 12
0
        public void TestEmptyTemplate()
        {
            string template = string.Empty;
            UriTemplate uriTemplate = new UriTemplate(template);
            Uri uri = uriTemplate.BindByName(variables);
            Assert.AreEqual(string.Empty, uri.ToString());

            UriTemplateMatch match = uriTemplate.Match(uri);
            Assert.IsNotNull(match);
            Assert.AreEqual(0, match.Bindings.Count);
        }
Exemplo n.º 13
0
        public bool Matches(RequestContext ctx)
        {
            if (!String.IsNullOrEmpty(method) && ctx.Request.HttpMethod != method) return false;

            var uriTemplate = new UriTemplate(uri);
            var serverPath = ctx.Request.Url.GetServerBaseUri();
            var uriTemplateMatch = uriTemplate.Match(new Uri(serverPath), ctx.Request.Url);
            if (uriTemplateMatch == null) return false;

            ctx.Request.LoadArguments(uriTemplateMatch.BoundVariables);
            return true;
        }
 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));
 }
Exemplo n.º 15
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();
        }
Exemplo n.º 16
0
        public void MatchLoadTest()
        {
            var testResult = LoadTest.Execute("Dictionary", index =>
            {
                var subIndex = index % uris.Length;

                var uri = uris[subIndex];

                template.Match(baseAddress, uri);
            }, 1000000);

            Trace.TraceInformation(testResult.ToString());

            //Assert.AreEqual(actualResult1, actualResult2);
        }
Exemplo n.º 17
0
 /// <summary>
 /// Tries to match the path with the templateString
 /// </summary>
 /// <param name="templateString">The template string.</param>
 /// <param name="pathString">The path string.</param>
 /// <param name="match">The match.</param>
 /// <returns>True if successful.</returns>
 protected bool TryMatchPath(string templateString, string pathString, 
     out UriTemplateMatch match)
 {
     var uriTemplate = new UriTemplate(templateString);
       var pathUri = new Uri(UriBaseAddress, pathString);
       try {
     match = uriTemplate.Match(UriBaseAddress, pathUri);
       } catch (Exception ex) {
     Logger.WriteLineIf(LogLevel.Error, _log_props, string.Format(
       "Failed to match path. Exception: {0}", ex));
     match = null;
       }
       if (match == null) {
     return false;
       } else {
     return true;
       }
 }
Exemplo n.º 18
0
        public void TestSimpleExpansionPrefix()
        {
            string template = "{var:3}";
            UriTemplate uriTemplate = new UriTemplate(template);
            Uri uri = uriTemplate.BindByName(variables);
            Assert.AreEqual("val", uri.ToString());

            UriTemplateMatch match = uriTemplate.Match(uri, new[] { "list" }, new[] { "keys" });
            Assert.IsNotNull(match);
            Assert.AreEqual("val", match.Bindings["var"].Value);

            match = uriTemplate.Match(uri, requiredVariables, new[] { "list" }, new[] { "keys" });
            Assert.IsNotNull(match);
            Assert.AreEqual("val", match.Bindings["var"].Value);
        }
Exemplo n.º 19
0
        public void TestSimpleExpansionAssociativeMapVariable()
        {
            string template = "{keys}";
            UriTemplate uriTemplate = new UriTemplate(template);
            Uri uri = uriTemplate.BindByName(variables);
            string[] allowed =
                {
                    "comma,%2C,dot,.,semi,%3B",
                    "comma,%2C,semi,%3B,dot,.",
                    "dot,.,comma,%2C,semi,%3B",
                    "dot,.,semi,%3B,comma,%2C",
                    "semi,%3B,comma,%2C,dot,.",
                    "semi,%3B,dot,.,comma,%2C"
                };

            CollectionAssert.Contains(allowed, uri.ToString());

            UriTemplateMatch match = uriTemplate.Match(uri, new[] { "list" }, new[] { "keys" });
            Assert.IsNotNull(match);
            CollectionAssert.AreEqual((ICollection)variables["keys"], (ICollection)match.Bindings["keys"].Value);

            match = uriTemplate.Match(uri, requiredVariables, new[] { "list" }, new[] { "keys" });
            Assert.IsNotNull(match);
            CollectionAssert.AreEqual((ICollection)variables["keys"], (ICollection)match.Bindings["keys"].Value);
        }
Exemplo n.º 20
0
        public void TestReservedExpansionPrefixVariable()
        {
            string template = "{+path:6}/here";
            UriTemplate uriTemplate = new UriTemplate(template);
            Uri uri = uriTemplate.BindByName(variables);
            Assert.AreEqual("/foo/b/here", uri.ToString());

            UriTemplateMatch match = uriTemplate.Match(uri, new[] { "list" }, new[] { "keys" });
            Assert.IsNotNull(match);
            Assert.AreEqual("/foo/b", match.Bindings["path"].Value);

            match = uriTemplate.Match(uri, requiredVariables, new[] { "list" }, new[] { "keys" });
            Assert.IsNotNull(match);
            Assert.AreEqual("/foo/b", match.Bindings["path"].Value);
        }
Exemplo n.º 21
0
        public void TestPathSegmentExpansionMultipleReferencesPrefix()
        {
            string template = "{/var:1,var}";
            UriTemplate uriTemplate = new UriTemplate(template);
            Uri uri = uriTemplate.BindByName(variables);
            Assert.AreEqual("/v/value", uri.ToString());

            UriTemplateMatch match = uriTemplate.Match(uri, new[] { "list" }, new[] { "keys" });
            Assert.IsNotNull(match);
            Assert.AreEqual(variables["var"], match.Bindings["var"].Value);

            match = uriTemplate.Match(uri, requiredVariables, new[] { "list" }, new[] { "keys" });
            Assert.IsNotNull(match);
            Assert.AreEqual(variables["var"], match.Bindings["var"].Value);
        }
Exemplo n.º 22
0
        public void TestPathParameterExpansionPrefixVariable()
        {
            string template = "{;hello:5}";
            UriTemplate uriTemplate = new UriTemplate(template);
            Uri uri = uriTemplate.BindByName(variables);
            Assert.AreEqual(";hello=Hello", uri.ToString());

            UriTemplateMatch match = uriTemplate.Match(uri, new[] { "list" }, new[] { "keys" });
            Assert.IsNotNull(match);
            Assert.AreEqual("Hello", match.Bindings["hello"].Value);

            match = uriTemplate.Match(uri, requiredVariables, new[] { "list" }, new[] { "keys" });
            Assert.IsNotNull(match);
            Assert.AreEqual("Hello", match.Bindings["hello"].Value);
        }
Exemplo n.º 23
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");
		}
Exemplo n.º 24
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");
		}
Exemplo n.º 25
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}~|~");
        }
Exemplo n.º 26
0
		public void SimpleWebGet () {
			UriTemplate t = new UriTemplate ("GetBlog");
			Assert.IsNotNull(t.Match(new Uri("http://localhost:8000/BlogService"),
				new Uri("http://localhost:8000/BlogService/GetBlog")), "Matches simple WebGet method");
			Assert.IsNull(t.Match (new Uri ("http://localhost:8000/BlogService"),
				new Uri ("http://localhost:8000/BlogService/GetData")), "Doesn't match wrong WebGet method");
		}
Exemplo n.º 27
0
        void HandleRequest(HttpListenerContext context)
        {
            var url = context.Request.Url;
              Logger.WriteLineIf(LogLevel.Info, _log_props,
            string.Format("Received request for {0} from {1}", url,
            context.Request.RemoteEndPoint));

              UriTemplate uriTemplate = new UriTemplate(ControllerSegment +
            "/{nameSpace}/{name}/{piece}");
              // It doesn't matter which hostname is used here.
              UriTemplateMatch match = uriTemplate.Match(new Uri(string.Format(
            "http://{0}/", context.Request.UserHostAddress)), url);
              if (match != null) {
            try {
              var torrentBytes = GetPieceTorrent(match.BoundVariables[0],
                match.BoundVariables[1],
            // Let the exception be caught by the caller.
                Int32.Parse(match.BoundVariables[2]));

              context.Response.ContentType = "text/plain";
              context.Response.StatusCode = (int)HttpStatusCode.OK;
              context.Response.ContentLength64 = torrentBytes.Length;
              context.Response.OutputStream.Write(torrentBytes, 0, torrentBytes.Length);
              Logger.WriteLineIf(LogLevel.Info, _log_props,
            string.Format("Successfully handled request for {0} from {1}", url,
            context.Request.RemoteEndPoint));
            } catch (FileNotFoundException ex) {
              Logger.WriteLineIf(LogLevel.Error, _log_props,
            string.Format("Exception thrown when getting piece torrent: {0}", ex));
              context.Response.StatusCode = (int)HttpStatusCode.NotFound;
            }
              } else {
            Logger.WriteLineIf(LogLevel.Verbose, _log_props, string.Format(
              "Request URL ({0}) doesn't match the service. Returning 404 response.",
              context.Request.Url));
            context.Response.StatusCode = (int)HttpStatusCode.NotFound;
              }
        }
Exemplo n.º 28
0
        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.ToString());

            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);
        }
Exemplo n.º 29
0
		public void MatchWildcard3 ()
		{
			var t = new UriTemplate ("*?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 (3, m.WildcardPathSegments.Count, "#2");
			Assert.AreEqual ("hoge", m.WildcardPathSegments [0], "#3");
			Assert.AreEqual ("ppp", m.WildcardPathSegments [1], "#4");
			Assert.AreEqual ("qqq", m.WildcardPathSegments [2], "#5");
		}
Exemplo n.º 30
0
		public void NamedWildcard ()
		{
			UriTemplate template = new UriTemplate ("{*path}");
			UriTemplateMatch match = template.Match (new Uri ("http://localhost"), new Uri ("http://localhost/something"));
			Assert.IsNotNull (match, "#1");
			Assert.AreEqual ("something", match.BoundVariables ["path"], "#2");
		}
Exemplo n.º 31
0
        public void TestCompoundQueryExpansionAssociativeMapVariable()
        {
            string template = "{?keys*}";
            UriTemplate uriTemplate = new UriTemplate(template);
            Uri uri = uriTemplate.BindByName(variables);
            string[] allowed =
                {
                    "?comma=%2C&dot=.&semi=%3B",
                    "?comma=%2C&semi=%3B&dot=.",
                    "?dot=.&comma=%2C&semi=%3B",
                    "?dot=.&semi=%3B&comma=%2C",
                    "?semi=%3B&comma=%2C&dot=.",
                    "?semi=%3B&dot=.&comma=%2C"
                };

            CollectionAssert.Contains(allowed, uri.ToString());

            UriTemplateMatch match = uriTemplate.Match(uri, new[] { "list" }, new[] { "keys" });
            Assert.IsNotNull(match);
            CollectionAssert.AreEqual((ICollection)variables["keys"], (ICollection)match.Bindings["keys"].Value);

            match = uriTemplate.Match(uri, requiredVariables, new[] { "list" }, new[] { "keys" });
            Assert.IsNotNull(match);
            CollectionAssert.AreEqual((ICollection)variables["keys"], (ICollection)match.Bindings["keys"].Value);
        }