/// <summary>
        /// Adds basic authorization credentials to the headers of the <see cref="Browser"/>.
        /// </summary>
        /// <param name="browserContext">The <see cref="BrowserContext"/> that the data should be added to.</param>
        /// <param name="username">The username to be encoded.</param>
        /// <param name="password">The password to be encoded.</param>
        public static void BasicAuth(this BrowserContext browserContext, string username, string password)
        {
            var credentials = string.Format("{0}:{1}", username, password);

            var encodedCredentials = Convert.ToBase64String(Encoding.UTF8.GetBytes(credentials));

            browserContext.Header("Authorization", "Basic " + encodedCredentials);
        }
Example #2
0
        private void SetCookies(BrowserContext context)
        {
            if (!this.cookies.Any())
            {
                return;
            }

            var cookieString = this.cookies.Aggregate(string.Empty, (current, cookie) => current + string.Format("{0}={1};", HttpUtility.UrlEncode(cookie.Key), HttpUtility.UrlEncode(cookie.Value)));

            context.Header("Cookie", cookieString);
        }
        /// <summary>
        /// Adds a application/xml request body to the <see cref="Browser"/>.
        /// </summary>
        /// <param name="browserContext">The <see cref="BrowserContext"/> that the data should be added to.</param>
        /// <param name="model">The model to be serialized to xml.</param>
        /// <param name="serializer">Optionally opt in to using a different XML serializer.</param>
        public static void XMLBody <TModel>(this BrowserContext browserContext, TModel model, ISerializer serializer = null)
        {
            if (serializer == null)
            {
                serializer = new DefaultXmlSerializer(browserContext.Environment);
            }

            var contextValues =
                (IBrowserContextValues)browserContext;

            contextValues.Body = new MemoryStream();

            serializer.Serialize("application/xml", model, contextValues.Body);
            browserContext.Header("Content-Type", "application/xml");
        }
        /// <summary>
        /// Adds a application/json request body to the <see cref="Browser"/>.
        /// </summary>
        /// <param name="browserContext">The <see cref="BrowserContext"/> that the data should be added to.</param>
        /// <param name="model">The model to be serialized to json.</param>
        /// <param name="serializer">Optionally opt in to using a different JSON serializer.</param>
        public static void JsonBody <TModel>(this BrowserContext browserContext, TModel model, ISerializer serializer = null)
        {
            if (serializer == null)
            {
                serializer = new DefaultJsonSerializer();
            }

            var contextValues =
                (IBrowserContextValues)browserContext;

            contextValues.Body = new MemoryStream();

            serializer.Serialize("application/json", model, contextValues.Body);
            browserContext.Header("Content-Type", "application/json");
        }
        public static void Accept(this BrowserContext browserContext, MediaRange mediaRange, decimal quality)
        {
            var contextValues =
                (IBrowserContextValues)browserContext;

            if (contextValues.Headers.ContainsKey("accept"))
            {
                if (contextValues.Headers["accept"].Count().Equals(1))
                {
                    if (contextValues.Headers["accept"].Any(x => x.Equals("*/*")))
                    {
                        contextValues.Headers.Remove("accept");
                    }
                }
            }

            var mediaTypeWithQuality =
                string.Concat(mediaRange, ";q=", Convert.ToString(quality, CultureInfo.InvariantCulture));

            browserContext.Header("accept", mediaTypeWithQuality);
        }
Example #6
0
        private void SetCookies(BrowserContext context)
        {
            if (!this.cookies.Any())
            {
                return;
            }

            var cookieString = this.cookies.Aggregate(string.Empty, (current, cookie) => current + string.Format("{0}={1};", HttpUtility.UrlEncode(cookie.Key), HttpUtility.UrlEncode(cookie.Value)));

            context.Header("Cookie", cookieString);
        }
 /// <summary>
 /// Adds a header to indicate this request is an "ajax request"
 /// <seealso cref="RequestExtensions.IsAjaxRequest"/>
 /// </summary>
 /// <param name="browserContext">The <see cref="BrowserContext"/> that the data should be added to.</param>
 public static void AjaxRequest(this BrowserContext browserContext)
 {
     browserContext.Header("X-Requested-With", "XMLHttpRequest");
 }