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
47 changes: 47 additions & 0 deletions packages/Postgrest/Postgrest.Tests/Options/SchemaProfileTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using FluentAssertions.Execution;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Postgrest.Tests.Models;
using Supabase.Postgrest;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Server;
using static Supabase.Postgrest.Constants;

namespace Postgrest.Tests.Options;

/// <summary>
/// Which profile header carries the configured <see cref="ClientOptions.Schema" /> on a count: PostgREST reads the
/// schema of a <c>HEAD</c> request from <c>Accept-Profile</c>, as it does for a <c>GET</c>.
/// </summary>
[TestClass]
[TestCategory("Contract")]
public class SchemaProfileTests
{
private const string Schema = "personal";

private WireMockServer server = null!;

[TestInitialize]
public void SetUp()
{
this.server = WireMockServer.Start();
this.server.Given(Request.Create().UsingHead())
.RespondWith(Response.Create().WithStatusCode(200).WithHeader("Content-Range", "0-0/1"));
}

[TestCleanup]
public void TearDown() => this.server.Stop();

[TestMethod]
public async Task Count_ShouldSendTheSchemaAsAcceptProfile()
{
await new Client(this.server.Url!, new ClientOptions { Schema = Schema }).Table<Todo>().Count(CountType.Exact);
var headers = this.server.LogEntries.Single().RequestMessage!.Headers!;
using var scope = new AssertionScope();
headers.Should().ContainKey("Accept-Profile").WhoseValue.Should().Equal([Schema], "PostgREST selects the schema of a HEAD request from Accept-Profile");
headers.Should().NotContainKey("Content-Profile", "PostgREST ignores Content-Profile on a HEAD request and falls back to the default schema");
}
}
10 changes: 9 additions & 1 deletion packages/Postgrest/Postgrest.Tests/Options/SchemaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Postgrest.Tests.Options;

/// <summary>
/// A client configured with a non-public <see cref="ClientOptions.Schema" /> resolves tables in that
/// schema, so a query hits the schema-scoped rows rather than the public ones.
/// schema, so a query or a count hits the schema-scoped rows rather than the public ones.
/// </summary>
[TestClass]
[TestCategory("E2E")]
Expand All @@ -23,4 +23,12 @@ public async Task Get_ShouldResolveTablesInTheConfiguredSchema()
var response = await client.Table<User>().Filter(x => x.Username!, Operator.Equals, "leroyjenkins").Get();
response.Models.Should().ContainSingle().Which.Username.Should().Be("leroyjenkins");
}

[TestMethod]
public async Task Count_ShouldCountRowsInTheConfiguredSchema()
{
var client = LocalStack.Client(new ClientOptions { Schema = "personal" });
var count = await client.Table<User>().Filter(x => x.Username!, Operator.Equals, "leroyjenkins").Count(CountType.Exact);
count.Should().Be(1, "leroyjenkins exists only in the personal schema, and PostgREST picks the schema of a HEAD from Accept-Profile");
}
}
5 changes: 4 additions & 1 deletion packages/Postgrest/Postgrest/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public static Dictionary<string, string> PrepareRequestHeaders(HttpMethod method

if (!string.IsNullOrEmpty(options.Schema))
{
headers.Add(method == HttpMethod.Get ? "Accept-Profile" : "Content-Profile", options.Schema);
headers.Add(ProfileHeaderFor(method), options.Schema);
}

if (rangeFrom != int.MinValue)
Expand All @@ -252,4 +252,7 @@ public static Dictionary<string, string> PrepareRequestHeaders(HttpMethod method

return headers;
}

private static string ProfileHeaderFor(HttpMethod method) =>
method == HttpMethod.Get || method == HttpMethod.Head ? "Accept-Profile" : "Content-Profile";
}
Loading