Exemplo n.º 1
0
 private void IncrementMatchCount(IMockedRequest handler)
 {
     lock (lockObject)
     {
         matchCounts.TryGetValue(handler, out int count);
         matchCounts[handler] = count + 1;
     }
 }
Exemplo n.º 2
0
 /// <summary>
 /// Returns the number of times the specified request specification has been met
 /// </summary>
 /// <param name="request">The mocked request</param>
 /// <returns>The number of times the request has matched</returns>
 public int GetMatchCount(IMockedRequest request)
 {
     lock (lockObject)
     {
         matchCounts.TryGetValue(request, out int count);
         return(count);
     }
 }
Exemplo n.º 3
0
        private Task <HttpResponseMessage> SendAsync(IMockedRequest handler, HttpRequestMessage request, CancellationToken cancellationToken)
        {
            Interlocked.Increment(ref outstandingRequests);

            IncrementMatchCount(handler);

            if (!AutoFlush)
            {
                flusher = new TaskCompletionSource <object>();
                pendingFlushers.Enqueue(flusher);
            }

            return(flusher.Task.ContinueWith(_ =>
            {
                Interlocked.Decrement(ref outstandingRequests);

                cancellationToken.ThrowIfCancellationRequested();

                var completionSource = new TaskCompletionSource <HttpResponseMessage>();

                cancellationToken.Register(() => completionSource.TrySetCanceled());

                handler.SendAsync(request, cancellationToken)
                .ContinueWith(resp =>
                {
                    resp.Result.RequestMessage = request;

                    if (resp.IsFaulted)
                    {
                        completionSource.TrySetException(resp.Exception);
                    }
                    else if (resp.IsCanceled)
                    {
                        completionSource.TrySetCanceled();
                    }
                    else
                    {
                        completionSource.TrySetResult(resp.Result);
                    }
                });

                return completionSource.Task;
            }).Unwrap());
        }
Exemplo n.º 4
0
        private Task <HttpResponseMessage> SendAsync(IMockedRequest handler, HttpRequestMessage request, CancellationToken cancellationToken)
        {
            Interlocked.Increment(ref outstandingRequests);

            if (!AutoFlush)
            {
                flusher = new TaskCompletionSource <object>();
                pendingFlushers.Enqueue(flusher);
            }

            return(flusher.Task.ContinueWith(_ =>
            {
                Interlocked.Decrement(ref outstandingRequests);

                return handler.SendAsync(request, cancellationToken)
                .ContinueWith(resp =>
                {
                    resp.Result.RequestMessage = request;
                    return resp.Result;
                });
            }).Unwrap());
        }
        private Task<HttpResponseMessage> SendAsync(IMockedRequest handler, HttpRequestMessage request, CancellationToken cancellationToken)
        {
            Interlocked.Increment(ref outstandingRequests);

            if (!AutoFlush)
            {
                flusher = new TaskCompletionSource<object>();
                pendingFlushers.Enqueue(flusher);
            }

            return flusher.Task.ContinueWith(_ =>
                {
                    Interlocked.Decrement(ref outstandingRequests);

                    cancellationToken.ThrowIfCancellationRequested();

                    var completionSource = new TaskCompletionSource<HttpResponseMessage>();

                    cancellationToken.Register(() => completionSource.TrySetCanceled());

                    handler.SendAsync(request, cancellationToken)
                        .ContinueWith(resp =>
                        {
                            resp.Result.RequestMessage = request;

                            if (resp.IsFaulted)
                            {
                                completionSource.TrySetException(resp.Exception);
                            }
                            else if (resp.IsCanceled)
                            {
                                completionSource.TrySetCanceled();
                            }
                            else
                            {
                                completionSource.TrySetResult(resp.Result);
                            }
                        });

                    return completionSource.Task;
                }).Unwrap();
        }
 /// <summary>
 /// Adds a request expectation
 /// </summary>
 /// <remarks>
 /// Request expectations:
 /// 
 /// <list>
 /// <item>Match once</item>
 /// <item>Match in order</item>
 /// <item>Match before any backend definitions</item>
 /// </list>
 /// </remarks>
 /// <param name="handler">The <see cref="T:IMockedRequest"/> that will handle the request</param>
 public void AddRequestExpectation(IMockedRequest handler)
 {
     requestExpectations.Enqueue(handler);
 }
 /// <summary>
 /// Adds a backend definition
 /// </summary>
 /// <remarks>
 /// Backend definitions:
 /// 
 /// <list>
 /// <item>Match multiple times</item>
 /// <item>Match in any order</item>
 /// <item>Match after all request expectations have been met</item>
 /// </list>
 /// </remarks>
 /// <param name="handler">The <see cref="T:IMockedRequest"/> that will handle the request</param>
 public void AddBackendDefinition(IMockedRequest handler)
 {
     backendDefinitions.Add(handler);
 }
Exemplo n.º 8
0
 /// <summary>
 /// Adds a backend definition
 /// </summary>
 /// <remarks>
 /// Backend definitions:
 ///
 /// <list>
 /// <item>Match multiple times</item>
 /// <item>Match in any order</item>
 /// <item>Match after all request expectations have been met</item>
 /// </list>
 /// </remarks>
 /// <param name="handler">The <see cref="T:IMockedRequest"/> that will handle the request</param>
 public void AddBackendDefinition(IMockedRequest handler)
 {
     backendDefinitions.Add(handler);
 }
Exemplo n.º 9
0
 /// <summary>
 /// Adds a request expectation
 /// </summary>
 /// <remarks>
 /// Request expectations:
 ///
 /// <list>
 /// <item>Match once</item>
 /// <item>Match in order</item>
 /// <item>Match before any backend definitions</item>
 /// </list>
 /// </remarks>
 /// <param name="handler">The <see cref="T:IMockedRequest"/> that will handle the request</param>
 public void AddRequestExpectation(IMockedRequest handler)
 {
     requestExpectations.Enqueue(handler);
 }
Exemplo n.º 10
0
        private Task<HttpResponseMessage> SendAsync(IMockedRequest handler, HttpRequestMessage request, CancellationToken cancellationToken)
        {
            Interlocked.Increment(ref outstandingRequests);

            if (!AutoFlush)
            {
                flusher = new TaskCompletionSource<object>();
                pendingFlushers.Enqueue(flusher);
            }

            return flusher.Task.ContinueWith(_ =>
                {
                    Interlocked.Decrement(ref outstandingRequests);

                    return handler.SendAsync(request, cancellationToken)
                        .ContinueWith(resp =>
                        {
                            resp.Result.RequestMessage = request;
                            return resp.Result;
                        });
                }).Unwrap();
        }