Пример #1
0
            public void RespondWithMessage()
            {
                var injector = new MockInjector();
                var listener = injector.SetupAndGetListener();
                var server = injector.Create<Server>();
                var soapDecoder = injector.GetMock<ISoapDecoder>();
                var request = new Mock<IHttpListenerRequest>();
                var response = new Mock<IHttpListenerResponse>();
                injector.SetupProxy(request);
                injector.SetupProxy(response);
                var message = Message.CreateMessage(MessageVersion.Default, "moo");

                listener.Setup(l => l.GetContextAsync().Result.Request).Returns(request.Object);
                listener.Setup(l => l.GetContextAsync().Result.Response).Returns(response.Object);

                server.Receive();
                server.Respond(message, request.Object);

                soapDecoder.Verify(s => s.FromMessage(response.Object, message, request.Object));
            }
Пример #2
0
            public void RespondWithXml()
            {
                var injector = new MockInjector();
                var listener = injector.SetupAndGetListener();
                var server = injector.Create<Server>();
                var soapDecoder = injector.GetMock<ISoapDecoder>();
                var request = new Mock<IHttpListenerRequest>();
                var response = new Mock<IHttpListenerResponse>();
                injector.SetupProxy(request);
                injector.SetupProxy(response);
                const string xml = "<moo>";

                listener.Setup(l => l.GetContextAsync().Result.Request).Returns(request.Object);
                listener.Setup(l => l.GetContextAsync().Result.Response).Returns(response.Object);

                server.Receive();
                server.Respond(xml);

                soapDecoder.Verify(s => s.FromXml(response.Object, xml));
            }
Пример #3
0
            public void RespondWithCustomAction()
            {
                var injector = new MockInjector();
                var listener = injector.SetupAndGetListener();
                var server = injector.Create<Server>();
                var request = new Mock<IHttpListenerRequest>();
                var response = new Mock<IHttpListenerResponse>();
                injector.SetupProxy(request);
                injector.SetupProxy(response);

                listener.Setup(l => l.GetContextAsync().Result.Request).Returns(request.Object);
                listener.Setup(l => l.GetContextAsync().Result.Response).Returns(response.Object);

                var called = false;
                server.Receive();
                server.Respond(r =>
                {
                    r.Should().Be(response.Object);
                    called = true;
                });

                called.Should().BeTrue();
            }
Пример #4
0
            public void RespondWithData()
            {
                var injector = new MockInjector();
                var listener = injector.SetupAndGetListener();
                var server = injector.Create<Server>();
                var soapDecoder = injector.GetMock<ISoapDecoder>();
                var request = new Mock<IHttpListenerRequest>();
                var response = new Mock<IHttpListenerResponse>();
                injector.SetupProxy(request);
                injector.SetupProxy(response);
                var data = new { };

                listener.Setup(l => l.GetContextAsync().Result.Request).Returns(request.Object);
                listener.Setup(l => l.GetContextAsync().Result.Response).Returns(response.Object);

                server.Receive();
                server.Respond("moo", data, request.Object);

                soapDecoder.Verify(s => s.FromData(response.Object, "moo", data, request.Object));
            }
Пример #5
0
            public void RespondOnlyWorksOnce()
            {
                var injector = new MockInjector();
                var listener = injector.SetupAndGetListener();
                var server = injector.Create<Server>();
                var request = new Mock<IHttpListenerRequest>();
                var response = new Mock<IHttpListenerResponse>();
                injector.SetupProxy(request);
                injector.SetupProxy(response);

                listener.Setup(l => l.GetContextAsync().Result.Request).Returns(request.Object);
                listener.Setup(l => l.GetContextAsync().Result.Response).Returns(response.Object);

                server.Receive();
                server.Respond(r => r.ContentType = "space");
                server.Invoking(s => s.Respond(r => r.ContentType = "moo"))
                      .ShouldThrow<InvalidOperationException>()
                      .WithMessage("no request", ComparisonMode.Substring);
            }
Пример #6
0
            public void Abort()
            {
                var injector = new MockInjector();
                var listener = injector.SetupAndGetListener();
                var server = injector.Create<Server>();
                var request = new Mock<IHttpListenerRequest>();
                var response = new Mock<IHttpListenerResponse>();
                injector.SetupProxy(request);
                injector.SetupProxy(response);

                listener.Setup(l => l.GetContextAsync().Result.Request).Returns(request.Object);
                listener.Setup(l => l.GetContextAsync().Result.Response).Returns(response.Object);

                server.Receive();
                server.AbortResponse();

                response.Verify(r => r.Abort(), Times.Once());
            }
Пример #7
0
            public void TwiceWhenPreviousResponseAborted()
            {
                var injector = new MockInjector();
                var listener = injector.SetupAndGetListener();
                var server = injector.Create<Server>();
                var request1 = new Mock<IHttpListenerRequest>();
                injector.SetupProxy(request1);
                var response1 = new Mock<IHttpListenerResponse>();
                injector.SetupProxy(response1);
                var request2 = new Mock<IHttpListenerRequest>();
                injector.SetupProxy(request2);

                listener.SetupSequence(l => l.GetContextAsync().Result.Request)
                        .Returns(request1.Object)
                        .Returns(request2.Object);

                listener.Setup(l => l.GetContextAsync().Result.Response)
                        .Returns(response1.Object);

                server.Receive();
                server.AbortResponse();
                server.Receive()
                      .Should().Be(request2.Object);
            }
Пример #8
0
            public void TwiceWhenPreviousRequestNotRespondedTo()
            {
                var injector = new MockInjector();
                var listener = injector.SetupAndGetListener();
                var server = injector.Create<Server>();
                var request = new Mock<IHttpListenerRequest>();
                injector.SetupProxy(request);

                listener.Setup(l => l.GetContextAsync().Result.Request).Returns(request.Object);

                server.Receive();

                server.Invoking(s => s.Receive())
                      .ShouldThrow<InvalidOperationException>();
            }
Пример #9
0
            public void OnceWhenAlreadyListening()
            {
                var injector = new MockInjector();
                var listener = injector.SetupAndGetListener();
                var server = injector.Create<Server>();
                var request = new Mock<IHttpListenerRequest>();
                injector.SetupProxy(request);

                listener.Setup(l => l.IsListening).Returns(true);
                listener.Setup(l => l.GetContextAsync().Result.Request).Returns(request.Object);

                server.Receive()
                      .Should().Be(request.Object);

                listener.Verify(l => l.Start(), Times.Never());
            }