Exemplo n.º 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)
     }));
 }
Exemplo n.º 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;
     }));
 }
Exemplo n.º 3
0
 /// <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 }
     }));
 }
Exemplo n.º 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
     }));
 }
Exemplo n.º 5
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 void Throw(this MockedRequest source, Exception exception)
 {
     source.Respond(req =>
     {
         throw exception;
     });
 }
Exemplo n.º 6
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 void Respond(this MockedRequest source, HttpStatusCode statusCode, HttpContent content)
        {
            HttpResponseMessage message = new HttpResponseMessage(statusCode);

            message.Content = content;

            source.Respond(message);
        }
Exemplo n.º 7
0
        /// <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));
        }
Exemplo n.º 8
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 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);
        }
Exemplo n.º 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;
            }));
        }
Exemplo n.º 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;
            }));
        }
Exemplo n.º 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;
     }));
 }
Exemplo n.º 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;
            }));
        }
Exemplo n.º 19
0
        /// <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);
        }
Exemplo n.º 20
0
 /// <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));
 }
Exemplo n.º 21
0
 /// <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));
 }
Exemplo n.º 22
0
 /// <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)));
 }
Exemplo n.º 23
0
 /// <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);
 }
Exemplo n.º 24
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 void Respond(this MockedRequest source, HttpStatusCode statusCode, string mediaType, string content)
 {
     source.Respond(statusCode, new StringContent(content, Encoding.UTF8, mediaType));
 }
Exemplo n.º 25
0
 /// <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);
 }
Exemplo n.º 26
0
 /// <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());
 }
Exemplo n.º 28
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>
 public static void Respond(this MockedRequest source, HttpStatusCode statusCode)
 {
     source.Respond(new HttpResponseMessage(statusCode));
 }
Exemplo n.º 29
0
 /// <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));
 }
Exemplo n.º 30
0
        /// <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);
        }
Exemplo n.º 31
0
        /// <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);
        }
Exemplo n.º 32
0
 /// <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)));
 }
Exemplo n.º 33
0
        /// <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);
        }
Exemplo n.º 34
0
 /// <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)));
 }