示例#1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MemoryHtmlPage"/> class.
        /// </summary>
        /// <param name="browser">
        /// The browser.
        /// </param>
        /// <param name="html">
        /// The HTML.
        /// </param>
        /// <exception cref="ArgumentNullException">The <paramref name="browser"/> parameter is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">The <paramref name="html"/> parameter is <c>null</c>, empty or only contains white space.</exception>
        public MemoryHtmlPage(IBrowser browser, string html)
        {
            if (browser == null)
            {
                throw new ArgumentNullException("browser");
            }

            if (string.IsNullOrWhiteSpace(html))
            {
                throw new ArgumentException(Resources.HtmlPage_NoHtmlContentProvided, "html");
            }

            IEnumerable<HttpOutcome> outcomes = new List<HttpOutcome>
            {
                new HttpOutcome(_targetLocation, HttpMethod.Get, HttpStatusCode.OK, "OK", TimeSpan.FromMilliseconds(5))
            };
            var result = new HttpResult(outcomes);

            Initialize(browser, HttpStatusCode.OK, "OK", result);

            using (var reader = new StringReader(html))
            {
                SetContent(reader);
            }
        }
示例#2
0
        /// <inheritdoc />
        public override void Initialize(IBrowser browser, HttpResponseMessage response, HttpResult result)
        {
            base.Initialize(browser, response, result);

            _targetLocation = result.Outcomes.Last().Location;
        }
        /// <inheritdoc />
        /// <exception cref="System.ArgumentNullException">
        ///     The <paramref name="browser" /> parameter is <c>null</c>.
        /// </exception>
        /// <exception cref="System.ArgumentNullException">
        ///     The <paramref name="response" /> parameter is <c>null</c>.
        /// </exception>
        /// <exception cref="System.ArgumentNullException">
        ///     The <paramref name="result" /> parameter is <c>null</c>.
        /// </exception>
        public void Initialize(IBrowser browser, HttpResponseMessage response, HttpResult result)
        {
            if (browser == null)
            {
                throw new ArgumentNullException("browser");
            }

            if (response == null)
            {
                throw new ArgumentNullException("response");
            }

            if (result == null)
            {
                throw new ArgumentNullException("result");
            }

            // Look at the content type header to determine the correct type of page to return
            var mediaType = DetermineMediaType(response);

            var contentType = browser.ContentTypeResolver.DeterminePageType(mediaType);

            if (contentType == PageContentType.Html)
            {
                _resolvedPage = new DynamicHtmlPage();
            }
            else if (contentType == PageContentType.Binary)
            {
                _resolvedPage = new BinaryPageWrapper();
            }
            else
            {
                _resolvedPage = new TextPageWrapper();
            }

            ResolvedPage.Initialize(browser, response, result);
        }
示例#4
0
 /// <summary>
 /// Initializes the page with the specified values.
 /// </summary>
 /// <param name="browser">
 /// The browser.
 /// </param>
 /// <param name="statusCode">
 /// The status code.
 /// </param>
 /// <param name="statusDescription">
 /// The status description.
 /// </param>
 /// <param name="result">
 /// The result.
 /// </param>
 protected void Initialize(
     IBrowser browser, 
     HttpStatusCode statusCode, 
     string statusDescription, 
     HttpResult result)
 {
     _browser = browser;
     _statusCode = statusCode;
     _statusDescription = statusDescription;
     _result = result;
 }
 /// <summary>
 ///     Initializes a new instance of the <see cref="HttpOutcomeException" /> class.
 /// </summary>
 /// <param name="result">The result.</param>
 /// <param name="inner">The inner.</param>
 public HttpOutcomeException(HttpResult result, Exception inner)
     : this(GenerateFailureMessage(result, inner), inner)
 {
     Result = result;
 }
