Skip to content

Commit

Permalink
Add tests for #1027
Browse files Browse the repository at this point in the history
  • Loading branch information
clairernovotny committed Jan 24, 2021
1 parent bc63b94 commit 9e5260e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
2 changes: 2 additions & 0 deletions InterfaceStubGenerator.Core/InterfaceStubGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public void GenerateInterfaceStubs(GeneratorExecutionContext context)
namespace {refitInternalNamespace}
{{
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
[AttributeUsage (AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Interface | AttributeTargets.Delegate)]
sealed class PreserveAttribute : Attribute
{{
Expand Down Expand Up @@ -165,6 +166,7 @@ namespace {ns}
[global::System.Diagnostics.DebuggerNonUserCode]
[{preserveAttributeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}]
[global::System.Reflection.Obfuscation(Exclude=true)]
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
partial class AutoGenerated{classDeclaration}
: {interfaceSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}{GenerateConstraints(interfaceSymbol)}
Expand Down
58 changes: 58 additions & 0 deletions Refit.Tests/AuthenticatedClientHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ public interface IMyAuthenticatedService
Task<string> GetAuthenticatedWithTokenInMethod([Authorize("Bearer")] string token);
}

public interface IInheritedAuthenticatedServiceWithHeaders : IAuthenticatedServiceWithHeaders
{
[Get("/get-inherited-thing")]
Task<string> GetInheritedThing();
}

[Headers("Authorization: Bearer")]
public interface IAuthenticatedServiceWithHeaders
{
[Get("/get-base-thing")]
Task<string> GetThingFromBase();
}

[Fact]
public void DefaultHandlerIsHttpClientHandler()
Expand Down Expand Up @@ -146,5 +158,51 @@ public async void AuthenticatedHandlerWithTokenInParameterUsesAuth()

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

[Fact]
public async void AuthentictedMethodFromBaseClassWithHeadersAttributeUsesAuth()
{
var handler = new MockHttpMessageHandler();
var settings = new RefitSettings()
{
AuthorizationHeaderValueGetter = () => Task.FromResult("tokenValue"),
HttpMessageHandlerFactory = () => handler
};

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

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

var result = await fixture.GetThingFromBase();

handler.VerifyNoOutstandingExpectation();

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

[Fact]
public async void AuthentictedMethodFromInheritedClassWithHeadersAttributeUsesAuth()
{
var handler = new MockHttpMessageHandler();
var settings = new RefitSettings()
{
AuthorizationHeaderValueGetter = () => Task.FromResult("tokenValue"),
HttpMessageHandlerFactory = () => handler
};

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

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

var result = await fixture.GetInheritedThing();

handler.VerifyNoOutstandingExpectation();

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

0 comments on commit 9e5260e

Please sign in to comment.