Skip to content

Commit

Permalink
Remove the breaking change
Browse files Browse the repository at this point in the history
Signed-off-by: TWEESTY <chausse.nicolas@gmail.com>
  • Loading branch information
TWEESTY committed Feb 10, 2024
1 parent 7c00d75 commit 548ebf3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 33 deletions.
5 changes: 2 additions & 3 deletions src/Dapr.Client/DaprClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ public abstract class DaprClient : IDisposable
/// <param name="appId">
/// An optional <c>app-id</c>. If specified, the <c>app-id</c> will be configured as the value of
/// <see cref="HttpClient.BaseAddress" /> so that relative URIs can be used.
/// If specified, the client could not call different services.
/// </param>
/// <param name="daprEndpoint">The HTTP endpoint of the Dapr process to use for service invocation calls.</param>
/// <param name="daprApiToken">The token to be added to all request headers to Dapr runtime.</param>
Expand All @@ -81,7 +80,7 @@ public static HttpClient CreateInvokeHttpClient(string appId = null, string dapr
{
InnerHandler = new HttpClientHandler(),
DaprApiToken = daprApiToken,
AppId = appId,
DefaultAppId = appId,
};

if (daprEndpoint is string)
Expand Down Expand Up @@ -211,7 +210,7 @@ public abstract Task PublishEventAsync(
string topicName,
Dictionary<string, string> metadata,
CancellationToken cancellationToken = default);

/// <summary>
/// // Bulk Publishes multiple events to the specified topic.
/// </summary>
Expand Down
24 changes: 14 additions & 10 deletions src/Dapr.Client/InvocationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ public string DaprEndpoint
}

/// <summary>
/// Gets or sets the AppId used for service invocation
/// Gets or sets the default AppId used for service invocation
/// </summary>
/// <returns>The AppId used for service invocation</returns>
public string? AppId { get; set; }
public string? DefaultAppId { get; set; }

// Internal for testing
internal string? DaprApiToken
Expand Down Expand Up @@ -136,26 +136,30 @@ internal bool TryRewriteUri(Uri? uri, [NotNullWhen(true)] out Uri? rewritten)
return false;
}

// If the URI has a host which does not correspond to the AppId, it does not make sense.
// We do this check for optimization, to not add some overhead time for the "normal" use case
if (this.AppId is not null && !this.AppId.Equals(uri.Host, StringComparison.CurrentCultureIgnoreCase))
string host;

// It is just for optimization, to not add some overhead time
if (this.DefaultAppId is not null && uri.Host.Equals(this.DefaultAppId, StringComparison.InvariantCultureIgnoreCase))
{
rewritten = null;
return false;
host = this.DefaultAppId;
}
else
{
host = this.GetOriginalHostFromUri(uri);
}

var builder = new UriBuilder(uri)
{
Scheme = this.parsedEndpoint.Scheme,
Host = this.parsedEndpoint.Host,
Port = this.parsedEndpoint.Port,
Path = $"/v1.0/invoke/{this.AppId ?? this.GetOriginalHostFromUri(uri)}/method" + uri.AbsolutePath,
Path = $"/v1.0/invoke/{host}/method" + uri.AbsolutePath,
};

rewritten = builder.Uri;
return true;
}

/// <summary>
/// Get the original host (case sensitive) from the URI (thanks to uri.OriginalString)
/// Mandatory to get the original host if the app id has at least one uppercase and the app id has not been sent to the handler
Expand All @@ -168,7 +172,7 @@ private string GetOriginalHostFromUri(Uri uri)
ArgumentNullException.ThrowIfNull(uri);

