private static Request CreateRequest(string method, string path, Action <BrowserContext> browserContext) { var context = new BrowserContext(); browserContext.Invoke(context); var contextValues = (IBrowserContextValues)context; BuildRequestBody(contextValues); var requestStream = RequestStream.FromStream(contextValues.Body, 0, true); return(new Request(method, path, contextValues.Headers, requestStream, contextValues.Protocol, contextValues.QueryString)); }
/// <summary> /// Initializes a new instance of the <see cref="BrowserResponse"/> class. /// </summary> /// <param name="context">The <see cref="NancyContext"/> that <see cref="Browser"/> was invoked with.</param> /// <param name="hostBrowser">Host browser object</param> /// <param name="browserContext">An <see cref="BrowserContext"/> instance.</param> /// <exception cref="ArgumentNullException">The value of the <paramref name="context"/> parameter was <see langword="null"/>.</exception> public BrowserResponse(NancyContext context, Browser hostBrowser, BrowserContext browserContext) { if (context == null) { throw new ArgumentNullException("context", "The value of the context parameter cannot be null."); } if (hostBrowser == null) { throw new ArgumentNullException("hostBrowser", "The value of the hostBrowser parameter cannot be null."); } this.hostBrowser = hostBrowser; this.browserContext = browserContext; this.Context = context; }
/// <summary> /// Initializes a new instance of the <see cref="BrowserResponse"/> class. /// </summary> /// <param name="context">The <see cref="NancyContext"/> that <see cref="Browser"/> was invoked with.</param> /// <param name="hostBrowser">Host browser object</param> /// <param name="browserContext">An <see cref="BrowserContext"/> instance.</param> /// <exception cref="ArgumentNullException">The value of the <paramref name="context"/> parameter was <see langword="null"/>.</exception> public BrowserResponse(NancyContext context, Browser hostBrowser, BrowserContext browserContext) { if (context == null) { throw new ArgumentNullException("context", "The value of the context parameter cannot be null."); } if (hostBrowser == null) { throw new ArgumentNullException("hostBrowser", "The value of the hostBrowser parameter cannot be null."); } this.hostBrowser = hostBrowser; this.browserContext = browserContext; this.Context = context; }
private IBrowserContextValues BuildBrowserContextValues(Action <BrowserContext> browserContext) { var context = new BrowserContext(this.environment); this.SetCookies(context); this.defaultBrowserContext.Invoke(context); browserContext.Invoke(context); var contextValues = (IBrowserContextValues)context; if (!contextValues.Headers.ContainsKey("user-agent")) { contextValues.Headers.Add("user-agent", new[] { "Nancy.Testing.Browser" }); } return(contextValues); }
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); }
public override string ToString() { var context = new BrowserContext(); var onContext = OnContext; if (onContext != null) { onContext(context); } IBrowserContextValues values = context; var builder = new StringBuilder() .Append(GetMethod()).Append(" ").Append(Path); if (false == String.IsNullOrEmpty(values.QueryString)) { builder.Append(values.QueryString); } builder.AppendLine(); builder = values.Headers.Aggregate( builder, (sb, header) => header.Value.Aggregate( sb, (b, value) => b.Append(header.Key).Append(": ").Append(value).AppendLine())); if (false == String.IsNullOrEmpty(values.FormValues)) { var body = HttpUtility.ParseQueryString(values.FormValues); builder = body.AllKeys.Aggregate( builder.AppendLine(), (sb, key) => sb.Append(key).Append(": ").Append(body[key]).AppendLine()); } else { builder.Append(values.BodyString); } return builder.ToString(); }
public override string ToString() { var context = new BrowserContext(); OnContext(context); IBrowserContextValues values = context; var builder = new StringBuilder() .Append(GetMethod()).Append(" ").Append(Path); if (false == String.IsNullOrEmpty(values.QueryString)) { builder.Append('?').Append(values.QueryString); } if (false == String.IsNullOrEmpty(values.FormValues)) { var body = HttpUtility.ParseQueryString(values.FormValues); builder = body.AllKeys.Aggregate( builder.AppendLine(), (sb, key) => builder.Append(key).Append(": ").Append(body[key]).AppendLine()); } return builder.AppendLine().ToString(); }
private Request CreateRequest(string method, string path, Action<BrowserContext> browserContext) { var context = new BrowserContext(); this.SetCookies(context); browserContext.Invoke(context); var contextValues = (IBrowserContextValues)context; BuildRequestBody(contextValues); var requestStream = RequestStream.FromStream(contextValues.Body, 0, true); var certBytes = (contextValues.ClientCertificate == null) ? new byte[] {} : contextValues.ClientCertificate.GetRawCertData(); return new Request(method, new Url { Path = path, Scheme = contextValues.Protocol, Query = contextValues.QueryString ?? string.Empty }, requestStream, contextValues.Headers, contextValues.UserHostAddress, certBytes); }
private void DefaultBrowserContext(BrowserContext context) { context.HttpRequest(); }
private static Request CreateRequest(string method, string path, Action<BrowserContext> browserContext) { var context = new BrowserContext(); browserContext.Invoke(context); var contextValues = (IBrowserContextValues)context; BuildRequestBody(contextValues); var requestStream = RequestStream.FromStream(contextValues.Body, 0, true); return new Request(method, path, contextValues.Headers, requestStream, contextValues.Protocol, contextValues.QueryString); }
private Request CreateRequest(string method, Url url, Action<BrowserContext> browserContext) { var context = new BrowserContext(); this.SetCookies(context); this.defaultBrowserContext.Invoke(context); browserContext.Invoke(context); var contextValues = (IBrowserContextValues)context; if (!contextValues.Headers.ContainsKey("user-agent")) { contextValues.Headers.Add("user-agent", new[] { "Nancy.Testing.Browser" }); } BuildRequestBody(contextValues); var requestStream = RequestStream.FromStream(contextValues.Body, 0, true); var certBytes = (contextValues.ClientCertificate == null) ? new byte[] { } : contextValues.ClientCertificate.GetRawCertData(); var requestUrl = url; requestUrl.Scheme = string.IsNullOrWhiteSpace(contextValues.Protocol) ? requestUrl.Scheme : contextValues.Protocol; requestUrl.HostName = string.IsNullOrWhiteSpace(contextValues.HostName) ? requestUrl.HostName : contextValues.HostName; requestUrl.Query = string.IsNullOrWhiteSpace(url.Query) ? (contextValues.QueryString ?? string.Empty) : url.Query; return new Request(method, requestUrl, requestStream, contextValues.Headers, contextValues.UserHostAddress, certBytes); }
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); }
private void AddHangaroundsFormValues(BrowserContext with, int numberOfHangarounds) { if (numberOfHangarounds > 0) { for (var i = 0; i < numberOfHangarounds; i++) { var number = i.ToString(); with.FormValue("Hangarounds.Name" + number, "Hangaround #" + number); } } }
private static void DefaultBrowserContext(BrowserContext context) { context.HttpRequest(); }
private IBrowserContextValues BuildBrowserContextValues(Action<BrowserContext> browserContext) { var context = new BrowserContext(this.environment); this.SetCookies(context); this.defaultBrowserContext.Invoke(context); browserContext.Invoke(context); var contextValues = (IBrowserContextValues)context; if (!contextValues.Headers.ContainsKey("user-agent")) { contextValues.Headers.Add("user-agent", new[] { "Nancy.Testing.Browser" }); } return contextValues; }
/// <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"); }
private void AddEvilFormValue(BrowserContext with, string evilstring) { var evil = evilstring.ToLower() == "good" ? false : true; with.FormValue("Evil", evil.ToString()); }
public static void Accept(this BrowserContext browserContext, MediaRange mediaRange) { browserContext.Accept(mediaRange, 1.0m); }
/// <summary> /// Adds a multipart/form-data encoded request body to the <see cref="Browser"/>, using the default boundary name. /// </summary> /// <param name="browserContext">The <see cref="BrowserContext"/> that the data should be added to.</param> /// <param name="multipartFormData">The multipart/form-data encoded data that should be added.</param> public static void MultiPartFormData(this BrowserContext browserContext, BrowserContextMultipartFormData multipartFormData) { MultiPartFormData(browserContext, multipartFormData, BrowserContextMultipartFormData.DefaultBoundaryName); }