Skip to content

Commit

Permalink
feat: add fragment exception test and updated ParamSub test
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMakkison committed Nov 3, 2024
1 parent f846723 commit 98cc73d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
20 changes: 20 additions & 0 deletions Refit.Tests/RestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,9 @@ public interface IFragmentApi
[Get("/foo#")]
Task EmptyFragment();

[Get("/foo#first#second")]
Task ManyFragments();

[Get("/foo#{frag}")]
Task ParameterFragment(string frag);

Expand Down Expand Up @@ -2496,6 +2499,23 @@ public async Task ShouldStripEmptyFragment()
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()
{
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 98cc73d

Please sign in to comment.