// If there is no upper character inside the original string, we can directly return the uri host
if(!uri.OriginalString.Any(char.IsUpper))
if (!uri.OriginalString.Any(char.IsUpper))
{
return uri.Host;
}
Expand Down
41 changes: 21 additions & 20 deletions test/Dapr.Client.Test/InvocationHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public class InvocationHandlerTests
public void DaprEndpoint_InvalidScheme()
{
var handler = new InvocationHandler();
var ex = Assert.Throws<ArgumentException>(() =>
{
var ex = Assert.Throws<ArgumentException>(() =>
{
handler.DaprEndpoint = "ftp://localhost:3500";
});

Expand All @@ -43,7 +43,7 @@ public void DaprEndpoint_InvalidUri()
{
var handler = new InvocationHandler();
Assert.Throws<UriFormatException>(() =>
{
{
handler.DaprEndpoint = "";
});

Expand Down Expand Up @@ -81,34 +81,48 @@ public void TryRewriteUri_FailsForRelativeUris()
[Theory]
[InlineData(null, "http://bank", "https://some.host:3499/v1.0/invoke/bank/method/")]
[InlineData("bank", "http://bank", "https://some.host:3499/v1.0/invoke/bank/method/")]
[InlineData("Bank", "http://bank", "https://some.host:3499/v1.0/invoke/Bank/method/")]
[InlineData("invalid", "http://bank", "https://some.host:3499/v1.0/invoke/bank/method/")]
[InlineData(null, "http://Bank", "https://some.host:3499/v1.0/invoke/Bank/method/")]
[InlineData("Bank", "http://Bank", "https://some.host:3499/v1.0/invoke/Bank/method/")]
[InlineData("bank", "http://Bank", "https://some.host:3499/v1.0/invoke/bank/method/")]
[InlineData("invalid", "http://Bank", "https://some.host:3499/v1.0/invoke/Bank/method/")]
[InlineData(null, "http://bank:3939", "https://some.host:3499/v1.0/invoke/bank/method/")]
[InlineData("bank", "http://bank:3939", "https://some.host:3499/v1.0/invoke/bank/method/")]
[InlineData("invalid", "http://bank:3939", "https://some.host:3499/v1.0/invoke/bank/method/")]
[InlineData(null, "http://Bank:3939", "https://some.host:3499/v1.0/invoke/Bank/method/")]
[InlineData("Bank", "http://Bank:3939", "https://some.host:3499/v1.0/invoke/Bank/method/")]
[InlineData("invalid", "http://Bank:3939", "https://some.host:3499/v1.0/invoke/Bank/method/")]
[InlineData(null, "http://app-id.with.dots", "https://some.host:3499/v1.0/invoke/app-id.with.dots/method/")]
[InlineData("app-id.with.dots", "http://app-id.with.dots", "https://some.host:3499/v1.0/invoke/app-id.with.dots/method/")]
[InlineData("invalid", "http://app-id.with.dots", "https://some.host:3499/v1.0/invoke/app-id.with.dots/method/")]
[InlineData(null, "http://App-id.with.dots", "https://some.host:3499/v1.0/invoke/App-id.with.dots/method/")]
[InlineData("App-id.with.dots", "http://App-id.with.dots", "https://some.host:3499/v1.0/invoke/App-id.with.dots/method/")]
[InlineData("invalid", "http://App-id.with.dots", "https://some.host:3499/v1.0/invoke/App-id.with.dots/method/")]
[InlineData(null, "http://bank:3939/", "https://some.host:3499/v1.0/invoke/bank/method/")]
[InlineData("bank", "http://bank:3939/", "https://some.host:3499/v1.0/invoke/bank/method/")]
[InlineData("invalid", "http://bank:3939/", "https://some.host:3499/v1.0/invoke/bank/method/")]
[InlineData(null, "http://Bank:3939/", "https://some.host:3499/v1.0/invoke/Bank/method/")]
[InlineData("Bank", "http://Bank:3939/", "https://some.host:3499/v1.0/invoke/Bank/method/")]
[InlineData("invalid", "http://Bank:3939/", "https://some.host:3499/v1.0/invoke/Bank/method/")]
[InlineData(null, "http://bank:3939/some/path", "https://some.host:3499/v1.0/invoke/bank/method/some/path")]
[InlineData("bank", "http://bank:3939/some/path", "https://some.host:3499/v1.0/invoke/bank/method/some/path")]
[InlineData("invalid", "http://bank:3939/some/path", "https://some.host:3499/v1.0/invoke/bank/method/some/path")]
[InlineData(null, "http://Bank:3939/some/path", "https://some.host:3499/v1.0/invoke/Bank/method/some/path")]
[InlineData("Bank", "http://Bank:3939/some/path", "https://some.host:3499/v1.0/invoke/Bank/method/some/path")]
[InlineData("invalid", "http://Bank:3939/some/path", "https://some.host:3499/v1.0/invoke/Bank/method/some/path")]
[InlineData(null, "http://bank:3939/some/path?q=test&p=another#fragment", "https://some.host:3499/v1.0/invoke/bank/method/some/path?q=test&p=another#fragment")]
[InlineData("bank", "http://bank:3939/some/path?q=test&p=another#fragment", "https://some.host:3499/v1.0/invoke/bank/method/some/path?q=test&p=another#fragment")]
[InlineData("invalid", "http://bank:3939/some/path?q=test&p=another#fragment", "https://some.host:3499/v1.0/invoke/bank/method/some/path?q=test&p=another#fragment")]
[InlineData(null, "http://Bank:3939/some/path?q=test&p=another#fragment", "https://some.host:3499/v1.0/invoke/Bank/method/some/path?q=test&p=another#fragment")]
[InlineData("Bank", "http://Bank:3939/some/path?q=test&p=another#fragment", "https://some.host:3499/v1.0/invoke/Bank/method/some/path?q=test&p=another#fragment")]
[InlineData("invalid", "http://Bank:3939/some/path?q=test&p=another#fragment", "https://some.host:3499/v1.0/invoke/Bank/method/some/path?q=test&p=another#fragment")]
public void TryRewriteUri_WithNoAppId_RewritesUriToDaprInvoke(string? appId, string uri, string expected)
{
var handler = new InvocationHandler()
{
DaprEndpoint = "https://some.host:3499",
AppId = appId,
DefaultAppId = appId,
};

Assert.True(handler.TryRewriteUri(new Uri(uri), out var rewritten));
Expand All @@ -121,25 +135,12 @@ public async Task SendAsync_InvalidNotSetUri_ThrowsException()
var handler = new InvocationHandler();
var ex = await Assert.ThrowsAsync<ArgumentException>(async () =>
{
await CallSendAsync(handler, new HttpRequestMessage(){ }); // No URI set
await CallSendAsync(handler, new HttpRequestMessage() { }); // No URI set
});

Assert.Contains("The request URI '' is not a valid Dapr service invocation destination.", ex.Message);
}

[Fact]
public async Task SendAsync_InvalidUriWithAppId_ThrowsException()
{
var handler = new InvocationHandler() { AppId = "bank" };
string fakeUrl = "http://invalid/test";
var ex = await Assert.ThrowsAsync<ArgumentException>(async () =>
{
await CallSendAsync(handler, new HttpRequestMessage() { RequestUri = new Uri(fakeUrl) }); // URI does not correspond to the appid
});

Assert.Contains($"The request URI '{fakeUrl}' is not a valid Dapr service invocation destination.", ex.Message);
}

[Fact]
public async Task SendAsync_RewritesUri()
{
Expand Down Expand Up @@ -176,7 +177,7 @@ public async Task SendAsync_RewritesUri_AndAppId()

DaprEndpoint = "https://localhost:5000",
DaprApiToken = null,
AppId = "bank"
DefaultAppId = "bank"
};

var request = new HttpRequestMessage(HttpMethod.Post, uri);
Expand Down Expand Up @@ -221,7 +222,7 @@ private async Task<HttpResponseMessage> CallSendAsync(InvocationHandler handler,

try
{
return await (Task<HttpResponseMessage>)method!.Invoke(handler, new object[]{ message, cancellationToken, })!;
return await (Task<HttpResponseMessage>)method!.Invoke(handler, new object[] { message, cancellationToken, })!;
}
catch (TargetInvocationException tie) // reflection always adds an extra layer of exceptions.
{
Expand Down

0 comments on commit 548ebf3

Please sign in to comment.