{"record":{"id":"8d237ade63694c94","repo":"dotnet/yarp","slug":"expected-to-receive-texttosend-but-got-text","errorCode":null,"errorMessage":"Expected to receive '{textToSend}', but got '{text}'.","messagePattern":"Expected to receive '(.+?)', but got '(.+?)'\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"testassets/TestClient/Scenarios/WebSocketsScenario.cs","lineNumber":54,"sourceCode":"                WebSocketMessageType.Text,\n                endOfMessage: true,\n                cancellation);\n\n            var message = await client.ReceiveAsync(buffer, cancellation);\n            if (message.MessageType != WebSocketMessageType.Text)\n            {\n                throw new Exception($\"Expected to receive a text message, got '{message.MessageType}' instead.\");\n            }\n\n            if (!message.EndOfMessage)\n            {\n                throw new Exception(\"Expected to receive EndOfMessage = true.\");\n            }\n\n            var text = Encoding.UTF8.GetString(buffer.AsSpan(0, message.Count));\n            if (text != textToSend)\n            {\n                throw new Exception($\"Expected to receive '{textToSend}', but got '{text}'.\");\n            }\n\n            Console.Write(\".\");\n        }\n\n        Console.WriteLine();\n        Console.WriteLine($\"Completed 256 text messages in {stopwatch.ElapsedMilliseconds} ms.\");\n\n        Console.WriteLine(\"Sending binary messages...\");\n        stopwatch.Restart();\n        for (var i = 0; i < 256; i++)\n        {\n            var textToSend = $\"Hello {i}\";\n            var numBytes = Encoding.UTF8.GetBytes(textToSend, buffer.AsSpan());\n            await client.SendAsync(new ArraySegment<byte>(buffer, 0, numBytes),\n                WebSocketMessageType.Binary,\n                endOfMessage: true,\n                cancellation);","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/dotnet/yarp/blob/bd11867bee7df522e7fd3effb08a9c85fd616908/testassets/TestClient/Scenarios/WebSocketsScenario.cs#L36-L72","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { await WebSocketsScenario.RunAsync(client, args, cancellation); }\ncatch (Exception ex) when (ex.Message.Contains(\"Expected to receive\") && ex.Message.Contains(\"but got\")) {\n    Console.Error.WriteLine(\"WebSocket echo payload mismatch; backend returned different bytes.\");\n    return 1;\n}","preventionTips":["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."],"tags":["test","websockets","scenario","data-integrity","test-client","yarp"],"backgroundTag":null,"analyzedSha":"bd11867bee7df522e7fd3effb08a9c85fd616908","analyzedAt":"2026-08-13T21:29:49.359Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}