public void PartialResolveAllVariablesTest([Values("#", "+", "")] string exprOp)
 {
     var template = new UriTemplate("{" + exprOp + "param1,param2}");
     var partialTemplate = template.GetResolver().Bind("param1", "param1").Bind("param2", "param2").ResolveTemplate();
     var expected = (exprOp == "+" ? string.Empty : exprOp) + "param1,param2";
     Assert.AreEqual(expected, partialTemplate.ToString());
 }
        public void PartialResolveNothingTest([Values("#", "+", "")] string exprOp)
        {
            var template = new UriTemplate("{" + exprOp + "param1,param2}");
            var partialTemplate = template.GetResolver().Bind("unknown", "1").ResolveTemplate();

            Assert.AreEqual(template.ToString(), partialTemplate.ToString());
        }
        public void AnotherExampleTest()
        {
            var template = new UriTemplate("http://example.org/{area}/last-news{?type}");

            var uri = template.GetResolver()
                .Bind("area", "world")
                .Bind("type", new string[] { "it", "music", "art" })
                .Resolve();

            Assert.AreEqual("http://example.org/world/last-news?type=it,music,art", uri);
        }
        public void TestResolving()
        {
            var template = new UriTemplate("http://store2.io/api/v2/catalogs/{pin}/products/scroll{?pageToken,pretty}");

            var uri = template.Resolve(new Dictionary<string, object>
            {
                {"pin", "13"}, // int here fails!
                {"pageToken", null}
            });

            Assert.AreEqual("http://store2.io/api/v2/catalogs/13/products/scroll", uri);
        }
        public void MultiplePathSegmentTest()
        {
            var template = new UriTemplate("http://example.com/{path1}/{path2}");

            var actual = template.Resolve(new Dictionary<string, object>
            {
                { "path1", "foo" },
                { "path2", "bar" }
            });

            Assert.AreEqual("http://example.com/foo/bar", actual);
        }
        public void ExampleTest()
        {
            var template = new UriTemplate("http://example.org/{area}/last-news{?type,count}");

            var uri = template.Resolve(new Dictionary<string, object>
            {
                { "area", "world" },
                { "type", "actual" },
                { "count", "10" }
            });

            Assert.AreEqual("http://example.org/world/last-news?type=actual&count=10", uri);
        }
        public void ComplexTest()
        {
            var template = new UriTemplate("http://example.com{/paths*}{?q1,q2}{#f*}");

            var actual = template.Resolve(new Dictionary<string, object>
            {
                { "paths", new string[] { "foo", "bar" } },
                { "q1", "abc" },
                { "f", new Dictionary<string, string> { { "key1", "val1" }, { "key2", null } } }
            });

            Assert.AreEqual("http://example.com/foo/bar?q1=abc#key1=val1,key2=", actual);
        }
        public void PartialResolveMultiplePathsTest()
        {
            UriTemplate partialTemplate;
            var template = new UriTemplate("{/path1,path2,path3}");

            partialTemplate = template.GetResolver().Bind("path1", "value").ResolveTemplate();
            Assert.AreEqual("/value{/path2,path3}", partialTemplate.ToString());

            partialTemplate = template.GetResolver().Bind("path2", "value").ResolveTemplate();
            Assert.AreEqual("{/path1}/value{/path3}", partialTemplate.ToString());

            partialTemplate = template.GetResolver().Bind("path3", "value").ResolveTemplate();
            Assert.AreEqual("{/path1,path2}/value", partialTemplate.ToString());
        }
        public void PartialResolveMultipleContinuationsTest()
        {
            UriTemplate partialTemplate;
            var template = new UriTemplate("{&name1,name2,name3}");

            partialTemplate = template.GetResolver().Bind("name1", "value").ResolveTemplate();
            Assert.AreEqual("&name1=value{&name2,name3}", partialTemplate.ToString());

            partialTemplate = template.GetResolver().Bind("name2", "value").ResolveTemplate();
            Assert.AreEqual("{&name1}&name2=value{&name3}", partialTemplate.ToString());

            partialTemplate = template.GetResolver().Bind("name3", "value").ResolveTemplate();
            Assert.AreEqual("{&name1,name2}&name3=value", partialTemplate.ToString());
        }
        public void PartialResolveSimpleTest()
        {
            var template = new UriTemplate("{host}{/path}{?query}{#fragment}");

            template = template.GetResolver().Bind("host", "example.com").ResolveTemplate();
            Assert.AreEqual("example.com{/path}{?query}{#fragment}", template.ToString());

            template = template.GetResolver().Bind("path", "path").ResolveTemplate();
            Assert.AreEqual("example.com/path{?query}{#fragment}", template.ToString());

            template = template.GetResolver().Bind("query", "value").ResolveTemplate();
            Assert.AreEqual("example.com/path?query=value{#fragment}", template.ToString());

            template = template.GetResolver().Bind("fragment", "fragment").ResolveTemplate();
            Assert.AreEqual("example.com/path?query=value#fragment", template.ToString());
        }
        public void QueryParamsWithoutValuesTest()
        {
            var template = new UriTemplate("http://example.com/foo{?q1,q2}");

            var actual = template.Resolve(new Dictionary<string, object>());

            Assert.AreEqual("http://example.com/foo", actual);
        }
        public void QueryParamsWithOneValueTest()
        {
            var template = new UriTemplate("http://example.com/foo{?q1,q2}");

            var actual = template.Resolve(new Dictionary<string, object>()
            {
                { "q1", "abc" }
            });

            Assert.AreEqual("http://example.com/foo?q1=abc", actual);
        }
