示例#1
0
 /// <summary>
 /// Sets the response of the current <see cref="T:MockedRequest"/>
 /// </summary>
 /// <param name="source">The source mocked request</param>
 /// <param name="statusCode">The <see cref="T:HttpStatusCode"/> of the response</param>
 /// <param name="handler">The delegate that will return a <see cref="T:HttpContent"/> determined at runtime</param>
 public static MockedRequest Respond(this MockedRequest source, HttpStatusCode statusCode, Func <HttpRequestMessage, HttpContent> handler)
 {
     return(source.Respond(req => new HttpResponseMessage(statusCode)
     {
         Content = handler(req)
     }));
 }
示例#2
0
 /// <summary>
 /// Sets the response of the current <see cref="T:MockedRequest"/> to a lambda which throws the specified exception.
 /// </summary>
 /// <param name="source">The source mocked request</param>
 /// <param name="exception">The exception to throw</param>
 public static MockedRequest Throw(this MockedRequest source, Exception exception)
 {
     return(source.Respond(req =>
     {
         throw exception;
     }));
 }
 /// <summary>
 /// Includes requests contain a particular form data value
 /// </summary>
 /// <param name="source">The source mocked request</param>
 /// <param name="name">The form data key to match</param>
 /// <param name="value">The form data value to match (including null)</param>
 /// <returns>The <see cref="T:MockedRequest"/> instance</returns>
 public static MockedRequest WithFormData(this MockedRequest source, string name, string value)
 {
     return(WithFormData(source, new Dictionary <string, string>
     {
         { name, value }
     }));
 }
示例#4
0
 /// <summary>
 /// Sets the response of the current <see cref="T:MockedRequest"/>
 /// </summary>
 /// <param name="source">The source mocked request</param>
 /// <param name="statusCode">The <see cref="T:HttpStatusCode"/> of the response</param>
 /// <param name="content">The content of the response</param>
 public static MockedRequest Respond(this MockedRequest source, HttpStatusCode statusCode, HttpContent content)
 {
     return(source.Respond(req => new HttpResponseMessage(statusCode)
     {
         Content = content
     }));
 }
 /// <summary>
 /// Sets the response of the current <see cref="T:MockedRequest"/> to a lambda which throws the specified exception.
 /// </summary>
 /// <param name="source">The source mocked request</param>
 /// <param name="exception">The exception to throw</param>
 public static void Throw(this MockedRequest source, Exception exception)
 {
     source.Respond(req =>
     {
         throw exception;
     });
 }
        /// <summary>
        /// Sets the response of the current <see cref="T:MockedRequest"/>
        /// </summary>
        /// <param name="source">The source mocked request</param>
        /// <param name="statusCode">The <see cref="T:HttpStatusCode"/> of the response</param>
        /// <param name="content">The content of the response</param>
        public static void Respond(this MockedRequest source, HttpStatusCode statusCode, HttpContent content)
        {
            HttpResponseMessage message = new HttpResponseMessage(statusCode);

            message.Content = content;

            source.Respond(message);
        }
        /// <summary>
        /// Creates a new instance of MockHttpMessageHandler
        /// </summary>
        public MockHttpMessageHandler(BackendDefinitionBehavior backendDefinitionBehavior = BackendDefinitionBehavior.NoExpectations)
        {
            this.backendDefinitionBehavior = backendDefinitionBehavior;

            AutoFlush = true;
            fallback  = new MockedRequest();
            fallback.Respond(req => CreateDefaultFallbackMessage(req));
        }
        /// <summary>
        /// Sets the response of the current <see cref="T:MockedRequest"/>
        /// </summary>
        /// <param name="source">The source mocked request</param>
        /// <param name="statusCode">The <see cref="T:HttpStatusCode"/> of the response</param>
        /// <param name="content">The content of the response</param>
        /// <param name="mediaType">The media type of the response</param>
        public static void Respond(this MockedRequest source, HttpStatusCode statusCode, string mediaType, Stream content)
        {
            var streamContent = new StreamContent(content);

            streamContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(mediaType);

            source.Respond(statusCode, streamContent);
        }
        /// <summary>
        /// Adds a backend definition 
        /// </summary>
        /// <param name="handler">The source handler</param>
        /// <param name="url">The URL (absolute or relative, may contain * wildcards) to match</param>
        /// <returns>The <see cref="T:MockedRequest"/> instance</returns>
        public static MockedRequest When(this MockHttpMessageHandler handler, string url)
        {
            var message = new MockedRequest(url);

            handler.AddBackendDefinition(message);

            return message;
        }
        /// <summary>
        /// Adds a request expectation
        /// </summary>
        /// <param name="handler">The source handler</param>
        /// <param name="url">The URL (absolute or relative, may contain * wildcards) to match</param>
        /// <returns>The <see cref="T:MockedRequest"/> instance</returns>
        public static MockedRequest Expect(this MockHttpMessageHandler handler, string url)
        {
            var message = new MockedRequest(url);

            handler.AddRequestExpectation(message);

            return message;
        }
        /// <summary>
        /// Adds a request expectation
        /// </summary>
        /// <param name="handler">The source handler</param>
        /// <param name="url">The URL (absolute or relative, may contain * wildcards) to match</param>
        /// <returns>The <see cref="T:MockedRequest"/> instance</returns>
        public static MockedRequest Expect(this MockHttpMessageHandler handler, string url)
        {
            var message = new MockedRequest(url);

            handler.AddRequestExpectation(message);

            return(message);
        }
        /// <summary>
        /// Adds a backend definition
        /// </summary>
        /// <param name="handler">The source handler</param>
        /// <param name="url">The URL (absolute or relative, may contain * wildcards) to match</param>
        /// <returns>The <see cref="T:MockedRequest"/> instance</returns>
        public static MockedRequest When(this MockHttpMessageHandler handler, string url)
        {
            var message = new MockedRequest(url);

            handler.AddBackendDefinition(message);

            return(message);
        }
        /// <summary>
        /// Adds a backend definition 
        /// </summary>
        /// <param name="handler">The source handler</param>
        /// <param name="method">The HTTP method to match</param>
        /// <param name="url">The URL (absolute or relative, may contain * wildcards) to match</param>
        /// <returns>The <see cref="T:MockedRequest"/> instance</returns>
        public static MockedRequest When(this MockHttpMessageHandler handler, HttpMethod method, string url)
        {
            var message = new MockedRequest(url);
            message.With(new MethodMatcher(method));

            handler.AddBackendDefinition(message);

            return message;
        }
        /// <summary>
        /// Adds a request expectation
        /// </summary>
        /// <param name="handler">The source handler</param>
        /// <param name="method">The HTTP method to match</param>
        /// <param name="url">The URL (absolute or relative, may contain * wildcards) to match</param>
        /// <returns>The <see cref="T:MockedRequest"/> instance</returns>
        public static MockedRequest Expect(this MockHttpMessageHandler handler, HttpMethod method, string url)
        {
            var message = new MockedRequest(url);

            message.With(new MethodMatcher(method));

            handler.AddRequestExpectation(message);

            return(message);
        }
