|
1 | 1 | using ModelContextProtocol.Client; |
2 | 2 | using ModelContextProtocol.Protocol; |
3 | 3 | using ModelContextProtocol.Tests.Utils; |
| 4 | +using System.IO.Pipelines; |
4 | 5 | using System.Runtime.InteropServices; |
5 | 6 | using System.Text; |
| 7 | +using System.Text.Json; |
6 | 8 |
|
7 | 9 | namespace ModelContextProtocol.Tests.Transport; |
8 | 10 |
|
9 | 11 | public class StdioClientTransportTests(ITestOutputHelper testOutputHelper) : LoggedTest(testOutputHelper) |
10 | 12 | { |
11 | 13 | public static bool IsStdErrCallbackSupported => !PlatformDetection.IsMonoRuntime; |
| 14 | + public static bool IsWindows => PlatformDetection.IsWindows; |
12 | 15 |
|
13 | 16 | [Fact] |
14 | 17 | public async Task CreateAsync_ValidProcessInvalidServer_Throws() |
@@ -136,4 +139,54 @@ public async Task EscapesCliArgumentsCorrectly(string? cliArgumentValue) |
136 | 139 | var content = Assert.IsType<TextContentBlock>(Assert.Single(result.Content)); |
137 | 140 | Assert.Equal(cliArgumentValue ?? "", content.Text); |
138 | 141 | } |
| 142 | + |
| 143 | + [Fact] |
| 144 | + public async Task SendMessageAsync_Should_Use_LF_Not_CRLF() |
| 145 | + { |
| 146 | + using var serverInput = new MemoryStream(); |
| 147 | + Pipe serverOutputPipe = new(); |
| 148 | + |
| 149 | + var transport = new StreamClientTransport(serverInput, serverOutputPipe.Reader.AsStream(), LoggerFactory); |
| 150 | + await using var sessionTransport = await transport.ConnectAsync(TestContext.Current.CancellationToken); |
| 151 | + |
| 152 | + var message = new JsonRpcRequest { Method = "test", Id = new RequestId(44) }; |
| 153 | + |
| 154 | + await sessionTransport.SendMessageAsync(message, TestContext.Current.CancellationToken); |
| 155 | + |
| 156 | + byte[] bytes = serverInput.ToArray(); |
| 157 | + |
| 158 | + // The output should end with exactly \n (0x0A), not \r\n (0x0D 0x0A). |
| 159 | + Assert.True(bytes.Length > 1, "Output should contain message data"); |
| 160 | + Assert.Equal((byte)'\n', bytes[^1]); |
| 161 | + Assert.NotEqual((byte)'\r', bytes[^2]); |
| 162 | + |
| 163 | + // Also verify the JSON content is valid |
| 164 | + var json = Encoding.UTF8.GetString(bytes).TrimEnd('\n'); |
| 165 | + var expected = JsonSerializer.Serialize(message, McpJsonUtilities.DefaultOptions); |
| 166 | + Assert.Equal(expected, json); |
| 167 | + } |
| 168 | + |
| 169 | + [Fact(Skip = "Non-Windows platform", SkipUnless = nameof(IsWindows))] |
| 170 | + public async Task ReadMessagesAsync_Should_Accept_CRLF_Delimited_Messages() |
| 171 | + { |
| 172 | + Pipe serverInputPipe = new(); |
| 173 | + Pipe serverOutputPipe = new(); |
| 174 | + |
| 175 | + var transport = new StreamClientTransport(serverInputPipe.Writer.AsStream(), serverOutputPipe.Reader.AsStream(), LoggerFactory); |
| 176 | + await using var sessionTransport = await transport.ConnectAsync(TestContext.Current.CancellationToken); |
| 177 | + |
| 178 | + var message = new JsonRpcRequest { Method = "test", Id = new RequestId(44) }; |
| 179 | + var json = JsonSerializer.Serialize(message, McpJsonUtilities.DefaultOptions); |
| 180 | + |
| 181 | + // Write a \r\n-delimited message to the server's output (which the client reads) |
| 182 | + await serverOutputPipe.Writer.WriteAsync(Encoding.UTF8.GetBytes($"{json}\r\n"), TestContext.Current.CancellationToken); |
| 183 | + |
| 184 | + var canRead = await sessionTransport.MessageReader.WaitToReadAsync(TestContext.Current.CancellationToken); |
| 185 | + |
| 186 | + Assert.True(canRead, "Should be able to read a \\r\\n-delimited message"); |
| 187 | + Assert.True(sessionTransport.MessageReader.TryPeek(out var readMessage)); |
| 188 | + Assert.NotNull(readMessage); |
| 189 | + Assert.IsType<JsonRpcRequest>(readMessage); |
| 190 | + Assert.Equal("44", ((JsonRpcRequest)readMessage).Id.ToString()); |
| 191 | + } |
139 | 192 | } |
0 commit comments