public void serializeAndDeserialize() {
            var rvm = new RequestViewModel {
                Body = "body",
                Headers = new[] { "h1", "h2" },
                Method = HttpMethod.Post.Method,
                Url = "url"
            };

            var filepath = FileUtils.CreateTempFilePath("xml");
            rvm.Save(filepath);

            var rvm_deserialized = RequestViewModel.Load(filepath);

            rvm.ShouldBeEquivalentTo(rvm_deserialized);
        }
        public static List<string> TryCreate(RequestViewModel vm, out RequestModel requestModel)
        {
            var validationErrors = new List<string>();

            Uri url = null;
            if(vm.Url.IsBlank())
                validationErrors.Add("Request URL may not be empty");
            else {
                var forgivingUrl = vm.Url.Contains("://") ? vm.Url : "http://" + vm.Url;
                if(!Uri.TryCreate(forgivingUrl, UriKind.Absolute, out url))
                    validationErrors.Add("Request URL is invalid");
            }

            var requestHeaders = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
            var contentHeaders = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
            var acceptEncodings = new HashSet<string>();
            foreach (var line in vm.Headers) {
                if (line.IsBlank())
                    continue; //allow empty lines

                var match = Regex.Match(line, @"^([0-9a-zA-Z-]+)\s*\:(.*)$", RegexOptions.Compiled);
                if (!match.Success)
                    validationErrors.Add("Invalid header line (format incorrect): " + line);
                else {

                    var key = match.Groups[1].Value.Trim();
                    var value = match.Groups[2].Value.Trim();
                    if (requestHeaders.ContainsKey(key) || contentHeaders.ContainsKey(key))
                        validationErrors.Add("Invalid header line (duplicate key, comma-separate multiple values for one key): " + line);
                    else if (String.Equals(key, "authorization", StringComparison.OrdinalIgnoreCase) && !url.UserInfo.IsBlank()) {
                        validationErrors.Add("Invalid header line (Authorization header cannot be specified when user information is given in the url): " + line);
                    } else {
                        //var values = value.Split(',').Select(x => x.Trim()).ToList().AsReadOnly();
                        //some ugliness to leverage system.net.http request and content header validation
                        var hrhValidator = (HttpRequestHeaders)Activator.CreateInstance(typeof(HttpRequestHeaders), true);
                        try {
                            hrhValidator.Add(key, value);
                            requestHeaders.Add(key, value);
                            if(key.Equals("accept-encoding", StringComparison.OrdinalIgnoreCase)) {
                                var encodings = value.Split(',').Select(x => x.Trim().ToLower()).Where(x => x != "");
                                foreach(var enc in encodings)
                                    acceptEncodings.Add(enc);
                            }
                        } catch (InvalidOperationException) { //i.e. header belongs in content headers
                            var hchValidator = (HttpContentHeaders)Activator.CreateInstance(typeof(HttpContentHeaders), BindingFlags.Instance | BindingFlags.CreateInstance | BindingFlags.NonPublic, null, new[] { (object)(Func<long?>)(() => (long?)null) }, CultureInfo.CurrentCulture);
                            try {
                                hchValidator.Add(key, value);
                                contentHeaders.Add(key, value);
                            } catch (Exception e) {
                                validationErrors.Add(string.Format("Invalid header line ({0}): {1}", e.Message, line));
                            }
                        } catch (Exception e) {
                            validationErrors.Add(string.Format("Invalid header line ({0}): {1}", e.Message, line));
                        }
                    }
                }
            }

            if (validationErrors.Count > 0)
                requestModel = null;
            else {
                requestModel = new RequestModel() {
                    Url = url,
                    Method = new HttpMethod(vm.Method),
                    RequestHeaders = requestHeaders,
                    ContentHeaders = contentHeaders,
                    AcceptEncodings = acceptEncodings,
                    Body = vm.Body
                };
            }

            return validationErrors;
        }
