Exemplo n.º 1
0
        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());
        }
Exemplo n.º 2
0
        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;
            }
        }
Exemplo n.º 3
0
 public Startup(IHostingEnvironment env)
 {
     _httpService = FakeHttpServiceRepository.GetServiceMockById(env.ApplicationName);
 }