Skip to content

Commit

Permalink
feat: add URL fragment tests (#1900)
Browse files Browse the repository at this point in the history
* feat: add `RestService` fragment tests

* feat: add fragment exception test and updated ParamSub test
  • Loading branch information
TimothyMakkison authored Nov 3, 2024
1 parent fa3a57b commit e13386f
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 1 deletion.
124 changes: 124 additions & 0 deletions Refit.Tests/RestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, object> Args { get; set; }
Expand Down Expand Up @@ -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<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 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<IFragmentApi>("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<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
17 changes: 16 additions & 1 deletion Refit.Tests/RestServiceExceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ public interface IInvalidParamSubstitution
Task<string> GetValue(string path);
}

public interface IInvalidFragmentParamSubstitution
{
[Get("/{#path}")]
Task<string> GetValue(string path);
}

public interface IUrlNoMatchingParameters
{
[Get("/{value}")]
Expand Down Expand Up @@ -158,10 +164,19 @@ public void RoundTripWithTrailingWhitespaceShouldThrow()
}

[Fact]
public void InvalidParamSubstitutionShouldNotThrow()
public async Task InvalidParamSubstitutionShouldThrow()
{
var service = RestService.For<IInvalidParamSubstitution>("https://api.github.com");
Assert.NotNull(service);

await Assert.ThrowsAsync<ApiException>(() => service.GetValue("throws"));
}

[Fact]
public void InvalidFragmentParamSubstitutionShouldThrow()
{
var exception = Assert.Throws<ArgumentException>(() => RestService.For<IInvalidFragmentParamSubstitution>("https://api.github.com"));
AssertExceptionContains("but no method parameter matches", exception);
}

[Fact]
Expand Down

0 comments on commit e13386f

Please sign in to comment.