public async Task Can_refresh_access_tokens_in_parallel() { var logicalThreadCount = 10; var callsPerThread = 10; var maxCallsPerAccessToken = 20; var tokens = new TestTokens(maxCallsPerAccessToken); var handlerUnderTest = new RefreshTokenDelegatingHandler( new TestableOidcTokenRefreshClient(tokens, 2.Milliseconds()), tokens.InitialAccessToken, tokens.InitialRefreshToken, new TestServer(tokens, 0.Milliseconds())); using (var client = new TestClient(handlerUnderTest)) { async Task PerformPingRequests() { for (var i = 0; i < callsPerThread; i++) { await client.SecuredPing(); } } var tasks = Enumerable.Range(0, logicalThreadCount).Select(i => PerformPingRequests()); await Task.WhenAll(tasks); } tokens.Count.Should().BeGreaterOrEqualTo(logicalThreadCount * callsPerThread / maxCallsPerAccessToken); }
public async Task Can_refresh_access_tokens_with_sliding_refresh_tokens() { const int maxCallsPerAccessToken = 2; var tokens = new TestTokens(maxCallsPerAccessToken, _writeLine); var handlerUnderTest = new RefreshTokenDelegatingHandler( new TestableOidcTokenRefreshClient(tokens, TimeSpan.Zero), tokens.InitialAccessToken, tokens.InitialRefreshToken, new TestServer(tokens, TimeSpan.Zero)); using (var client = new TestClient(handlerUnderTest)) { tokens.Count.Should().Be(1); await client.SecuredPing(); tokens.Count.Should().Be(1); await client.SecuredPing(); tokens.Count.Should().Be(1); await client.SecuredPing(); tokens.Count.Should().Be(2); } }
public TestableOidcTokenRefreshClient(TestTokens tokens, TimeSpan delayForRefresh) : base(new OidcClientOptions { Authority = "http://test-authority" }) { _tokens = tokens; _delayForRefresh = delayForRefresh; }
public async Task <LinkTodosResponse> AttemptToLinkTodos(Guid id1, Guid id2) { var flurlResponse = await _appFactory.FlurlClient() .Request($"/todo/{id1}/link/{id2}") .WithHeader("Authorization", $"Bearer {TestTokens.GenerateToken()}") .WithHeader("Accept", MediaTypeNames.Application.Json) .WithHeader("Content-Type", MediaTypeNames.Application.Json) .SetQueryParam("customerId", Any.String()) .AllowAnyHttpStatus() .PostAsync(); return(new LinkTodosResponse(flurlResponse)); }
public async Task ShouldNotAllowTokensWithBadIssuerInAddTodoRequest() { //GIVEN var driver = new EndpointsAdapterDriver(); //WHEN var httpResponseMock = await driver.AttemptToAddTodoItem( request => request.WithHeader("Authorization", $"Bearer {TestTokens.GenerateTokenFromBadIssuer()}") ); //THEN httpResponseMock.ShouldBeForbidden401(); }
public async Task <AddTodoResponse> AttemptToAddNewTodo(AddTodoDtoBuilder addTodoDtoBuilder) { var addTodoDto = addTodoDtoBuilder.Build(); var response = await _appFactory.FlurlClient() .Request("/todo") .WithHeader("Authorization", "Bearer " + TestTokens.GenerateToken()) .WithHeader("Content-Type", MediaTypeNames.Application.Json) .WithHeader("Accept", MediaTypeNames.Application.Json) .SetQueryParam("customerId", Any.String()) .AllowAnyHttpStatus() .PostJsonAsync(addTodoDto); return(new AddTodoResponse(response)); }
public async Task <AddTodoItemAdapterResponse> AttemptToAddTodoItem(Func <HttpRequestMock, HttpRequestMock> customize) { var httpContextMock = HttpContextMock.Default(); await _adapter.AddTodoEndpoint.Handle( customize(httpContextMock.Request() .AppendPathSegment("todo") .WithHeader("Authorization", $"Bearer {TestTokens.GenerateToken()}") .WithHeader("Content-Type", MediaTypeNames.Application.Json) .WithHeader("Accept", MediaTypeNames.Application.Json) .WithQueryParam("customerId", Any.String()) .WithJsonBody(new { title = "Meeting", content = "there's a meeting you need to attend" })).RealInstance, httpContextMock.Response().RealInstance, new CancellationToken() ); var httpResponseMock = httpContextMock.Response(); return(new AddTodoItemAdapterResponse(httpResponseMock)); }
public TestServer(TestTokens tokens, TimeSpan pingDelay) { _tokens = tokens; _pingDelay = pingDelay; }