Example #1
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="_s"></param>
        /// <param name="_file"></param>
        public static bool ProcessPostmanFile(Server _s, String _file)
        {
            // Read file to variable
            String content = "";
            try
            {
                using (StreamReader sr = new StreamReader(_file))
                {
                    content = sr.ReadToEnd();
                }
            }
            catch
            {
                MessageDialog.ShowError(Director.Properties.Resources.CannotReadFromFile);
                return false;
            }

            PostmanServer s = new PostmanServer();
            try
            {
                JObject o = JObject.Parse(content);
                s.name = (String) o["name"];
                s.requests = new List<PostmanRequest>();

                foreach (JObject re in o["requests"])
                {
                    var tmp = new PostmanRequest();
                    tmp.name = (String)re["name"];
                    tmp.method = (String)re["method"];
                    tmp.url = (String)re["url"];
                    tmp.headers = (String)re["headers"];

                    // Data
                    if (((String)re["dataMode"]) == "raw")
                    {
                        tmp.data = (String)re["data"];
                    }

                    s.requests.Add(tmp);
                }
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
                MessageDialog.ShowError(Director.Properties.Resources.InvalidPostmanFile);
                return false;
            }

            // Set server header?
            if (_s.Name == null || _s.Name.Length == 0)
                _s.Name = s.name;

            if (_s.Url == null || _s.Url.Length == 0)
            {
                _s.Url = s.requests.Count > 0 ? s.requests[0].url : "";
            }

            // Create scenario
            var scNew = _s.CreateNewScenario();
            scNew.Name = s.name;

            foreach (var pr in s.requests)
            {
                var tmp = scNew.CreateNewRequest();
                tmp.Name = pr.name;
                tmp.HTTP_METHOD = pr.method;
                tmp.RequestTemplateType = ContentTypeUtils.GuessContentType (pr.data);
                tmp.RequestTemplate = pr.data;
                tmp.Url = pr.url;

                // Create headers
                foreach (var h in pr.HeaderList)
                {
                    tmp.Headers.Add(new Header()
                    {
                        Name = h.name, Value = h.value
                    });
                }
            }

            return true;
        }
Example #2
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="_s"></param>
        /// <param name="_file"></param>
        public static bool ProcessPostmanFile(Server _s, String _file)
        {
            // Read file to variable
            String content = "";

            try
            {
                using (StreamReader sr = new StreamReader(_file))
                {
                    content = sr.ReadToEnd();
                }
            }
            catch
            {
                MessageDialog.ShowError(Director.Properties.Resources.CannotReadFromFile);
                return(false);
            }

            PostmanServer s = new PostmanServer();

            try
            {
                JObject o = JObject.Parse(content);
                s.name     = (String)o["name"];
                s.requests = new List <PostmanRequest>();

                foreach (JObject re in o["requests"])
                {
                    var tmp = new PostmanRequest();
                    tmp.name    = (String)re["name"];
                    tmp.method  = (String)re["method"];
                    tmp.url     = (String)re["url"];
                    tmp.headers = (String)re["headers"];

                    // Data
                    if (((String)re["dataMode"]) == "raw")
                    {
                        tmp.data = (String)re["data"];
                    }

                    s.requests.Add(tmp);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                MessageDialog.ShowError(Director.Properties.Resources.InvalidPostmanFile);
                return(false);
            }

            // Set server header?
            if (_s.Name == null || _s.Name.Length == 0)
            {
                _s.Name = s.name;
            }

            if (_s.Url == null || _s.Url.Length == 0)
            {
                _s.Url = s.requests.Count > 0 ? s.requests[0].url : "";
            }

            // Create scenario
            var scNew = _s.CreateNewScenario();

            scNew.Name = s.name;

            foreach (var pr in s.requests)
            {
                var tmp = scNew.CreateNewRequest();
                tmp.Name                = pr.name;
                tmp.HTTP_METHOD         = pr.method;
                tmp.RequestTemplateType = ContentTypeUtils.GuessContentType(pr.data);
                tmp.RequestTemplate     = pr.data;
                tmp.Url = pr.url;

                // Create headers
                foreach (var h in pr.HeaderList)
                {
                    tmp.Headers.Add(new Header()
                    {
                        Name = h.name, Value = h.value
                    });
                }
            }

            return(true);
        }