public void SendCustomActionExceptionStillReturnsResponse() { var injector = new MockInjector(); var client = injector.Create<Client>(); var endpointProvider = injector.GetMock<IEndpointProvider>(); var webRequestFactory = injector.GetMock<IWebRequestFactory>(); var request = new Mock<IHttpWebRequest>(); var response = new Mock<HttpWebResponse>(); response .Setup(r => r.GetResponseStream()) .Returns(new MemoryStream()); endpointProvider .Setup(e => e.ClientEndpoint) .Returns(new Queue<string>(new[] { "moo" })); webRequestFactory .Setup(f => f.CreateHttp("moo")) .Returns(request.Object); webRequestFactory .Setup(f => f.WithLoggingProxy(response.Object.ActLike<IHttpWebResponse>())) .Returns(response.Object.ActLike<IHttpWebResponse>()); request.Setup(r => r.GetResponse()) .Throws(new WebException( "moo", null, WebExceptionStatus.ProtocolError, response.Object)); client.Send(r => r.Should().Be(request.Object)) .Should().Be(response.Object); }
public void FromMessage() { var injector = new MockInjector(); SoapDecoder decoder = injector.Create<SoapDecoder>(); var contentTypeProvider = injector.GetMock<IContentTypeProvider>(); var messageProvider = injector.GetMock<IMessageProvider>(); var endpointProvider = injector.GetMock<IEndpointProvider>(); var securityHeaderFactory = injector.GetMock<ISecurityHeaderFactory>(); var response = injector.GetMock<IHttpListenerResponse>(); var responseMessage = Message.CreateMessage(MessageVersion.Default, "moo"); var requestMessage = Message.CreateMessage(MessageVersion.Default, "moo"); var request = injector.GetMock<IHttpListenerRequest>(); var stream = new MemoryStream(); messageProvider.Setup(m => m.GetMessage(stream)).Returns(requestMessage); request.Setup(r => r.InputStream).Returns(stream); request.Setup(r => r.ContentType).Returns("text/xml"); endpointProvider.Setup(e => e.SkipAuthentication).Returns(true); response.Setup(r => r.OutputStream).Returns(stream); contentTypeProvider.Setup(p => p.ValidContentTypes).Returns(new[] { "text/xml" }); var securityHeader = MessageHeader.CreateHeader("security", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "secure"); securityHeaderFactory.Setup(s => s.CreateWithoutUserToken(requestMessage)).Returns(securityHeader); decoder.FromMessage(response.Object, responseMessage, request.Object); responseMessage.Headers.Should().HaveCount(2) .And.Contain(securityHeader); }
public void Default() { var injector = new MockInjector(); var factory = injector.Create<SecurityHeaderFactory>(); var security = new Security { Timestamp = new Timestamp { Created = "2012-11-07T01:41:35.821Z" } }; var requestMessage = Message.CreateMessage(MessageVersion.Default, "moo"); MessageHeader securityHeader = MessageHeader.CreateHeader("Security", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", security, new CfMessagingSerializer(typeof(Security))); requestMessage.Headers.Add(securityHeader); var header = factory.CreateWithoutUserToken(requestMessage); header.Name.Should().Be("Security"); header.Namespace.Should().Be("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"); string expectedXml = @"<Security xmlns=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""> <Timestamp xmlns=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd""> <Created>2012-11-07T01:41:35.821Z</Created> </Timestamp> </Security>"; header.ToString().Should().Be(expectedXml); }
public void AllowsOtherExceptionsThroughFromStoppingListener() { var injector = new MockInjector(); var listener = injector.SetupAndGetListener(); var server = injector.Create<Server>(); listener.Setup(l => l.Stop()).Throws<ArgumentException>(); server.Invoking(s => s.Dispose()).ShouldThrow<ArgumentException>(); }
public void IgnoresInvalidOperationFromStoppingListener() { var injector = new MockInjector(); var listener = injector.SetupAndGetListener(); var server = injector.Create<Server>(); listener.Setup(l => l.Stop()).Throws<InvalidOperationException>(); server.Dispose(); }
public void Prefixes() { var injector = new MockInjector(); var listener = injector.SetupAndGetListener(); var baseUrlProvider = injector.GetMock<IEndpointProvider>(); baseUrlProvider.Setup(b => b.ServerBaseUrl).Returns("moo"); var server = injector.Create<Server>(); listener.Object.Prefixes.Should().BeEquivalentTo(new[] {"moo"}); }
public void OnlyDisposesOnce() { var injector = new MockInjector(); var listener = injector.SetupAndGetListener(); var server = injector.Create<Server>(); server.Dispose(); server.Dispose(); listener.Verify(l => l.Stop(), Times.Once()); }
public void ScenarioOption() { var injector = new MockInjector(); var program = injector.Create<Program>(); var runner = injector.GetMock<IRunner>(); var options = new Options { Scenario = "whee" }; program.Run(options) .Should().Be(0); runner.Verify(r => r.Run("whee", null, null)); }
public void SendCustomAction() { var injector = new MockInjector(); var client = injector.Create<Client>(); var endpointProvider = injector.GetMock<IEndpointProvider>(); var webRequestFactory = injector.GetMock<IWebRequestFactory>(); var request = new Mock<IHttpWebRequest>(); var response = new Mock<IHttpWebResponse>(); endpointProvider.Setup(e => e.ClientEndpoint).Returns(new Queue<string>(new[] { "moo" })); webRequestFactory.Setup(f => f.CreateHttp("moo")).Returns(request.Object); request.Setup(r => r.GetResponse()).Returns(response.Object); client.Send(r => r.Should().Be(request.Object)) .Should().Be(response.Object); }
public void SendCustomActionExceptionNoResponse() { var injector = new MockInjector(); var client = injector.Create<Client>(); var endpointProvider = injector.GetMock<IEndpointProvider>(); var webRequestFactory = injector.GetMock<IWebRequestFactory>(); var request = new Mock<IHttpWebRequest>(); endpointProvider.Setup(e => e.ClientEndpoint).Returns(new Queue<string>(new[] { "moo" })); webRequestFactory.Setup(f => f.CreateHttp("moo")).Returns(request.Object); request.Setup(r => r.GetResponse()) .Throws(new WebException( "moo", null, WebExceptionStatus.ProtocolError, null)); Action action = () => client.Send(r => r.Should().Be(request.Object)); action.ShouldThrow<WebException>(); }
public void ScenarioOptionThrowingException() { var injector = new MockInjector(); var program = injector.Create<Program>(); var runner = injector.GetMock<IRunner>(); var output = injector.GetMock<IOutput>(); var options = new Options { Scenario = "s" }; var exception = new InvalidOperationException("moo"); runner.Setup(r => r.Run(It.IsAny<string>(), It.IsAny<string[]>(), null)) .Throws(exception); program.Run(options) .Should().Be(1); output.Verify(o => o.Display(It.Is<ResultModel>(r => r.Result == Result.Fail && r.Message == exception.ToString()))); }
public void Default() { var injector = new MockInjector(); var factory = injector.Create<SecurityHeaderFactory>(); var endpointProvider = injector.GetMock<IEndpointProvider>(); var dateTimeProvider = injector.GetMock<IDateTimeProvider>(); var randomProvider = injector.GetMock<IRandomNumberGeneratorProvider>(); endpointProvider.Setup(e => e.Username).Returns("someuser"); endpointProvider.Setup(e => e.Password).Returns("somepass"); dateTimeProvider.Setup(d => d.Now).Returns(new DateTime(2013, 01, 20)); randomProvider.Setup(r => r.GetBytes(It.IsAny<byte[]>())) .Callback<byte[]>(bytes => { for (int i = 0; i < bytes.Length; i++) { bytes[i] = 7; } }); var header = factory.Create(); header.Name.Should().Be("Security"); header.Namespace.Should().Be("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"); const string expectedXml = @"<Security xmlns=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""> <UsernameToken> <Username>someuser</Username> <Password Type=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"">somepass</Password> <Nonce EncodingType=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary"">BwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBw==</Nonce> <Created xmlns=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"">2013-01-20T00:00:00.000Z</Created> </UsernameToken> </Security>"; header.ToString().Should().Be(expectedXml); }
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 SendMissingClientEndpoint() { var injector = new MockInjector(); var client = injector.Create<Client>(); var endpointProvider = injector.GetMock<IEndpointProvider>(); var webRequestFactory = injector.GetMock<IWebRequestFactory>(); var request = new Mock<IHttpWebRequest>(); var response = new Mock<IHttpWebResponse>(); webRequestFactory.Setup(f => f.CreateHttp("moo")).Returns(request.Object); request.Setup(r => r.GetResponse()).Returns(response.Object); var ex = Assert.Throws<NotSupportedException>(() => client.Send(r => r.Should().Be(request.Object))); ex.Message.Should().Be("Client endpoint should be specified with -c"); }
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 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 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 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 RespondWhenNoRequest() { var injector = new MockInjector(); var listener = injector.SetupAndGetListener(); var server = injector.Create<Server>(); server.Invoking(s => s.Respond(r => r.Abort())) .ShouldThrow<InvalidOperationException>() .WithMessage("no request", ComparisonMode.Substring); }
public void SendMessageSecurityHeader() { var injector = new MockInjector(); var client = injector.Create<Client>(); var endpointProvider = injector.GetMock<IEndpointProvider>(); var webRequestFactory = injector.GetMock<IWebRequestFactory>(); var securityHeaderFactory = injector.GetMock<ISecurityHeaderFactory>(); var soapDecoder = injector.GetMock<ISoapDecoder>(); var request = new Mock<IHttpWebRequest>(); var response = new Mock<IHttpWebResponse>(); var message = Message.CreateMessage(MessageVersion.Default, "moo"); var messageHeader = MessageHeader.CreateHeader("TestHeader", "", ""); endpointProvider.Setup(e => e.ClientEndpoint).Returns(new Queue<string>(new[] { "moo" })); securityHeaderFactory.Setup(s => s.Create()).Returns(messageHeader); webRequestFactory.Setup(f => f.CreateHttp("moo")).Returns(request.Object); request.Setup(r => r.GetResponse()).Returns(response.Object); client.Send(message); message.Headers.Single(h => h.Name == "TestHeader") .Should().Be(messageHeader); }
public void NoOptions() { var injector = new MockInjector(); var program = injector.Create<Program>(); program.Run(new Options()).Should().Be(1); }
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()); }
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 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 ScenarioOptionWithArguments() { var injector = new MockInjector(); var program = injector.Create<Program>(); var runner = injector.GetMock<IRunner>(); var options = new Options { Scenario = "moo", ScenarioArguments = new[] {"a1", "a2"} }; program.Run(options) .Should().Be(0); runner.Verify(r => r.Run("moo", new[] {"a1", "a2"}, null)); }
public void SendData() { var injector = new MockInjector(); var client = injector.Create<Client>(); var endpointProvider = injector.GetMock<IEndpointProvider>(); var webRequestFactory = injector.GetMock<IWebRequestFactory>(); var soapDecoder = injector.GetMock<ISoapDecoder>(); var securityHeaderFactory = injector.GetMock<ISecurityHeaderFactory>(); var request = new Mock<IHttpWebRequest>(); var response = new Mock<IHttpWebResponse>(); var data = new { }; var message = Message.CreateMessage(MessageVersion.Default, "moo"); endpointProvider.Setup(e => e.ClientEndpoint).Returns(new Queue<string>(new[] { "moo" })); securityHeaderFactory.Setup(s => s.Create()).Returns(MessageHeader.CreateHeader("Moo", "", "")); webRequestFactory.Setup(f => f.CreateHttp("moo")).Returns(request.Object); request.Setup(r => r.GetResponse()).Returns(response.Object); soapDecoder.Setup(s => s.ToMessage("moo", data)) .Returns(message); client.Send("moo", data) .Should().Be(response.Object); soapDecoder.Verify(s => s.FromMessage(request.Object, message)); }