From 888dc7fff39d650788155bbcacd85b2f629ad1ea Mon Sep 17 00:00:00 2001 From: Timothy Makkison Date: Fri, 1 Nov 2024 15:24:10 +0000 Subject: [PATCH] feat: add `RestService` fragment tests --- Refit.Tests/RestService.cs | 104 +++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) diff --git a/Refit.Tests/RestService.cs b/Refit.Tests/RestService.cs index 8515c1a2b..3c93a8841 100644 --- a/Refit.Tests/RestService.cs +++ b/Refit.Tests/RestService.cs @@ -273,6 +273,24 @@ public interface IValidApi Task Get(); } +public interface IFragmentApi +{ + [Get("/foo#name")] + Task Fragment(); + + [Get("/foo#")] + Task EmptyFragment(); + + [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; } @@ -2215,6 +2233,92 @@ public void NonGenericCreate() Assert.Equal(fixture.Client.BaseAddress.AbsoluteUri, expectedBaseAddress); } + [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 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() {