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)); }
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)); }
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(); }
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)); }
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); }
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()); }
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); }
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>(); }
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()); }