Exemple #1
0
            public new static MapperServer Start()
            {
                int port = PortUtil.FindFreeTcpPort();

                UrlPrefix = "http://localhost:" + port + "/";
                var server = new MapperServer(
                    context =>
                {
                    LastRequestMessage = new HttpListenerRequestMapper().Map(context.Request);
                    context.Response.Close();
                }, UrlPrefix);

                ((TinyHttpServer)server).Start();

                return(server);
            }
Exemple #2
0
        public void Should_call_handler_on_request()
        {
            // given
            var  port      = PortUtil.FindFreeTcpPort();
            bool called    = false;
            var  urlPrefix = "http://localhost:" + port + "/";
            var  server    = new TinyHttpServer(ctx => called = true, urlPrefix);

            server.Start();

            // when
            var httpClient = new HttpClient();

            httpClient.GetAsync(urlPrefix).Wait(3000);

            // then
            Check.That(called).IsTrue();
        }
        /// <summary>
        /// Dirty HACK to get HttpListenerResponse instances
        /// </summary>
        /// <returns>
        /// The <see cref="HttpListenerResponse"/>.
        /// </returns>
        public HttpListenerResponse CreateHttpListenerResponse()
        {
            var port                      = PortUtil.FindFreeTcpPort();
            var urlPrefix                 = "http://localhost:" + port + "/";
            var responseReady             = new AutoResetEvent(false);
            HttpListenerResponse response = null;

            _server = new TinyHttpServer(
                context =>
            {
                response = context.Response;
                responseReady.Set();
            }, urlPrefix);
            _server.Start();
            _responseMsgTask = new HttpClient().GetAsync(urlPrefix);
            responseReady.WaitOne();
            return(response);
        }
        private FluentMockServer(FluentMockServerSettings settings)
        {
            if (settings.Urls != null)
            {
                Urls = settings.Urls;
            }
            else
            {
                int port = settings.Port > 0 ? settings.Port.Value : PortUtil.FindFreeTcpPort();
                Urls = new[] { (settings.UseSSL == true ? "https" : "http") + "://localhost:" + port + "/" };
            }

#if NET45
            _httpServer = new OwinSelfHost(_options, Urls);
#else
            _httpServer = new AspNetCoreSelfHost(_options, Urls);
#endif
            Ports = _httpServer.Ports;

            _httpServer.StartAsync();

            if (settings.StartAdminInterface == true)
            {
                InitAdmin();
            }

            if (settings.ReadStaticMappings == true)
            {
                ReadStaticMappings();
            }

            if (settings.ProxyAndRecordSettings != null)
            {
                InitProxyAndRecord(settings.ProxyAndRecordSettings);
            }
        }
        private FluentMockServer(IFluentMockServerSettings settings)
        {
            settings.Logger = settings.Logger ?? new WireMockConsoleLogger();
            _logger         = settings.Logger;

            _logger.Info("WireMock.Net by Stef Heyenrath (https://github.com/WireMock-Net/WireMock.Net)");
            _logger.Debug("WireMock.Net server settings {0}", JsonConvert.SerializeObject(settings, Formatting.Indented));

            if (settings.Urls != null)
            {
                Urls = settings.Urls.Select(u => u.EndsWith("/") ? u : $"{u}/").ToArray();
            }
            else
            {
                int port = settings.Port > 0 ? settings.Port.Value : PortUtil.FindFreeTcpPort();
                Urls = new[] { (settings.UseSSL == true ? "https" : "http") + "://localhost:" + port + "/" };
            }

            _options.PreWireMockMiddlewareInit  = settings.PreWireMockMiddlewareInit;
            _options.PostWireMockMiddlewareInit = settings.PostWireMockMiddlewareInit;
            _options.Logger = _logger;

#if NETSTANDARD
            _httpServer = new AspNetCoreSelfHost(_options, Urls);
#else
            _httpServer = new OwinSelfHost(_options, Urls);
#endif
            Ports = _httpServer.Ports;

            _httpServer.StartAsync();

            using (var ctsStartTimeout = new CancellationTokenSource(settings.StartTimeout))
            {
                while (!_httpServer.IsStarted)
                {
                    // Throw out exception if service start fails
                    if (_httpServer.RunningException != null)
                    {
                        throw new Exception($"Service start failed with error: {_httpServer.RunningException.Message}", _httpServer.RunningException);
                    }

                    // Respect start timeout setting by throwing TimeoutException
                    if (ctsStartTimeout.IsCancellationRequested)
                    {
                        throw new TimeoutException($"Service start timed out after {TimeSpan.FromMilliseconds(settings.StartTimeout)}");
                    }

                    ctsStartTimeout.Token.WaitHandle.WaitOne(ServerStartDelay);
                }
            }

            if (settings.AllowPartialMapping == true)
            {
                AllowPartialMapping();
            }

            if (settings.StartAdminInterface == true)
            {
                if (!string.IsNullOrEmpty(settings.AdminUsername) && !string.IsNullOrEmpty(settings.AdminPassword))
                {
                    SetBasicAuthentication(settings.AdminUsername, settings.AdminPassword);
                }

                InitAdmin();
            }

            if (settings.ReadStaticMappings == true)
            {
                ReadStaticMappings();
            }

            if (settings.WatchStaticMappings == true)
            {
                WatchStaticMappings();
            }

            if (settings.ProxyAndRecordSettings != null)
            {
                InitProxyAndRecord(settings.ProxyAndRecordSettings);
            }

            if (settings.MaxRequestLogCount != null)
            {
                SetMaxRequestLogCount(settings.MaxRequestLogCount);
            }
        }
        private FluentMockServer(IFluentMockServerSettings settings)
        {
            Log.DebugFormat("WireMock.Net server settings {0}", JsonConvert.SerializeObject(settings, Formatting.Indented));

            if (settings.Urls != null)
            {
                Urls = settings.Urls.Select(u => u.EndsWith("/") ? u : $"{u}/").ToArray();
            }
            else
            {
                int port = settings.Port > 0 ? settings.Port.Value : PortUtil.FindFreeTcpPort();
                Urls = new[] { (settings.UseSSL == true ? "https" : "http") + "://localhost:" + port + "/" };
            }

            _options.PreWireMockMiddlewareInit  = settings.PreWireMockMiddlewareInit;
            _options.PostWireMockMiddlewareInit = settings.PostWireMockMiddlewareInit;

#if NETSTANDARD
            _httpServer = new AspNetCoreSelfHost(_options, Urls);
#else
            _httpServer = new OwinSelfHost(_options, Urls);
#endif
            IsStarted = _httpServer.IsStarted;

            Ports = _httpServer.Ports;

            _httpServer.StartAsync();

            // Fix for 'Bug: Server not listening after Start() returns (on macOS)'
            Task.Delay(ServerStartDelay).Wait();

            if (settings.AllowPartialMapping == true)
            {
                AllowPartialMapping();
            }

            if (settings.StartAdminInterface == true)
            {
                if (!string.IsNullOrEmpty(settings.AdminUsername) && !string.IsNullOrEmpty(settings.AdminPassword))
                {
                    SetBasicAuthentication(settings.AdminUsername, settings.AdminPassword);
                }

                InitAdmin();
            }

            if (settings.ReadStaticMappings == true)
            {
                ReadStaticMappings();
            }

            if (settings.WatchStaticMappings == true)
            {
                WatchStaticMappings();
            }

            if (settings.ProxyAndRecordSettings != null)
            {
                InitProxyAndRecord(settings.ProxyAndRecordSettings);
            }

            if (settings.MaxRequestLogCount != null)
            {
                SetMaxRequestLogCount(settings.MaxRequestLogCount);
            }
        }