/// <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 JsonSerializer(); //} var serializer = new JsonSerializer(); var contextValues = (IBrowserContextValues)browserContext; contextValues.Body = new MemoryStream(); using (JsonTextWriter jsonTextWriter = new JsonTextWriter(new StreamWriter(contextValues.Body, UTF8Encoding.UTF8)) { CloseOutput = false }) { serializer.Serialize(jsonTextWriter, model); jsonTextWriter.Flush(); contextValues.Body.Seek(0, SeekOrigin.Begin); } //serializer.Serialize("application/json", model, contextValues.Body); browserContext.Header("Content-Type", "application/json"); }
/// <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); }
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"); }