diff --git a/src/PortableRest.Tests/ResourceUriTests.cs b/src/PortableRest.Tests/ResourceUriTests.cs index 1ff7202..91af8c2 100644 --- a/src/PortableRest.Tests/ResourceUriTests.cs +++ b/src/PortableRest.Tests/ResourceUriTests.cs @@ -87,5 +87,15 @@ public void BuildResourceUriWhereResourceUriIsNull() Assert.AreEqual("https://www.googleapis.com/discovery/v1/apis?&name=adexchangebuyer", request.GetResourceUri("https://www.googleapis.com/discovery/v1/apis?&name=adexchangebuyer").ToString()); } + + [TestMethod] + public void TestUriIdempodence() + { + var request = new RestRequest("/test"); + request.AddQueryString("test", "value"); + var originalResource = request.Resource; + request.GetResourceUri("http://some.base.url"); + Assert.AreEqual(originalResource, request.Resource); + } } } \ No newline at end of file diff --git a/src/PortableRest/RestRequest.cs b/src/PortableRest/RestRequest.cs index 3dcbfbb..6ec6854 100644 --- a/src/PortableRest/RestRequest.cs +++ b/src/PortableRest/RestRequest.cs @@ -252,9 +252,10 @@ public void AddQueryString(string key, object value) /// public Uri GetResourceUri(string baseUrl) { + string resource = Resource; foreach (var segment in UrlSegments.Where(c => !c.IsQueryString)) { - Resource = Resource.Replace("{" + segment.Key + "}", Uri.EscapeUriString(segment.Value)); + resource = resource.Replace("{" + segment.Key + "}", Uri.EscapeUriString(segment.Value)); } if (UrlSegments.Any(c => c.IsQueryString)) @@ -265,12 +266,12 @@ public Uri GetResourceUri(string baseUrl) current.Append(string.Format("&{0}={1}", Uri.EscapeUriString(next.Key), Uri.EscapeDataString(next.Value)))) .ToString(); - Resource = string.Format(Resource.Contains("?") ? "{0}{1}" : "{0}?{1}", Resource, queryString); + resource = string.Format(resource.Contains("?") ? "{0}{1}" : "{0}?{1}", resource, queryString); } - Resource = CombineUriParts(baseUrl, Resource); + resource = CombineUriParts(baseUrl, resource); - return new Uri(Resource, UriKind.RelativeOrAbsolute); + return new Uri(resource, UriKind.RelativeOrAbsolute); } #endregion