Skip to content

Commit

Permalink
feat: add RestService fragment tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMakkison committed Nov 1, 2024
1 parent 9619841 commit 888dc7f
Showing 1 changed file with 104 additions and 0 deletions.
104 changes: 104 additions & 0 deletions Refit.Tests/RestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, object> Args { get; set; }
Expand Down Expand Up @@ -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<IFragmentApi>("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<IFragmentApi>("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<IFragmentApi>("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<IFragmentApi>("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<IFragmentApi>("https://github.com", settings);

await fixture.QueryAfterFragment();

mockHttp.VerifyNoOutstandingExpectation();
}

[Fact]
public async Task TypeCollisionTest()
{
Expand Down

0 comments on commit 888dc7f

Please sign in to comment.