static async Task TryItOut() { var factory = FluentHttpClientFactory.Create(new ClientFactory()); var client = factory.CreateClient("Reqres", b => b.OnRequest(r => r.Headers.Add("User-Agent", "TheClient"))); var getResponse = await client .Get("api/users/2") .OnRequest(r => LogRequestDetails(client.Name, r)) .OnResponse(r => LogResponseDetails(client.Name, r)) .ExecuteAsync <User>(); System.Console.WriteLine($"Get: {getResponse.Result}"); System.Console.WriteLine(); var settings = new JsonSerializerSettings { Formatting = Formatting.Indented }; var request = new CreateUserRequest { Name = "Tim", Job = "TheBoss" }; var postResponse = await client .Post("api/users") .WithHeader("User-Agent", "TheComputer") .Configure(c => c.Serialization = new JsonSerialization(settings)) .ExecuteAsync <CreateUserRequest, CreateUserResponse>(request); System.Console.WriteLine($"Post: {postResponse.Result}"); }
public void ShouldThrowExceptionIfNoConfiguration() { var key = "test"; var factory = new FluentHttpClientFactory(_cacheMock.Object, null); //var message = //"FluentHttpClientFactory was created with a null configuration." + Environment.NewLine + //"If you are creating FluentHttpClientFactory manually, please provide an IConfigurationRoot in the constructor of FluentHttpClientFactory." + Environment.NewLine + //"If you are calling services.UseFluentHttp(), then change that call to services.UseFluentHttp(configuration) where configuration is the application configuration."; Assert.Throws <InvalidOperationException>(() => factory.Create(key)); }
public void ShouldCreateAbsoluteUrlClient() { var factory = new FluentHttpClientFactory(_cacheMock.Object, null, _configurationMock.Object); var client = factory.Create(); _cacheMock.Verify(c => c.Exists(FluentHttpClientFactory.ABSOLUTE_CLIENT_KEY), Times.Once()); _cacheMock.Verify(c => c.Cache(FluentHttpClientFactory.ABSOLUTE_CLIENT_KEY, It.Is <HttpClient>(http => http.BaseAddress == null)), Times.Once()); _cacheMock.Verify(c => c.Get(FluentHttpClientFactory.ABSOLUTE_CLIENT_KEY), Times.Once()); Assert.NotNull(client); }
public void ShouldCreateConnectionStringClient() { var key = "test"; var factory = new FluentHttpClientFactory(_cacheMock.Object, null, _configurationMock.Object); var client = factory.Create(key); _cacheMock.Verify(c => c.Exists(key), Times.Once()); _cacheMock.Verify(c => c.Cache(key, It.Is <HttpClient>(http => http.BaseAddress == new Uri(_baseUrl))), Times.Once()); _cacheMock.Verify(c => c.Get(key), Times.Once()); Assert.NotNull(client); }
public void ShouldGetCachedAbsoluteUrlClient() { _cacheMock.Setup(c => c.Exists(FluentHttpClientFactory.ABSOLUTE_CLIENT_KEY)).Returns(true); var factory = new FluentHttpClientFactory(_cacheMock.Object, null, _configurationMock.Object); var client = factory.Create(); _cacheMock.Verify(c => c.Exists(FluentHttpClientFactory.ABSOLUTE_CLIENT_KEY), Times.Once()); _cacheMock.Verify(c => c.Cache(FluentHttpClientFactory.ABSOLUTE_CLIENT_KEY, It.IsAny <HttpClient>()), Times.Never()); _cacheMock.Verify(c => c.Get(FluentHttpClientFactory.ABSOLUTE_CLIENT_KEY), Times.Once()); Assert.NotNull(client); }
public void ShouldGetCachedConnectionStringClient() { var key = "test"; _cacheMock.Setup(c => c.Exists(key)).Returns(true); var factory = new FluentHttpClientFactory(_cacheMock.Object, null, _configurationMock.Object); var client = factory.Create(key); _cacheMock.Verify(c => c.Exists(key), Times.Once()); _cacheMock.Verify(c => c.Cache(key, It.IsAny <HttpClient>()), Times.Never()); _cacheMock.Verify(c => c.Get(key), Times.Once()); Assert.NotNull(client); }