public FakeHttpService( string serviceId = null, bool throwOnUnusedHandlers = false) { _handlers = new List <Tuple <Expression <Func <HttpRequest, bool> >, Func <HttpResponse, Task> > >(); _unusedHandlers = new List <Expression <Func <HttpRequest, bool> > >(); _throwOnUnusedHandlers = throwOnUnusedHandlers; ServiceId = serviceId ?? Guid.NewGuid().ToString(); _serviceIdIsUserSpecified = serviceId != null; FakeHttpServiceRepository.Register(this); var config = new ConfigurationBuilder().Build(); var builder = new WebHostBuilder() .UseConfiguration(config) .UseKestrel() .UseStartup <Startup>() .UseSetting("applicationName", ServiceId) .UseUrls("http://127.0.0.1:0"); _host = builder.Build(); _host.Start(); BaseAddress = new Uri(_host .ServerFeatures.Get <IServerAddressesFeature>() .Addresses.First()); }
public void Dispose() { Task.Run(() => _host.Dispose()).Wait(); FakeHttpServiceRepository.Unregister(this); var shouldThrowForMissingRequests = _throwOnUnusedHandlers && _unusedHandlers.Any(); if (shouldThrowForMissingRequests) { var unusedHandlerSummary = _unusedHandlers .Select(h => new ConstantMemberEvaluationVisitor().Visit(h).ToString()) .Aggregate((c, n) => $"{c}{Environment.NewLine}{n}"); var exception = new InvalidOperationException( $@"{GetType().Name} {ToString()} expected requests {unusedHandlerSummary} but they were not made."); throw exception; } }
public Startup(IHostingEnvironment env) { _httpService = FakeHttpServiceRepository.GetServiceMockById(env.ApplicationName); }