public void SendMessageAsyncSendsMessage()
 {
     var action = "DoSomething";
     var request = new ServerRequest
     {
         ServerName = "TestServer"
     };
     var url = "http://somewhere/";
     var factory = new TestClientFactory((u, a, d) =>
     {
         Assert.AreEqual(url + "server/TestServer/RawXmlMessage.aspx", u.AbsoluteUri);
         Assert.AreEqual("POST", a);
         Assert.AreEqual(action, d["action"]);
         Assert.AreEqual(request.ToString(), d["message"]);
         var theResponse = new Response
         {
             RequestIdentifier = request.Identifier
         };
         return Encoding.UTF8.GetBytes(theResponse.ToString());
     });
     var connection = new HttpConnection(new Uri(url), factory);
     var completed = false;
     connection.SendMessageCompleted += (o, e) =>
     {
         completed = true;
         Assert.IsFalse(e.Cancelled);
         Assert.IsNull(e.Error);
     };
     connection.SendMessageAsync(action, request);
     Assert.IsTrue(completed);
 }
 public void SendMessageSendsAndReceivesAMessage()
 {
     var action = "DoSomething";
     var request = new ServerRequest
     {
         ServerName = "TestServer"
     };
     var url = "http://somewhere/";
     var factory = new TestClientFactory((u, a, d) =>
     {
         Assert.AreEqual(url + "server/TestServer/RawXmlMessage.aspx", u.AbsoluteUri);
         Assert.AreEqual("POST", a);
         Assert.AreEqual(action, d["action"]);
         Assert.AreEqual(request.ToString(), d["message"]);
         var theResponse = new Response
         {
             RequestIdentifier = request.Identifier
         };
         return Encoding.UTF8.GetBytes(theResponse.ToString());
     });
     var connection = new HttpConnection(new Uri(url), factory);
     var response = connection.SendMessage(action, request);
     Assert.IsInstanceOf<Response>(response);
     Assert.AreEqual(request.Identifier, response.RequestIdentifier);
 }
        public void ProcessMessageCorrectlyHandlesAValidMessage()
        {
            // Setup the messages
            ProjectRequest request = new ProjectRequest("123-45", "A test project");
            Response response = new Response(request);
            response.Result = ResponseResult.Success;

            // Initialises the mocks
            ICruiseServer server = mocks.DynamicMock<ICruiseServer>();
            Expect.Call(server.ForceBuild(request)).Return(response);
            mocks.ReplayAll();

            // Run the actual test
            var manager = new ThoughtWorks.CruiseControl.Core.CruiseServerClient(server);
            string responseText = manager.ProcessMessage("ForceBuild", request.ToString());
            Assert.AreEqual(response.ToString(), responseText);
            mocks.VerifyAll();
        }
Exemplo n.º 4
0
        /// <summary>
        /// Processes a message.
        /// </summary>
        /// <param name="action">The action to use.</param>
        /// <param name="message">The request message in an XML format.</param>
        /// <returns>The response message in an XML format.</returns>
        public override string ProcessMessage(string action, string message)
        {
            Response response = new Response();

            try
            {
                // Find the type of message
                var messageXml = new XmlDocument();
                messageXml.LoadXml(message);
                var messageType = XmlConversionUtil.FindMessageType(messageXml.DocumentElement.Name);
                if (messageType == null)
                {
                    response.Result = ResponseResult.Failure;
                    response.ErrorMessages.Add(
                        new ErrorMessage(
                            string.Format(
                                "Unable to translate message: '{0}' is unknown",
                                messageXml.DocumentElement.Name)));
                }

                // Convert the message and invoke the action
                var request = XmlConversionUtil.ConvertXmlToObject(messageType, message);
                response = connection.SendMessage(action, request as ServerRequest);
            }
            catch (Exception error)
            {
                response.Result = ResponseResult.Failure;
                response.ErrorMessages.Add(
                    new ErrorMessage("Unable to process error: " + error.Message));
            }

            return response.ToString();
        }
        /// <summary>
        /// Processes a message.
        /// </summary>
        /// <param name="action">The action to use.</param>
        /// <param name="message">The request message in an XML format.</param>
        /// <returns>The response message in an XML format.</returns>
        public virtual string ProcessMessage(string action, string message)
        {
            var response = new Response();

            try
            {
                // Find the action and message type, extract the message and invoke the method
                response = ExtractAndInvokeMessage(message, action, new RemotingChannelSecurityInformation());
            }
            catch (Exception error)
            {
                response.Result = ResponseResult.Failure;
                response.ErrorMessages.Add(
                    new ErrorMessage("Unable to process: " + error.Message));
            }

            var responseText = response.ToString();
            return responseText;
        }