示例#15
0
        /// <summary>
        /// Sets the response of the current <see cref="T:MockedRequest"/>
        /// </summary>
        /// <param name="source">The source mocked request</param>
        /// <param name="statusCode">The <see cref="T:HttpStatusCode"/> of the response</param>
        /// <param name="mediaType">The media type of the response</param>
        /// <param name="handler">A delegate that will return a content stream at runtime</param>
        public static MockedRequest Respond(this MockedRequest source, HttpStatusCode statusCode, string mediaType, Func <HttpRequestMessage, Stream> handler)
        {
            return(source.Respond(statusCode, request =>
            {
                var content = handler(request);

                var streamContent = new StreamContent(content);
                streamContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(mediaType);

                return streamContent;
            }));
        }
示例#16
0
        /// <summary>
        /// Sets the response of the current <see cref="T:MockedRequest"/>
        /// </summary>
        /// <param name="source">The source mocked request</param>
        /// <param name="statusCode">The <see cref="T:HttpStatusCode"/> of the response</param>
        /// <param name="headers">A list of HTTP header name/value pairs to add to the response.</param>
        /// <param name="mediaType">The media type of the response</param>
        /// <param name="handler">A delegate that will return a content stream at runtime</param>
        public static MockedRequest Respond(this MockedRequest source, HttpStatusCode statusCode, IEnumerable <KeyValuePair <string, string> > headers, string mediaType, Func <HttpRequestMessage, Stream> handler)
        {
            return(source.Respond(statusCode, headers, request =>
            {
                var content = handler(request);

                var streamContent = new StreamContent(content);
                streamContent.Headers.ContentType = new MediaTypeHeaderValue(mediaType);

                return streamContent;
            }));
        }
