Ejemplo n.º 1
0
        private Response Post(string stepName, string url, object parameters, PostType type)
        {
            RecordStepStart(stepName);

            var client = GetWebClient();

            try
            {
                switch (type)
                {
                case PostType.Form:
                    _data = client.UploadValues(url, NameValueCollectionConversions.ConvertFromObject(parameters));
                    break;

                case PostType.Json:
                    var serializer = new JavaScriptSerializer();
                    client.Headers.Add("Content-Type", "application/json; charset=utf-8");
                    _data = client.UploadData(url, Encoding.UTF8.GetBytes(serializer.Serialize(parameters)));
                    client.Headers.Remove("Content-Type");
                    break;
                }

                return(BuildResponse(client.Response));
            }
            catch (WebException)
            {
                return(BuildErrorResponse());
            }
            finally
            {
                RecordStepEnd(stepName);
            }
        }
Ejemplo n.º 2
0
        public Response Get(string url, object parameters = null, string stepName = null)
        {
            RecordStepStart(stepName);
            var client      = GetWebClient();
            var nvc         = NameValueCollectionConversions.ConvertFromObject(parameters);
            var queryString = string.Join("&",
                                          Array.ConvertAll(nvc.AllKeys,
                                                           key => string.Format("{0}={1}", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(nvc[key]))));

            try
            {
                if (!string.IsNullOrEmpty(queryString))
                {
                    url = url + "?" + queryString;
                }
                _data = client.DownloadData(url);
                return(BuildResponse(client.Response));
            }
            catch (WebException)
            {
                return(BuildErrorResponse());
            }
            finally
            {
                RecordStepEnd(stepName);
            }
        }
Ejemplo n.º 3
0
        private RequestResult ProcessRequest(string url, HttpVerbs httpVerb, object formValues, NameValueCollection headers)
        {
            var formNameValueCollection = NameValueCollectionConversions.ConvertFromObject(formValues);

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

            // Fix up URLs that incorrectly start with / or ~/
            if (url.StartsWith("~/"))
            {
                url = url.Substring(2);
            }
            else if (url.StartsWith("/"))
            {
                url = url.Substring(1);
            }

            // Parse out the querystring if provided
            var query = "";
            var querySeparatorIndex = url.IndexOf("?", StringComparison.Ordinal);

            if (querySeparatorIndex >= 0)
            {
                query = url.Substring(querySeparatorIndex + 1);
                url   = url.Substring(0, querySeparatorIndex);
            }

            // Perform the request
            LastRequestData.Reset();
            var output        = new StringWriter();
            var httpVerbName  = httpVerb.ToString().ToLower();
            var workerRequest = new SimulatedWorkerRequest(url, query, output, Cookies, httpVerbName, formNameValueCollection, headers);

            HttpRuntime.ProcessRequest(workerRequest);

            // Capture the output
            AddAnyNewCookiesToCookieCollection();
            Session = LastRequestData.HttpSessionState;
            return(new RequestResult
            {
                ResponseText = output.ToString(),
                ActionExecutedContext = LastRequestData.ActionExecutedContext,
                ResultExecutedContext = LastRequestData.ResultExecutedContext,
                Response = LastRequestData.Response,
            });
        }
        private RequestResult ProcessRequest(string url, HttpVerbs httpVerb, object formData, string acceptHeader, string authorizeUsername, string authorizePassword)
        {
            NameValueCollection headers = new NameValueCollection();

            if (!String.IsNullOrEmpty(acceptHeader))
            {
                headers.Add("Accept", acceptHeader);
                // -> http://en.wikipedia.org/wiki/List_of_HTTP_header_fields
            }
            if (!String.IsNullOrEmpty(authorizeUsername))
            {
                headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(authorizeUsername + ":" + authorizePassword)));
                // -> http://en.wikipedia.org/wiki/Basic_access_authentication
            }

            var formNameValueCollection = NameValueCollectionConversions.ConvertFromObject(formData);

            return(ProcessRequest(url, httpVerb, formNameValueCollection, headers));
        }
Ejemplo n.º 5
0
 public When_converting_an_object_that_has_a_nested_anonymous_object()
 {
     converted = NameValueCollectionConversions.ConvertFromObject(new { Form = new { name = "hello", age = 30 } });
 }
Ejemplo n.º 6
0
 public When_converting_an_object_has_2_properties_to_name_value_collection()
 {
     converted = NameValueCollectionConversions.ConvertFromObject(new { name = "hello", age = 30 });
 }
Ejemplo n.º 7
0
 public When_converting_an_object_with_one_string_property_to_name_value_collection()
 {
     convertedFromObjectWithString = NameValueCollectionConversions.ConvertFromObject(new { name = "hello" });
 }
 public void Should_have_key_of_name_with_value_hello()
 {
     convertedFromObjectWithString = NameValueCollectionConversions.ConvertFromObject(new { name = "hello" });
     Assert.That(convertedFromObjectWithString["name"], Is.EqualTo("hello"));
 }
Ejemplo n.º 9
0
        /// <summary>
        /// Sends a post to your url. Url should NOT start with a /
        /// </summary>
        /// <param name="url"></param>
        /// <param name="formData"></param>
        /// <example>
        /// <code>
        /// var result = Post("registration/create", new
        /// {
        ///     Form = new
        ///     {
        ///         InvoiceNumber = "10000",
        ///         AmountDue = "10.00",
        ///         Email = "*****@*****.**",
        ///         Password = "******",
        ///         ConfirmPassword = "******"
        ///     }
        /// });
        /// </code>
        /// </example>
        public RequestResult Post(string url, object formData)
        {
            var formNameValueCollection = NameValueCollectionConversions.ConvertFromObject(formData);

            return(ProcessRequest(url, HttpVerbs.Post, formNameValueCollection));
        }