Exemple #1
0
        protected override Task <HttpResponseMessage> SendAsync(HttpRequestMessage request,
                                                                CancellationToken cancellationToken)
        {
            if (_exception != null)
            {
                throw _exception;
            }

            var tcs = new TaskCompletionSource <HttpResponseMessage>();

            var requestUri = new Uri(request.RequestUri.AbsoluteUri);
            var option     = new HttpMessageOptions
            {
                RequestUri  = requestUri,
                HttpMethod  = request.Method,
                HttpContent = request.Content,
                Headers     = request.Headers.ToDictionary(kv => kv.Key, kv => kv.Value)
            };

            var expectedOption = GetExpectedOption(option);

            if (expectedOption == null)
            {
                var setupOptionsText = new StringBuilder();

                var setupOptions = _lotsOfOptions.Values.ToList();
                for (var i = 0; i < setupOptions.Count; i++)
                {
                    if (i > 0)
                    {
                        setupOptionsText.Append(" ");
                    }
                    setupOptionsText.Append($"{i + 1}) {setupOptions[i]}.");
                }

                var errorMessage = $"No HttpResponseMessage found for the Request => What was called: [{option}]. At least one of these option(s) should have been matched: [{setupOptionsText}]";
                throw new InvalidOperationException(errorMessage);
            }

            // Increment the number of times this option had been 'called'.
            expectedOption.IncrementNumberOfTimesCalled();

            // Pass the request along.
            expectedOption.HttpResponseMessage.RequestMessage = request;

            tcs.SetResult(expectedOption.HttpResponseMessage);
            return(tcs.Task);
        }
Exemple #2
0
        private HttpMessageOptions GetExpectedOption(HttpMessageOptions option)
        {
            if (option == null)
            {
                throw new ArgumentNullException(nameof(option));
            }

            return(_lotsOfOptions.Values.SingleOrDefault(x => (x.RequestUri == option.RequestUri ||
                                                               x.RequestUri == HttpMessageOptions.NoValue) &&
                                                         (x.HttpMethod == option.HttpMethod ||
                                                          x.HttpMethod == null) &&
                                                         (x.HttpContent == option.HttpContent ||
                                                          x.HttpContent == null) &&
                                                         (x.Headers == null ||
                                                          x.Headers.Count == 0) ||
                                                         (x.Headers != null &&
                                                          HeaderExists(x.Headers, option.Headers))));
        }
Exemple #3
0
        private static void IncrementCalls(HttpMessageOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            var type         = typeof(HttpMessageOptions);
            var propertyInfo = type.GetProperty("NumberOfTimesCalled");

            if (propertyInfo == null)
            {
                return;
            }

            var existingValue = (int)propertyInfo.GetValue(options);

            propertyInfo.SetValue(options, ++existingValue);
        }
Exemple #4
0
        protected override Task <HttpResponseMessage> SendAsync(HttpRequestMessage request,
                                                                CancellationToken cancellationToken)
        {
            if (_exception != null)
            {
                throw _exception;
            }

            var tcs = new TaskCompletionSource <HttpResponseMessage>();

            var requestUri = request.RequestUri.AbsoluteUri;
            var option     = new HttpMessageOptions
            {
                RequestUri  = requestUri,
                HttpMethod  = request.Method,
                HttpContent = request.Content,
                Headers     = request.Headers.ToDictionary(kv => kv.Key, kv => kv.Value)
            };

            var expectedOption = GetExpectedOption(option);

            if (expectedOption == null)
            {
                // Nope - no keys found exactly OR starting-with...

                var responsesText = string.Join(";", _lotsOfOptions.Values);
                var errorMessage  =
                    $"No HttpResponseMessage found for the Request Uri: {request.RequestUri}. Please provide one in the FakeHttpMessageHandler constructor Or use a '*' for any request uri. Search-Key: '{requestUri}. Setup: {(!_lotsOfOptions.Any() ? "- no responses -" : _lotsOfOptions.Count.ToString())} responses: {responsesText}";
                throw new InvalidOperationException(errorMessage);
            }

            // Increment the number of times this option had been 'called'.
            IncrementCalls(expectedOption);

            var result = CreateResponseMessageForRequest(request, expectedOption.HttpResponseMessage);

            tcs.SetResult(result);
            return(tcs.Task);
        }
Exemple #5
0
        private HttpMessageOptions GetExpectedOption(HttpMessageOptions option)
        {
            if (option == null)
            {
                throw new ArgumentNullException(nameof(option));
            }

            // NOTE: We only compare the *setup* HttpMessageOptions properties if they were provided.
            //       So if there was no HEADERS provided ... but the real 'option' has some, we still ignore
            //       and don't compare.
            return(_lotsOfOptions.Values.SingleOrDefault(x => (x.RequestUri == null || // Don't care about the Request Uri.
                                                               x.RequestUri.AbsoluteUri.Equals(option.RequestUri.AbsoluteUri, StringComparison.OrdinalIgnoreCase)) &&

                                                         (x.HttpMethod == null ||      // Don't care about the HttpMethod.
                                                          x.HttpMethod == option.HttpMethod) &&

                                                         (x.HttpContent == null ||      // Don't care about the Content.
                                                          ContentAreEqual(x.HttpContent, option.HttpContent)) &&

                                                         (x.Headers == null ||      // Don't care about the Header.
                                                          x.Headers.Count == 0 ||   // No header's were supplied, so again don't care/
                                                          HeadersAreEqual(x.Headers, option.Headers))));
        }
Exemple #6
0
 /// <summary>
 /// A fake message handler.
 /// </summary>
 /// <remarks>TIP: If you have a requestUri = "*", this is a catch-all ... so if none of the other requestUri's match, then it will fall back to this dictionary item.</remarks>
 public FakeHttpMessageHandler(HttpMessageOptions options) : this(new List <HttpMessageOptions> {
     options
 })
 {
 }