Skip to content

Commit

Permalink
Ensure token parameters are marked as mapped. Fixes #1016
Browse files Browse the repository at this point in the history
  • Loading branch information
clairernovotny committed Jan 24, 2021
1 parent 77f04c2 commit bc63b94
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,4 @@ Refit-Tests/test-results
/InterfaceStubGenerator.App/Properties/launchSettings.json
*.binlog
/InterfaceStubGenerator.Core/Properties/launchSettings.json
*.svclog
26 changes: 26 additions & 0 deletions Refit.Tests/AuthenticatedClientHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public interface IMyAuthenticatedService
[Get("/auth")]
[Headers("Authorization: Bearer")]
Task<string> GetAuthenticated();

[Get("/auth")]
Task<string> GetAuthenticatedWithTokenInMethod([Authorize("Bearer")] string token);
}


Expand Down Expand Up @@ -120,5 +123,28 @@ public async void AuthenticatedHandlerWithParamUsesAuth()

Assert.Equal("Ok", result);
}


[Fact]
public async void AuthenticatedHandlerWithTokenInParameterUsesAuth()
{
var handler = new MockHttpMessageHandler();
var settings = new RefitSettings()
{
HttpMessageHandlerFactory = () => handler
};

handler.Expect(HttpMethod.Get, "http://api/auth")
.WithHeaders("Authorization", "Bearer tokenValue")
.Respond("text/plain", "Ok");

var fixture = RestService.For<IMyAuthenticatedService>("http://api", settings);

var result = await fixture.GetAuthenticatedWithTokenInMethod("tokenValue");

handler.VerifyNoOutstandingExpectation();

Assert.Equal("Ok", result);
}
}
}
1 change: 1 addition & 0 deletions Refit/RequestBuilderImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,7 @@ Func<object[], Task<HttpRequestMessage>> BuildRequestFactoryForMethod(RestMethod
if (restMethod.AuthorizeParameterInfo != null && restMethod.AuthorizeParameterInfo.Item2 == i)
{
headersToAdd["Authorization"] = $"{restMethod.AuthorizeParameterInfo.Item1} {param}";
isParameterMappedToRequest = true;
}

//if property, add to populate into HttpRequestMessage.Properties
Expand Down

0 comments on commit bc63b94

Please sign in to comment.