public WebHostBuilderServiceFabricExtensionTests() { settings = new Dictionary <string, string>(); // create mock IWebHostBuilder to test functionality of Service Fabric WebHostBuilder extension var mockBuilder = new Mock <IWebHostBuilder>(); // setup call backs for Getting and setting settings. mockBuilder.Setup(y => y.GetSetting(It.IsAny <string>())).Returns <string>(name => { string value; this.settings.TryGetValue(name, out value); return(value); }); mockBuilder.Setup(y => y.UseSetting(It.IsAny <string>(), It.IsAny <string>())).Callback <string, string>((name, value) => { this.settings.Add(name, value); }); mockBuilder.Setup(y => y.ConfigureServices(It.IsAny <Action <IServiceCollection> >())).Callback(() => this.servicesConfigured = true); this.builder = mockBuilder.Object; this.servicesConfigured = false; var context = TestMocksRepository.GetMockStatelessServiceContext(); this.listener = new KestrelCommunicationListener(context, (uri, listen) => BuildFunc(uri, listen)); }
public IWebHost BuildFunc(string url, AspNetCoreCommunicationListener listener) { this.UrlFromListenerToBuildFunc = url; // Create mock IServerAddressesFeature and set required things used by tests. var mockServerAddressFeature = new Mock <IServerAddressesFeature>(); mockServerAddressFeature.Setup(y => y.Addresses).Returns(new string[] { url }); var featureColelction = new FeatureCollection(); featureColelction.Set(mockServerAddressFeature.Object); // Create mock IWebHost and set required things used by tests. var mockWebHost = new Mock <IWebHost>(); mockWebHost.Setup(y => y.ServerFeatures).Returns(featureColelction); // setup call backs for Start , Dispose. mockWebHost.Setup(y => y.Start()).Callback(() => this.IsStarted = true); mockWebHost.Setup(y => y.Dispose()).Callback(() => this.IsStarted = false); // tell listener whether to generate UniqueServiceUrls if (this.IntegrationOptions.Equals(ServiceFabricIntegrationOptions.UseUniqueServiceUrl)) { listener.ConfigureToUseUniqueServiceUrl(); } return(mockWebHost.Object); }
private IWebHost BuildFunc(string url, AspNetCoreCommunicationListener listener) { var mockServerAddressFeature = new Mock <IServerAddressesFeature>(); mockServerAddressFeature.Setup(y => y.Addresses).Returns(new string[] { url }); var featureColelction = new FeatureCollection(); featureColelction.Set(mockServerAddressFeature.Object); // Create mock IWebHost and set required things used by this test. var mockWebHost = new Mock <IWebHost>(); return(mockWebHost.Object); }
// Method made internal instead of private to check test registration in service collection from unit tests internal IWebHost BuildWebHost(string url, AspNetCoreCommunicationListener listener) { IWebHostBuilder hostBuilder = new WebHostBuilder() .UseKestrel(m_kestrelOptions) .ConfigureServices(collection => ConfigureServices(collection)) .UseContentRoot(Directory.GetCurrentDirectory()) .UseStartup <TStartup>() .UseServiceFabricIntegration(listener, m_options) .UseUrls(url) .UseDefaultServiceProvider(config => { config.ValidateOnBuild = true; config.ValidateScopes = true; }); m_builderExtension(hostBuilder); return(hostBuilder.Build()); }
internal IWebHost Create(string url, AspNetCoreCommunicationListener listener) { TraceWriter.WriteInfoWithId( TraceType, this.serviceContext.TraceId, $"Starting Kestrel on {url}"); return(new WebHostBuilder() .UseKestrel() .ConfigureServices( services => { services.AddSingleton <StatelessServiceContext>(this.serviceContext); services.AddSingleton <VolumeMap>(new VolumeMap(this.serviceContext)); }) .UseContentRoot(Directory.GetCurrentDirectory()) .UseStartup <Startup>() .UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None) .UseUrls(url) .Build()); }
public ServiceFabricMiddlewareTests() { // setup HttpRequest mock var mockHttpRequest = new Mock <HttpRequest>(); mockHttpRequest.SetupAllProperties(); // setup HttpResponse mock var mockHttpResponse = new Mock <HttpResponse>(); mockHttpResponse.SetupAllProperties(); // setup HttpContext mock var mockHttpContext = new Mock <HttpContext>(); mockHttpContext.Setup(y => y.Response).Returns(mockHttpResponse.Object); mockHttpContext.Setup(y => y.Request).Returns(mockHttpRequest.Object); this.httpContext = mockHttpContext.Object; var context = TestMocksRepository.GetMockStatelessServiceContext(); this.listener = new KestrelCommunicationListener(context, BuildFunc); }
private IWebHostBuilder GetWebHostBuilder(StatelessServiceContext serviceContext, string url, AspNetCoreCommunicationListener listener) { return(new WebHostBuilder() .UseHttpSys() .ConfigureServices ( services => { services.AddSingleton <StatelessServiceContext>(serviceContext); } ) .UseContentRoot(Directory.GetCurrentDirectory()) .UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None) .UseUrls(url) .ConfigureAppConfiguration((builderContext, config) => { IHostingEnvironment env = builderContext.HostingEnvironment; config.SetBasePath(env.ContentRootPath) .AddJsonFile("azureKeyVault.json", false, true) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddEnvironmentVariables(); var builtConfig = config.Build(); config.AddAzureKeyVault($"https://{builtConfig["azureKeyVault:vault"]}.vault.azure.net/", builtConfig["azureKeyVault:clientId"], builtConfig["azureKeyVault:clientSecret"]); config.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); })); }
public static IWebHost CreateWebHostBuilder(StatelessServiceContext serviceContext, string url, AspNetCoreCommunicationListener listener) { return(new WebHostBuilder() .UseKestrel() .ConfigureAppConfiguration((builderContext, config) => { config .SetBasePath(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName)) .AddJsonFile("sharedsettings.json", optional: true) .AddJsonFile("appsettings.json", optional: false) .AddJsonFile($"appsettings.{builderContext.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true); }) .ConfigureServices(services => services.AddSingleton <StatelessServiceContext>(serviceContext)) .UseContentRoot(Directory.GetCurrentDirectory()) .ConfigureLogging((hostingContext, logging) => { logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging")); logging.AddDebug(); }) .UseStartup <Startup>() .UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None) .UseUrls(url) .Build()); }
private static IWebHostBuilder CreateWebHostBuilder(StatelessServiceContext serviceContext, string url, AspNetCoreCommunicationListener listener) => new WebHostBuilder() .ConfigureServices( services => services .AddSingleton(serviceContext)) .UseContentRoot(Directory.GetCurrentDirectory()) .UseStartup <Startup>() .UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.UseReverseProxyIntegration) .UseUrls(url);