public void TestSendTwoWay_XML() { using (NamedPipeServerStream pipeServer = new NamedPipeServerStream( connectionUri.Uri.AbsolutePath, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous)) { string xml = "<SomeTestMessage><Element1 attribute1=\"attributeValue\"></Element1><Element2>Some element content</Element2></SomeTestMessage>"; Message msg = GeneralTestHelper.CreateMessageWithBase64EncodedBody(xml, Encoding.UTF8); //Setting up the BTS.IsSolicitResponse property msg.Properties.Add("http://schemas.microsoft.com/BizTalk/2003/system-properties#IsSolicitResponse", true); OutboundTestHelper testHelper = new OutboundTestHelper(pipeServer); //We set the response message content testHelper.responseXml = "<SomeTestMessageResponse><Element1 attribute1=\"attributeValue\"></Element1><Element2>Some element content</Element2></SomeTestMessageResponse>"; pipeServer.BeginWaitForConnection(cb => testHelper .ClientConnectedSyncronous(cb, ctx => SendResponse(ctx)), testHelper); Message responseMsg = outboundHandler.Execute(msg, new TimeSpan(0, 0, 10)); var mockMessage = ConvertToMockMessage(testHelper.memStream); Assert.AreEqual(mockMessage.Body, xml, "Contents of the request message is different"); Assert.AreEqual(testHelper.responseXml, GeneralTestHelper.GetBodyAsString(responseMsg, Encoding.UTF8), "Contents of the response message is different"); } }
public void TestSendTwoWay_XML_MediumMessage() { using (NamedPipeServerStream pipeServer = new NamedPipeServerStream( connectionUri.Uri.AbsolutePath, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous)) { Message msg = GeneralTestHelper.CreateMessageWithBase64EncodedBody( File.ReadAllBytes("MediumMessage.xml")); //Setting up the BTS.IsSolicitResponse property msg.Properties.Add("http://schemas.microsoft.com/BizTalk/2003/system-properties#IsSolicitResponse", true); OutboundTestHelper testHelper = new OutboundTestHelper(pipeServer); //We set the response message content testHelper.responsePath = "MediumMessage.xml"; pipeServer.BeginWaitForConnection(cb => testHelper.ClientConnectedSyncronous(cb), testHelper); Message responseMsg = outboundHandler.Execute(msg, new TimeSpan(0, 0, 10)); //Calculating the hashes of the messages and the file byte[] responseMessageBytes = GeneralTestHelper.GetBodyAsBytes(responseMsg); string receivedMessageHash = GeneralTestHelper.CalculateBytesHash(testHelper.memStream.ToArray()); string responseMessageHash = GeneralTestHelper.CalculateBytesHash(responseMessageBytes); string fileHash = GeneralTestHelper.CalculateFileHash("MediumMessage.xml"); //Validating the results Assert.AreEqual(fileHash, receivedMessageHash, "Contents of the request message is different"); Assert.AreEqual(fileHash, responseMessageHash, "Contents of the response message is different"); } }
public void TestTwoWay_XML() { PipeSecurity ps = new PipeSecurity(); ps.AddAccessRule(new PipeAccessRule("USERS", PipeAccessRights.CreateNewInstance | PipeAccessRights.ReadWrite, System.Security.AccessControl.AccessControlType.Allow)); //We first spin a pipe server to make sure that the send port will be able to connect using (NamedPipeServerStream pipeServer = new NamedPipeServerStream( "TwoWaySend", PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous, 1024, 1024, ps)) { string xml = "<SomeTestMessage><Element1 attribute1=\"attributeValue\"></Element1><Element2>Some element content</Element2></SomeTestMessage>"; string responseXml = "<SomeTestMessageResponse><Element1 attribute1=\"attributeValue\"></Element1><Element2>Some element content</Element2></SomeTestMessageResponse>"; OutboundTestHelper testHelper = new OutboundTestHelper(pipeServer, responseXml); pipeServer.BeginWaitForConnection(cb => testHelper.ClientConnectedSyncronous(cb), testHelper); System.Threading.Thread.Sleep(100); //Here we spin the pipe client that will send the message to BizTalk using (NamedPipeClientStream pipeClient = new NamedPipeClientStream("localhost", "TwoWayReceive", PipeDirection.InOut, PipeOptions.Asynchronous)) { byte[] xmlBytes = Encoding.UTF8.GetBytes(xml); pipeClient.Connect(10000); pipeClient.Write(xmlBytes, 0, xmlBytes.Count()); pipeClient.Flush(); pipeClient.WriteByte(0x00);//writing the EOF byte pipeClient.Flush(); pipeClient.WaitForPipeDrain(); //Here we wait for the event to be signalled bool waitExpired = testHelper.syncEvent.WaitOne(10000); Assert.IsTrue(waitExpired, "The waiting time for the response has expired prior to receiving the response"); //The event was signalled, we get the message stirng from the outBuffer string receivedXml = Encoding.UTF8.GetString(testHelper.memStream.ToArray(), 0, (int)testHelper.memStream.Length); Assert.AreEqual(xml, receivedXml, "Contents of received message is different"); //Here we read from the pipeClient the response message byte[] responseBytes = new byte[256]; using (MemoryStream memStream = new MemoryStream(256)) { int byteCountRead = 0; bool eofReached = false; while (!eofReached) { byteCountRead = pipeClient.Read(responseBytes, 0, responseBytes.Length); if (byteCountRead > 2) { eofReached = (responseBytes[byteCountRead - 1] == 0x0 && responseBytes[byteCountRead - 2] != 0x0 && responseBytes[byteCountRead - 3] != 0x0); } else if (byteCountRead > 1 && !eofReached) { eofReached = (responseBytes[byteCountRead - 1] == 0x0 && responseBytes[byteCountRead - 2] == 0x0); } else if (byteCountRead == 1) { eofReached = responseBytes[byteCountRead - 1] == 0x0; } memStream.Write(responseBytes, 0, eofReached ? byteCountRead - 1 : byteCountRead); } string receivedResponseXml = GeneralTestHelper.GetMessageFromArray( memStream.ToArray(), (int)memStream.Length, Encoding.UTF8); Assert.AreEqual(responseXml, receivedResponseXml, "Contents of the response message is different"); } } } }