Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
nytian committed Oct 8, 2024
1 parent 48df767 commit 376fb69
Showing 1 changed file with 66 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Security.Claims;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.DurableTask.Client;
using Microsoft.DurableTask.Client.Grpc;
Expand All @@ -10,7 +11,7 @@ namespace Microsoft.Azure.Functions.Worker.Tests
/// </summary>
public class FunctionsDurableTaskClientTests
{
private FunctionsDurableTaskClient GetTestFunctionsDurableTaskClient(string baseUrl = null)
private FunctionsDurableTaskClient GetTestFunctionsDurableTaskClient(string? baseUrl = null)
{
// construct mock client

Expand Down Expand Up @@ -59,17 +60,34 @@ public async void TerminateDoesNotThrow()
/// Test that the `CreateHttpManagementPayload` method returns the expected payload structure without HttpRequestData.
/// </summary>
[Fact]
public void CreateHttpManagementPayload_WithBaseUrl_ReturnsExpectedStructure()
public void CreateHttpManagementPayload_WithBaseUrl()
{
string BaseUrl = "http://localhost:7071/runtime/webhooks/durabletask";
FunctionsDurableTaskClient client = this.GetTestFunctionsDurableTaskClient(BaseUrl);
string instanceId = "testInstanceId";
string instanceId = "testInstanceIdWithHostBaseUrl";

dynamic payload = client.CreateHttpManagementPayload(instanceId);

AssertHttpManagementPayload(payload, BaseUrl, instanceId);
}

/// <summary>
/// Test that the `CreateHttpManagementPayload` method returns the expected payload structure with HttpRequestData.
/// </summary>
[Fact]
public void CreateHttpManagementPayload_WithHttpRequestData()
{
string requestUrl = "http://localhost:7075/orchestrators/E1_HelloSequence";
FunctionsDurableTaskClient client = this.GetTestFunctionsDurableTaskClient();
string instanceId = "testInstanceIdWithRequest";
var context = new TestFunctionContext();
var request = new TestHttpRequestData(context, new Uri(requestUrl));

dynamic payload = client.CreateHttpManagementPayload(instanceId, request);

AssertHttpManagementPayload(payload, "http://localhost:7075/runtime/webhooks/durabletask", instanceId);
}

private static void AssertHttpManagementPayload(dynamic payload, string BaseUrl, string instanceId)
{
Assert.Equal(instanceId, payload.id);
Expand All @@ -81,4 +99,49 @@ private static void AssertHttpManagementPayload(dynamic payload, string BaseUrl,
Assert.Equal($"{BaseUrl}/instances/{instanceId}/resume?reason={{{{text}}}}", payload.resumePostUri);
}
}

/// <summary>
/// A minimal implementation of FunctionContext for testing purposes.
/// It's used to create a TestHttpRequestData instance, which requires a FunctionContext.
/// </summary>
public class TestFunctionContext : FunctionContext
{
public override string InvocationId => throw new NotImplementedException();
public override string FunctionId => throw new NotImplementedException();
public override TraceContext TraceContext => throw new NotImplementedException();
public override BindingContext BindingContext => throw new NotImplementedException();
public override RetryContext RetryContext => throw new NotImplementedException();
public override IServiceProvider InstanceServices { get; set; } = new EmptyServiceProvider();
public override FunctionDefinition FunctionDefinition => throw new NotImplementedException();
public override IDictionary<object, object> Items { get; set; } = new Dictionary<object, object>();
public override IInvocationFeatures Features => throw new NotImplementedException();
}

/// <summary>
/// A minimal implementation of IServiceProvider for testing purposes.
/// </summary>
public class EmptyServiceProvider : IServiceProvider
{
public object GetService(Type serviceType) => null;

Check warning on line 125 in test/Worker.Extensions.DurableTask.Tests/FunctionsDurableTaskClientTests.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.

Check warning on line 125 in test/Worker.Extensions.DurableTask.Tests/FunctionsDurableTaskClientTests.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.

Check warning on line 125 in test/Worker.Extensions.DurableTask.Tests/FunctionsDurableTaskClientTests.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.

Check warning on line 125 in test/Worker.Extensions.DurableTask.Tests/FunctionsDurableTaskClientTests.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.

Check warning on line 125 in test/Worker.Extensions.DurableTask.Tests/FunctionsDurableTaskClientTests.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Possible null reference return.

Check warning on line 125 in test/Worker.Extensions.DurableTask.Tests/FunctionsDurableTaskClientTests.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.
}

// <summary>
/// A test implementation of HttpRequestData used for unit testing.
/// This class allows us to create instances of HttpRequestData, which is normally abstract.
/// </summary>
public class TestHttpRequestData : HttpRequestData
{
public TestHttpRequestData(FunctionContext functionContext, Uri url) : base(functionContext)
{
Url = url;
}

public override Stream Body => throw new NotImplementedException();
public override HttpHeadersCollection Headers => throw new NotImplementedException();
public override IReadOnlyCollection<IHttpCookie> Cookies => throw new NotImplementedException();
public override Uri Url { get; }
public override IEnumerable<ClaimsIdentity> Identities => throw new NotImplementedException();
public override string Method => throw new NotImplementedException();
public override HttpResponseData CreateResponse() => throw new NotImplementedException();
}
}

0 comments on commit 376fb69

Please sign in to comment.