コード例 #1
0
        /// <summary>
        /// The async delegate that handles an incoming request, passing it through to a real
        /// service and record the response if <paramref name="serviceBaseUri"/> was specified,
        /// or plays back responses from a pre-recorded <paramref name="session"/> if it was not.
        /// </summary>
        /// <param name="context">The context for the incoming request.</param>
        /// <param name="baseUri">The incoming request's base Uri.</param>
        /// <param name="session">The object that stores request/response information.</param>
        private static void HandleMockRequest(
            HttpListenerContext context,
            Uri baseUri,
            HttpSession session)
        {
            if (session.ServiceBaseUri != null)
            {
                // Issue the request to a real service and record the response
                HttpMessage message = GetResponseInfoFromService(
                    context.Request,
                    baseUri,
                    session.ServiceBaseUri,
                    session);
                session.Messages.RecordMessage(message);
            }

            // Construct the mock response from responses in the session.
            HttpMessage recordedMessage = session.Messages.GetMessage(context.Request.Url);

            if (session.RequestValidator != null)
            {
                HttpMessage.Request actualRequestInfo = ConstructRequestInfo(context.Request);
                session.RequestValidator(recordedMessage, actualRequestInfo);
            }

            if (session.ResponseModifier != null)
            {
                session.ResponseModifier(recordedMessage);
            }

            ConstructListenerResponse(recordedMessage.ResponseInfo, baseUri, context.Response);
            context.Response.Close();
        }
コード例 #2
0
        /// <summary>
        /// Create an <see cref="HttpMessage.Request"/> object out of the given
        /// <paramref name="originalRequest"/>.
        /// </summary>
        /// <param name="originalRequest">The original request to mimic.</param>
        /// <returns>An <see cref="HttpMessage.Request"/> object containing the request.</returns>
        private static HttpMessage.Request ConstructRequestInfo(
            HttpListenerRequest originalRequest)
        {
            HttpMessage.Request requestInfo = new HttpMessage.Request();

            // Copy the request Uri and Method
            requestInfo.RequestUri = originalRequest.Url;
            requestInfo.Method     = originalRequest.HttpMethod;

            // Copy all relevant headers to the request
            requestInfo.Headers = new HttpMessage.HeaderCollection();
            foreach (string headerKey in originalRequest.Headers.AllKeys)
            {
                if (headerKey.Equals("User-Agent", StringComparison.OrdinalIgnoreCase))
                {
                    requestInfo.UserAgent = originalRequest.Headers[headerKey];
                }
                else if (headerKey.Equals("Content-Type", StringComparison.OrdinalIgnoreCase))
                {
                    requestInfo.ContentType = originalRequest.Headers[headerKey];
                }
                else if (headerKey.Equals("Accept", StringComparison.OrdinalIgnoreCase))
                {
                    requestInfo.Accept = originalRequest.Headers[headerKey];
                }
                else if (!headerKey.Equals("Connection", StringComparison.OrdinalIgnoreCase) &&
                         !headerKey.Equals("Host", StringComparison.OrdinalIgnoreCase) &&
                         !headerKey.Equals("Content-Length", StringComparison.OrdinalIgnoreCase) &&
                         !headerKey.Equals("Expect", StringComparison.OrdinalIgnoreCase))
                {
                    requestInfo.Headers.Add(new HttpMessage.Header
                    {
                        Name  = headerKey,
                        Value = originalRequest.Headers[headerKey]
                    });
                }
            }

            // Copy response cookies
            requestInfo.Cookies = new HttpMessage.CookieCollection();
            foreach (Cookie cookie in originalRequest.Cookies)
            {
                requestInfo.Cookies.Add(new HttpMessage.Cookie
                {
                    Name  = cookie.Name,
                    Value = cookie.Value,
                });
            }

            // Copy response stream
            if (originalRequest.Headers.AllKeys.Contains("Content-Length"))
            {
                using (StreamReader reader = new StreamReader(originalRequest.InputStream))
                {
                    requestInfo.RequestText = reader.ReadToEnd();
                }
            }

            return(requestInfo);
        }
コード例 #3
0
        /// <summary>
        /// Retrieves an <see cref="HttpMessage"/> object from a real service.
        /// </summary>
        /// <param name="request">The request to mimic on the real service.</param>
        /// <param name="baseUri">The request's base Uri.</param>
        /// <param name="serviceBaseUri">The real service's base Uri.</param>
        /// <param name="session">The object that stores request/response information.</param>
        /// <returns>An <see cref="HttpMessage"/> object containing the request/response.</returns>
        private static HttpMessage GetResponseInfoFromService(
            HttpListenerRequest request,
            Uri baseUri,
            Uri serviceBaseUri,
            HttpSession session)
        {
            // Construct the request to make
            HttpMessage message = new HttpMessage();

            message.RequestInfo = ConstructRequestInfo(request);

            // Clone the request and modify it for the real service
            HttpMessage.Request requestToSend = message.RequestInfo.Clone();
            requestToSend.RequestUri = ChangeUriBase(request.Url, baseUri, serviceBaseUri);
            if (session.RequestModifier != null)
            {
                session.RequestModifier(requestToSend);
            }

            HttpWebResponse response = MakeServiceRequest(requestToSend);

            message.ResponseInfo = ConstructResponseInfo(serviceBaseUri, response);
            return(message);
        }
コード例 #4
0
        /// <summary>
        /// Make an request to the real service and retrieve the response.
        /// </summary>
        /// <param name="originalRequest">The original request to mimic.</param>
        /// <param name="requestUri">The Uri to make the request to.</param>
        /// <returns>The response from the service.</returns>
        private static HttpWebResponse MakeServiceRequest(
            HttpMessage.Request originalRequest)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(
                originalRequest.RequestUri);

            request.Method = originalRequest.Method;

            // Copy all relevant headers to the request
            if (originalRequest.UserAgent != null)
            {
                request.UserAgent = originalRequest.UserAgent;
            }

            if (originalRequest.ContentType != null)
            {
                request.ContentType = originalRequest.ContentType;
            }

            if (originalRequest.Accept != null)
            {
                request.Accept = originalRequest.Accept;
            }

            foreach (HttpMessage.Header header in originalRequest.Headers)
            {
                request.Headers.Add(header.Name, header.Value);
            }

            // Copy all request cookies
            request.CookieContainer = new CookieContainer();
            foreach (HttpMessage.Cookie cookie in originalRequest.Cookies)
            {
                Cookie requestCookie = new Cookie(cookie.Name, cookie.Value);
                requestCookie.Domain = request.RequestUri.Host;
                requestCookie.Path   = request.RequestUri.LocalPath;
                request.CookieContainer.Add(requestCookie);
            }

            // Copy request streams
            if (originalRequest.RequestText != null)
            {
                using (StreamWriter requestStream = new StreamWriter(request.GetRequestStream()))
                {
                    requestStream.Write(originalRequest.RequestText);
                }
            }

            // Send the real request and obtain the response
            HttpWebResponse response;

            try
            {
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException ex)
            {
                response = (HttpWebResponse)ex.Response;
            }

            return(response);
        }