public async Task ServiceProxyRpcMessageShouldRemainSame() { const string serviceName = "DemoService"; int defaultPort = DisposablePort.GetPort().Port; var dict = new Dictionary <string, string> { { $"Discovery.Services.{serviceName}.Source", "Config" }, { $"Discovery.Services.{serviceName}.Hosts", "host1" }, { $"Discovery.Services.{serviceName}.DefaultPort", defaultPort.ToString() } }; Uri uri = null; string requestMessage = null; Func <HttpClientConfiguration, HttpMessageHandler> messageHandlerFactory = _ => { var messageHandler = new MockHttpMessageHandler(); messageHandler .When("*").Respond(async req => { requestMessage = await req.Content.ReadAsStringAsync(); uri = req.RequestUri; return(HttpResponseFactory.GetResponse(HttpStatusCode.Accepted)); }); return(messageHandler); }; using (var kernel = new TestingKernel <ConsoleLog>(k => { k.Rebind <IDiscovery>().To <ServiceDiscovery.Rewrite.Discovery>().InSingletonScope(); k.Rebind <Func <HttpClientConfiguration, HttpMessageHandler> >().ToMethod(c => messageHandlerFactory); }, dict)) { var providerFactory = kernel.Get <Func <string, ServiceProxyProvider> >(); TracingContext.SetRequestID("g"); var serviceProxy = providerFactory(serviceName); string expectedHost = "override-host"; int expectedPort = DisposablePort.GetPort().Port; TracingContext.SetHostOverride(serviceName, expectedHost, expectedPort); var request = new HttpServiceRequest("testMethod", null, new Dictionary <string, object>()); using (TracingContext.Tags.SetUnencryptedTag("test", 1)) await serviceProxy.Invoke(request, typeof(string)); var body = requestMessage; Console.WriteLine($"error: {body}"); JsonConvert.DeserializeObject <GigyaRequestProtocol>(body, new JsonSerializerSettings() { MissingMemberHandling = MissingMemberHandling.Error }); uri.Host.ShouldBe(expectedHost); uri.Port.ShouldBe(expectedPort); } }
public async Task CreateListener_HttpsEnabled_CreationFailsNoCertificateFound() { var endpointDefinition = Substitute.For <IServiceEndPointDefinition>(); using (var httpPort = DisposablePort.GetPort()) using (var httpsPort = DisposablePort.GetPort()) { endpointDefinition.HttpPort.Returns(ci => httpPort.Port); endpointDefinition.HttpsPort.Returns(ci => httpsPort.Port); // HTTPS enabled by a non-null HTTPS port endpointDefinition.ClientCertificateVerification.Returns(ci => ClientCertificateVerificationMode.VerifyIdenticalRootCertificate); var certificateLocator = Substitute.For <ICertificateLocator>(); certificateLocator.GetCertificate(Arg.Any <string>()).Throws <Exception>(); Assert.Throws <Exception>(() => new HttpServiceListener(Substitute.For <IActivator>(), Substitute.For <IWorker>(), endpointDefinition, certificateLocator, Substitute.For <ILog>(), Enumerable.Empty <ICustomEndpoint>(), Substitute.For <IEnvironment>(), new JsonExceptionSerializer(Substitute.For <IStackTraceEnhancer>(), new JsonExceptionSerializationSettings(() => new ExceptionSerializationConfig(false, false))), new ServiceSchema(), () => new LoadShedding(), Substitute.For <IServerRequestPublisher>(), new CurrentApplicationInfo( nameof(HttpServiceListenerTests), Environment.UserName, System.Net.Dns.GetHostName()))); certificateLocator.Received(1).GetCertificate("Service"); } }
public async Task OneHostHasNetworkErrorShouldMoveToNextHost() { var port = DisposablePort.GetPort().Port; var dict = new Dictionary <string, string> { { "Discovery.Services.DemoService.Source", "Config" }, { "Discovery.Services.DemoService.Hosts", "host1,host2" }, { "Discovery.Services.DemoService.DefaultPort", port.ToString() } }; int counter = 0; Func <HttpClientConfiguration, HttpMessageHandler> messageHandlerFactory = _ => { var messageHandler = new MockHttpMessageHandler(); messageHandler .When("*") .Respond(req => { if (req.Method == HttpMethod.Get && req.RequestUri.Scheme == "https") { throw new HttpRequestException(); } counter++; if (req.RequestUri.Host == "host1") { throw new HttpRequestException(); } return(HttpResponseFactory.GetResponse(content: $"'{req.RequestUri.Host}'")); }); return(messageHandler); }; using (var kernel = new TestingKernel <ConsoleLog>( k => { k.Rebind <IDiscovery>().To <ServiceDiscovery.Rewrite.Discovery>().InSingletonScope(); k.Rebind <Func <HttpClientConfiguration, HttpMessageHandler> >().ToMethod(c => messageHandlerFactory); }, dict) ) { var providerFactory = kernel.Get <Func <string, ServiceProxyProvider> >(); var serviceProxy = providerFactory("DemoService"); serviceProxy.DefaultPort = port; TracingContext.SetRequestID("1"); var request = new HttpServiceRequest("testMethod", null, new Dictionary <string, object>()); for (int i = 0; i < 3; i++) { var server = await serviceProxy.Invoke(request, typeof(string)); server.ShouldBe("host2"); } counter.ShouldBe(3); } }
public async Task CreateListener_HttpsDisabled_CreationSucceedsNoNeedForCertificate() { var endpointDefinition = Substitute.For <IServiceEndPointDefinition>(); using (var httpPort = DisposablePort.GetPort()) { endpointDefinition.HttpsPort.Returns(ci => httpPort.Port); endpointDefinition.HttpsPort.Returns(ci => null); // HTTPS disabled by a null HTTPS port var certificateLocator = Substitute.For <ICertificateLocator>(); certificateLocator.GetCertificate(Arg.Any <string>()).Throws <Exception>(); using (var listener = new HttpServiceListener(Substitute.For <IActivator>(), Substitute.For <IWorker>(), endpointDefinition, certificateLocator, Substitute.For <ILog>(), Enumerable.Empty <ICustomEndpoint>(), Substitute.For <IEnvironment>(), new JsonExceptionSerializer(Substitute.For <IStackTraceEnhancer>(), new JsonExceptionSerializationSettings(() => new ExceptionSerializationConfig(false, false))), new ServiceSchema(), () => new LoadShedding(), Substitute.For <IServerRequestPublisher>(), new CurrentApplicationInfo( nameof(HttpServiceListenerTests), Environment.UserName, System.Net.Dns.GetHostName()) )) { listener.Start(); } certificateLocator.DidNotReceive().GetCertificate(Arg.Any <string>()); } }
public async Task WorkloadMetricsCalledOnce() { var args = new ServiceArguments(ServiceStartupMode.CommandLineNonInteractive, ConsoleOutputMode.Disabled, SiloClusterMode.PrimaryNode, DisposablePort.GetPort().Port, initTimeOutSec: 10); IWorkloadMetrics workloadMetricsFake = Substitute.For <IWorkloadMetrics>(); var srvHost = new ServiceHostFake <IWorkloadMetrics>( workloadMetricsFake, new HostEnvironment(new TestHostEnvironmentSource())) { FailServiceStartOnConfigError = false };; Task.Run(() => srvHost.Run(args)); await srvHost.WaitForServiceStartedAsync(); srvHost.Dispose(); workloadMetricsFake.Received(1).Init(); workloadMetricsFake.Received().Dispose(); }
public async Task RequestContextShouldOverrideHostOnly() { const string serviceName = "DemoService"; int defaultPort = DisposablePort.GetPort().Port; var dict = new Dictionary <string, string> { { $"Discovery.Services.{serviceName}.Source", "Config" }, { $"Discovery.Services.{serviceName}.Hosts", "host1" }, { $"Discovery.Services.{serviceName}.DefaultPort", defaultPort.ToString() } }; var kernel = new TestingKernel <ConsoleLog>(k => k.Rebind <IDiscovery>().To <ServiceDiscovery.Rewrite.Discovery>().InSingletonScope(), dict); var providerFactory = kernel.Get <Func <string, ServiceProxyProvider> >(); var serviceProxy = providerFactory(serviceName); var messageHandler = new MockHttpMessageHandler(); messageHandler .When("*") .Respond(req => HttpResponseFactory.GetResponse(content: $"'{req.RequestUri.Host}:{req.RequestUri.Port}'")); string overrideHost = "override-host"; serviceProxy.HttpMessageHandler = messageHandler; TracingContext.SetHostOverride(serviceName, overrideHost); var request = new HttpServiceRequest("testMethod", null, new Dictionary <string, object>()); for (int i = 0; i < 50; i++) { var host = (string)await serviceProxy.Invoke(request, typeof(string)); host.ShouldBe($"{overrideHost}:{defaultPort}"); } }
public async Task FailedHostShouldBeRemovedFromHostList() { var port = DisposablePort.GetPort().Port; var dict = new Dictionary <string, string> { { "Discovery.Services.DemoService.Source", "local" }, { "Discovery.Services.DemoService.DefaultPort", port.ToString() } }; int counter = 0; Func <HttpClientConfiguration, HttpMessageHandler> messageHandlerFactory = _ => { var messageHandler = new MockHttpMessageHandler(); messageHandler .When("*") .Respond(req => { bool disableReachabilityChecker = req.Content == null; if (disableReachabilityChecker) { throw new HttpRequestException(); } counter++; throw new HttpRequestException(); }); return(messageHandler); }; using (var kernel = new TestingKernel <ConsoleLog>( k => { k.Rebind <IDiscovery>().To <ServiceDiscovery.Rewrite.Discovery>().InSingletonScope(); k.Rebind <Func <HttpClientConfiguration, HttpMessageHandler> >().ToMethod(c => messageHandlerFactory); }, dict) ) { var providerFactory = kernel.Get <Func <string, ServiceProxyProvider> >(); var serviceProxy = providerFactory("DemoService"); serviceProxy.DefaultPort = port; //Disable TracingContext.SetRequestID("1"); CallContext.FreeNamedDataSlot("#ORL_RC"); var request = new HttpServiceRequest("testMethod", null, new Dictionary <string, object>()); for (int i = 0; i < 10; i++) { Func <Task> act = () => serviceProxy.Invoke(request, typeof(string)); await act.ShouldThrowAsync <ServiceUnreachableException>(); } counter.ShouldBe(1); } }
public async Task AllHostsAreHavingNetworkErrorsShouldTryEachOnce() { var port = DisposablePort.GetPort().Port; var dict = new Dictionary <string, string> { { "Discovery.Services.DemoService.Source", "Config" }, { "Discovery.Services.DemoService.Hosts", "host1,host2" }, { "Discovery.Services.DemoService.DefaultPort", port.ToString() } }; int counter = 0; Func <HttpClientConfiguration, HttpMessageHandler> messageHandlerFactory = _ => { var messageHandler = new MockHttpMessageHandler(); messageHandler .When("*") .Respond(req => { bool disableReachabilityChecker = req.Content == null; if (disableReachabilityChecker) { throw new HttpRequestException(); } counter++; throw new HttpRequestException(); }); return(messageHandler); }; using (var kernel = new TestingKernel <ConsoleLog>( k => { k.Rebind <IDiscovery>().To <ServiceDiscovery.Rewrite.Discovery>().InSingletonScope(); k.Rebind <Func <HttpClientConfiguration, HttpMessageHandler> >().ToMethod(c => messageHandlerFactory); }, dict) ) { var providerFactory = kernel.Get <Func <string, ServiceProxyProvider> >(); var serviceProxy = providerFactory("DemoService"); serviceProxy.DefaultPort = port; var request = new HttpServiceRequest("testMethod", null, new Dictionary <string, object>()); Func <Task> act = () => serviceProxy.Invoke(request, typeof(string)); await act.ShouldThrowAsync <ServiceUnreachableException>(); counter.ShouldBe(2); } }
public async Task RequestContextOverrideShouldFailOnFirstAttempt() { var port = DisposablePort.GetPort().Port; var dict = new Dictionary <string, string> { { "Discovery.Services.DemoService.Source", "Config" }, { "Discovery.Services.DemoService.Hosts", "notImpotent" }, { "Discovery.Services.DemoService.DefaultPort", port.ToString() } }; using (var kernel = new TestingKernel <ConsoleLog>( k => k.Rebind <IDiscovery>().To <ServiceDiscovery.Rewrite.Discovery>().InSingletonScope(), dict) ) { var providerFactory = kernel.Get <Func <string, ServiceProxyProvider> >(); var serviceProxy = providerFactory("DemoService"); serviceProxy.DefaultPort = port; //Disable TracingContext.SetRequestID("1"); CallContext.FreeNamedDataSlot("#ORL_RC"); int counter = 0; var messageHandler = new MockHttpMessageHandler(); messageHandler .When("*") .Respond(req => { counter++; throw new HttpRequestException(); }); string overrideHost = "override-host"; int overridePort = 5318; TracingContext.SetHostOverride("DemoService", overrideHost, overridePort); serviceProxy.HttpMessageHandler = messageHandler; var request = new HttpServiceRequest("testMethod", null, new Dictionary <string, object>()); for (int i = 0; i < 3; i++) { Func <Task> act = () => serviceProxy.Invoke(request, typeof(string)); await act.ShouldThrowAsync <HttpRequestException>(); } counter.ShouldBe(3); } }
public async Task AllRequestsForSameCallID_SameHostSelected() { var port = DisposablePort.GetPort().Port; var dict = new Dictionary <string, string> { { "Discovery.Services.DemoService.Source", "Config" }, { "Discovery.Services.DemoService.Hosts", "host1,host2" }, { "Discovery.Services.DemoService.DefaultPort", port.ToString() } }; Func <HttpClientConfiguration, HttpMessageHandler> messageHandlerFactory = _ => { var messageHandler = new MockHttpMessageHandler(); messageHandler .When("*") .Respond(req => HttpResponseFactory.GetResponse(content: $"'{req.RequestUri.Host}'")); return(messageHandler); }; using (var kernel = new TestingKernel <ConsoleLog>( k => { k.Rebind <IDiscovery>().To <ServiceDiscovery.Rewrite.Discovery>().InSingletonScope(); k.Rebind <Func <HttpClientConfiguration, HttpMessageHandler> >().ToMethod(c => messageHandlerFactory); }, dict)) { var providerFactory = kernel.Get <Func <string, ServiceProxyProvider> >(); var serviceProxy = providerFactory("DemoService"); serviceProxy.DefaultPort = port; //If we set Request Id we would like always to select same Host TracingContext.SetRequestID("dumyId1"); var request = new HttpServiceRequest("testMethod", null, new Dictionary <string, object>()); var hostOfFirstReq = (string)await serviceProxy.Invoke(request, typeof(string)); string host; for (int i = 0; i < 50; i++) { host = (string)await serviceProxy.Invoke(request, typeof(string)); host.ShouldBe(hostOfFirstReq); } TracingContext.SetRequestID("dumyId2"); host = (string)await serviceProxy.Invoke(request, typeof(string)); host.ShouldNotBe(hostOfFirstReq); } }
public async Task ServiceTesterWhenServiceFailedToGracefullyShutdownShouldThrow() { // shutdownWaitTimeSec: 0 var serviceArguments = new ServiceArguments( ServiceStartupMode.CommandLineNonInteractive, ConsoleOutputMode.Disabled, SiloClusterMode.PrimaryNode, DisposablePort.GetPort().Port, onStopWaitTimeSec: 0); _tester = new ServiceTester <CalculatorServiceHost>(serviceArguments); Action act = () => _tester.Dispose(); act.ShouldThrow <Exception>().Message.ShouldContain("service failed to shutdown gracefully"); }
public async Task ValidatorCalledOnce() { IValidator validatorFake = Substitute.For <IValidator>(); ServiceHostFake <IValidator> srvHost = new ServiceHostFake <IValidator>(validatorFake); var args = new ServiceArguments(ServiceStartupMode.CommandLineNonInteractive, ConsoleOutputMode.Disabled, SiloClusterMode.PrimaryNode, DisposablePort.GetPort().Port, initTimeOutSec: 10); Task.Run(() => srvHost.Run(args)); await srvHost.WaitForServiceStartedAsync(); srvHost.Dispose(); validatorFake.Received(1).Validate(); }
public async Task HealthCheck_ServcieDrain_StatueShouldBe521() { int port = DisposablePort.GetPort().Port; //serviceDrainTimeSec: var serviceArguments = new ServiceArguments(ServiceStartupMode.CommandLineNonInteractive, ConsoleOutputMode.Disabled, SiloClusterMode.PrimaryNode, port, serviceDrainTimeSec: 1, instanceName: "test", initTimeOutSec: 10); var customServiceTester = new ServiceTester <CalculatorServiceHost>(serviceArguments); var dispose = Task.Run(() => customServiceTester.Dispose()); await Task.Delay(200); var httpResponseMessage = await new HttpClient().GetAsync(new Uri($"http://{CurrentApplicationInfo.HostName}:{port}/{nameof(IProgrammableHealth).Substring(1)}.status")); httpResponseMessage.StatusCode.ShouldBe((HttpStatusCode)521); await dispose; }
public async Task HttpsDisabled_NoCertificate_CallSucceeds() { var host = "host1"; var httpsPortOffset = 5; var port = DisposablePort.GetPort().Port; var dict = new Dictionary <string, string> { { "Discovery.Services.DemoService.Source", "Config" }, { "Discovery.Services.DemoService.Hosts", host }, { "Discovery.Services.DemoService.DefaultPort", port.ToString() }, { "Discovery.Services.DemoService.UseHttpsOverride", "false" } }; int httpsTestCount = 0; bool httpsMethodCalled = false; Func <HttpClientConfiguration, HttpMessageHandler> messageHandlerFactory = _ => { var messageHandler = new MockHttpMessageHandler(); messageHandler .When("https://*") .Respond(req => HttpResponseFactory.GetResponseWithException(ExceptionSerializer, new SocketException())); messageHandler .When("http://*") .Respond(req => HttpResponseFactory.GetResponse(content: "'some HTTP response'")); return(messageHandler); }; using (var kernel = new TestingKernel <ConsoleLog>( k => { k.Rebind <IDiscovery>().To <ServiceDiscovery.Rewrite.Discovery>().InSingletonScope(); k.Rebind <Func <HttpClientConfiguration, HttpMessageHandler> >().ToMethod(c => messageHandlerFactory); var certificateLocator = Substitute.For <ICertificateLocator>(); certificateLocator .When(cl => cl.GetCertificate(Arg.Any <string>())) .Do(x => throw new Exception()); k.Rebind <ICertificateLocator>().ToConstant(certificateLocator); var httpsAuthenticator = Substitute.For <IHttpsAuthenticator>(); httpsAuthenticator .When(a => a.AddHttpMessageHandlerAuthentication(Arg.Any <HttpClientHandler>(), Arg.Any <HttpClientConfiguration>())) .Do(x => throw new Exception()); k.Rebind <IHttpsAuthenticator>().ToConstant(httpsAuthenticator); }, dict) ) { var providerFactory = kernel.Get <Func <string, ServiceProxyProvider> >(); var serviceProxy = providerFactory("DemoService"); serviceProxy.DefaultPort = port; TracingContext.SetRequestID("1"); var request = new HttpServiceRequest("testMethod", null, new Dictionary <string, object>()); await serviceProxy.Invoke(request, typeof(string)); } }
public async Task HttpsStoppedListening_FallbackToHttp() { var host = "host1"; var httpsPortOffset = 5; var port = DisposablePort.GetPort().Port; var dict = new Dictionary <string, string> { { "Discovery.Services.DemoService.Source", "Config" }, { "Discovery.Services.DemoService.Hosts", host }, { "Discovery.Services.DemoService.DefaultPort", port.ToString() }, { "Discovery.Services.DemoService.TryHttps", "true" } }; int httpsTestCount = 0; bool httpsMethodCalled = false; Func <HttpClientConfiguration, HttpMessageHandler> messageHandlerFactory = _ => { var messageHandler = new MockHttpMessageHandler(); messageHandler .When("https://*") .Respond(req => { if (httpsMethodCalled) { throw new HttpRequestException("", new WebException("", WebExceptionStatus.ProtocolError)); } if (req.RequestUri.AbsoluteUri == $"https://{host}:{port + httpsPortOffset}/") { httpsTestCount++; return(HttpResponseFactory.GetResponse(content: "'some HTTPS response'")); } if (req.RequestUri.AbsoluteUri == $"https://{host}:{port + httpsPortOffset}/DemoService.testMethod") { httpsMethodCalled = true; return(HttpResponseFactory.GetResponse(content: "'some HTTPS response'")); } throw new HttpRequestException("Invalid uri"); }); messageHandler .When("http://*") .Respond(req => { if (req.RequestUri.AbsoluteUri == $"http://{host}:{port}/DemoService.testMethod") { return(HttpResponseFactory.GetResponse(content: "'some HTTP response'")); } if (req.RequestUri.AbsoluteUri == $"http://{host}:{port}/") { return(HttpResponseFactory.GetResponse(content: "'{X-Gigya-ServerHostname: someValue}'")); } throw new HttpRequestException("Invalid uri"); }); return(messageHandler); }; using (var kernel = new TestingKernel <ConsoleLog>( k => { k.Rebind <IDiscovery>().To <ServiceDiscovery.Rewrite.Discovery>().InSingletonScope(); k.Rebind <Func <HttpClientConfiguration, HttpMessageHandler> >().ToMethod(c => messageHandlerFactory); var getConfig = k.Get <Func <DiscoveryConfig> >(); k.Rebind <Func <DiscoveryConfig> >().ToMethod(c => { var config = getConfig(); config.UseHttpsOverride = false; return(() => config); }); }, dict) ) { var providerFactory = kernel.Get <Func <string, ServiceProxyProvider> >(); var serviceProxy = providerFactory("DemoService"); serviceProxy.DefaultPort = port; TracingContext.SetRequestID("1"); var request = new HttpServiceRequest("testMethod", null, new Dictionary <string, object>()); var server = await serviceProxy.Invoke(request, typeof(string)); server.ShouldBe("some HTTP response"); Assert.That(() => httpsTestCount, Is.EqualTo(1).After(10).Seconds.PollEvery(1).Seconds); server = await serviceProxy.Invoke(request, typeof(string)); server.ShouldBe("some HTTPS response"); server = await serviceProxy.Invoke(request, typeof(string)); server.ShouldBe("some HTTP response"); } }
public void SetUp() { try { Service = new NonOrleansServiceTester <SlowServiceHost>( new ServiceArguments(ServiceStartupMode.CommandLineNonInteractive, basePortOverride: DisposablePort.GetPort().Port)) .GetServiceProxyWithCaching <ISlowService>(); } catch (Exception ex) { Console.Write(ex.ToString()); throw; } }
public void SetUp() { try { var serviceTester = new NonOrleansServiceTester <SlowServiceHost>( new ServiceArguments(ServiceStartupMode.CommandLineNonInteractive, basePortOverride: DisposablePort.GetPort().Port)); serviceTester.CommunicationKernel.Rebind <ICertificateLocator>().To <DummyCertificateLocator>().InSingletonScope(); Service = serviceTester.GetServiceProxyWithCaching <ISlowService>(); } catch (Exception ex) { Console.Write(ex.ToString()); throw; } }
public async Task HttpsListening_CallHttpsAfterFirstHttpCall() { var host = "host1"; var httpsPortOffset = 5; var port = DisposablePort.GetPort().Port; var dict = new Dictionary <string, string> { { "Discovery.Services.DemoService.Source", "Config" }, { "Discovery.Services.DemoService.Hosts", host }, { "Discovery.Services.DemoService.DefaultPort", port.ToString() } }; int httpsTestCount = 0; Func <bool, string, HttpMessageHandler> messageHandlerFactory = (_, __) => { var messageHandler = new MockHttpMessageHandler(); messageHandler .When("https://*") .Respond(req => { if (req.RequestUri.AbsoluteUri == $"https://{host}:{port + httpsPortOffset}/") { httpsTestCount++; return(HttpResponseFactory.GetResponse(content: "'some HTTPS response'")); } if (req.RequestUri.AbsoluteUri == $"https://{host}:{port + httpsPortOffset}/DemoService.testMethod") { return(HttpResponseFactory.GetResponse(content: "'some HTTPS response'")); } throw new HttpRequestException("Invalid uri"); }); messageHandler .When("http://*") .Respond(req => { if (req.RequestUri.AbsoluteUri == $"http://{host}:{port}/DemoService.testMethod") { return(HttpResponseFactory.GetResponse(content: "'some HTTP response'")); } throw new HttpRequestException("Invalid uri"); }); return(messageHandler); }; using (var kernel = new TestingKernel <ConsoleLog>( k => { k.Rebind <IDiscovery>().To <ServiceDiscovery.Rewrite.Discovery>().InSingletonScope(); k.Rebind <Func <bool, string, HttpMessageHandler> >().ToMethod(c => messageHandlerFactory); }, dict) ) { var providerFactory = kernel.Get <Func <string, ServiceProxyProvider> >(); var serviceProxy = providerFactory("DemoService"); serviceProxy.DefaultPort = port; TracingContext.SetRequestID("1"); var request = new HttpServiceRequest("testMethod", null, new Dictionary <string, object>()); for (int i = 0; i < 10; i++) { bool httpsTestFinished = httpsTestCount > 0; var server = await serviceProxy.Invoke(request, typeof(string)); server.ShouldBe(httpsTestFinished ? "some HTTPS response" : "some HTTP response"); } Assert.That(() => httpsTestCount, Is.EqualTo(1).After(10).Seconds.PollEvery(1).Seconds); } }