Esempio n. 3
0
        private void bind(RequestViewModel requestVm)
        {
            txtRequestUrl.Text = requestVm.Url;

            var method = requestVm.Method;
            (rbGrpHttpMethods.FirstOrDefault(x => ((HttpMethod) x.Tag).Method == method) ?? rbHttpGet).Checked = true;

            txtRequestHeaders.Text = (requestVm.Headers ?? new string[0]).Join(txtRequestHeaders.EndOfLine.EolString);
            txtRequestBody.Text = requestVm.Body;
        }
Esempio n. 4
0
        private void addRequestResponseSnapshot(RequestViewModel requestVm, ResponseModel responseModel)
        {
            //update the model
            snapshots.Add(new RequestResponseSnapshot() { request = requestVm, response = responseModel });

            //update the view
            bindSnapshots();
        }
        private void bind(RequestViewModel requestVm)
        {
            txtRequestUrl.Text = requestVm.Url;

            var method = requestVm.Method;
            (rbGrpHttpMethods.FirstOrDefault(x => (x.Tag as string) == method)
                ?? (method.IsBlank() ? rbHttpGet : rbHttpOther)).Checked = true;

            if (rbHttpOther.Checked)
                txtHttpOther.Text = method;
            else
                txtHttpOther.Text = "";

            txtRequestHeaders.Text = (requestVm.Headers ?? new string[0]).Join(txtRequestHeaders.EndOfLine.EolString);
            txtRequestBody.Text = requestVm.Body;
        }
Esempio n. 6
0
        public static List <string> TryCreate(RequestViewModel vm, out RequestModel requestModel)
        {
            var validationErrors = new List <string>();

            Uri url = null;

            if (vm.Url.IsBlank())
            {
                validationErrors.Add("Request URL may not be empty");
            }
            else
            {
                var forgivingUrl = vm.Url.Contains("://") ? vm.Url : "http://" + vm.Url;
                if (!Uri.TryCreate(forgivingUrl, UriKind.Absolute, out url))
                {
                    validationErrors.Add("Request URL is invalid");
                }
            }

            var requestHeaders  = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);
            var contentHeaders  = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);
            var acceptEncodings = new HashSet <string>();

            foreach (var line in vm.Headers)
            {
                if (line.IsBlank())
                {
                    continue; //allow empty lines
                }
                var match = Regex.Match(line, @"^([0-9a-zA-Z-]+)\s*\:(.*)$", RegexOptions.Compiled);
                if (!match.Success)
                {
                    validationErrors.Add("Invalid header line (format incorrect): " + line);
                }
                else
                {
                    var key   = match.Groups[1].Value.Trim();
                    var value = match.Groups[2].Value.Trim();
                    if (requestHeaders.ContainsKey(key) || contentHeaders.ContainsKey(key))
                    {
                        validationErrors.Add("Invalid header line (duplicate key, comma-separate multiple values for one key): " + line);
                    }
                    else if (String.Equals(key, "authorization", StringComparison.OrdinalIgnoreCase) && !url.UserInfo.IsBlank())
                    {
                        validationErrors.Add("Invalid header line (Authorization header cannot be specified when user information is given in the url): " + line);
                    }
                    else
                    {
                        //var values = value.Split(',').Select(x => x.Trim()).ToList().AsReadOnly();
                        //some ugliness to leverage system.net.http request and content header validation
                        var hrhValidator = (HttpRequestHeaders)Activator.CreateInstance(typeof(HttpRequestHeaders), true);
                        try {
                            hrhValidator.Add(key, value);
                            requestHeaders.Add(key, value);
                            if (key.Equals("accept-encoding", StringComparison.OrdinalIgnoreCase))
                            {
                                var encodings = value.Split(',').Select(x => x.Trim().ToLower()).Where(x => x != "");
                                foreach (var enc in encodings)
                                {
                                    acceptEncodings.Add(enc);
                                }
                            }
                        } catch (InvalidOperationException) { //i.e. header belongs in content headers
                            var hchValidator = (HttpContentHeaders)Activator.CreateInstance(typeof(HttpContentHeaders), BindingFlags.Instance | BindingFlags.CreateInstance | BindingFlags.NonPublic, null, new[] { (object)(Func <long?>)(() => (long?)null) }, CultureInfo.CurrentCulture);
                            try {
                                hchValidator.Add(key, value);
                                contentHeaders.Add(key, value);
                            } catch (Exception e) {
                                validationErrors.Add(string.Format("Invalid header line ({0}): {1}", e.Message, line));
                            }
                        } catch (Exception e) {
                            validationErrors.Add(string.Format("Invalid header line ({0}): {1}", e.Message, line));
                        }
                    }
                }
            }

            if (validationErrors.Count > 0)
            {
                requestModel = null;
            }
            else
            {
                requestModel = new RequestModel()
                {
                    Url             = url,
                    Method          = new HttpMethod(vm.Method),
                    RequestHeaders  = requestHeaders,
                    ContentHeaders  = contentHeaders,
                    AcceptEncodings = acceptEncodings,
                    Body            = vm.Body
                };
            }

            return(validationErrors);
        }