Exemplo n.º 6
0
 public void ToStringSerialisesAllValues()
 {
     Response response = new Response();
     response.ErrorMessages.Add(new ErrorMessage("Error 1"));
     response.ErrorMessages.Add(new ErrorMessage("Error 2"));
     response.RequestIdentifier = "request";
     response.Result = ResponseResult.Success;
     response.Timestamp = DateTime.Now;
     string actual = response.ToString();
     string expected = string.Format("<response xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " +
         "timestamp=\"{2:yyyy-MM-ddTHH:mm:ss.FFFFFFFzzz}\" identifier=\"{0}\" result=\"{1}\">" +
         "<error>Error 1</error>" +
         "<error>Error 2</error>" +
         "</response>",
         response.RequestIdentifier,
         response.Result,
         response.Timestamp);
     Assert.AreEqual(expected, actual);
 }
Exemplo n.º 7
0
 public void ToStringSerialisesDefaultValues()
 {
     Response response = new Response();
     string actual = response.ToString();
     string expected = string.Format("<response xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " +
         "timestamp=\"{1:yyyy-MM-ddTHH:mm:ss.FFFFFFFzzz}\" result=\"{0}\" />",
         response.Result,
         response.Timestamp);
     Assert.AreEqual(expected, actual);
 }
        public void SendMessageEncryptsMessage()
        {
            var innerConnection = new TestConnection();
            var outerConnection = new EncryptingConnection(innerConnection);
            var request = new ServerRequest();
            var expectedResponse = new Response(request);
            var actionName = "DoSomething";
            string iv = null;
            string key = null;

            innerConnection.SendMessageAction = (a, r) =>
            {
                Response sendResponse = null;
                if (a == "RetrievePublicKey")
                {
                    sendResponse = this.GenerateKeyResponse(r);
                }
                else if (a == "InitialiseSecureConnection")
                {
                    Assert.IsInstanceOf<LoginRequest>(r);
                    sendResponse = this.GenerateConnectioResponse(r as LoginRequest, out iv, out key);
                }
                else if (a == "ProcessSecureRequest")
                {
                    Assert.IsInstanceOf<EncryptedRequest>(r);
                    var actualRequest = r as EncryptedRequest;
                    Assert.AreEqual(actionName, actualRequest.Action);

                    var crypto = new RijndaelManaged();
                    crypto.Key = Convert.FromBase64String(key);
                    crypto.IV = Convert.FromBase64String(iv);
                    var requestData = DecryptMessage(crypto, actualRequest.EncryptedData);
                    Assert.AreEqual(request.ToString(), requestData);

                    var encryptedResponse = new EncryptedResponse();
                    encryptedResponse.Result = ResponseResult.Success;
                    encryptedResponse.EncryptedData = EncryptMessage(crypto, expectedResponse.ToString());
                    sendResponse = encryptedResponse;
                }
                else
                {
                    Assert.Fail("Unknown action: " + a);
                }

                return sendResponse;
            };
            var response = outerConnection.SendMessage(actionName, request);
            Assert.IsNotNull(response);
            Assert.AreEqual(expectedResponse.RequestIdentifier, response.RequestIdentifier);
        }