public async Task AuthorisationTokenSuccessTests(string expectedAccessToken) { var expectedResponse = JsonConvert.SerializeObject(new OidcAuthenticationToken { AccessToken = expectedAccessToken }); var config = new OidcAuthenticationConfig { ClientId = "bob", ClientSecret = "bobsecret", Uri = "https://localhost/authendpoint" }; var mockHttp = new MockHttpMessageHandler(); mockHttp.When(HttpMethod.Post, config.Uri) .WithHeaders("client_id", config.ClientId) .WithHeaders("client_secret", config.ClientSecret) .WithContentType("application/json-patch+json; charset=utf-8", string.Empty) .Respond(HttpStatusCode.Created, "application/json-patch+json", expectedResponse); var httpClientFactory = new HttpClientFactory(new Dictionary <string, HttpClient> { { new Uri(config.Uri).Host, mockHttp.ToHttpClient() } }); var handler = new MmAuthenticationHandler(httpClientFactory, config, _bigBrother); var httpClient = mockHttp.ToHttpClient(); var token = await handler.GetTokenAsync(_cancellationToken); Assert.NotNull(token); Assert.NotEmpty(token); Assert.StartsWith("Bearer ", token); }
public async Task AuthorisationTokenSuccessTests(string expectedAccessToken) { var expectedResponse = JsonConvert.SerializeObject(new OidcAuthenticationToken { AccessToken = expectedAccessToken }); var config = new OidcAuthenticationConfig { ClientId = "bob", ClientSecret = "bobsecret", Uri = "http://localhost/authendpoint" }; var mockHttp = new MockHttpMessageHandler(); mockHttp.When(HttpMethod.Post, config.Uri) .WithHeaders("client_id", config.ClientId) .WithHeaders("client_secret", config.ClientSecret) .WithContentType("application/json-patch+json", string.Empty) .Respond(HttpStatusCode.Created, "application/json-patch+json", expectedResponse); var handler = new MmAuthenticationHandler(config); var httpClient = mockHttp.ToHttpClient(); await handler.GetToken(httpClient); Assert.NotNull(httpClient.DefaultRequestHeaders.Authorization); Assert.Equal(expectedAccessToken, httpClient.DefaultRequestHeaders.Authorization.Parameter); Assert.Equal("Bearer", httpClient.DefaultRequestHeaders.Authorization.Scheme); }