dotnet/yarp · error · Exception
Expected to receive '{textToSend}', but got '{text}'.
Error message
Expected to receive '{textToSend}', but got '{text}'. What it means
Asserted in WebSocketsScenario after decoding the received frame: the echoed UTF-8 text must equal what was sent ("Hello {i}"). A mismatch means bytes were corrupted, truncated, reordered, or a different message was returned.
Source
Thrown at testassets/TestClient/Scenarios/WebSocketsScenario.cs:54
WebSocketMessageType.Text,
endOfMessage: true,
cancellation);
var message = await client.ReceiveAsync(buffer, cancellation);
if (message.MessageType != WebSocketMessageType.Text)
{
throw new Exception($"Expected to receive a text message, got '{message.MessageType}' instead.");
}
if (!message.EndOfMessage)
{
throw new Exception("Expected to receive EndOfMessage = true.");
}
var text = Encoding.UTF8.GetString(buffer.AsSpan(0, message.Count));
if (text != textToSend)
{
throw new Exception($"Expected to receive '{textToSend}', but got '{text}'.");
}
Console.Write(".");
}
Console.WriteLine();
Console.WriteLine($"Completed 256 text messages in {stopwatch.ElapsedMilliseconds} ms.");
Console.WriteLine("Sending binary messages...");
stopwatch.Restart();
for (var i = 0; i < 256; i++)
{
var textToSend = $"Hello {i}";
var numBytes = Encoding.UTF8.GetBytes(textToSend, buffer.AsSpan());
await client.SendAsync(new ArraySegment<byte>(buffer, 0, numBytes),
WebSocketMessageType.Binary,
endOfMessage: true,
cancellation);View on GitHub (pinned to bd11867bee)
Solutions
- Use the matching test backend that echoes the exact bytes sent.
- Ensure the receive buffer is sized for the message and that message.Count bounds the decode (the scenario does this correctly; custom variants must too).
- Remove payload-transforming intermediaries.
- Compare sent vs received bytes (not just decoded text) to localize corruption.
Defensive patterns
Strategy: try-catch
Try / catch
try { await WebSocketsScenario.RunAsync(client, args, cancellation); }
catch (Exception ex) when (ex.Message.Contains("Expected to receive") && ex.Message.Contains("but got")) {
Console.Error.WriteLine("WebSocket echo payload mismatch; backend returned different bytes.");
return 1;
} Prevention
- Use a backend that echoes the exact payload bytes.
- Ensure the receive buffer is cleared/sized correctly and decode only message.Count bytes.
- Compare raw bytes, not just decoded text, when debugging.
When it happens
Trigger: Running WebSocketsScenario against a WebSocket endpoint whose echo does not return the exact sent payload, or where framing/count caused the decode to cover the wrong bytes.
Common situations: Backend echoing the wrong buffer or a previous message; truncation due to a too-small receive buffer; encoding mismatch; an intermediary mutating payload; the receive buffer not being cleared between iterations.
Related errors
- Received {buffer[0]}, expected {i}.
- Expected to receive a text message, got '{message.MessageTyp
- Expected to receive EndOfMessage = true.
- Expected status 409 Conflict!
- Expected status 101 Switching Protocols!
AI-assisted analysis of dotnet/yarp@bd11867bee (2026-08-13).
Data as JSON: /api/errors/8d237ade63694c94.
Report an issue: GitHub.