private static XmlDocument SimpleRequest(string method, ref Uri uri, HttpRequestFilter filter, out WebHeaderCollection responseHeaders, params string[] parameters)
        {
            string absUri = UrlHelper.SafeToAbsoluteUri(uri);

            if (parameters.Length > 0)
            {
                FormData formData = new FormData(true, parameters);

                if (absUri.IndexOf('?') == -1)
                    absUri += "?" + formData.ToString();
                else
                    absUri += "&" + formData.ToString();
            }

            RedirectHelper.SimpleRequest simpleRequest = new RedirectHelper.SimpleRequest(method, filter);
            HttpWebResponse response = RedirectHelper.GetResponse(absUri, new RedirectHelper.RequestFactory(simpleRequest.Create));
            try
            {
                uri = response.ResponseUri;
                responseHeaders = response.Headers;
                return ParseXmlResponse(response);
            }
            finally
            {
                if (response != null)
                    response.Close();
            }
        }
        public void GetQueryStringForSimpleResponseReturnsEncodedResponse()
        {
            var message = "test message";
            var data = new FormData() { Response = message };
            var expectedMessage = WebUtility.UrlEncode(message);

            var result = data.ToString();

            Assert.Contains(expectedMessage, result);
            Assert.Equal($"response={expectedMessage}", result);
        }
        public void GetQueryStringForFullResponseReturnsEncodedResponse()
        {
            var message = "test message";
            var status = HttpStatusCode.Created;
            var delay = 100;
            var data = new FormData { Response = message, Status = status, Delay = delay };
            var expectedMessage = WebUtility.UrlEncode(message);
            var expected = $"response={expectedMessage}&status={(int)HttpStatusCode.Created}&delay={delay}";

            var result = data.ToString();

            Assert.Contains(expectedMessage, result);
            Assert.Equal(expected, result);
        }
 public void GetQueryStringForEmptyDataReturnsEmpty()
 {
     var data = new FormData();
     Assert.Empty(data.ToString());
 }
        public async Task PostRequestWithFormDataReturnsEchoMessage()
        {
            var request = $"{CONST_ServerUrl}/api/echo/";
            var data = new FormData() {Delay = 10, Status = HttpStatusCode.OK, Response = CONST_Message };

            var result = await this.SendPostAsync(request, data.ToString());
            var response = await result.Content.ReadAsStringAsync();
            var status = result.StatusCode;
            var isSuccess = result.IsSuccessStatusCode;

            Assert.True(isSuccess);
            Assert.Equal(HttpStatusCode.OK, status);
            Assert.Equal(CONST_Message, response);
        }
        /// <summary>
        ///     写入提交数据
        /// </summary>
        private static HttpWebRequest Write(this HttpWebRequest request, FormData formData)
        {
            if (formData.Count > 0)
            {
                //var formDataString = formData.GetFormDataString();
                request.Write(Encoding.UTF8.GetBytes(formData.ToString()));
#if DEBUG
                request.ShowDebugInfo();
                Debug.WriteLine("Form Data:");
                var i = 0;
                foreach (var data in formData)
                {
                    Debug.WriteLine("[{0,-2}] {1,-15} : {2}", ++i, data.Key, data.Value);
                }
#endif
            }
            return request;
        }