diff --git a/Refit.Tests/RestService.cs b/Refit.Tests/RestService.cs index 9d81b1185..984a8be35 100644 --- a/Refit.Tests/RestService.cs +++ b/Refit.Tests/RestService.cs @@ -303,6 +303,27 @@ public interface IQueryApi Task ParameterMappedQuery(string key, string value); } +public interface IFragmentApi +{ + [Get("/foo#name")] + Task Fragment(); + + [Get("/foo#")] + Task EmptyFragment(); + + [Get("/foo#first#second")] + Task ManyFragments(); + + [Get("/foo#{frag}")] + Task ParameterFragment(string frag); + + [Get("/foo?key=value#name")] + Task FragmentAfterQuery(); + + [Get("/foo#?key=value")] + Task QueryAfterFragment(); +} + public class HttpBinGet { public Dictionary Args { get; set; } @@ -2444,6 +2465,109 @@ public async Task ParameterMappedQueryShouldEscape() mockHttp.VerifyNoOutstandingExpectation(); } + [Fact] + public async Task ShouldStripFragment() + { + var mockHttp = new MockHttpMessageHandler(); + var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp, }; + + mockHttp + .Expect(HttpMethod.Get, "https://github.com/foo") + .Respond(HttpStatusCode.OK); + + var fixture = RestService.For("https://github.com", settings); + + await fixture.Fragment(); + + mockHttp.VerifyNoOutstandingExpectation(); + } + + [Fact] + public async Task ShouldStripEmptyFragment() + { + var mockHttp = new MockHttpMessageHandler(); + var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp, }; + + mockHttp + .Expect(HttpMethod.Get, "https://github.com/foo") + .Respond(HttpStatusCode.OK); + + var fixture = RestService.For("https://github.com", settings); + + await fixture.EmptyFragment(); + + mockHttp.VerifyNoOutstandingExpectation(); + } + + [Fact] + public async Task ShouldStripManyFragments() + { + var mockHttp = new MockHttpMessageHandler(); + var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp, }; + + mockHttp + .Expect(HttpMethod.Get, "https://github.com/foo") + .Respond(HttpStatusCode.OK); + + var fixture = RestService.For("https://github.com", settings); + + await fixture.ManyFragments(); + + mockHttp.VerifyNoOutstandingExpectation(); + } + + [Fact] + public async Task ShouldStripParameterFragment() + { + var mockHttp = new MockHttpMessageHandler(); + var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp, }; + + mockHttp + .Expect(HttpMethod.Get, "https://github.com/foo") + .Respond(HttpStatusCode.OK); + + var fixture = RestService.For("https://github.com", settings); + + await fixture.ParameterFragment("ignore"); + + mockHttp.VerifyNoOutstandingExpectation(); + } + + [Fact] + public async Task ShouldStripFragmentAfterQuery() + { + var mockHttp = new MockHttpMessageHandler(); + var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp, }; + + mockHttp + .Expect(HttpMethod.Get, "https://github.com/foo") + .WithExactQueryString("key=value") + .Respond(HttpStatusCode.OK); + + var fixture = RestService.For("https://github.com", settings); + + await fixture.FragmentAfterQuery(); + + mockHttp.VerifyNoOutstandingExpectation(); + } + + [Fact] + public async Task ShouldStripQueryAfterFragment() + { + var mockHttp = new MockHttpMessageHandler(); + var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp, }; + + mockHttp + .Expect(HttpMethod.Get, "https://github.com/foo") + .Respond(HttpStatusCode.OK); + + var fixture = RestService.For("https://github.com", settings); + + await fixture.QueryAfterFragment(); + + mockHttp.VerifyNoOutstandingExpectation(); + } + [Fact] public async Task TypeCollisionTest() { diff --git a/Refit.Tests/RestServiceExceptions.cs b/Refit.Tests/RestServiceExceptions.cs index 0fd771665..3554c418b 100644 --- a/Refit.Tests/RestServiceExceptions.cs +++ b/Refit.Tests/RestServiceExceptions.cs @@ -57,6 +57,12 @@ public interface IInvalidParamSubstitution Task GetValue(string path); } +public interface IInvalidFragmentParamSubstitution +{ + [Get("/{#path}")] + Task GetValue(string path); +} + public interface IUrlNoMatchingParameters { [Get("/{value}")] @@ -158,10 +164,19 @@ public void RoundTripWithTrailingWhitespaceShouldThrow() } [Fact] - public void InvalidParamSubstitutionShouldNotThrow() + public async Task InvalidParamSubstitutionShouldThrow() { var service = RestService.For("https://api.github.com"); Assert.NotNull(service); + + await Assert.ThrowsAsync(() => service.GetValue("throws")); + } + + [Fact] + public void InvalidFragmentParamSubstitutionShouldThrow() + { + var exception = Assert.Throws(() => RestService.For("https://api.github.com")); + AssertExceptionContains("but no method parameter matches", exception); } [Fact]