public OwinResponseMapperTests()
        {
            _stream = new Mock <Stream>();
            _stream.SetupAllProperties();
            _stream.Setup(s => s.WriteAsync(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <CancellationToken>())).Returns(CompletedTask);

            _fileSystemHandlerMock = new Mock <IFileSystemHandler>();
            _fileSystemHandlerMock.SetupAllProperties();

            _optionsMock = new Mock <IWireMockMiddlewareOptions>();
            _optionsMock.SetupAllProperties();
            _optionsMock.SetupGet(o => o.FileSystemHandler).Returns(_fileSystemHandlerMock.Object);

            _headers = new Mock <IHeaderDictionary>();
            _headers.SetupAllProperties();
#if NET452
            _headers.Setup(h => h.AppendValues(It.IsAny <string>(), It.IsAny <string[]>()));
#else
            _headers.Setup(h => h.Add(It.IsAny <string>(), It.IsAny <StringValues>()));
#endif

            _responseMock = new Mock <IResponse>();
            _responseMock.SetupAllProperties();
            _responseMock.SetupGet(r => r.Body).Returns(_stream.Object);
            _responseMock.SetupGet(r => r.Headers).Returns(_headers.Object);

            _sut = new OwinResponseMapper(_optionsMock.Object);
        }
示例#2
0
        private void StartServers()
        {
#if NET46
            _logger.Info("WireMock.Net server using .net 4.6.1 or higher");
#else
            _logger.Info("WireMock.Net server using .net 4.5.x");
#endif
            var servers = new List <IDisposable>();

            try
            {
                var requestMapper  = new OwinRequestMapper();
                var responseMapper = new OwinResponseMapper(_options);
                var matcher        = new MappingMatcher(_options);

                /*
                 * Action<IAppBuilder> startup = app =>
                 * {
                 * app.Use<GlobalExceptionMiddleware>(_options, responseMapper);
                 * _options.PreWireMockMiddlewareInit?.Invoke(app);
                 * app.Use<WireMockMiddleware>(_options, requestMapper, responseMapper, matcher);
                 * _options.PostWireMockMiddlewareInit?.Invoke(app);
                 * };*/
                m_appBuilder.Use <GlobalExceptionMiddleware>(_options, responseMapper);
                m_appBuilder.Use <IgnorePrefixesMiddleware>(_options);
                _options.PreWireMockMiddlewareInit?.Invoke(m_appBuilder);
                m_appBuilder.Use <WireMockMiddleware>(_options, requestMapper, responseMapper, matcher);
                _options.PostWireMockMiddlewareInit?.Invoke(m_appBuilder);

                /*foreach (var url in Urls)
                 * {
                 * servers.Add(WebApp.Start(url, startup));
                 * }*/

                IsStarted = true;

                // WaitHandle is signaled when the token is cancelled,
                // which will be more efficient than Thread.Sleep in while loop
                _cts.Token.WaitHandle.WaitOne();
            }
            catch (Exception e)
            {
                // Expose exception of starting host, otherwise it's hard to be troubleshooting if keeping quiet
                // For example, WebApp.Start will fail with System.MissingMemberException if Microsoft.Owin.Host.HttpListener.dll is being located
                // https://stackoverflow.com/questions/25090211/owin-httplistener-not-located/31369857
                _runningException = e;
                _logger.Error(e.ToString());
            }
            finally
            {
                IsStarted = false;
                // Dispose all servers in finally block to make sure clean up allocated resource on error happening
                servers.ForEach(s => s.Dispose());
            }
        }