/// <inheritdoc />
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            if (Mode == HttpRecorderMode.Passthrough)
            {
                var response = await base.SendAsync(request, cancellationToken);

                return(response);
            }

            await _interactionLock.WaitAsync();

            try
            {
                await ResolveExecutionMode(cancellationToken);

                if (_executionMode == HttpRecorderMode.Replay)
                {
                    if (_interaction == null)
                    {
                        _interaction = await _repository.LoadAsync(InteractionName, cancellationToken);
                    }

                    var interactionMessage = _matcher.Match(request, _interaction);
                    if (interactionMessage == null)
                    {
                        throw new HttpRecorderException($"Unable to find a matching interaction for request {request.Method} {request.RequestUri}.");
                    }

                    return(await PostProcessResponse(interactionMessage.Response));
                }

                var start         = DateTimeOffset.Now;
                var sw            = Stopwatch.StartNew();
                var innerResponse = await base.SendAsync(request, cancellationToken);

                sw.Stop();

                var newInteractionMessage = new InteractionMessage(
                    innerResponse,
                    new InteractionMessageTimings(start, sw.Elapsed));

                _interaction = new Interaction(
                    InteractionName,
                    _interaction == null ? new[] { newInteractionMessage } : _interaction.Messages.Append(newInteractionMessage));

                await _repository.StoreAsync(_interaction, cancellationToken);

                return(await PostProcessResponse(newInteractionMessage.Response));
            }
            finally
            {
                _interactionLock.Release();
            }
        }
예제 #2
0
        public HttpResponseModel FindRegisteredResponse(HttpRequestModel request)
        {
            _logger.WriteVerbose(request.ToString());
            var matchingRequests = _repository.FindByLocalPath(request.LocalPath);

            //no match found
            if (matchingRequests == null || matchingRequests.Length == 0)
            {
                return(null);
            }

            var matchingRequest = _matcher.Match(matchingRequests, request);

            if (matchingRequest == null)
            {
                return(null);
            }
            //we found a matching request , so return the mocked Response
            return(_repository.Get(matchingRequest));
        }