Esempio n. 7
0
        public static List <string> TryCreate(RequestViewModel vm, out RequestModel requestModel)
        {
            var validationErrors = new List <string>();

            Uri url = null;

            if (vm.Url.IsBlank())
            {
                validationErrors.Add("Request URL may not be empty");
            }
            else
            {
                var knownProtocals = new[] { "http://", "https://", "ftp://", "ftps://", "file:///" };
                var forgivingUrl   = knownProtocals.Any(x => vm.Url.StartsWith(x)) ? vm.Url : "http://" + vm.Url;
                if (!Uri.TryCreate(forgivingUrl, UriKind.Absolute, out url))
                {
                    validationErrors.Add("Request URL is invalid");
                }
            }

            var requestHeaders = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);
            var contentHeaders = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            foreach (var line in vm.Headers)
            {
                if (line.IsBlank())
                {
                    continue; //allow empty lines
                }
                var match = Regex.Match(line, @"^([^\:]+)\:(.+)$", RegexOptions.Compiled);
                if (!match.Success)
                {
                    validationErrors.Add("Invalid header line (format incorrect): " + line);
                }
                else
                {
                    var key   = match.Groups[1].Value.Trim();
                    var value = match.Groups[2].Value.Trim();
                    if (key.IsBlank() || value.IsBlank())
                    {
                        validationErrors.Add("Invalid header line (key or value is blank): " + line);
                    }
                    else if (requestHeaders.ContainsKey(key) || contentHeaders.ContainsKey(key))
                    {
                        validationErrors.Add("Invalid header line (duplicate key, comma-separate multiple values for one key): " + line);
                    }
                    else
                    {
                        //var values = value.Split(',').Select(x => x.Trim()).ToList().AsReadOnly();
                        //some ugliness to leverage system.net.http request and content header validation
                        var hrhValidator = (HttpRequestHeaders)Activator.CreateInstance(typeof(HttpRequestHeaders), true);
                        try {
                            hrhValidator.Add(key, value);
                            requestHeaders.Add(key, value);
                        } catch (InvalidOperationException) { //i.e. header belongs in content headers
                            var hchValidator = (HttpContentHeaders)Activator.CreateInstance(typeof(HttpContentHeaders), BindingFlags.Instance | BindingFlags.CreateInstance | BindingFlags.NonPublic, null, new[] { (object)(Func <long?>)(() => (long?)null) }, CultureInfo.CurrentCulture);
                            try {
                                hchValidator.Add(key, value);
                                contentHeaders.Add(key, value);
                            } catch (Exception e) {
                                validationErrors.Add(string.Format("Invalid header line ({0}): {1}", e.Message, line));
                            }
                        } catch (Exception e) {
                            validationErrors.Add(string.Format("Invalid header line ({0}): {1}", e.Message, line));
                        }
                    }
                }
            }

            if (validationErrors.Count > 0)
            {
                requestModel = null;
            }
            else
            {
                requestModel = new RequestModel()
                {
                    Url            = url,
                    Method         = new HttpMethod(vm.Method),
                    RequestHeaders = requestHeaders,
                    ContentHeaders = contentHeaders,
                    Body           = vm.Body
                };
            }

            return(validationErrors);
        }