Skip to content

Commit

Permalink
ArgumentException if hostUrl IsNullOrWhitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
tstivers1990 committed Feb 17, 2019
1 parent 4c864fe commit 89d9db7
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Refit.Tests/InterfaceStubGenerator.cs
Expand Up @@ -123,7 +123,7 @@ public void GenerateTemplateInfoForInterfaceListSmokeTest()
.ToList();

var result = fixture.GenerateTemplateInfoForInterfaceList(input);
Assert.Equal(11, result.ClassList.Count);
Assert.Equal(12, result.ClassList.Count);
}

[Fact]
Expand Down
32 changes: 32 additions & 0 deletions Refit.Tests/RefitStubs.Net46.cs
Expand Up @@ -1241,3 +1241,35 @@ public virtual Task<HttpResponseMessage> Get(int httpstatuscode)
}
}
}

namespace Refit.Tests
{
using Refit.Tests.RefitInternalGenerated;

/// <inheritdoc />
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[global::System.Diagnostics.DebuggerNonUserCode]
[Preserve]
[global::System.Reflection.Obfuscation(Exclude=true)]
partial class AutoGeneratedIValidApi : IValidApi
{
/// <inheritdoc />
public HttpClient Client { get; protected set; }
readonly IRequestBuilder requestBuilder;

/// <inheritdoc />
public AutoGeneratedIValidApi(HttpClient client, IRequestBuilder requestBuilder)
{
Client = client;
this.requestBuilder = requestBuilder;
}

/// <inheritdoc />
public virtual Task Get()
{
var arguments = new object[] { };
var func = requestBuilder.BuildRestResultFuncForMethod("Get", new Type[] { });
return (Task)func(Client, arguments);
}
}
}
32 changes: 32 additions & 0 deletions Refit.Tests/RefitStubs.NetCore2.cs
Expand Up @@ -1241,3 +1241,35 @@ public virtual Task<HttpResponseMessage> Get(int httpstatuscode)
}
}
}

namespace Refit.Tests
{
using Refit.Tests.RefitInternalGenerated;

/// <inheritdoc />
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[global::System.Diagnostics.DebuggerNonUserCode]
[Preserve]
[global::System.Reflection.Obfuscation(Exclude=true)]
partial class AutoGeneratedIValidApi : IValidApi
{
/// <inheritdoc />
public HttpClient Client { get; protected set; }
readonly IRequestBuilder requestBuilder;

/// <inheritdoc />
public AutoGeneratedIValidApi(HttpClient client, IRequestBuilder requestBuilder)
{
Client = client;
this.requestBuilder = requestBuilder;
}

/// <inheritdoc />
public virtual Task Get()
{
var arguments = new object[] { };
var func = requestBuilder.BuildRestResultFuncForMethod("Get", new Type[] { });
return (Task)func(Client, arguments);
}
}
}
54 changes: 54 additions & 0 deletions Refit.Tests/RestService.cs
Expand Up @@ -139,6 +139,12 @@ public interface ITrimTrailingForwardSlashApi
Task Get();
}

public interface IValidApi
{
[Get("/someendpoint")]
Task Get();
}

public class HttpBinGet
{
public Dictionary<string, object> Args { get; set; }
Expand Down Expand Up @@ -1206,5 +1212,53 @@ public void ShouldTrimTrailingForwardSlashFromBaseUrl()

Assert.Equal(fixture.Client.BaseAddress.AbsoluteUri, expectedBaseAddress);
}

[Fact]
public void ShouldThrowArgumentExceptionIfHostUrlIsNull()
{
try
{
RestService.For<IValidApi>(hostUrl: null);
}
catch (ArgumentException ex)
{
Assert.Equal("hostUrl", ex.ParamName);
return;
}

Assert.False(true, "Exception not thrown.");
}

[Fact]
public void ShouldThrowArgumentExceptionIfHostUrlIsEmpty()
{
try
{
RestService.For<IValidApi>(hostUrl: "");
}
catch (ArgumentException ex)
{
Assert.Equal("hostUrl", ex.ParamName);
return;
}

Assert.False(true, "Exception not thrown.");
}

[Fact]
public void ShouldThrowArgumentExceptionIfHostUrlIsWhitespace()
{
try
{
RestService.For<IValidApi>(hostUrl: " ");
}
catch (ArgumentException ex)
{
Assert.Equal("hostUrl", ex.ParamName);
return;
}

Assert.False(true, "Exception not thrown.");
}
}
}
9 changes: 8 additions & 1 deletion Refit/RestService.cs
Expand Up @@ -34,6 +34,13 @@ public static T For<T>(HttpClient client, RefitSettings settings)

public static T For<T>(string hostUrl, RefitSettings settings)
{
if (string.IsNullOrWhiteSpace(hostUrl))
{
throw new ArgumentException(
$"`{nameof(hostUrl)}` must not be null or whitespace.",
nameof(hostUrl));
}

// check to see if user provided custom auth token
HttpMessageHandler innerHandler = null;
if (settings != null)
Expand All @@ -49,7 +56,7 @@ public static T For<T>(string hostUrl, RefitSettings settings)
}
}

var client = new HttpClient(innerHandler ?? new HttpClientHandler()) { BaseAddress = new Uri(hostUrl?.TrimEnd('/')) };
var client = new HttpClient(innerHandler ?? new HttpClientHandler()) { BaseAddress = new Uri(hostUrl.TrimEnd('/')) };
return For<T>(client, settings);
}

Expand Down

0 comments on commit 89d9db7

Please sign in to comment.