コード例 #1
0
 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());
 }
コード例 #2
0
        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());
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        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);
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        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);
        }
コード例 #7
0
        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);
        }
コード例 #8
0
        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());
        }
コード例 #9
0
        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());
        }
コード例 #10
0
        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());
        }
コード例 #11
0
        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);
        }
コード例 #12
0
        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);
        }
コード例 #13
0
 internal UriTemplateResolver(UriTemplate template)
 {
     this.template  = template;
     this.variables = new Dictionary <string, object>(StringComparer.Ordinal);
 }
コード例 #14
0
 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());
 }
コード例 #15
0
 public void PartialResolveMultipleQueryWithReorderingTest()
 {
     var template = new UriTemplate("{?param1,param2}");
     var actual = template.GetResolver().Bind("param2", "test").ResolveTemplate().ToString();
     Assert.AreEqual("?param2=test{&param1}", actual);
 }
コード例 #16
0
        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);
        }
コード例 #17
0
        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);
        }
コード例 #18
0
        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);
        }
コード例 #19
0
ファイル: ComposerViewModel.cs プロジェクト: paulswartz/Mason
    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)) });
    }
コード例 #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))
            });
        }