Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Refit.Tests/InterfaceStubGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void GenerateTemplateInfoForInterfaceListSmokeTest()
.ToList();

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

[Fact]
Expand Down
64 changes: 64 additions & 0 deletions Refit.Tests/RefitStubs.Net46.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,38 @@ public virtual Task<ApiResponse<Stream>> GetRemoteFileWithMetadata(string filena
}
}

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 AutoGeneratedITrimTrailingForwardSlashApi : ITrimTrailingForwardSlashApi
{
/// <inheritdoc />
public HttpClient Client { get; protected set; }
readonly IRequestBuilder requestBuilder;

/// <inheritdoc />
public AutoGeneratedITrimTrailingForwardSlashApi(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);
}
}
}

namespace Refit.Tests
{
using Refit.Tests.RefitInternalGenerated;
Expand Down Expand Up @@ -1209,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);
}
}
}
64 changes: 64 additions & 0 deletions Refit.Tests/RefitStubs.NetCore2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,38 @@ public virtual Task<ApiResponse<Stream>> GetRemoteFileWithMetadata(string filena
}
}

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 AutoGeneratedITrimTrailingForwardSlashApi : ITrimTrailingForwardSlashApi
{
/// <inheritdoc />
public HttpClient Client { get; protected set; }
readonly IRequestBuilder requestBuilder;

/// <inheritdoc />
public AutoGeneratedITrimTrailingForwardSlashApi(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);
}
}
}

namespace Refit.Tests
{
using Refit.Tests.RefitInternalGenerated;
Expand Down Expand Up @@ -1209,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);
}
}
}
73 changes: 73 additions & 0 deletions Refit.Tests/RestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,20 @@ public interface IBodylessApi
Task Head();
}

public interface ITrimTrailingForwardSlashApi
{
HttpClient Client { get; }

[Get("/someendpoint")]
Task Get();
}

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

public class HttpBinGet
{
public Dictionary<string, object> Args { get; set; }
Expand Down Expand Up @@ -1187,5 +1201,64 @@ public async Task CanSerializeContentAsXml()

mockHttp.VerifyNoOutstandingExpectation();
}

[Fact]
public void ShouldTrimTrailingForwardSlashFromBaseUrl()
{
var expectedBaseAddress = "http://example.com/api";
var inputBaseAddress = "http://example.com/api/";

var fixture = RestService.For<ITrimTrailingForwardSlashApi>(inputBaseAddress);

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
Original file line number Diff line number Diff line change
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) };
var client = new HttpClient(innerHandler ?? new HttpClientHandler()) { BaseAddress = new Uri(hostUrl.TrimEnd('/')) };
return For<T>(client, settings);
}

Expand Down