Skip to content

Commit 82bd980

Browse files
authored
Add test coverage for AdditionalHeaders in Streamable HTTP transport (#1218)
1 parent a83d619 commit 82bd980

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

tests/ModelContextProtocol.AspNetCore.Tests/MapMcpStreamableHttpTests.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.AspNetCore.Http;
23
using Microsoft.Extensions.DependencyInjection;
34
using Microsoft.Extensions.Primitives;
45
using ModelContextProtocol.Client;
@@ -355,4 +356,60 @@ public async Task EnablePollingAsync_ThrowsInvalidOperationException_WhenNoEvent
355356
Assert.NotNull(capturedException);
356357
Assert.Contains("event stream store", capturedException.Message, StringComparison.OrdinalIgnoreCase);
357358
}
359+
360+
[Fact]
361+
public async Task AdditionalHeaders_AreSent_InPostAndDeleteRequests()
362+
{
363+
Assert.SkipWhen(Stateless, "DELETE requests are not sent in stateless mode due to lack of session ID.");
364+
365+
bool wasPostRequest = false;
366+
bool wasDeleteRequest = false;
367+
368+
Builder.Services.AddMcpServer().WithHttpTransport(ConfigureStateless).WithTools<EchoHttpContextUserTools>();
369+
370+
await using var app = Builder.Build();
371+
372+
app.Use(next =>
373+
{
374+
return async context =>
375+
{
376+
Assert.Equal("Bearer testToken", context.Request.Headers["Authorize"]);
377+
if (context.Request.Method == HttpMethods.Post)
378+
{
379+
wasPostRequest = true;
380+
}
381+
else if (context.Request.Method == HttpMethods.Delete)
382+
{
383+
wasDeleteRequest = true;
384+
}
385+
await next(context);
386+
};
387+
});
388+
389+
app.MapMcp();
390+
391+
await app.StartAsync(TestContext.Current.CancellationToken);
392+
393+
var transportOptions = new HttpClientTransportOptions
394+
{
395+
Endpoint = new("http://localhost:5000/"),
396+
Name = "In-memory Streamable HTTP Client",
397+
TransportMode = HttpTransportMode.StreamableHttp,
398+
AdditionalHeaders = new Dictionary<string, string>
399+
{
400+
["Authorize"] = "Bearer testToken"
401+
},
402+
};
403+
404+
await using var mcpClient = await ConnectAsync(transportOptions: transportOptions);
405+
406+
// Do a tool call to ensure there's more than just the initialize request
407+
await mcpClient.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
408+
409+
// Dispose the client to trigger the DELETE request
410+
await mcpClient.DisposeAsync();
411+
412+
Assert.True(wasPostRequest, "POST request was not made");
413+
Assert.True(wasDeleteRequest, "DELETE request was not made");
414+
}
358415
}

0 commit comments

Comments
 (0)