// ------------------------------------------------------------ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. // ------------------------------------------------------------ using System; using System.Collections.Concurrent; using System.Net.Http; using System.Threading; using System.Threading.Tasks; using Dapr.Actors.Client; using Dapr.Actors.Communication; using FluentAssertions; using Moq; using Xunit; namespace Dapr.Actors.Test { public class ApiTokenTests { [Fact] public void CreateProxyWithRemoting_WithApiToken() { var actorId = new ActorId("abc"); var handler = new TestHttpClientHandler(); var options = new ActorProxyOptions { DaprApiToken = "test_token", }; var factory = new ActorProxyFactory(options, handler); var mockDaprInteractor = new Mock(); var proxy = factory.CreateActorProxy(actorId, "TestActor", null, mockDaprInteractor.Object); mockDaprInteractor.Setup(interactor => interactor.InvokeActorMethodWithRemotingAsync(It.IsAny(), It.IsAny(), It.IsAny())); proxy.SetCountAsync(1, new CancellationToken()); // handler.Requests.TryDequeue(out var entry).Should().BeTrue(); // var headerValues = entry.Request.Headers.GetValues("dapr-api-token"); // headerValues.Should().Contain("test_token"); byte[] headerValue = null; mockDaprInteractor.Verify(interactor => interactor.InvokeActorMethodWithRemotingAsync(It.IsAny(), It.Is(a => a.GetHeader().TryGetHeaderValue("dapr-api-token", out headerValue) == false), It.IsAny()), Times.Once); } [Fact] public void CreateProxyWithRemoting_WithNoApiToken() { var actorId = new ActorId("abc"); var handler = new TestHttpClientHandler(); var mockDaprInteractor = new Mock(); var factory = new ActorProxyFactory(null, handler); var proxy = factory.CreateActorProxy(actorId, "TestActor", null, mockDaprInteractor.Object); mockDaprInteractor.Setup(interactor => interactor.InvokeActorMethodWithRemotingAsync(It.IsAny(), It.IsAny(), It.IsAny())); proxy.SetCountAsync(1, new CancellationToken()); byte[] headerValue = null; mockDaprInteractor.Verify(interactor => interactor.InvokeActorMethodWithRemotingAsync(It.IsAny(), It.Is(a => a.GetHeader().TryGetHeaderValue("dapr-api-token", out headerValue) == false), It.IsAny()), Times.Once); } // [Fact] // public async void CreateProxyWithNoRemoting_WithApiToken() // { // var actorId = new ActorId("abc"); // var handler = new TestHttpClientHandler(); // var options = new ActorProxyOptions // { // DaprApiToken = "test_token", // }; // var factory = new ActorProxyFactory(options, handler); // var mockDaprInteractor = new Mock(); // mockDaprInteractor.Setup(interactor => interactor.InvokeActorMethodWithoutRemotingAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())); // var proxy = factory.Create(actorId, "TestActor", null, mockDaprInteractor.Object); // await proxy.InvokeMethodAsync("SetCountAsync", 1, new CancellationToken()); // // handler.Requests.TryDequeue(out var entry).Should().BeTrue(); // // var headerValues = entry.Request.Headers.GetValues("dapr-api-token"); // // headerValues.Should().Contain("test_token"); // byte[] headerValue = null; // mockDaprInteractor.Verify(interactor => interactor.InvokeActorMethodWithoutRemotingAsync(It.IsAny(), It.Is(a => a.GetHeader().TryGetHeaderValue("dapr-api-token", out headerValue) == false), It.IsAny()), Times.Once); // } [Fact(Skip = "Failing due to #573")] public void CreateProxyWithNoRemoting_WithNoApiToken() { var actorId = new ActorId("abc"); var handler = new TestHttpClientHandler(); var factory = new ActorProxyFactory(null, handler); var proxy = factory.Create(actorId, "TestActor"); var task = proxy.InvokeMethodAsync("SetCountAsync", 1, new CancellationToken()); handler.Requests.TryDequeue(out var entry).Should().BeTrue(); Action action = () => entry.Request.Headers.GetValues("dapr-api-token"); action.Should().Throw(); } public class Entry { public Entry(HttpRequestMessage request) { this.Request = request; this.Completion = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); } public TaskCompletionSource Completion { get; } public HttpRequestMessage Request { get; } } private class TestHttpClientHandler : HttpClientHandler { public TestHttpClientHandler() { this.Requests = new ConcurrentQueue(); } public ConcurrentQueue Requests { get; } public Action Handler { get; set; } protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { var entry = new Entry(request); this.Handler?.Invoke(entry); this.Requests.Enqueue(entry); using (cancellationToken.Register(() => entry.Completion.TrySetCanceled())) { return await entry.Completion.Task.ConfigureAwait(false); } } } } }