示例#6
0
        /// <inheritdoc />
        /// <exception cref="System.ArgumentNullException">
        ///     The <paramref name="browser" /> parameter is <c>null</c>.
        /// </exception>
        /// <exception cref="System.ArgumentNullException">
        ///     The <paramref name="response" /> parameter is <c>null</c>.
        /// </exception>
        /// <exception cref="System.ArgumentNullException">
        ///     The <paramref name="result" /> parameter is <c>null</c>.
        /// </exception>
        public virtual void Initialize(IBrowser browser, HttpResponseMessage response, HttpResult result)
        {
            if (browser == null)
            {
                throw new ArgumentNullException("browser");
            }

            if (response == null)
            {
                throw new ArgumentNullException("response");
            }

            if (result == null)
            {
                throw new ArgumentNullException("result");
            }

            Initialize(browser, response.StatusCode, response.ReasonPhrase, result);

            SetContent(response.Content);
        }
 /// <summary>
 ///     Initializes a new instance of the <see cref="HttpOutcomeException" /> class.
 /// </summary>
 /// <param name="result">
 ///     The result.
 /// </param>
 /// <param name="targetLocation">
 ///     The target location.
 /// </param>
 /// <exception cref="System.ArgumentNullException">
 ///     The <paramref name="result" /> parameter is <c>null</c>.
 /// </exception>
 public HttpOutcomeException(HttpResult result, Uri targetLocation)
     : base(GenerateIncorrectLocationMessage(result, targetLocation))
 {
     Result = result;
 }
 /// <summary>
 ///     Initializes a new instance of the <see cref="HttpOutcomeException" /> class.
 /// </summary>
 /// <param name="result">
 ///     The result.
 /// </param>
 /// <param name="expectedStatusCode">
 ///     The expected status code.
 /// </param>
 /// <exception cref="System.ArgumentNullException">
 ///     The <paramref name="result" /> parameter is <c>null</c>.
 /// </exception>
 public HttpOutcomeException(HttpResult result, HttpStatusCode expectedStatusCode)
     : base(GenerateIncorrectStatusMessage(result, expectedStatusCode))
 {
     Result = result;
 }
        /// <summary>
        ///     Generates the incorrect status message.
        /// </summary>
        /// <param name="result">
        ///     The result.
        /// </param>
        /// <param name="expectedStatusCode">
        ///     The expected status code.
        /// </param>
        /// <returns>
        ///     A <see cref="string" /> value.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        ///     The <paramref name="result" /> parameter is <c>null</c>.
        /// </exception>
        private static string GenerateIncorrectStatusMessage(HttpResult result, HttpStatusCode expectedStatusCode)
        {
            if (result == null)
            {
                throw new ArgumentNullException("result");
            }

            var lastOutcome = result.Outcomes.Last();

            var outcomes = result.Outcomes.Aggregate(string.Empty, (x, y) => x + Environment.NewLine + y);

            var message = string.Format(
                CultureInfo.CurrentCulture,
                Resources.HttpOutcomeException_InvalidResponseStatus,
                expectedStatusCode,
                lastOutcome.StatusCode,
                outcomes);

            return message;
        }
        /// <summary>
        ///     Generates the incorrect location message.
        /// </summary>
        /// <param name="result">
        ///     The result.
        /// </param>
        /// <param name="targetLocation">
        ///     The target location.
        /// </param>
        /// <returns>
        ///     A <see cref="string" /> value.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        ///     The <paramref name="result" /> parameter is <c>null</c>.
        /// </exception>
        private static string GenerateIncorrectLocationMessage(HttpResult result, Uri targetLocation)
        {
            if (result == null)
            {
                throw new ArgumentNullException("result");
            }

            var lastOutcome = result.Outcomes.Last();

            var outcomes = result.Outcomes.Aggregate(string.Empty, (x, y) => x + Environment.NewLine + y);

            // We have been requested to go to a location that doesn't match the requested page
            var message = string.Format(
                CultureInfo.CurrentCulture,
                Resources.HttpOutcomeException_InvalidLocation,
                lastOutcome.Location,
                targetLocation,
                outcomes);

            return message;
        }
        /// <summary>
        ///     Generates the failure message.
        /// </summary>
        /// <param name="result">The result.</param>
        /// <param name="inner">The inner.</param>
        /// <returns>The failure message.</returns>
        /// <exception cref="System.ArgumentNullException">
        ///     The <paramref name="result" /> parameter is <c>null</c>.
        /// </exception>
        /// <exception cref="System.ArgumentNullException">
        ///     The <paramref name="inner" /> parameter is <c>null</c>.
        /// </exception>
        private static string GenerateFailureMessage(HttpResult result, Exception inner)
        {
            if (result == null)
            {
                throw new ArgumentNullException("result");
            }

            if (inner == null)
            {
                throw new ArgumentNullException("inner");
            }

            var lastOutcome = result.Outcomes.Last();

            var outcomes = result.Outcomes.Aggregate(string.Empty, (x, y) => x + Environment.NewLine + y);

            // We have been requested to go to a location that doesn't match the requested page
            var message = string.Format(
                CultureInfo.CurrentCulture,
                "The {0} request to {1} has failed with the message '{2}'. The responses for this request were: {3}",
                lastOutcome.Method,
                lastOutcome.Location,
                inner.Message,
                outcomes);

            return message;
        }