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
14 changes: 14 additions & 0 deletions packages/Functions/Functions.Tests/ClientContractTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,20 @@ public async Task Invoke_ShouldPreferOptionHeadersOverDynamicHeaders()
this.HeaderOf("x-shared").Should().Be("from-options");
}

[TestMethod]
public async Task Invoke_ShouldSendTheCurrentToken_GivenReusedOptions()
{
var token = "first-token";
this.client.GetHeaders = () => new Dictionary<string, string> { { "Authorization", $"Bearer {token}" } };
this.RespondWith(200, "ok");
var options = new InvokeFunctionOptions();
await this.client.Invoke(FunctionName, options: options);
token = "refreshed-token";
this.server.ResetLogEntries();
await this.client.Invoke(FunctionName, options: options);
this.HeaderOf("Authorization").Should().Be("Bearer refreshed-token", "the Supabase client refreshes the token behind GetHeaders");
}

[TestMethod]
public async Task Invoke_ShouldSendOptionContentType_GivenLowercaseHeaderName()
{
Expand Down
17 changes: 7 additions & 10 deletions packages/Functions/Functions/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,23 +171,20 @@ private async Task<HttpResponseMessage> HandleRequest(
{
options ??= new InvokeFunctionOptions();

if (this.GetHeaders != null)
{
options.Headers = this.GetHeaders().MergeLeft(options.Headers);
}
var headers = (this.GetHeaders?.Invoke() ?? new Dictionary<string, string>()).MergeLeft(options.Headers);

token ??= this.token;
if (!string.IsNullOrEmpty(token))
{
options.Headers["Authorization"] = $"Bearer {token}";
headers["Authorization"] = $"Bearer {token}";
}

options.Headers["X-Client-Info"] = Util.GetAssemblyVersion(typeof(Client));
headers["X-Client-Info"] = Util.GetAssemblyVersion(typeof(Client));

var region = options.FunctionRegion ?? this.region;
if (region != FunctionRegion.Any)
{
options.Headers["x-region"] = region.ToString();
headers["x-region"] = region.ToString();
}

var uri = BuildUri(url);
Expand All @@ -202,7 +199,7 @@ private async Task<HttpResponseMessage> HandleRequest(

try
{
var response = await RetryExecutor.SendAsync(this.httpClient, () => BuildRequestMessage(options, uri), this.Options.Retry, completionOption, linkedCts.Token);
var response = await RetryExecutor.SendAsync(this.httpClient, () => BuildRequestMessage(options, headers, uri), this.Options.Retry, completionOption, linkedCts.Token).ConfigureAwait(false);
statusCode = (int) response.StatusCode;
var isRelayError = response.Headers.Contains("x-relay-error");
activity.SetHttpResponseTags(statusCode.Value);
Expand Down Expand Up @@ -288,12 +285,12 @@ private static Uri BuildUri(string url)
return builder.Uri;
}

private static HttpRequestMessage BuildRequestMessage(InvokeFunctionOptions options, Uri uri)
private static HttpRequestMessage BuildRequestMessage(InvokeFunctionOptions options, Dictionary<string, string> headers, Uri uri)
{
var content = new StringContent(JsonSerializer.Serialize(options.Body, SerializerOptions), Encoding.UTF8, "application/json");
var request = new HttpRequestMessage(options.HttpMethod, uri) { Content = content };

foreach (var kvp in options.Headers)
foreach (var kvp in headers)
{
if (string.Equals(kvp.Key, "Content-Type", StringComparison.OrdinalIgnoreCase))
{
Expand Down
Loading