public void When_adding_invalid_parameters_to_query_string() { Uri uri = null; Should.Throw <ArgumentException>(() => uri.AddParametersToQueryString("foo", "bar")) .Message.ShouldBe("Value cannot be null. (Parameter 'uri')"); uri = new Uri("http://www.foo.com"); Should.Throw <ArgumentException>(() => uri.AddParametersToQueryString(null, "bar")) .Message.ShouldBe("String must not be null, empty or whitespace."); Should.Throw <ArgumentException>(() => uri.AddParametersToQueryString("foo", null)) .Message.ShouldBe("String must not be null, empty or whitespace."); Should.Throw <ArgumentException>(() => uri.AddParametersToQueryString(string.Empty, "bar")) .Message.ShouldBe("String must not be null, empty or whitespace."); Should.Throw <ArgumentException>(() => uri.AddParametersToQueryString("foo", string.Empty)) .Message.ShouldBe("String must not be null, empty or whitespace."); Should.Throw <ArgumentException>(() => uri.AddParametersToQueryString(" ", "bar")) .Message.ShouldBe("String must not be null, empty or whitespace."); Should.Throw <ArgumentException>(() => uri.AddParametersToQueryString("foo", " ")) .Message.ShouldBe("String must not be null, empty or whitespace."); }
public void When_adding_multiple_parameters_to_query_string_with_existing_parameters() { var uri = new Uri("http://www.baz.com/abc.svc?name=abc"); var parameters = new Dictionary <string, string> { ["#foo"] = "#some value, & stuff", ["baz"] = "bar" }; var newUri = uri.AddParametersToQueryString(parameters); newUri.Query.ShouldBe("?name=abc&%23foo=%23some+value%2C+%26+stuff&baz=bar"); var parsed = newUri.ParseQueryString().ToArray(); parsed.Length.ShouldBe(3); parsed[0].Key.ShouldBe("name"); parsed[0].Value.ShouldBe("abc"); parsed[1].Key.ShouldBe("#foo"); parsed[1].Value.ShouldBe("#some value, & stuff"); parsed[2].Key.ShouldBe("baz"); parsed[2].Value.ShouldBe("bar"); }
public static Uri GetResolvedTarget(this Uri resolvedTarget, Dictionary <string, object> linkParameters, bool addNonTemplatedParametersToQueryString) { if (resolvedTarget == null) { return(null); } var uriTemplate = new UriTemplate(resolvedTarget.OriginalString); if (addNonTemplatedParametersToQueryString) { var templateParameters = uriTemplate.GetParameterNames(); if (linkParameters.Any(p => !templateParameters.Contains(p.Key))) { resolvedTarget = resolvedTarget.AddParametersToQueryString(null, linkParameters.Keys.ToArray()); uriTemplate = new UriTemplate(resolvedTarget.OriginalString); } } if (resolvedTarget.OriginalString.Contains("{")) { ApplyParametersToTemplate(uriTemplate, linkParameters); resolvedTarget = new Uri(uriTemplate.Resolve(), UriKind.RelativeOrAbsolute); return(resolvedTarget); } return(resolvedTarget); }
public void When_adding_empty_parameters_to_query_string_with_no_parameters() { var uri = new Uri("http://www.baz.com/abc.svc"); var parameters = new Dictionary <string, string>(); var newUri = uri.AddParametersToQueryString(parameters); newUri.Query.ShouldBe(string.Empty); var parsed = newUri.ParseQueryString().ToArray(); parsed.Length.ShouldBe(0); }
public void When_adding_empty_parameters_to_query_string_with_existing_parameters() { var uri = new Uri("http://www.baz.com/abc.svc?name=abc"); var parameters = new Dictionary <string, string>(); var newUri = uri.AddParametersToQueryString(parameters); newUri.Query.ShouldBe("?name=abc"); var parsed = newUri.ParseQueryString().ToArray(); parsed.Length.ShouldBe(1); parsed[0].Key.ShouldBe("name"); parsed[0].Value.ShouldBe("abc"); }
public void When_adding_encoded_value_to_query_string() { var uri = new Uri("http://www.baz.com/abc.svc?"); const string Key = "%23foo"; const string Value = "%23some+value%2C+%26+stuff"; var newUri = uri.AddParametersToQueryString(Key, Value); newUri.Query.ShouldBe("?%2523foo=%2523some%2Bvalue%252C%2B%2526%2Bstuff"); var parsed = newUri.ParseQueryString().ToArray(); parsed.Length.ShouldBe(1); parsed[0].Key.ShouldBe(Key); parsed[0].Value.ShouldBe(Value); }
public void When_adding_parameters_to_query_string_with_no_parameters() { var uri = new Uri("http://www.baz.com/abc.svc"); const string Key = "#foo"; const string Value = "#some value, & stuff"; var newUri = uri.AddParametersToQueryString(Key, Value); newUri.Query.ShouldBe("?%23foo=%23some+value%2C+%26+stuff"); var parsed = newUri.ParseQueryString().ToArray(); parsed.Length.ShouldBe(1); parsed[0].Key.ShouldBe("#foo"); parsed[0].Value.ShouldBe(Value); }
/// <summary> /// Adds or appends parameters and values to the query-string. /// </summary> public static Uri AddParametersToQueryString(this Uri uri, string parameter, long value) { return(uri.AddParametersToQueryString(parameter, value.ToString())); }
/// <summary> /// Adds or appends parameters and values to the query-string. /// </summary> public static Uri AddParametersToQueryString(this Uri uri, string parameter, int value) { return(uri.AddParametersToQueryString(parameter, (long)value)); }