Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to marshal across FunctionContext.Items into HttpContext.… #5397

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -43,6 +43,11 @@ public AzureHttpContext(HttpRequestData requestData)
{
request.Headers.TryAdd(key, new StringValues(value.ToArray()));
}

foreach(var (key, value) in requestData.FunctionContext.Items)
{
Items.Add(key, value);
}
}

public override IFeatureCollection Features => _innerContext.Features;
Expand Down
@@ -1,8 +1,10 @@
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using HotChocolate.AzureFunctions.IsolatedProcess.Tests.Helpers;
using HotChocolate.Types;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -34,8 +36,7 @@ public async Task AzFuncIsolatedProcess_EndToEndTestAsync()
}");

// Execute Query Test for end-to-end validation...
// NOTE: This uses the new Az Func Isolated Process extension
// to execute via HttpRequestData...
// NOTE: This uses the new Az Func Isolated Process extension to execute via HttpRequestData...
var response = await requestExecutor.ExecuteAsync(request).ConfigureAwait(false);

// Read, Parse & Validate the response...
Expand All @@ -47,6 +48,53 @@ public async Task AzFuncIsolatedProcess_EndToEndTestAsync()
Assert.Equal("Luke Skywalker",json.data.person.ToString());
}

[Fact]
public async Task AzFuncIsolatedProcess_FunctionsContextItemsTestAsync()
{
const string DarkSideLeaderKey = "DarkSideLeader";

var host = new MockIsolatedProcessHostBuilder()
.AddGraphQLFunction(graphQL =>
{
graphQL.AddQueryType(
d => d.Name("Query").Field("person").Resolve(ctx =>
{

var darkSideLeader = ctx.ContextData.TryGetValue(nameof(HttpContext), out var httpContext)
? (httpContext as HttpContext)?.Items[DarkSideLeaderKey] as string
: default;

return darkSideLeader;
}));
})
.Build();

// The executor should resolve without error as a Required service...
var requestExecutor = host.Services.GetRequiredService<IGraphQLRequestExecutor>();

// Build an HttpRequestData that is valid for the Isolated Process to execute with...
var request = TestHttpRequestDataHelper.NewGraphQLHttpRequestData(
host.Services,
@"query {
person
}");

//Set Up our global Items now available from the Functions Context...
request.FunctionContext.Items.Add(DarkSideLeaderKey, "Darth Vader");

// Execute Query Test for end-to-end validation...
// NOTE: This uses the new Az Func Isolated Process extension to execute via HttpRequestData...
var response = await requestExecutor.ExecuteAsync(request).ConfigureAwait(false);

// Read, Parse & Validate the response...
var resultContent = await ReadResponseAsStringAsync(response).ConfigureAwait(false);
Assert.False(string.IsNullOrWhiteSpace(resultContent));

dynamic json = JObject.Parse(resultContent!);
Assert.Null(json.errors);
Assert.Equal("Darth Vader", json.data.person.ToString());
}

[Fact]
public async Task AzFuncIsolatedProcess_BananaCakePopTestAsync()
{
Expand Down