diff --git a/Refit.Tests/RequestBuilder.cs b/Refit.Tests/RequestBuilder.cs index 4c06cb9da..259145f91 100644 --- a/Refit.Tests/RequestBuilder.cs +++ b/Refit.Tests/RequestBuilder.cs @@ -1770,6 +1770,15 @@ Task FetchSomeStuffWithHardcodedAndOtherQueryParameters( [Get("/foo/bar?param=first {id} and second {id}")] Task FetchSomeStuffWithTheIdInAParameterMultipleTimes(int id); + [Get("/foo?q=app_metadata.id:\"{id}\"")] + Task FetchSomeStuffWithDoubleQuotesInUrl(int id); + + [Get("/foo/bar/({id})")] + Task GetWithTrainingParenthesis(int id); + + [Get("/foo/bar/{id}/")] + Task GetWithTrailingSlash(int id); + [Post("/foo/bar/{id}")] [Headers("Content-Type: literally/anything")] Task PostSomeStuffWithHardCodedContentTypeHeader(int id, [Body] string content); @@ -2594,6 +2603,37 @@ public void QueryParamWithPathDelimiterShouldBeEncoded() ); } + [Fact] + public void QueryParamWhichEndsInDoubleQuotesShouldNotBeTruncated() + { + var fixture = new RequestBuilderImplementation(); + var factory = fixture.BuildRequestFactoryForMethod( + "FetchSomeStuffWithDoubleQuotesInUrl" + ); + var output = factory([42]); + + var uri = new Uri(new Uri("http://api"), output.RequestUri!); + + Assert.Equal("/foo?q=app_metadata.id%3A%2242%22", uri.PathAndQuery); + } + + [Theory] + [InlineData("GetWithTrainingParenthesis", ")", "/foo/bar/(1)")] + [InlineData("GetWithTrailingSlash", "/", "/foo/bar/1/")] + public void ShouldCaptureLastCharacterWhenRouteEndsWithConstant(string methodToTest, string constantChar, string contains) + { + var fixture = new RequestBuilderImplementation(); + var factory = fixture.BuildRequestFactoryForMethod( + methodToTest + ); + var output = factory(["1"]); + + var uri = new Uri(new Uri("http://api/"), output.RequestUri!); + + Assert.EndsWith(constantChar, uri.PathAndQuery, StringComparison.Ordinal); + Assert.Contains(contains, uri.PathAndQuery, StringComparison.Ordinal); + } + [Fact] public void ParameterizedQueryParamsShouldBeInUrlAndValuesEncodedWhenMixedReplacementAndQueryBadId() { diff --git a/Refit/RestMethodInfo.cs b/Refit/RestMethodInfo.cs index b87c8248d..3c502e808 100644 --- a/Refit/RestMethodInfo.cs +++ b/Refit/RestMethodInfo.cs @@ -401,12 +401,12 @@ ParameterInfo[] parameterInfo } } + if (index >= relativePath.Length) return (ret, fragmentList); + // add trailing string - if (index < relativePath.Length - 1) - { - var trailingConstant = relativePath.Substring(index, relativePath.Length - index); - fragmentList.Add(ParameterFragment.Constant(trailingConstant)); - } + var trailingConstant = relativePath.Substring(index); + fragmentList.Add(ParameterFragment.Constant(trailingConstant)); + return (ret, fragmentList); }