Example #1
0
        /// <summary>
        /// Set a predefined response to a request. Calls made with an existing
        /// endpoint will replace the previous response.
        /// </summary>
        /// <param name="endpointRegex">Pattern for the requested endpoint</param>
        /// <param name="givenResponse">A predefined response</param>
        public void SetResponsePattern(string endpointRegex, MockHttpResponse givenResponse)
        {
            if (!Regex.IsMatch(endpointRegex, @"https?:\/\/"))
            {
                endpointRegex = Regex.Escape(mBaseUrl) + endpointRegex;
            }
            var absoluteUrlPattern = new Regex(endpointRegex);

            var httpResponse = GetOrMakeValue(absoluteUrlPattern, mMockResponses, () => new HttpResponseMessage());

            httpResponse.StatusCode = givenResponse.StatusCode;
            httpResponse.Content    = new ByteArrayContent(Encoding.UTF8.GetBytes(givenResponse.Content));
            httpResponse.Content.Headers.ContentType = givenResponse.ContentType;

            httpResponse.Headers.Clear();
            foreach (var header in givenResponse.Headers)
            {
                httpResponse.Headers.Add(header.Item1, header.Item2);
            }
        }
Example #2
0
 /// <summary>
 /// Set a predefined response to a request. Calls made with an existing
 /// endpoint will replace the previous response.
 /// </summary>
 /// <param name="endpoint">The endpoint being requested</param>
 /// <param name="response">A predefined response</param>
 public void SetResponse(string endpoint, MockHttpResponse response)
 {
     SetResponsePattern($"{Regex.Escape(endpoint)}$", response);
 }
Example #3
0
 /// <summary>
 /// Set a predefined response to a request. Calls made with an existing
 /// endpoint will replace the previous response.
 /// </summary>
 /// <param name="endpointRegex">Pattern for the requested endpoint</param>
 /// <param name="response">A predefined response</param>
 public MockHttpClient SetResponsePattern(string urlRegex, MockHttpResponse response)
 {
     mMessageHandler.SetResponsePattern(urlRegex, response);
     return(this);
 }
Example #4
0
 /// <summary>
 /// /// Set a predefined response to a request. Calls made with an existing
 /// endpoint will replace the previous response.
 /// </summary>
 /// <param name="endpoint">The endpoint being requested</param>
 /// <param name="response">A predefined response</param>
 public MockHttpClient SetResponse(string endpoint, MockHttpResponse response)
 {
     mMessageHandler.SetResponse(endpoint, response);
     return(this);
 }