public async Task TokenClient_create_new_token_test() { var tokenEndPoint = "https://api.baasic.com/v1/Test/Login"; var username = "******"; var password = "******"; var handler = new Mock <HttpMessageHandler>(); handler.Protected().Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()).Returns(async(HttpRequestMessage request, CancellationToken cancellationToken) => { request.RequestUri.AbsoluteUri.Should().Be(tokenEndPoint); request.Content.Headers.ContentType.MediaType.Should().Be("application/x-www-form-urlencoded"); request.Method.Should().Be(HttpMethod.Post); var content = await request.Content.ReadAsStringAsync(); var values = content.Split('&').ToDictionary(k => k.Split('=')[0], v => v.Split('=')[1]); values["grant_type"].Should().Be("password"); values["username"].Should().Be(username); values["password"].Should().Be(password); return(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(@"{ token_type: ""bearer"", access_token: ""accessToken"", expires_in: 7200 }", System.Text.UTF8Encoding.UTF8, "application/json") }); }); Mock <HttpClientFactory> httpClientFactory = new Mock <HttpClientFactory>(new Mock <IDependencyResolver>().Object); httpClientFactory.Setup(p => p.Create()).Returns(() => { return(new HttpClient(handler.Object)); }); Mock <IBaasicClientFactory> baasicClientFactory = new Mock <IBaasicClientFactory>(); baasicClientFactory.Setup(f => f.Create(It.IsAny <IClientConfiguration>())).Returns((IClientConfiguration config) => new BaasicClient(config, httpClientFactory.Object, new JsonFormatter())); Mock <IClientConfiguration> clientConfiguration = new Mock <IClientConfiguration>(); clientConfiguration.Setup(p => p.DefaultMediaType).Returns(ClientConfiguration.HalJsonMediaType); clientConfiguration.Setup(p => p.DefaultTimeout).Returns(TimeSpan.FromSeconds(1)); clientConfiguration.Setup(p => p.ApplicationIdentifier).Returns("Test"); clientConfiguration.Setup(p => p.SecureBaseAddress).Returns("https://api.baasic.com/v1"); clientConfiguration.Setup(p => p.BaseAddress).Returns("http://api.baasic.com/v1"); var target = new TokenClient(clientConfiguration.Object, baasicClientFactory.Object, new JsonFormatter()); var token = await target.CreateAsync(username, password); token.IsValid.Should().BeTrue(); token.Scheme.Should().Be("bearer"); token.Token.Should().Be("accessToken"); var mockTokenHandler = new Mock <ITokenHandler>(); mockTokenHandler.Setup(h => h.Save(It.IsAny <IAuthenticationToken>())).Returns((IAuthenticationToken tokenToSave) => { tokenToSave.IsValid.Should().BeTrue(); tokenToSave.Scheme.Should().Be("bearer"); tokenToSave.Token.Should().Be("accessToken"); return(true); }); clientConfiguration.Setup(p => p.TokenHandler).Returns(mockTokenHandler.Object); target = new TokenClient(clientConfiguration.Object, baasicClientFactory.Object, new JsonFormatter()); await target.CreateAsync(username, password); }