public async Task Exception_result_not_null() { using var server = TestServerBuilder.BuildInHeaderOrQueryParamsServer(options => { options.Realm = TestServerBuilder.Realm; options.KeyName = FakeApiKeys.KeyName; options.Events.OnValidateKey = context => { Assert.Null(context.Result); return(Task.CompletedTask); }; options.Events.OnAuthenticationFailed = context => { Assert.Null(context.Result); Assert.NotNull(context.Exception); Assert.IsType <InvalidOperationException>(context.Exception); Assert.Equal(ExpectedExceptionMessage, context.Exception.Message); context.NoResult(); Assert.NotNull(context.Result); return(Task.CompletedTask); }; }); using var client = server.CreateClient(); using var request = new HttpRequestMessage(HttpMethod.Get, TestServerBuilder.BaseUrl); request.Headers.Add(FakeApiKeys.KeyName, FakeApiKeys.FakeKey); using var response = await client.SendAsync(request); Assert.False(response.IsSuccessStatusCode); Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode); }
public ApiKeyInHeaderOrQueryParamsHandlerTests() { _server = TestServerBuilder.BuildInHeaderOrQueryParamsServer(); _client = _server.CreateClient(); _serverWithProvider = TestServerBuilder.BuildInHeaderOrQueryParamsServerWithProvider(); _clientWithProvider = _serverWithProvider.CreateClient(); }
private HttpClient BuildClient(Func <ApiKeyValidateKeyContext, Task> onValidateKey) { var server = TestServerBuilder.BuildInHeaderOrQueryParamsServer(options => { options.KeyName = FakeApiKeys.KeyName; options.Realm = TestServerBuilder.Realm; options.Events.OnValidateKey = onValidateKey; }); _serversToDispose.Add(server); return(server.CreateClient()); }
public void ApiKeyProviderType_verify_null() { using var server = TestServerBuilder.BuildInHeaderOrQueryParamsServer(); var services = server.Host.Services; var apiKeyOptionsSnapshot = services.GetService <IOptionsSnapshot <ApiKeyOptions> >(); var apiKeyOptions = apiKeyOptionsSnapshot.Get(ApiKeyDefaults.AuthenticationScheme); Assert.NotNull(apiKeyOptions); Assert.Null(apiKeyOptions.ApiKeyProviderType); var apiKeyProvider = services.GetService <IApiKeyProvider>(); Assert.Null(apiKeyProvider); }
public async Task HandleAuthenticate_OnValidateKey_result_null_without_provider_and_OnAuthenticationFailed_throws() { var expectedExceptionMessage = $"Either {nameof(ApiKeyEvents.OnValidateKey)} delegate on configure options {nameof(ApiKeyOptions.Events)} should be set or use an extention method with type parameter of type {nameof(IApiKeyProvider)}."; using var server = TestServerBuilder.BuildInHeaderOrQueryParamsServer(options => { options.Realm = TestServerBuilder.Realm; options.KeyName = FakeApiKeys.KeyName; options.Events.OnValidateKey = context => { Assert.Null(context.Result); return(Task.CompletedTask); }; options.Events.OnAuthenticationFailed = context => { Assert.NotNull(context.Exception); Assert.IsType <InvalidOperationException>(context.Exception); Assert.Equal(expectedExceptionMessage, context.Exception.Message); return(Task.CompletedTask); }; }); using var client = server.CreateClient(); using var request = new HttpRequestMessage(HttpMethod.Get, TestServerBuilder.BaseUrl); request.Headers.Add(FakeApiKeys.KeyName, FakeApiKeys.FakeKey); var exception = await Assert.ThrowsAsync <InvalidOperationException>(async() => { using var response = await client.SendAsync(request); Assert.False(response.IsSuccessStatusCode); Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode); }); Assert.Equal(expectedExceptionMessage, exception.Message); }
private async Task RunAuthInitAsync(Action <ApiKeyOptions> configureOptions) { var server = TestServerBuilder.BuildInHeaderOrQueryParamsServer(configureOptions); await server.CreateClient().GetAsync(TestServerBuilder.BaseUrl); }