Beispiel #13
0
 internal UriTemplateResolver(UriTemplate template)
 {
     this.template  = template;
     this.variables = new Dictionary <string, object>(StringComparer.Ordinal);
 }
 public void ResolveIsNotAvailableForPartialVariablesTest([Values("#", "+", "")] string exprOp)
 {
     var template = new UriTemplate("{" + exprOp + "param1,param2}");
     Assert.Throws<UriTemplateException>(() => template.GetResolver().Bind("param1", "test").ResolveTemplate());
     Assert.Throws<UriTemplateException>(() => template.GetResolver().Bind("param2", "test").ResolveTemplate());
 }
 public void PartialResolveMultipleQueryWithReorderingTest()
 {
     var template = new UriTemplate("{?param1,param2}");
     var actual = template.GetResolver().Bind("param2", "test").ResolveTemplate().ToString();
     Assert.AreEqual("?param2=test{&param1}", actual);
 }
        public void QueryParamsExpandedTest()
        {
            var template = new UriTemplate("http://example.com/foo{?q*}");

            var actual = template.Resolve(new Dictionary<string, object>
            {
                { "q", new Dictionary<string, string> { { "q1", "abc" }, { "q2", "def" } } }
            });

            Assert.AreEqual("http://example.com/foo?q1=abc&q2=def", actual);
        }
        public void PathSegmentTest()
        {
            var template = new UriTemplate("http://example.com/{path}");

            var actual = template.Resolve(new Dictionary<string, object>
            {
                { "path", "foo" }
            });

            Assert.AreEqual("http://example.com/foo", actual);
        }
        public void QueryParamsWithMultipleValuesTest()
        {
            var template = new UriTemplate("http://example.com/foo{?q1,q2}");

            var actual = template.Resolve(new Dictionary<string, object>
            {
                { "q1", new string[] { "abc", "def", "ghi" } },
                { "q2", "10" }
            });

            Assert.AreEqual("http://example.com/foo?q1=abc,def,ghi&q2=10", actual);
        }
