public void SetupResponseExpectationAgainstAction()
        {
            StubServiceHost.FindDefaultService <IWork>()
            .Setup(s => s.Perform(It.IsAny <System.ServiceModel.Channels.Message>()))
            .Returns(new StringStream("<response />"));

            var client = StubServiceClient <IWork> .Create();

            try
            {
                var result = client.Perform(
                    System.ServiceModel.Channels.Message.CreateMessage(
                        MessageVersion.Soap11,
                        "urn:services.stateless.be:unit:work:perform:request",
                        XmlReader.Create(new StringReader("<request />"))));

                var reader = result.GetReaderAtBodyContents();
                reader.MoveToContent();
                var outerXml = reader.ReadOuterXml();
                Assert.That(outerXml, Is.EqualTo("<response />"));

                client.Close();
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }
        public void SetupCallbackExpectationAgainstVoidOperation()
        {
            var calledBack = false;

            StubServiceHost.FindDefaultService <IWork>()
            .Setup(s => s.Execute(It.IsAny <System.ServiceModel.Channels.Message>()))
            .Callback(() => calledBack = true);

            var client = StubServiceClient <IWork> .Create();

            try
            {
                client.Execute(
                    System.ServiceModel.Channels.Message.CreateMessage(
                        MessageVersion.Soap11,
                        "urn:services.stateless.be:unit:work:execute:request",
                        XmlReader.Create(new StringReader("<request />"))));
                client.Close();
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
            Assert.That(calledBack, Is.True);
        }
        public void RelayAsyncMessage()
        {
            StubServiceHost.FindDefaultService <ICalculatorService>()
            .Setup(s => s.BeginMultiply(It.IsAny <XmlCalculatorRequest>(), It.IsAny <AsyncCallback>(), It.IsAny <object>()))
            .Returns(new StringStream(string.Format(CALCULATOR_RESPONSE_XML, 2)));

            ICalculatorService client = null;

            try
            {
                client = SimpleServiceClient <CalculatorService, ICalculatorService> .Create();

                var calculatorResult = Task <XmlCalculatorResponse> .Factory
                                       .FromAsync(client.BeginMultiply, client.EndMultiply, new XmlCalculatorRequest(CALCULATOR_REQUEST_XML), null)
                                       .Result;

                Assert.AreEqual(string.Format(CALCULATOR_RESPONSE_XML, 2), calculatorResult.RawXmlBody);
                client.Close();
            }
            catch (Exception)
            {
                if (client != null)
                {
                    client.Abort();
                }
                throw;
            }
        }
        public void RelaySyncMessage()
        {
            StubServiceHost.FindDefaultService <ICalculatorService>()
            .Setup(s => s.Add(It.IsAny <XmlCalculatorRequest>()))
            .Returns(new StringStream(string.Format(CALCULATOR_RESPONSE_XML, 3)));

            ICalculatorService client = null;

            try
            {
                client = SimpleServiceClient <CalculatorService, ICalculatorService> .Create();

                var calculatorResult = client.Add(new XmlCalculatorRequest(CALCULATOR_REQUEST_XML));
                Assert.AreEqual(string.Format(CALCULATOR_RESPONSE_XML, 3), calculatorResult.RawXmlBody);
                client.Close();
            }
            catch (Exception)
            {
                if (client != null)
                {
                    client.Abort();
                }
                throw;
            }
        }
        public void RelayCompositeMessage()
        {
            const string responseXml = "<CalculatorResponse xmlns=\"urn:services.stateless.be:unit:calculator\">" +
                                       "<s0:Result xmlns:s0=\"urn:services.stateless.be:unit:calculator\">one</s0:Result>" +
                                       "<s0:Result xmlns:s0=\"urn:services.stateless.be:unit:calculator\">two</s0:Result>" +
                                       "</CalculatorResponse>";

            StubServiceHost.FindDefaultService <ICalculatorService>()
            .Setup(s => s.Add(It.IsAny <XmlCalculatorRequest>()))
            .Returns(new StringStream(responseXml));

            ICalculatorService client = null;

            try
            {
                client = SimpleServiceClient <CalculatorService, ICalculatorService> .Create();

                var calculatorResult = client.Add(new XmlCalculatorRequest(CALCULATOR_REQUEST_XML));
                Assert.AreEqual(responseXml, calculatorResult.RawXmlBody);
                client.Close();
            }
            catch (Exception)
            {
                if (client != null)
                {
                    client.Abort();
                }
                throw;
            }
        }
 public void CannotSetupExpectationAgainstNonServiceContract()
 {
     Assert.That(
         () => StubServiceHost.FindDefaultService <IDisposable>()
         .Setup(s => s.Dispose())
         .Aborts(),
         Throws.TypeOf <ArgumentException>()
         .With.Message.EqualTo("TContract type parameter 'IDisposable' is not a service contract."));
 }
        public void RelaySyncMessageTimesOut()
        {
            StubServiceHost.FindDefaultService <ICalculatorService>()
            .Setup(s => s.Subtract(It.IsAny <XmlCalculatorRequest>()))
            .Callback(() => Thread.Sleep(3000))
            .Returns(new StringStream(string.Format(CALCULATOR_RESPONSE_XML, 1)));

            var client = SimpleServiceClient <CalculatorService, ICalculatorService> .Create();

            Assert.That(
                () => client.Subtract(new XmlCalculatorRequest(CALCULATOR_REQUEST_XML)),
                Throws.TypeOf <FaultException <ExceptionDetail> >()
                .With.Message.Contains("The request channel timed out while waiting for a reply"));
            client.Close();
        }
        public void SetupFailureExpectationAgainstVoidOperation()
        {
            StubServiceHost.FindDefaultService <IWork>()
            .Setup(s => s.Execute(It.IsAny <System.ServiceModel.Channels.Message>()))
            .Aborts();

            var client = StubServiceClient <IWork> .Create();

            Assert.That(
                () => client.Execute(
                    System.ServiceModel.Channels.Message.CreateMessage(
                        MessageVersion.Soap11,
                        "urn:services.stateless.be:unit:work:execute:request",
                        XmlReader.Create(new StringReader("<request />")))),
                Throws.TypeOf <CommunicationException>());
            client.Abort();
        }
        public void RelayAsyncMessageTimesOut()
        {
            StubServiceHost.FindDefaultService <ICalculatorService>()
            .Setup(s => s.BeginDivide(It.IsAny <XmlCalculatorRequest>(), It.IsAny <AsyncCallback>(), It.IsAny <object>()))
            .Callback(() => Thread.Sleep(3000))
            .Returns(new StringStream(string.Format(CALCULATOR_RESPONSE_XML, 2)));

            var client = SimpleServiceClient <CalculatorService, ICalculatorService> .Create();

            Assert.That(
                () => Task <XmlCalculatorResponse> .Factory
                .FromAsync(client.BeginDivide, client.EndDivide, new XmlCalculatorRequest(CALCULATOR_REQUEST_XML), null)
                .Result,
                Throws.TypeOf <AggregateException>()
                .With.InnerException.TypeOf <FaultException <ExceptionDetail> >()
                .With.InnerException.Message.Contains("has exceeded the allotted timeout"));
            client.Close();
        }
        // ReSharper disable once UnusedMember.Local
        private void EnsureSetupExpressionsCompile()
        {
            StubServiceHost.FindDefaultService <IWork>()
            .Setup(s => s.Execute(It.IsAny <System.ServiceModel.Channels.Message>()))
            .Callback(null)
            .Aborts();

            StubServiceHost.FindDefaultService <IWork>()
            .Setup(s => s.Perform(It.IsAny <System.ServiceModel.Channels.Message>()))
            .Callback(null)
            .Returns("file");

            StubServiceHost.DefaultService
            .Setup(s => s.Request(new DocumentSpec("s", "a")))
            .Callback(null)
            .Aborts();

            StubServiceHost.DefaultService
            .Setup(s => s.Request(new DocumentSpec("s", "a")))
            .Callback(null)
            .Returns("file");
        }
Пример #11
0
        public void ProcessAsyncSucceeds()
        {
            StubServiceHost.FindDefaultService <IPerformServiceSync>()
            .Setup(s => s.Process(It.IsAny <XlangCalculatorRequest>()));

            IPerformServiceSync client = null;

            try
            {
                var request = new XlangCalculatorRequest(CALCULATOR_REQUEST_XML);
                client = Client <IPerformServiceSync> .Create(SimpleServiceHost <CalculatorService, IPerformService> .Endpoint);

                client.Process(request);
                client.Close();
            }
            catch (Exception)
            {
                if (client != null)
                {
                    client.Abort();
                }
                throw;
            }
        }