public async void ContentTooLong() { var command = StompParser.Command.MESSAGE; var eol = "\n"; var header = "content-length:5"; await Assert.ThrowsAsync <ContentLengthException>(async() => { using (var memStream = new MemoryStream()) using (var streamWriter = new StreamWriter(memStream)) { streamWriter.Write(command); streamWriter.Write(eol); streamWriter.Write(header); streamWriter.Write(eol); streamWriter.Write(eol); streamWriter.Write("0123456789"); streamWriter.Write((char)0x00); streamWriter.Flush(); memStream.Position = 0; var parsedCommand = await StompParser.ReadStompCommand(memStream.AsPrependableStream()); var parsedHeaders = await StompParser.ReadStompHeaders(parsedCommand.Item1, parsedCommand.Item2); var parsedBody = await StompParser .ReadStompBody(parsedHeaders.Item1, parsedHeaders.Item2, CancellationToken.None); } }); }
public async void ContentLengthMatchesValue() { var bodyString = "0123456789abcdefghijk1234567890abcd"; var command = StompParser.Command.MESSAGE; var header = new KeyValuePair <string, string> ("content-length", bodyString.Length.ToString()); var headerArray = new KeyValuePair <string, string> [1]; headerArray[0] = header; var body = Encoding.UTF8.GetBytes(bodyString); var expectation = new StompFrame(command, headerArray, body); using (var memStream = new MemoryStream()) using (var streamWriter = new StreamWriter(memStream)) { var eol = "\r\n"; streamWriter.Write(command); streamWriter.Write(eol); foreach (var headerPair in headerArray) { streamWriter.Write($"{headerPair.Key}:{headerPair.Value}"); } streamWriter.Write(eol); streamWriter.Write(eol); streamWriter.Write(bodyString); streamWriter.Write((char)0x00); streamWriter.Flush(); memStream.Position = 0; var parsedCommand = await StompParser.ReadStompCommand(memStream.AsPrependableStream()); var parsedHeaders = await StompParser.ReadStompHeaders(parsedCommand.Item1, parsedCommand.Item2); var parsedBody = await StompParser .ReadStompBody(parsedHeaders.Item1, parsedHeaders.Item2, CancellationToken.None); Assert.StrictEqual(expectation.Command, parsedCommand.Item2.Command); Assert.Equal(expectation.Headers[0], parsedHeaders.Item2.Headers[0]); Assert.True(Encoding.UTF8.GetString(expectation.Body.ToArray()) == Encoding.UTF8.GetString(parsedBody.Item2.Body.ToArray())); Assert.Equal(parsedBody.Item2.Body.Length, bodyString.Length); } }