示例#17
0
 /// <summary>
 /// Sets the response of the current <see cref="T:MockedRequest"/>
 /// </summary>
 /// <param name="source">The source mocked request</param>
 /// <param name="statusCode">The <see cref="T:HttpStatusCode"/> of the response</param>
 /// <param name="headers">A list of HTTP header name/value pairs to add to the response.</param>
 /// <param name="handler">The delegate that will return a <see cref="T:HttpContent"/> determined at runtime</param>
 public static MockedRequest Respond(this MockedRequest source, HttpStatusCode statusCode, IEnumerable <KeyValuePair <string, string> > headers, Func <HttpRequestMessage, HttpContent> handler)
 {
     return(source.Respond(req =>
     {
         var res = new HttpResponseMessage(statusCode)
         {
             Content = handler(req),
         };
         foreach (var header in headers)
         {
             res.Headers.TryAddWithoutValidation(header.Key, header.Value);
         }
         return res;
     }));
 }
示例#18
0
        /// <summary>
        /// Sets the response of the current <see cref="T:MockedRequest"/>
        /// </summary>
        /// <param name="source">The source mocked request</param>
        /// <param name="statusCode">The <see cref="T:HttpStatusCode"/> of the response</param>
        /// <param name="content">The content of the response</param>
        /// <param name="mediaType">The media type of the response</param>
        public static MockedRequest Respond(this MockedRequest source, HttpStatusCode statusCode, string mediaType, Stream content)
        {
            return(source.Respond(statusCode, _ =>
            {
                if (content.CanSeek)
                {
                    content.Seek(0L, SeekOrigin.Begin);
                }

                var ms = new MemoryStream();
                content.CopyTo(ms);
                ms.Seek(0L, SeekOrigin.Begin);

                var streamContent = new StreamContent(ms);
                streamContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(mediaType);

                return streamContent;
            }));
        }
        /// <summary>
        /// Includes requests contain a set of headers
        /// </summary>
        /// <param name="source">The source mocked request</param>
        /// <param name="headers">A string containing headers as they would appear in the HTTP request</param>
        /// <returns>The <see cref="T:MockedRequest"/> instance</returns>
        public static MockedRequest WithHeaders(this MockedRequest source, string headers)
        {
            source.With(new HeadersMatcher(headers));

            return(source);
        }
 /// <summary>
 /// Sets the response of the current <see cref="T:MockedRequest"/> to defer to another <see cref="T:HttpMessageListener"/>
 /// </summary>
 /// <param name="source">The source mocked request</param>
 /// <param name="handler">The <see cref="T:HttpMessageHandlert"/> that will handle requests</param>
 public static void Respond(this MockedRequest source, HttpMessageHandler handler)
 {
     source.Respond(new HttpClient(handler));
 }
 /// <summary>
 /// Sets the response of the current <see cref="T:MockedRequest"/> to defer to another <see cref="T:HttpClient"/>
 /// </summary>
 /// <param name="source">The source mocked request</param>
 /// <param name="httpClient">The <see cref="T:HttpClient"/> that will handle requests</param>
 public static void Respond(this MockedRequest source, HttpClient httpClient)
 {
     source.Respond(req => httpClient.SendAsync(req));
 }
 /// <summary>
 /// Sets the response of the current <see cref="T:MockedRequest"/>
 /// </summary>
 /// <param name="source">The source mocked request</param>
 /// <param name="handler">The delegate that will return a <see cref="T:HttpResponseMessage"/> determined at runtime</param>
 public static void Respond(this MockedRequest source, Func <HttpRequestMessage, HttpResponseMessage> handler)
 {
     source.Respond(req => TaskEx.FromResult(handler(req)));
 }
 /// <summary>
 /// Sets the response of the current <see cref="T:MockedRequest"/>, with an OK (200) status code
 /// </summary>
 /// <param name="source">The source mocked request</param>
 /// <param name="content">The content of the response</param>
 /// <param name="mediaType">The media type of the response</param>
 public static void Respond(this MockedRequest source, string mediaType, Stream content)
 {
     source.Respond(HttpStatusCode.OK, mediaType, content);
 }
 /// <summary>
 /// Sets the response of the current <see cref="T:MockedRequest"/>
 /// </summary>
 /// <param name="source">The source mocked request</param>
 /// <param name="statusCode">The <see cref="T:HttpStatusCode"/> of the response</param>
 /// <param name="content">The content of the response</param>
 /// <param name="mediaType">The media type of the response</param>
 public static void Respond(this MockedRequest source, HttpStatusCode statusCode, string mediaType, string content)
 {
     source.Respond(statusCode, new StringContent(content, Encoding.UTF8, mediaType));
 }
 /// <summary>
 /// Sets the response of the current <see cref="T:MockedRequest"/>, with an OK (200) status code
 /// </summary>
 /// <param name="source">The source mocked request</param>
 /// <param name="content">The content of the response</param>
 public static void Respond(this MockedRequest source, HttpContent content)
 {
     source.Respond(HttpStatusCode.OK, content);
 }
 /// <summary>
 /// Constraints the request using custom logic
 /// </summary>
 /// <param name="source">The source mocked request</param>
 /// <param name="matcher">The delegate that will be used to constrain the request</param>
 /// <returns>The <see cref="T:MockedRequest"/> instance</returns>
 public static MockedRequest With(this MockedRequest source, Func <HttpRequestMessage, bool> matcher)
 {
     return(source.With(new CustomMatcher(matcher)));
 }
 /// <summary>
 /// Creates a new instance of MockHttpMessageHandler
 /// </summary>
 public MockHttpMessageHandler()
 {
     AutoFlush = true;
     fallback = new MockedRequest();
     fallback.Respond(fallbackResponse = CreateDefaultFallbackMessage());
 }
 /// <summary>
 /// Sets the response of the current <see cref="T:MockedRequest"/>
 /// </summary>
 /// <param name="source">The source mocked request</param>
 /// <param name="statusCode">The <see cref="T:HttpStatusCode"/> of the response</param>
 public static void Respond(this MockedRequest source, HttpStatusCode statusCode)
 {
     source.Respond(new HttpResponseMessage(statusCode));
 }
 /// <summary>
 /// Sets the response of the current <see cref="T:MockedRequest"/>
 /// </summary>
 /// <param name="source">The source mocked request</param>
 /// <param name="message">The complete <see cref="T:HttpResponseMessage"/> to return</param>
 public static void Respond(this MockedRequest source, HttpResponseMessage message)
 {
     source.Respond(_ => TaskEx.FromResult(message));
 }
        /// <summary>
        /// Includes requests contain a set of query string values
        /// </summary>
        /// <param name="source">The source mocked request</param>
        /// <param name="values">The query string key/value pairs to match</param>
        /// <returns>The <see cref="T:MockedRequest"/> instance</returns>
        public static MockedRequest WithQueryString(this MockedRequest source, IEnumerable <KeyValuePair <string, string> > values)
        {
            source.With(new QueryStringMatcher(values));

            return(source);
        }
        /// <summary>
        /// Includes requests contain a set of query string values
        /// </summary>
        /// <param name="source">The source mocked request</param>
        /// <param name="values">A formatted query string containing key/value pairs to match</param>
        /// <returns>The <see cref="T:MockedRequest"/> instance</returns>
        public static MockedRequest WithQueryString(this MockedRequest source, string values)
        {
            source.With(new QueryStringMatcher(values));

            return(source);
        }
 /// <summary>
 /// Requires that the request match any of the specified set of matchers
 /// </summary>
 /// <param name="source">The source mocked request</param>
 /// <param name="matchers">A list of matchers to evaluate</param>
 /// <returns>The <see cref="T:MockedRequest"/> instance</returns>
 public static MockedRequest WithAny(this MockedRequest source, IEnumerable <IMockedRequestMatcher> matchers)
 {
     return(source.With(new AnyMatcher(matchers)));
 }
        /// <summary>
        /// Includes requests contain a set of query string values
        /// </summary>
        /// <param name="source">The source mocked request</param>
        /// <param name="values">The query string key/value pairs to match</param>
        /// <returns>The <see cref="T:MockedRequest"/> instance</returns>
        public static MockedRequest WithFormData(this MockedRequest source, IEnumerable <KeyValuePair <string, string> > values)
        {
            source.With(new FormDataMatcher(values));

            return(source);
        }
 /// <summary>
 /// Requires that the request match any of the specified set of matchers
 /// </summary>
 /// <param name="source">The source mocked request</param>
 /// <param name="matchers">A list of matchers to evaluate</param>
 /// <returns>The <see cref="T:MockedRequest"/> instance</returns>
 public static MockedRequest WithAny(this MockedRequest source, params IMockedRequestMatcher[] matchers)
 {
     return(source.With(new AnyMatcher(matchers)));
 }