Skip to content

Commit d267959

Browse files
authored
Expose Headers on NatsSvcMsg (#371)
* Expose headers on NatsSvcMsg and aa test for passing headers in svc requests * Revert accidental format change --------- Co-authored-by: Niklas Petersen <niklasfp@users.noreply.github.com>
1 parent 667b77e commit d267959

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

src/NATS.Client.Services/NatsSvcMsg.cs

+5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public NatsSvcMsg(NatsMsg<T> msg, NatsSvcEndpointBase? endPoint, Exception? exce
4747
/// </summary>
4848
public string? ReplyTo => _msg.ReplyTo;
4949

50+
/// <summary>
51+
/// Pass additional information using name-value pairs.
52+
/// </summary>
53+
public NatsHeaders? Headers => _msg.Headers;
54+
5055
/// <summary>
5156
/// Send a reply with an empty message body.
5257
/// </summary>

tests/NATS.Client.Services.Tests/ServicesTests.cs

+58
Original file line numberDiff line numberDiff line change
@@ -264,4 +264,62 @@ public async Task Add_multiple_service_listeners_ping_info_and_stats()
264264
Assert.Equal("1.0.0", stats.First(s => s.Name == "s1").Version);
265265
Assert.Equal("2.0.0", stats.First(s => s.Name == "s2").Version);
266266
}
267+
268+
[Fact]
269+
public async Task Pass_headers_to_request_and_in_response()
270+
{
271+
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
272+
var cancellationToken = cts.Token;
273+
274+
await using var server = NatsServer.Start();
275+
await using var nats = server.CreateClientConnection();
276+
var svc = new NatsSvcContext(nats);
277+
278+
await using var s1 = await svc.AddServiceAsync("s1", "1.0.0", cancellationToken: cancellationToken);
279+
280+
await s1.AddEndpointAsync<int>(
281+
name: "e1",
282+
handler: async m =>
283+
{
284+
if (m.Headers != null)
285+
{
286+
var headers = m.Headers;
287+
if (headers.TryGetValue("foo", out var foo))
288+
{
289+
if (foo != "bar")
290+
{
291+
await m.ReplyErrorAsync(m.Data, "Expected 'foo' = 'bar' header", cancellationToken: cancellationToken);
292+
return;
293+
}
294+
295+
await m.ReplyAsync(m.Data, headers: new NatsHeaders { { "bar", "baz" } }, cancellationToken: cancellationToken);
296+
return;
297+
}
298+
}
299+
300+
await m.ReplyErrorAsync(m.Data, "Missing 'foo' header", cancellationToken: cancellationToken);
301+
},
302+
cancellationToken: cancellationToken);
303+
304+
// With headers
305+
var headers = new NatsHeaders { { "foo", "bar" } };
306+
var response = await nats.RequestAsync<int, int>("e1", 999, headers, cancellationToken: cancellationToken);
307+
Assert.Equal(999, response.Data);
308+
Assert.Equal("baz", response.Headers?["bar"]);
309+
310+
// With headers, but not the expected one.
311+
headers = new NatsHeaders
312+
{
313+
{ "not-the-expected", "4711" },
314+
{ "also-not-the-expected", "4242" },
315+
};
316+
response = await nats.RequestAsync<int, int>("e1", 999, headers, cancellationToken: cancellationToken);
317+
Assert.Equal("999", response.Headers?["Nats-Service-Error-Code"]);
318+
Assert.Equal("Missing 'foo' header", response.Headers?["Nats-Service-Error"]);
319+
320+
// No headers.
321+
response = await nats.RequestAsync<int, int>("e1", 999, cancellationToken: cancellationToken);
322+
Assert.Equal("999", response.Headers?["Nats-Service-Error-Code"]);
323+
Assert.Equal("Missing 'foo' header", response.Headers?["Nats-Service-Error"]);
324+
}
267325
}

0 commit comments

Comments
 (0)