public void FramesWithCommandOnly_SuccessfullyRead() { foreach (var lineSeparator in LineSeparators) { foreach (var inCmd in InputCommands) { // Connected command does NOT support EOL as "\r\n" if (inCmd == StompCommands.Connected && lineSeparator == "\r\n") { continue; } using (MemoryStream inStream = new MemoryStream()) { inStream.Write(inCmd + lineSeparator + lineSeparator + "\0"); inStream.Seek(0, SeekOrigin.Begin); IStompFrameReader reader = new Stomp12FrameReader(inStream); Frame outFrame = reader.ReadFrameAsync().Result; Assert.AreEqual(inCmd, outFrame.Command); Assert.IsTrue(!outFrame.Headers.Any()); Assert.IsTrue(!outFrame.Body.Any()); } } } }
public void InvalidFrames_InvalidDataExceptionsThrown() { string[] inputInvalidFrames = { StompCommands.Connected + "\n" + "Problem: ConnectedWithCarriageReturn\n\r\n" + "\0", StompCommands.Receipt + "\n" + "ProblemIsHeaderWithoutValue\n", StompCommands.Receipt + "\n" + ":ProblemIsHeaderWithoutName\n", StompCommands.Error + "\n" + "Problem: Value:WithSemicolon\n", StompCommands.Error + "\n" + "Problem: Value\rWithCarriageReturn\n", StompCommands.Message + "\n" + "Problem: NonNullTerminated\ncontent-length: 0\n\n1", StompCommands.Message + "\n" + "Problem: NonNullTerminated\ncontent-length: 1\n\n12", }; foreach (var inFrame in inputInvalidFrames) { using (MemoryStream inStream = new MemoryStream()) { inStream.Write(StompCommands.Connected + "\r\n" + "\r\n" + "\0"); inStream.Seek(0, SeekOrigin.Begin); IStompFrameReader reader = new Stomp12FrameReader(inStream); try { Frame outFrame = reader.ReadFrameAsync().Result; Assert.Fail("AggregateException(InvalidDataException) expected."); } catch (AggregateException e) { Assert.IsTrue(e.InnerException is InvalidDataException); } } } }
public void EndOfStreamWhileReading_EndOfStreamExceptionThrown() { string inFrame = StompCommands.Error + "\r\nH1:V1\nH2:V2\n\r\nABC\0"; for (int i = 0; i < inFrame.Length; i++) { using (MemoryStream inStream = new MemoryStream()) { inStream.Write(inFrame.Substring(0, i)); inStream.Seek(0, SeekOrigin.Begin); IStompFrameReader reader = new Stomp12FrameReader(inStream); try { Frame outFrame = reader.ReadFrameAsync().Result; Assert.Fail("AggregateException(EndOfStreamException) expected."); } catch (AggregateException e) { Assert.IsTrue(e.InnerException is EndOfStreamException); } } } }
public void CanceledWhileReading_TaskCanceledExceptionThrown() { string inFrame = StompCommands.Error + "\r\nH1:V1\nH2:V2\n\r\nABC\0"; for (int i = 0; i < inFrame.Length; i++) { using (MemoryStream inStream = new NoEndChunkedMemoryStream()) { inStream.Write(inFrame.Substring(0, i)); inStream.Seek(0, SeekOrigin.Begin); IStompFrameReader reader = new Stomp12FrameReader(inStream); try { CancellationToken cToken = new CancellationTokenSource(10).Token; Frame outFrame = reader.ReadFrameAsync(cToken).Result; Assert.Fail("AggregateException(TaskCanceledException) expected."); } catch (AggregateException e) { Assert.IsTrue(e.InnerException is TaskCanceledException); } } } }
public void ValidHeartbeatFrame_SuccessfullyRead() { foreach (var lineSeparator in LineSeparators) { using (MemoryStream inStream = new MemoryStream()) { inStream.Write(lineSeparator); inStream.Seek(0, SeekOrigin.Begin); IStompFrameReader reader = new Stomp12FrameReader(inStream); Frame outFrame = reader.ReadFrameAsync().Result; Assert.AreEqual(StompCommands.Heartbeat, outFrame.Command); Assert.IsTrue(!outFrame.Headers.Any()); Assert.IsTrue(!outFrame.Body.Any()); } } }
public void FrameInChunks_SuccessfullyRead() { string inFrame = StompCommands.Message + "\r\nH0:V0\r\ncontent-length:20\r\n\r\n12345678901234567890\0"; using (MemoryStream inStream = new NoEndChunkedMemoryStream(chunkSize: 4)) { inStream.Write(inFrame); inStream.Seek(0, SeekOrigin.Begin); IStompFrameReader reader = new Stomp12FrameReader(inStream); Frame outFrame = reader.ReadFrameAsync().Result; Assert.AreEqual(StompCommands.Message, outFrame.Command); Assert.AreEqual(2, outFrame.Headers.Count()); Assert.AreEqual("V0", outFrame.GetAllHeaderValues("H0").First()); Assert.AreEqual("20", outFrame.GetAllHeaderValues("content-length").First()); Assert.AreEqual("12345678901234567890", outFrame.GetBodyAsString()); } }
public void FrameWithContentLengthHeader_BodySuccessfullyRead() { string inFrame1 = StompCommands.Message + "\n" + "content-length:0\n\n\0"; using (MemoryStream inStream = new MemoryStream()) { inStream.Write(inFrame1); inStream.Seek(0, SeekOrigin.Begin); IStompFrameReader reader = new Stomp12FrameReader(inStream); Frame outFrame = reader.ReadFrameAsync().Result; Assert.AreEqual(string.Empty, outFrame.GetBodyAsString()); } string inFrame2 = StompCommands.Receipt + "\n" + "content-length:10\n\n1234567890\0"; using (MemoryStream inStream = new MemoryStream()) { inStream.Write(inFrame2); inStream.Seek(0, SeekOrigin.Begin); IStompFrameReader reader = new Stomp12FrameReader(inStream); Frame outFrame = reader.ReadFrameAsync().Result; Assert.AreEqual("1234567890", outFrame.GetBodyAsString()); } string inFrame3 = StompCommands.Error + "\n" + "content-length: 4 \n\n\r\n\0\t\0"; using (MemoryStream inStream = new MemoryStream()) { inStream.Write(inFrame3); inStream.Seek(0, SeekOrigin.Begin); IStompFrameReader reader = new Stomp12FrameReader(inStream); Frame outFrame = reader.ReadFrameAsync().Result; Assert.AreEqual("\r\n\0\t", outFrame.GetBodyAsString()); } }
public void FrameWithHeadersAndContent_SuccessfullyRead() { string inFrame = StompCommands.Message + "\n" + "H0:V0\n HA :VA\nH1: V1 \nH2:V2_1\nH2:V2_2\n\n12345ABC\0"; using (MemoryStream inStream = new MemoryStream()) { inStream.Write(inFrame); inStream.Seek(0, SeekOrigin.Begin); IStompFrameReader reader = new Stomp12FrameReader(inStream); Frame outFrame = reader.ReadFrameAsync().Result; Assert.AreEqual(StompCommands.Message, outFrame.Command); Assert.AreEqual(5, outFrame.Headers.Count()); Assert.AreEqual("V0", outFrame.GetAllHeaderValues("H0").First()); Assert.AreEqual("VA", outFrame.GetAllHeaderValues(" HA ").First()); Assert.AreEqual(" V1 ", outFrame.GetAllHeaderValues("H1").First()); Assert.AreEqual(2, outFrame.GetAllHeaderValues("H2").Count()); Assert.IsTrue(outFrame.GetAllHeaderValues("H2").Contains("V2_1")); Assert.IsTrue(outFrame.GetAllHeaderValues("H2").Contains("V2_1")); Assert.AreEqual("12345ABC", outFrame.GetBodyAsString()); } }
/// <summary> /// Example to demonstrate STOMP transactions using the StompNet low level API. /// /// In this example, a single message is going to be sent to a queue and it will be read from /// the same queue. /// </summary> public static async Task ExampleWriterAndReader() { using (TcpClient tcpClient = new TcpClient(serverHostname, serverPort)) // Create a frame writer and frame reader. These classes are NOT thread safe. // Stomp12FrameWriter and Stomp12FrameReader are NOT thread safety. They could // wrapped into a StompSerialFrameWriter and a StompSerialFrameReader for // serializing operations (thread-safe). using (IStompFrameWriter writer = new Stomp12FrameWriter(tcpClient.GetStream())) using (IStompFrameReader reader = new Stomp12FrameReader(tcpClient.GetStream())) { Frame inFrame; //--------------------------------- // Write CONNECT. // await writer.WriteConnectAsync(virtualHost, login, passcode); // Read CONNECTED. // Keep reading while receiving Heartbeats. do { inFrame = await reader.ReadFrameAsync(); } while (inFrame.Command == StompCommands.Heartbeat); //Verify a CONNECTED frame was received. if (!AssertExpectedCommandFrame(inFrame, StompCommands.Connected)) { return; } Console.WriteLine("Connected"); //--------------------------------- // Write SEND command with receipt. // await writer.WriteSendAsync( aQueueName, messageContent + (" (WITH RECEIPT)."), "myreceiptid-123"); // Read RECEIPT. // Keep reading while receiving Heartbeats. do { inFrame = await reader.ReadFrameAsync(); } while (inFrame.Command == StompCommands.Heartbeat); //Verify a RECEIPT frame was received. if (!AssertExpectedCommandFrame(inFrame, StompCommands.Receipt)) { return; } // Process incoming RECEIPT. // Using the incoming frame, Interpret will create a new instance of a sub-class // of Frame depending on the Command. If the frame is a HEARTBEAT, the same frame // will be returned. Possible sub-classes: ConnectedFrame, ErrorFrame, MessageFrame, // ReceiptFrame. // Interpret throws exception if the incoming frame is malformed (not standard). ReceiptFrame rptFrame = StompInterpreter.Interpret(inFrame) as ReceiptFrame; if (rptFrame.ReceiptId != "myreceiptid-123") { Console.WriteLine("ERROR: Unexpected receipt " + rptFrame.ReceiptId + "."); return; } Console.WriteLine("Received matching receipt."); //--------------------------------- // Write SUBSCRIBE. // string subscriptionId = new Random().Next().ToString(); await writer.WriteSubscribeAsync(aQueueName, subscriptionId, ack : StompAckValues.AckAutoValue); // Read MESSAGE. // Keep reading while receiving Heartbeats. do { inFrame = await reader.ReadFrameAsync(); } while (inFrame.Command == StompCommands.Heartbeat); //Verify a MESSAGE frame was received. if (!AssertExpectedCommandFrame(inFrame, StompCommands.Message)) { return; } // Process incoming RECEIPT. MessageFrame msgFrame = StompInterpreter.Interpret(inFrame) as MessageFrame; Console.WriteLine("Received Message:"); Console.WriteLine(); Console.WriteLine("Destination: " + msgFrame.Destination); Console.WriteLine("ContentType: " + msgFrame.ContentType); Console.WriteLine("ContentLength: " + msgFrame.ContentLength); Console.WriteLine("Content:" + msgFrame.GetBodyAsString()); Console.WriteLine(); // Write DISCONNECT. await writer.WriteDisconnectAsync(); Console.WriteLine("Disconnected."); } }