Skip to content

Commit

Permalink
Expose headers on NatsSvcMsg and aa test for passing headers in svc r…
Browse files Browse the repository at this point in the history
…equests
  • Loading branch information
niklasfp committed Feb 4, 2024
1 parent 667b77e commit dcf2f22
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/NATS.Client.Services/NatsSvcMsg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public NatsSvcMsg(NatsMsg<T> msg, NatsSvcEndpointBase? endPoint, Exception? exce
/// </summary>
public string? ReplyTo => _msg.ReplyTo;

/// <summary>
/// Pass additional information using name-value pairs.
/// </summary>
public NatsHeaders? Headers => _msg.Headers;

/// <summary>
/// Send a reply with an empty message body.
/// </summary>
Expand Down
63 changes: 59 additions & 4 deletions tests/NATS.Client.Services.Tests/ServicesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,7 @@ await grp2.AddEndpointAsync<int>(
await using var s2 = await svc.AddServiceAsync(
new NatsSvcConfig("s2", "2.0.0")
{
Description = "es-two",
QueueGroup = "q2",
Metadata = new Dictionary<string, string> { { "k1", "v1" }, { "k2", "v2" }, },
StatsHandler = ep => JsonNode.Parse($"{{\"stat-k1\":\"stat-v1\",\"stat-k2\":\"stat-v2\",\"ep_name\": \"{ep.Name}\"}}")!,
Description = "es-two", QueueGroup = "q2", Metadata = new Dictionary<string, string> { { "k1", "v1" }, { "k2", "v2" }, }, StatsHandler = ep => JsonNode.Parse($"{{\"stat-k1\":\"stat-v1\",\"stat-k2\":\"stat-v2\",\"ep_name\": \"{ep.Name}\"}}")!,
},
cancellationToken: cancellationToken);

Expand Down Expand Up @@ -264,4 +261,62 @@ public async Task Add_multiple_service_listeners_ping_info_and_stats()
Assert.Equal("1.0.0", stats.First(s => s.Name == "s1").Version);
Assert.Equal("2.0.0", stats.First(s => s.Name == "s2").Version);
}

[Fact]
public async Task Pass_headers_to_request_and_in_response()
{
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var cancellationToken = cts.Token;

await using var server = NatsServer.Start();
await using var nats = server.CreateClientConnection();
var svc = new NatsSvcContext(nats);

await using var s1 = await svc.AddServiceAsync("s1", "1.0.0", cancellationToken: cancellationToken);

await s1.AddEndpointAsync<int>(
name: "e1",
handler: async m =>
{
if (m.Headers != null)
{
var headers = m.Headers;
if (headers.TryGetValue("foo", out var foo))
{
if (foo != "bar")
{
await m.ReplyErrorAsync(m.Data, "Expected 'foo' = 'bar' header", cancellationToken: cancellationToken);
return;
}

await m.ReplyAsync(m.Data, headers: new NatsHeaders { { "bar", "baz" } }, cancellationToken: cancellationToken);
return;
}
}

await m.ReplyErrorAsync(m.Data, "Missing 'foo' header", cancellationToken: cancellationToken);
},
cancellationToken: cancellationToken);

// With headers
var headers = new NatsHeaders { { "foo", "bar" } };
var response = await nats.RequestAsync<int, int>("e1", 999, headers, cancellationToken: cancellationToken);
Assert.Equal(999, response.Data);
Assert.Equal("baz", response.Headers?["bar"]);

// With headers, but not the expected one.
headers = new NatsHeaders
{
{ "not-the-expected", "4711" },
{ "also-not-the-expected", "4242" },
};
response = await nats.RequestAsync<int, int>("e1", 999, headers, cancellationToken: cancellationToken);
Assert.Equal("999", response.Headers?["Nats-Service-Error-Code"]);
Assert.Equal("Missing 'foo' header", response.Headers?["Nats-Service-Error"]);

// No headers.
response = await nats.RequestAsync<int, int>("e1", 999, cancellationToken: cancellationToken);
Assert.Equal("999", response.Headers?["Nats-Service-Error-Code"]);
Assert.Equal("Missing 'foo' header", response.Headers?["Nats-Service-Error"]);
}
}

0 comments on commit dcf2f22

Please sign in to comment.