| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | using Google.Apis.Requests; |
| | using Google.Apis.Util; |
| | using System; |
| | using System.Collections.Generic; |
| | using System.Net.Http; |
| | using Xunit; |
| |
|
| | namespace Google.Apis.Tests.Apis.Requests |
| | { |
| | |
| | public class RequestBuilderTest |
| | { |
| | |
| | [Fact] |
| | public void TestBasicWebRequest() |
| | { |
| | var builder = new RequestBuilder() |
| | { |
| | BaseUri = new Uri("http://www.example.com/") |
| | }; |
| |
|
| | var request = builder.CreateRequest(); |
| | Assert.Equal(HttpMethod.Get, request.Method); |
| | Assert.Equal(new Uri("http://www.example.com/"), request.RequestUri); |
| | } |
| |
|
| | |
| | [Fact] |
| | public void TestPostWebRequest() |
| | { |
| | var builder = new RequestBuilder() |
| | { |
| | BaseUri = new Uri("http://www.example.com/"), |
| | Method = "POST" |
| | }; |
| |
|
| | var request = builder.CreateRequest(); |
| | Assert.Equal(HttpMethod.Post, request.Method); |
| | Assert.Equal(new Uri("http://www.example.com/"), request.RequestUri); |
| | } |
| |
|
| | |
| | [Theory] |
| | [InlineData("http://www.example.com/", "test/path", "http://www.example.com/test/path")] |
| | [InlineData("http://www.example.com/", "test/a:path", "http://www.example.com/test/a:path")] |
| | [InlineData("http://www.example.com/", "a:test/path", "http://www.example.com/a:test/path")] |
| | [InlineData("http://www.example.com/", "a:test", "http://www.example.com/a:test")] |
| | [InlineData("http://www.example.com/z", "test/path", "http://www.example.com/test/path")] |
| | [InlineData("http://www.example.com/z", "test/a:path", "http://www.example.com/test/a:path")] |
| | [InlineData("http://www.example.com/z", "a:test/path", "http://www.example.com/a:test/path")] |
| | [InlineData("http://www.example.com/z", "a:test", "http://www.example.com/a:test")] |
| | [InlineData("http://www.example.com/z/", "test/path", "http://www.example.com/z/test/path")] |
| | [InlineData("http://www.example.com/z/", "test/a:path", "http://www.example.com/z/test/a:path")] |
| | [InlineData("http://www.example.com/z/", "a:test/path", "http://www.example.com/z/a:test/path")] |
| | [InlineData("http://www.example.com/z/", "a:test", "http://www.example.com/z/a:test")] |
| | [InlineData("http://www.example.com/z", "/test/path", "http://www.example.com/test/path")] |
| | [InlineData("http://www.example.com/z", "/test/a:path", "http://www.example.com/test/a:path")] |
| | [InlineData("http://www.example.com/z", "/a:test/path", "http://www.example.com/a:test/path")] |
| | [InlineData("http://www.example.com/z", "/a:test", "http://www.example.com/a:test")] |
| | [InlineData("http://www.example.com/z/", "/test/path", "http://www.example.com/test/path")] |
| | [InlineData("http://www.example.com/z/", "/test/a:path", "http://www.example.com/test/a:path")] |
| | [InlineData("http://www.example.com/z/", "/a:test/path", "http://www.example.com/a:test/path")] |
| | [InlineData("http://www.example.com/z/", "/a:test", "http://www.example.com/a:test")] |
| | [InlineData("http://www.example.com/z", "?abc", "http://www.example.com/z?abc")] |
| | [InlineData("http://www.example.com/z", "#abc", "http://www.example.com/z#abc")] |
| | [InlineData("http://www.example.com/z/", "?abc", "http://www.example.com/z/?abc")] |
| | [InlineData("http://www.example.com/z/", "#abc", "http://www.example.com/z/#abc")] |
| | public void TestBasePlusPath(string baseUri, string path, string expectedRequestUri) |
| | { |
| | var builder = new RequestBuilder() |
| | { |
| | BaseUri = new Uri(baseUri), |
| | Path = path, |
| | Method = "PUT" |
| | }; |
| |
|
| | var request = builder.CreateRequest(); |
| | Assert.Equal(HttpMethod.Put, request.Method); |
| | |
| | |
| | Assert.Equal(new Uri(expectedRequestUri), request.RequestUri); |
| | Assert.Equal(expectedRequestUri, request.RequestUri.AbsoluteUri); |
| | } |
| |
|
| | |
| | [Fact] |
| | public void TestSingleQueryParameter() |
| | { |
| | var builder = new RequestBuilder() |
| | { |
| | BaseUri = new Uri("http://www.example.com/"), |
| | }; |
| |
|
| | builder.AddParameter(RequestParameterType.Query, "testQueryParam", "testValue"); |
| |
|
| | var request = builder.CreateRequest(); |
| | Assert.Equal(new Uri("http://www.example.com/?testQueryParam=testValue"), request.RequestUri); |
| | } |
| |
|
| | |
| | [Fact] |
| | public void TestMultipleQueryParameter() |
| | { |
| | var builder = new RequestBuilder() |
| | { |
| | BaseUri = new Uri("http://www.example.com/"), |
| | }; |
| | builder.AddParameter(RequestParameterType.Query, "testQueryParamA", "testValueA"); |
| | builder.AddParameter(RequestParameterType.Query, "testQueryParamB", "testValueB"); |
| |
|
| | var request = builder.CreateRequest(); |
| | Assert.Equal(new Uri("http://www.example.com/?testQueryParamA=testValueA&testQueryParamB=testValueB"), request.RequestUri); |
| | } |
| |
|
| | |
| | [Fact] |
| | public void TestQueryParameterWithUrlEncode() |
| | { |
| | var builder = new RequestBuilder() |
| | { |
| | BaseUri = new Uri("http://www.example.com/"), |
| | }; |
| |
|
| | builder.AddParameter(RequestParameterType.Query, "test Query Param", "test %va/ue"); |
| |
|
| | var request = builder.CreateRequest(); |
| | Assert.Equal(new Uri("http://www.example.com/?test%20Query%20Param=test%20%25va%2Fue"), request.RequestUri); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | [Fact] |
| | public void TestMultipleQueryParameters() |
| | { |
| | var builder = new RequestBuilder() |
| | { |
| | BaseUri = new Uri("http://www.example.com/"), |
| | }; |
| |
|
| | builder.AddParameter(RequestParameterType.Query, "q", "value1"); |
| | builder.AddParameter(RequestParameterType.Query, "q", "value2"); |
| |
|
| | var request = builder.CreateRequest(); |
| | Assert.Equal(new Uri("http://www.example.com/?q=value1&q=value2"), request.RequestUri); |
| | } |
| |
|
| | |
| | [Fact] |
| | public void TestNullQueryParameters() |
| | { |
| | var builder = new RequestBuilder() |
| | { |
| | BaseUri = new Uri("http://www.example.com"), |
| | Path = "", |
| | }; |
| |
|
| | builder.AddParameter(RequestParameterType.Query, "q", null); |
| | builder.AddParameter(RequestParameterType.Query, "p", String.Empty); |
| |
|
| | Assert.Equal("http://www.example.com/?p", builder.BuildUri().AbsoluteUri); |
| | } |
| |
|
| | |
| | [Fact] |
| | public void TestPathParameter() |
| | { |
| | var builder = new RequestBuilder() |
| | { |
| | BaseUri = new Uri("http://www.example.com/"), |
| | Path = "test/{id}/foo/bar", |
| | }; |
| |
|
| | builder.AddParameter(RequestParameterType.Path, "id", "value"); |
| |
|
| | var request = builder.CreateRequest(); |
| | Assert.Equal(new Uri("http://www.example.com/test/value/foo/bar"), request.RequestUri); |
| | } |
| |
|
| | |
| | [Fact] |
| | public void TestInvalidPathParameters() |
| | { |
| | |
| | var builder = new RequestBuilder() |
| | { |
| | BaseUri = new Uri("http://www.example.com/"), |
| | Path = "test/{id}/foo/bar", |
| | }; |
| | Assert.Throws<ArgumentException>(() => |
| | { |
| | builder.CreateRequest(); |
| | }); |
| |
|
| | |
| | builder = new RequestBuilder() |
| | { |
| | BaseUri = new Uri("http://www.example.com/"), |
| | Path = "test/{id:SHOULD_BE_NUMBER}/foo/bar", |
| | }; |
| | builder.AddParameter(RequestParameterType.Path, "id", "hello"); |
| | Assert.Throws<ArgumentException>(() => |
| | { |
| | builder.CreateRequest(); |
| | }); |
| | } |
| |
|
| | |
| | [Theory] |
| | [InlineData(" %va/ue", "http://www.example.com/test/%20%25va%2Fue")] |
| | [InlineData("foo/bar/[baz] test.txt", "http://www.example.com/test/foo%2Fbar%2F%5Bbaz%5D%20test.txt")] |
| | public void TestPathParameterWithUrlEncode(string idValue, string expectedRequestUri) |
| | { |
| | var builder = new RequestBuilder() |
| | { |
| | BaseUri = new Uri("http://www.example.com/"), |
| | Path = "test/{id}", |
| | }; |
| |
|
| | builder.AddParameter(RequestParameterType.Path, "id", idValue); |
| |
|
| | var request = builder.CreateRequest(); |
| | |
| | |
| | Assert.Equal(new Uri(expectedRequestUri), request.RequestUri); |
| | Assert.Equal(expectedRequestUri, request.RequestUri.AbsoluteUri); |
| | } |
| |
|
| | |
| | [Fact] |
| | public void TestQueryAndPathParameters() |
| | { |
| | var builder = new RequestBuilder() |
| | { |
| | BaseUri = new Uri("http://www.test.com"), |
| | Path = "colors{?list}" |
| | }; |
| |
|
| | builder.AddParameter(RequestParameterType.Path, "list", "red"); |
| | builder.AddParameter(RequestParameterType.Path, "list", "yellow"); |
| | builder.AddParameter(RequestParameterType.Query, "on", "1"); |
| |
|
| | Assert.Equal("http://www.test.com/colors?list=red,yellow&on=1", builder.BuildUri().AbsoluteUri); |
| | } |
| |
|
| |
|
| | |
| | [Fact] |
| | public void TestPathParameters_Level1() |
| | { |
| | IDictionary<string, IEnumerable<string>> vars = new Dictionary<string, IEnumerable<string>>(); |
| | vars["var"] = new List<string> { "value" }; |
| | vars["hello"] = new List<string> { "Hello World!" }; |
| |
|
| | SubtestPathParameters(vars, "{var}", "value"); |
| | SubtestPathParameters(vars, "{hello}", "Hello%20World%21"); |
| | } |
| |
|
| | |
| | [Fact] |
| | public void TestPathParameters_Level2() |
| | { |
| | IDictionary<string, IEnumerable<string>> vars = new Dictionary<string, IEnumerable<string>>(); |
| | vars["var"] = new List<string> { "value" }; |
| | vars["hello"] = new List<string> { "Hello World!" }; |
| | vars["path"] = new List<string> { "/foo/bar" }; |
| |
|
| | SubtestPathParameters(vars, "{+var}", "value"); |
| | SubtestPathParameters(vars, "{+hello}", "Hello%20World!"); |
| | SubtestPathParameters(vars, "{+path}/here", "foo/bar/here"); |
| | SubtestPathParameters(vars, "here?ref={+path}", "here?ref=/foo/bar"); |
| | } |
| |
|
| | |
| | [Fact] |
| | public void TestPathParameters_Level3() |
| | { |
| | IDictionary<string, IEnumerable<string>> vars = new Dictionary<string, IEnumerable<string>>(); |
| | vars["var"] = new List<string> { "value" }; |
| | vars["hello"] = new List<string> { "Hello World!" }; |
| | vars["empty"] = new List<string> { "" }; |
| |
|
| | vars["path"] = new List<string> { "/foo/bar" }; |
| | vars["x"] = new List<string> { "1024" }; |
| | vars["y"] = new List<string> { "768" }; |
| |
|
| | SubtestPathParameters(vars, "map?{x,y}", "map?1024,768"); |
| | SubtestPathParameters(vars, "{x,hello,y}", "1024,Hello%20World%21,768"); |
| | SubtestPathParameters(vars, "{+x,hello,y}", "1024,Hello%20World!,768"); |
| | SubtestPathParameters(vars, "{+path,x}/here", "foo/bar,1024/here"); |
| | SubtestPathParameters(vars, "{#x,hello,y}", "#1024,Hello%20World!,768"); |
| | SubtestPathParameters(vars, "{#path,x}/here", "#/foo/bar,1024/here"); |
| | SubtestPathParameters(vars, "X{.var}", "X.value"); |
| | SubtestPathParameters(vars, "X{.x,y}", "X.1024.768"); |
| | SubtestPathParameters(vars, "{/var}", "value"); |
| | SubtestPathParameters(vars, "{/var,x}/here", "value/1024/here"); |
| | SubtestPathParameters(vars, "{;x,y}", ";x=1024;y=768"); |
| | SubtestPathParameters(vars, "{;x,y,empty}", ";x=1024;y=768;empty"); |
| | SubtestPathParameters(vars, "{?x,y}", "?x=1024&y=768"); |
| | SubtestPathParameters(vars, "{?x,y,empty}", "?x=1024&y=768&empty="); |
| | SubtestPathParameters(vars, "?fixed=yes{&x}", "?fixed=yes&x=1024"); |
| | SubtestPathParameters(vars, "{&x,y,empty}", "&x=1024&y=768&empty="); |
| | } |
| |
|
| | |
| | [Fact] |
| | public void TestPathParameters_Level4() |
| | { |
| | IDictionary<string, IEnumerable<string>> vars = new Dictionary<string, IEnumerable<string>>(); |
| | vars["var"] = new List<string> { "value" }; |
| | vars["hello"] = new List<string> { "Hello World!" }; |
| | vars["path"] = new List<string> { "/foo/bar" }; |
| | vars["list"] = new List<string> { "red", "green", "blue" }; |
| |
|
| | SubtestPathParameters(vars, "{var:3}", "val"); |
| | SubtestPathParameters(vars, "{var:30}", "value"); |
| | SubtestPathParameters(vars, "{list}", "red,green,blue"); |
| | SubtestPathParameters(vars, "{list*}", "red,green,blue"); |
| | SubtestPathParameters(vars, "{+path:6}/here", "foo/b/here"); |
| | SubtestPathParameters(vars, "{+list}", "red,green,blue"); |
| | SubtestPathParameters(vars, "{+list*}", "red,green,blue"); |
| | SubtestPathParameters(vars, "{#path:6}/here", "#/foo/b/here"); |
| | SubtestPathParameters(vars, "{#list}", "#red,green,blue"); |
| | SubtestPathParameters(vars, "{#list*}", "#red,green,blue"); |
| | SubtestPathParameters(vars, "X{.var:3}", "X.val"); |
| | SubtestPathParameters(vars, "X{.list}", "X.red,green,blue"); |
| | SubtestPathParameters(vars, "X{.list*}", "X.red.green.blue"); |
| | SubtestPathParameters(vars, "{/var:1,var}", "v/value"); |
| | SubtestPathParameters(vars, "{/list}", "red,green,blue"); |
| | SubtestPathParameters(vars, "{/list*}", "red/green/blue"); |
| | SubtestPathParameters(vars, "{/list*,path:4}", "red/green/blue/%2Ffoo"); |
| | SubtestPathParameters(vars, "{;hello:5}", ";hello=Hello"); |
| | SubtestPathParameters(vars, "{;list}", ";list=red,green,blue"); |
| | SubtestPathParameters(vars, "{;list*}", ";list=red;list=green;list=blue"); |
| | SubtestPathParameters(vars, "{?var:3}", "?var=val"); |
| | SubtestPathParameters(vars, "{?list}", "?list=red,green,blue"); |
| | SubtestPathParameters(vars, "{?list*}", "?list=red&list=green&list=blue"); |
| | SubtestPathParameters(vars, "{&var:3}", "&var=val"); |
| | SubtestPathParameters(vars, "{&list}", "&list=red,green,blue"); |
| | SubtestPathParameters(vars, "{&list*}", "&list=red&list=green&list=blue"); |
| | } |
| |
|
| | [Fact] |
| | public void ThaiComparisonRegression() |
| | { |
| | using var _ = new CultureSwitcher("th"); |
| | var builder = new RequestBuilder() |
| | { |
| | BaseUri = new Uri("https://accounts.google.com/o/oauth2/v2/auth"), |
| | Path = "?access_type=offline~" |
| | }; |
| | var actual = builder.BuildUri().AbsoluteUri; |
| | var expected = "https://accounts.google.com/o/oauth2/v2/auth?access_type=offline~"; |
| | Assert.Equal(expected, actual); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | private void SubtestPathParameters(IDictionary<string, IEnumerable<string>> dic, string path, string expected) |
| | { |
| | var builder = new RequestBuilder() |
| | { |
| | BaseUri = new Uri("http://www.example.com"), |
| | Path = path, |
| | }; |
| |
|
| | foreach (var pair in dic) |
| | { |
| | foreach (var value in pair.Value) |
| | { |
| | builder.AddParameter(RequestParameterType.Path, pair.Key, value); |
| | } |
| | } |
| |
|
| | Assert.Equal("http://www.example.com/" + expected, builder.BuildUri().AbsoluteUri); |
| | } |
| | } |
| | } |
| |
|