Skip to content

Commit a26f019

Browse files
Copilothalter73
andcommitted
Move StreamClientTransport tests into StdioClientTransportTests
Per review feedback, consolidate stream client transport tests into StdioClientTransportTests.cs for consistency with how StreamServerTransport tests live in StdioServerTransportTests.cs. Co-authored-by: halter73 <54385+halter73@users.noreply.github.com>
1 parent 8fddfa1 commit a26f019

2 files changed

Lines changed: 53 additions & 62 deletions

File tree

tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
using ModelContextProtocol.Client;
22
using ModelContextProtocol.Protocol;
33
using ModelContextProtocol.Tests.Utils;
4+
using System.IO.Pipelines;
45
using System.Runtime.InteropServices;
56
using System.Text;
7+
using System.Text.Json;
68

79
namespace ModelContextProtocol.Tests.Transport;
810

911
public class StdioClientTransportTests(ITestOutputHelper testOutputHelper) : LoggedTest(testOutputHelper)
1012
{
1113
public static bool IsStdErrCallbackSupported => !PlatformDetection.IsMonoRuntime;
14+
public static bool IsWindows => PlatformDetection.IsWindows;
1215

1316
[Fact]
1417
public async Task CreateAsync_ValidProcessInvalidServer_Throws()
@@ -136,4 +139,54 @@ public async Task EscapesCliArgumentsCorrectly(string? cliArgumentValue)
136139
var content = Assert.IsType<TextContentBlock>(Assert.Single(result.Content));
137140
Assert.Equal(cliArgumentValue ?? "", content.Text);
138141
}
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+
}
139192
}

tests/ModelContextProtocol.Tests/Transport/StreamClientTransportTests.cs

Lines changed: 0 additions & 62 deletions
This file was deleted.

0 commit comments

Comments
 (0)