Skip to content

Commit

Permalink
feat(http client): allow empty baseUrl (#64)
Browse files Browse the repository at this point in the history
* feat(http client): allow empty baseUrl

* update cl + bump

* update messagepack in test
  • Loading branch information
stephenlautier committed Mar 11, 2020
1 parent 70a9283 commit aa9b114
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 21 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Fluently Http Changelog

[_vNext_](https://github.com/sketch7/FluentlyHttpClient/compare/3.8.0...3.9.0) (2019-X-X)
[_vNext_](https://github.com/sketch7/FluentlyHttpClient/compare/3.8.1...3.9.0) (2020-X-X)

## [3.8.1](https://github.com/sketch7/FluentlyHttpClient/compare/3.8.0...3.8.1) (2020-03-11)

### Features

- **http client:** allow empty baseUrl and not mandatory anymore

## [3.8.0](https://github.com/sketch7/FluentlyHttpClient/compare/3.7.2...3.8.0) (2019-12-11)

Expand Down
2 changes: 1 addition & 1 deletion benchmark/FluentlyHttpClient.Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.11.4" />
<PackageReference Include="MessagePack" Version="1.7.3.4" />
<PackageReference Include="MessagePack" Version="1.9.3" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.2.0" />
<PackageReference Include="RichardSzalay.MockHttp" Version="5.0.0" />
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sketch7/fluently-http-client",
"version": "3.8.0",
"version": "3.8.1",
"versionSuffix": "",
"scripts": {
"pack": "bash ./tools/pack.sh",
Expand Down
7 changes: 3 additions & 4 deletions src/FluentlyHttpClient/FluentHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,9 @@ private HttpClient Configure(FluentHttpClientOptions options)
options.HttpMessageHandler
);

var httpClient = new HttpClient(httpHandler)
{
BaseAddress = new Uri(options.BaseUrl)
};
var httpClient = new HttpClient(httpHandler);
if (!string.IsNullOrEmpty(options.BaseUrl))
httpClient.BaseAddress = new Uri(options.BaseUrl);

httpClient.DefaultRequestHeaders.Add(HeaderTypes.Accept, Formatters.SelectMany(x => x.SupportedMediaTypes).Select(x => x.MediaType));
httpClient.Timeout = options.Timeout;
Expand Down
5 changes: 1 addition & 4 deletions src/FluentlyHttpClient/FluentHttpClientBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public FluentHttpClientOptions BuildOptions()
_formatterOptions.Resort();

string? baseUrl = null;
if (_baseUrl != null)
if (!string.IsNullOrEmpty(_baseUrl))
{
baseUrl = _baseUrl.TrimEnd('/');
if (_useBaseUrlTrailingSlash)
Expand Down Expand Up @@ -251,9 +251,6 @@ public IFluentHttpClient Build<THttpClient>(FluentHttpClientOptions? options = n
if (string.IsNullOrEmpty(options.Identifier))
throw ClientBuilderValidationException.FieldNotSpecified(nameof(options.Identifier));

if (string.IsNullOrEmpty(options.BaseUrl))
throw ClientBuilderValidationException.FieldNotSpecified(nameof(options.BaseUrl));

return ActivatorUtilities.CreateInstance<THttpClient>(_serviceProvider, options, _fluentHttpClientFactory);
}

Expand Down
22 changes: 13 additions & 9 deletions test/FluentHttpClientFactoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@
// ReSharper disable once CheckNamespace
namespace Test
{
public class ClientFactoryTest_Build
{
[Fact]
public void ShouldAllowEmptyBaseUrl()
{
var httpClient = GetNewClientFactory().CreateBuilder("abc")
.Build();

Assert.Null(httpClient.BaseUrl);
}
}

public class ClientFactory_WithRequestBuilderDefaults
{
[Fact]
Expand Down Expand Up @@ -196,15 +208,7 @@ public void ShouldRegisterSuccessfully()
[Fact]
public void ThrowsErrorWhenIdentifierNotSpecified()
{
var clientBuilder = GetNewClientFactory().CreateBuilder(null);
Assert.Throws<ClientBuilderValidationException>(() => clientBuilder.Register());
}

[Fact]
public void ThrowsErrorWhenUriNotSpecified()
{
var clientBuilder = GetNewClientFactory().CreateBuilder("abc");

var clientBuilder = GetNewClientFactory().CreateBuilder(null!);
Assert.Throws<ClientBuilderValidationException>(() => clientBuilder.Register());
}

Expand Down
9 changes: 9 additions & 0 deletions test/FluentHttpRequestBuilderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ public async Task WithoutUrl_ShouldUseBaseUrl()
Assert.Equal("https://sketch7.com/api/heroes/", response.Message.RequestMessage.RequestUri.ToString());
}

[Fact]
public void WithBaseUrlEmpty_ShouldBeValid()
{
var request = GetNewRequestBuilder(configureClient: c => c.WithBaseUrl(string.Empty))
.Build();

Assert.Equal("/api", request.Uri.ToString());
}

[Fact]
public async Task WithBaseUrlTrailingSlash_ShouldNotIncludeTrailingSlash()
{
Expand Down
2 changes: 1 addition & 1 deletion test/FluentlyHttpClient.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MessagePack" Version="1.7.3.4" />
<PackageReference Include="MessagePack" Version="1.9.3" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.2.0" />
Expand Down

0 comments on commit aa9b114

Please sign in to comment.