Beispiel #19
0
    private void Execute(FrameworkElement sender)
    {
      string url = Url;
      if (IsHRefTemplate)
      {
        try
        {
          JObject json = JsonConvert.DeserializeObject(Body) as JObject;
          if (json != null)
          {
            Resta.UriTemplates.UriTemplate t = new Resta.UriTemplates.UriTemplate(Url);
            IDictionary<string, object> parameters = new JsonObjectDictionary(json);
            url = t.Resolve(parameters);
          }
        }
        catch (Exception)
        {
          MessageBox.Show(GetOwnerWindow(), "Invalid URL template or template values");
          return;
        }
      }

      try
      {
        Uri tmp = new Uri(Url);
      }
      catch (Exception)
      {
        MessageBox.Show(GetOwnerWindow(), "Invalid URL");
        return;
      }

      if (string.IsNullOrEmpty(Method))
      {
        MessageBox.Show(GetOwnerWindow(), "Missing HTTP method");
        return;
      }

      ISession session = RamoneServiceManager.Session;

      Request req = session.Bind(url).Method(Method);

      if (Headers != null)
      {
        foreach (string line in Headers.Split('\n'))
        {
          int colonPos = line.IndexOf(':');
          if (colonPos > 0)
          {
            string header = line.Substring(0, colonPos).Trim();
            string value = line.Substring(colonPos + 1).Trim();
            if (!string.IsNullOrEmpty(header) && !string.IsNullOrEmpty(value))
            {
              if (header.Equals("accept", StringComparison.InvariantCultureIgnoreCase))
                req.Accept(value);
              else if (!header.Equals("content-type", StringComparison.InvariantCultureIgnoreCase))
                req.Header(header, value);
            }
          }
        }
      }

      if (SelectedType == MasonProperties.EncodingTypes.JSON && Body != null)
      {
        req.AsJson();
        req.Body(Body);
      }
      else if (SelectedType == MasonProperties.EncodingTypes.JSONFiles)
      {
        req.AsMultipartFormData();
        Hashtable files = new Hashtable();
        if (Body != null && !string.IsNullOrEmpty(JsonPartName))
          files[JsonPartName] = new Ramone.IO.StringFile { Filename = JsonPartName, ContentType = "application/json", Data = Body };
        foreach (ComposerFileViewModel file in Files)
        {
          if (!string.IsNullOrEmpty(file.Name) && !string.IsNullOrEmpty(file.Filename) && System.IO.File.Exists(file.Filename))
            files[file.Name] = new Ramone.IO.File(file.Filename);
        }
        req.Body(files);
      }

      Window w = Window.GetWindow(sender as DependencyObject);

      Publish(new ExecuteWebRequestEventArgs(session, req) { OnSuccess = (r => HandleSuccess(r, w)) });
    }
Beispiel #20
0
        private void Execute(FrameworkElement sender)
        {
            string url = Url;

            if (IsHRefTemplate)
            {
                try
                {
                    JObject json = JsonConvert.DeserializeObject(Body) as JObject;
                    if (json != null)
                    {
                        Resta.UriTemplates.UriTemplate t          = new Resta.UriTemplates.UriTemplate(Url);
                        IDictionary <string, object>   parameters = new JsonObjectDictionary(json);
                        url = t.Resolve(parameters);
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show(GetOwnerWindow(), "Invalid URL template or template values");
                    return;
                }
            }

            try
            {
                Uri tmp = new Uri(Url);
            }
            catch (Exception)
            {
                MessageBox.Show(GetOwnerWindow(), "Invalid URL");
                return;
            }

            if (string.IsNullOrEmpty(Method))
            {
                MessageBox.Show(GetOwnerWindow(), "Missing HTTP method");
                return;
            }

            ISession session = RamoneServiceManager.Session;

            Request req = session.Bind(url).Method(Method);

            if (Headers != null)
            {
                foreach (string line in Headers.Split('\n'))
                {
                    int colonPos = line.IndexOf(':');
                    if (colonPos > 0)
                    {
                        string header = line.Substring(0, colonPos).Trim();
                        string value  = line.Substring(colonPos + 1).Trim();
                        if (!string.IsNullOrEmpty(header) && !string.IsNullOrEmpty(value))
                        {
                            if (header.Equals("accept", StringComparison.InvariantCultureIgnoreCase))
                            {
                                req.Accept(value);
                            }
                            else if (!header.Equals("content-type", StringComparison.InvariantCultureIgnoreCase))
                            {
                                req.Header(header, value);
                            }
                        }
                    }
                }
            }

            if (SelectedType == MasonProperties.EncodingTypes.JSON && Body != null)
            {
                req.AsJson();
                req.Body(Body);
            }
            else if (SelectedType == MasonProperties.EncodingTypes.JSONFiles)
            {
                req.AsMultipartFormData();
                Hashtable files = new Hashtable();
                if (Body != null && !string.IsNullOrEmpty(JsonPartName))
                {
                    files[JsonPartName] = new Ramone.IO.StringFile {
                        Filename = JsonPartName, ContentType = "application/json", Data = Body
                    }
                }
                ;
                foreach (ComposerFileViewModel file in Files)
                {
                    if (!string.IsNullOrEmpty(file.Name) && !string.IsNullOrEmpty(file.Filename) && System.IO.File.Exists(file.Filename))
                    {
                        files[file.Name] = new Ramone.IO.File(file.Filename);
                    }
                }
                req.Body(files);
            }

            Window w = Window.GetWindow(sender as DependencyObject);

            Publish(new ExecuteWebRequestEventArgs(session, req)
            {
                OnSuccess = (r => HandleSuccess(r, w))
            });
        }