-
Notifications
You must be signed in to change notification settings - Fork 681
Expand file tree
/
Copy pathHttpClientTransportTests.cs
More file actions
329 lines (284 loc) · 13.1 KB
/
HttpClientTransportTests.cs
File metadata and controls
329 lines (284 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
using ModelContextProtocol.Client;
using ModelContextProtocol.Protocol;
using ModelContextProtocol.Tests.Utils;
using System.Net;
using System.Text;
namespace ModelContextProtocol.Tests.Transport;
public class HttpClientTransportTests : LoggedTest
{
private readonly HttpClientTransportOptions _transportOptions;
public HttpClientTransportTests(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
{
_transportOptions = new HttpClientTransportOptions
{
Endpoint = new Uri("http://localhost:8080"),
ConnectionTimeout = TimeSpan.FromSeconds(2),
Name = "Test Server",
TransportMode = HttpTransportMode.Sse,
AdditionalHeaders = new Dictionary<string, string>
{
["test"] = "header"
}
};
}
[Fact]
public void Constructor_Throws_For_Null_Options()
{
var exception = Assert.Throws<ArgumentNullException>(() => new HttpClientTransport(null!, LoggerFactory));
Assert.Equal("transportOptions", exception.ParamName);
}
[Fact]
public void Constructor_Throws_For_Null_HttpClient()
{
var exception = Assert.Throws<ArgumentNullException>(() => new HttpClientTransport(_transportOptions, httpClient: null!, LoggerFactory));
Assert.Equal("httpClient", exception.ParamName);
}
[Fact]
public async Task ConnectAsync_Should_Connect_Successfully()
{
using var mockHttpHandler = new MockHttpHandler();
using var httpClient = new HttpClient(mockHttpHandler);
await using var transport = new HttpClientTransport(_transportOptions, httpClient, LoggerFactory);
bool firstCall = true;
mockHttpHandler.RequestHandler = (request) =>
{
firstCall = false;
return Task.FromResult(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent("event: endpoint\r\ndata: http://localhost\r\n\r\n")
});
};
await using var session = await transport.ConnectAsync(TestContext.Current.CancellationToken);
Assert.NotNull(session);
Assert.False(firstCall);
}
[Fact]
public async Task ConnectAsync_Throws_Exception_On_Failure()
{
using var mockHttpHandler = new MockHttpHandler();
using var httpClient = new HttpClient(mockHttpHandler);
await using var transport = new HttpClientTransport(_transportOptions, httpClient, LoggerFactory);
var retries = 0;
mockHttpHandler.RequestHandler = (request) =>
{
retries++;
throw new Exception("Test exception");
};
var exception = await Assert.ThrowsAsync<Exception>(() => transport.ConnectAsync(TestContext.Current.CancellationToken));
Assert.Equal("Test exception", exception.Message);
Assert.Equal(1, retries);
}
[Fact]
public async Task ConnectAsync_Throws_HttpRequestException_With_ResponseBody_On_ErrorStatusCode()
{
using var mockHttpHandler = new MockHttpHandler();
using var httpClient = new HttpClient(mockHttpHandler);
await using var transport = new HttpClientTransport(_transportOptions, httpClient, LoggerFactory);
const string errorDetails = "Bad request: Invalid MCP protocol version";
mockHttpHandler.RequestHandler = (request) =>
{
return Task.FromResult(new HttpResponseMessage
{
StatusCode = HttpStatusCode.BadRequest,
ReasonPhrase = "Bad Request",
Content = new StringContent(errorDetails)
});
};
var httpException = await Assert.ThrowsAsync<HttpRequestException>(() => transport.ConnectAsync(TestContext.Current.CancellationToken));
Assert.Contains(errorDetails, httpException.Message);
Assert.Contains("400", httpException.Message);
#if NET
Assert.Equal(HttpStatusCode.BadRequest, httpException.StatusCode);
#endif
}
[Fact]
public async Task SendMessageAsync_Handles_Accepted_Response()
{
using var mockHttpHandler = new MockHttpHandler();
using var httpClient = new HttpClient(mockHttpHandler);
await using var transport = new HttpClientTransport(_transportOptions, httpClient, LoggerFactory);
var firstCall = true;
mockHttpHandler.RequestHandler = (request) =>
{
if (request.Method == HttpMethod.Post && request.RequestUri?.AbsoluteUri == "http://localhost:8080/sseendpoint")
{
return Task.FromResult(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent("accepted")
});
}
else
{
if (!firstCall)
throw new IOException("Abort");
else
firstCall = false;
return Task.FromResult(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent("event: endpoint\r\ndata: /sseendpoint\r\n\r\n")
});
}
};
await using var session = await transport.ConnectAsync(TestContext.Current.CancellationToken);
await session.SendMessageAsync(new JsonRpcRequest { Method = RequestMethods.Initialize, Id = new RequestId(44) }, CancellationToken.None);
Assert.True(true);
}
[Fact]
public async Task SendMessageAsync_Throws_HttpRequestException_With_ResponseBody_On_ErrorStatusCode()
{
using var mockHttpHandler = new MockHttpHandler();
using var httpClient = new HttpClient(mockHttpHandler);
await using var transport = new HttpClientTransport(_transportOptions, httpClient, LoggerFactory);
var firstCall = true;
const string errorDetails = "Invalid JSON-RPC message format: missing 'id' field";
mockHttpHandler.RequestHandler = (request) =>
{
if (request.Method == HttpMethod.Post && request.RequestUri?.AbsoluteUri == "http://localhost:8080/sseendpoint")
{
return Task.FromResult(new HttpResponseMessage
{
StatusCode = HttpStatusCode.BadRequest,
ReasonPhrase = "Bad Request",
Content = new StringContent(errorDetails)
});
}
else
{
if (!firstCall)
throw new IOException("Abort");
else
firstCall = false;
return Task.FromResult(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent("event: endpoint\r\ndata: /sseendpoint\r\n\r\n")
});
}
};
await using var session = await transport.ConnectAsync(TestContext.Current.CancellationToken);
var httpException = await Assert.ThrowsAsync<HttpRequestException>(() =>
session.SendMessageAsync(new JsonRpcRequest { Method = RequestMethods.Initialize, Id = new RequestId(44) }, CancellationToken.None));
Assert.Contains(errorDetails, httpException.Message);
Assert.Contains("400", httpException.Message);
#if NET
Assert.Equal(HttpStatusCode.BadRequest, httpException.StatusCode);
#endif
}
[Fact]
public async Task ReceiveMessagesAsync_Handles_Messages()
{
using var mockHttpHandler = new MockHttpHandler();
using var httpClient = new HttpClient(mockHttpHandler);
await using var transport = new HttpClientTransport(_transportOptions, httpClient, LoggerFactory);
var callIndex = 0;
mockHttpHandler.RequestHandler = (request) =>
{
callIndex++;
if (callIndex == 1)
{
return Task.FromResult(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent("event: endpoint\r\ndata: /sseendpoint\r\n\r\nevent: message\r\ndata: {\"jsonrpc\":\"2.0\", \"id\": \"44\", \"method\": \"test\", \"params\": null}\r\n\r\n")
});
}
throw new IOException("Abort");
};
await using var session = await transport.ConnectAsync(TestContext.Current.CancellationToken);
Assert.True(session.MessageReader.TryRead(out var message));
Assert.NotNull(message);
Assert.IsType<JsonRpcRequest>(message);
Assert.Equal("44", ((JsonRpcRequest)message).Id.ToString());
}
[Fact]
public async Task DisposeAsync_Should_Dispose_Resources()
{
using var mockHttpHandler = new MockHttpHandler();
using var httpClient = new HttpClient(mockHttpHandler);
mockHttpHandler.RequestHandler = request =>
{
return Task.FromResult(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent("event: endpoint\r\ndata: http://localhost\r\n\r\n")
});
};
await using var transport = new HttpClientTransport(_transportOptions, httpClient, LoggerFactory);
await using var session = await transport.ConnectAsync(TestContext.Current.CancellationToken);
await session.DisposeAsync();
var transportBase = Assert.IsAssignableFrom<TransportBase>(session);
Assert.False(transportBase.IsConnected);
}
[Fact]
public async Task StreamableHttp_InitialGetSseConnection_DoesNotCountAgainstMaxReconnectionAttempts()
{
// Arrange: The initial GET SSE connection (with no Last-Event-ID) is the initial connection,
// not a reconnection. It should not count against MaxReconnectionAttempts.
// With MaxReconnectionAttempts=2, we expect 1 initial + 2 reconnection = 3 total GET requests.
const int MaxReconnectionAttempts = 2;
var getRequestCount = 0;
var allGetRequestsDone = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
var options = new HttpClientTransportOptions
{
Endpoint = new Uri("http://localhost:8080"),
TransportMode = HttpTransportMode.StreamableHttp,
MaxReconnectionAttempts = MaxReconnectionAttempts,
DefaultReconnectionInterval = TimeSpan.FromMilliseconds(1),
};
using var mockHttpHandler = new MockHttpHandler();
using var httpClient = new HttpClient(mockHttpHandler);
await using var transport = new HttpClientTransport(options, httpClient, LoggerFactory);
mockHttpHandler.RequestHandler = (request) =>
{
if (request.Method == HttpMethod.Post)
{
// Return a successful initialize response with a session-id header.
// This triggers ReceiveUnsolicitedMessagesAsync which starts the GET SSE stream.
var response = new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(
"""{"jsonrpc":"2.0","id":1,"result":{"protocolVersion":"2025-03-26","capabilities":{},"serverInfo":{"name":"TestServer","version":"1.0.0"}}}""",
Encoding.UTF8,
"application/json"),
};
response.Headers.Add("Mcp-Session-Id", "test-session");
return Task.FromResult(response);
}
if (request.Method == HttpMethod.Get)
{
// Return 500 for all GET SSE requests to force the retry loop to exhaust all attempts.
var count = Interlocked.Increment(ref getRequestCount);
if (count == 1 + MaxReconnectionAttempts)
{
allGetRequestsDone.TrySetResult(true);
}
return Task.FromResult(new HttpResponseMessage
{
StatusCode = HttpStatusCode.InternalServerError,
});
}
if (request.Method == HttpMethod.Delete)
{
return Task.FromResult(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
});
}
throw new InvalidOperationException($"Unexpected request: {request.Method}");
};
// Act - Connect and send the initialize request, which starts the background GET SSE task.
await using var session = await transport.ConnectAsync(TestContext.Current.CancellationToken);
await session.SendMessageAsync(
new JsonRpcRequest { Method = RequestMethods.Initialize, Id = new RequestId(1) },
TestContext.Current.CancellationToken);
// Wait for all expected GET requests to be made before disposing.
await allGetRequestsDone.Task.WaitAsync(TimeSpan.FromSeconds(10), TestContext.Current.CancellationToken);
// Assert - Total GET requests = 1 initial connection + MaxReconnectionAttempts reconnections.
Assert.Equal(1 + MaxReconnectionAttempts, getRequestCount);
}
}