Beispiel #1
0
        /// <summary>
        /// intercepts the http request that was about to hit a downstream server and return a stub instead
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        protected async override Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var session = ContextRepo.GetSession();

            if (session == null)
            {
                throw new ApplicationException("No session found");
            }

            var requestWrapper = new HttpRequestMessageWrapper(request);

            ContextRepo.OutgoingRequests[session].Add(requestWrapper);

            // there could be more than one stub for the same endpoint, perhaps returning different responses, we grab the first
            var match = ContextRepo.StubbedEndpoints[session].FirstOrDefault(c => c.IsMatch(requestWrapper));

            if (match == null)
            {
                throw new ApplicationException($"No stubs found for [{requestWrapper.GetEndpoint()}], please make sure you called 'AppendHttpCallStub'");
            }

            ContextRepo.StubbedEndpoints[session].Remove(match);

            if (match.Exception != null)
            {
                throw match.Exception;
            }

            return(match.Response);
        }
        public void Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter)
        {
            var session = ContextRepo.GetSession();

            var log = new LoggedEvent(logLevel, formatter(state, exception), source);

            if (session == null)
            {
                ContextRepo.UnsessionedLogs.Add(log);
            }
            else
            {
                ContextRepo.SessionLogs[session].Add(log);
            }
        }