Ejemplo n.º 1
0
        public object Get(string url)
        {
            //  _writer.WriteLine("Sending GET request to " + _client.BaseAddress + url);
            ExtentTestManager.GetTest().Log(AventStack.ExtentReports.Status.Info, "Sending GET request to " + _client.BaseAddress + url);
            var message = _client.GetAsync(url).Result;

            return(message);
        }
Ejemplo n.º 2
0
        public void WriteToFile(string content, string label, string fileEXT = ".json")
        {
            var filenamereq = _random.RandomString() + fileEXT;

            File.WriteAllText(Path.Combine(_writer.Path, filenamereq), content);
            //_writer.WriteLine("<a href=\"" + filenamereq + "\">" + label + "</a>");
            ExtentTestManager.GetTest().Log(AventStack.ExtentReports.Status.Error, "<a href=\"" + filenamereq + "\">" + label + "</a>");
        }
Ejemplo n.º 3
0
        public T Delete <T>(string url)
        {
            //_writer.WriteLine("Sending DELETE request to " + _client.BaseAddress + url);
            ExtentTestManager.GetTest().Log(AventStack.ExtentReports.Status.Info, "Sending DELETE request to " + _client.BaseAddress + url);
            var message = _client.DeleteAsync(url).Result;

            return((T)DeserializeObject <T>(message));
        }
Ejemplo n.º 4
0
        public T Put <T>(string url, object payload)
        {
            // _writer.WriteLine("Sending PUT request to " + _client.BaseAddress + url);
            ExtentTestManager.GetTest().Log(AventStack.ExtentReports.Status.Info, "Sending POST request to " + _client.BaseAddress + url);
            string content = PayloadToString(payload);
            var    message = _client.PutAsync(url, PayloadToContent(payload)).Result;

            return((T)DeserializeObject <T>(message));
        }
Ejemplo n.º 5
0
        public async Task <object> SendObjectAsPayload(string url, object payload)
        {
            // _writer.WriteLine("Sending POST request to " + url);
            ExtentTestManager.GetTest().Log(AventStack.ExtentReports.Status.Info, "Sending POST request to " + _client.BaseAddress + url);
            var message = _client.PostAsync(url, new StringContent(payload.ToString(), Encoding.UTF8, "application/json")).Result;

            message.EnsureSuccessStatusCode();
            var Response = await message.Content.ReadAsStringAsync().ConfigureAwait(false);

            WriteToFile(Response, "Response");

            return(Response);
        }
Ejemplo n.º 6
0
        public T PostFile <T>(string url, string path)
        {
            // _writer.WriteLine("Sending POST request to " + _client.BaseAddress + url);
            ExtentTestManager.GetTest().Log(AventStack.ExtentReports.Status.Info, "Sending POST request to " + _client.BaseAddress + url);
            var stream  = new StreamContent(File.Open(path, FileMode.Open));
            var content = new MultipartFormDataContent();

            content.Add(stream, "file", Path.GetFileName(path));

            var message = _client.PostAsync(url, content).Result;

            return((T)DeserializeObject <T>(message));
        }
Ejemplo n.º 7
0
 private void TryAndLog(Action assertion, string message)
 {
     try
     {
         assertion.Invoke();
         ExtentTestManager.GetTest().Log(AventStack.ExtentReports.Status.Pass, message);
     }
     catch
     {
         //_writer.WriteLine(message + " Script has failed." + TestContext.CurrentContext.Test.Name);
         ExtentTestManager.GetTest().Log(AventStack.ExtentReports.Status.Error, message + " Script has failed." + TestContext.CurrentContext.Test.Name);
         AssertionsFailed = true;
     }
 }
Ejemplo n.º 8
0
        public string GetFileAndConvert(string url)
        {
            // _writer.WriteLine("Sending GET request to " + _client.BaseAddress + url);
            ExtentTestManager.GetTest().Log(AventStack.ExtentReports.Status.Info, "Sending GET request to " + _client.BaseAddress + url);
            var message  = _client.GetAsync(url).Result;
            var fileText = message.Content.ReadAsStringAsync().Result;

            fileText = JsonConvert.DeserializeObject <string>(fileText);
            byte[] stream = System.Convert.FromBase64String(fileText);
            var    path   = Path.GetTempFileName();

            File.WriteAllBytes(path, stream);
            return(path);
        }
Ejemplo n.º 9
0
        public string GetFile(string url)
        {
            // _writer.WriteLine("Sending GET request to " + _client.BaseAddress + url);
            ExtentTestManager.GetTest().Log(AventStack.ExtentReports.Status.Info, "Sending GET request to " + _client.BaseAddress + url);
            var message = _client.GetAsync(url).Result;
            var stream  = message.Content.ReadAsStreamAsync().Result;
            var path    = Path.GetTempFileName();

            using (var file = File.Create(path))
            {
                stream.Seek(0, SeekOrigin.Begin);
                stream.CopyTo(file);
                return(path);
            }
        }
Ejemplo n.º 10
0
        public async Task <TResponse> PostAsync <TResponse, TRequest>(string url, string filename)
        {
            //_writer.WriteLine("Sending POST request to " + url);
            ExtentTestManager.GetTest().Log(AventStack.ExtentReports.Status.Info, "Sending POST request to " + _client.BaseAddress + url);
            var path    = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Files", filename);
            var content = System.IO.File.ReadAllText(path);

            WriteToFile(content, "Request");
            var message = _client.PostAsync(url, new StringContent(content, Encoding.UTF8, "application/json")).Result;

            message.EnsureSuccessStatusCode();
            var Response = await message.Content.ReadAsStringAsync().ConfigureAwait(false);

            WriteToFile(Response, "Response");
            return(JsonConvert.DeserializeObject <TResponse>(Response));
        }
Ejemplo n.º 11
0
        private object DeserializeObject <T>(HttpResponseMessage message)
        {
            var content = message.Content.ReadAsStringAsync().Result;

            if (message.IsSuccessStatusCode)
            {
                object returnValue = default(T);
                if (typeof(T).IsPrimitive || typeof(T) == typeof(string))
                {
                    returnValue = Convert.ChangeType(content, typeof(T));
                }
                else
                {
                    returnValue = JsonConvert.DeserializeObject <T>(content);
                }
                //ExtentTestManager.GetTest().Log(AventStack.ExtentReports.Status.Info, "Response.");
                WriteToFile(content, "Response");

                return(returnValue);
            }
            else
            {
                object returnValue = default(T);
                if (typeof(T).IsPrimitive || typeof(T) == typeof(string))
                {
                    returnValue = Convert.ChangeType(content, typeof(T));
                }
                else
                {
                    returnValue = JsonConvert.DeserializeObject <T>(content);
                }

                ExtentTestManager.GetTest().Log(AventStack.ExtentReports.Status.Error, "Request failed.");
                WriteToFile(content, "Response");

                // _writer.WriteLine("Request failed. Response:");
                // _writer.WriteLine(content);


                return(null);
            }
        }
Ejemplo n.º 12
0
        public string CallWebService(string url, string action, string xml, Dictionary <string, string> headers = null)
        {
            // ServicePointManager.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; };

            var envelope = CreateSoapEnvelope(xml);
            var request  = CreateWebRequest(url, action, headers);

            ExtentTestManager.GetTest().Log(AventStack.ExtentReports.Status.Info, "Sending POST request");
            InsertSoapEnvelopeIntoWebRequest(envelope, request);
            WriteToFile(xml, "Request", ".xml");
            var result = request.BeginGetResponse(null, null);

            result.AsyncWaitHandle.WaitOne();

            using (var response = request.EndGetResponse(result))
            {
                using (var reader = new StreamReader(response.GetResponseStream()))
                {
                    return(reader.ReadToEnd());
                }
            }
        }
Ejemplo n.º 13
0
        public string PostXMLData(string url, string requestXml, bool logRequest = false)
        {
            // _writer.WriteLine("Sending POST request to " + _client.BaseAddress + url);
            ExtentTestManager.GetTest().Log(AventStack.ExtentReports.Status.Info, "Sending POST request to " + _client.BaseAddress + url);
            string content = requestXml;

            if (logRequest)
            {
                WriteToFile(content, "Request");
            }

            try
            {
                var message = _client.PostAsync(url, StringToContentXML(content)).Result;
            }
            catch (Exception e)
            {
                _writer.WriteLine("Request failed: " + e.InnerException.Message);
                throw e;
            }

            return(null);
        }
Ejemplo n.º 14
0
        public object Post <T>(string url, object payload, bool isNegTestCase, bool logRequest = false)
        {
            // _writer.WriteLine("Sending POST request to " + _client.BaseAddress + url);
            ExtentTestManager.GetTest().Log(AventStack.ExtentReports.Status.Info, "Sending POST request to " + _client.BaseAddress + url);
            string content = PayloadToString(payload);

            if (logRequest)
            {
                WriteToFile(content, "Request");
            }

            try
            {
                var message = _client.PostAsync(url, StringToContent(content)).Result;
                return((object)DeserializeObject <T>(message, isNegTestCase));
            }
            catch (Exception e)
            {
                //_writer.WriteLine("Request failed: " + e.InnerException.Message);
                ExtentTestManager.GetTest().Log(AventStack.ExtentReports.Status.Error, "Request failed: " + e.Message);
                throw e;
            }
        }
Ejemplo n.º 15
0
 public void log(string message)
 {
     ExtentTestManager.GetTest().Log(AventStack.ExtentReports.Status.Info, message);
 }
Ejemplo n.º 16
0
        public Http(string baseAddress, IDictionary <string, string> headers, IEnumerable <string> cookies, Writer writer, RandomHelper random, ExtentTestManager etm = null)
        {
            _writer = writer;
            _random = random;


            if (cookies != null)
            {
                var cookieContainer = new CookieContainer();
                foreach (var cookie in cookies)
                {
                    var tokCookie = cookie.Split(';');

                    var dictionary = new List <KeyValuePair <string, string> >();
                    foreach (var token in tokCookie)
                    {
                        var tokToken = token.Split('=');
                        if (tokToken.Count() == 2)
                        {
                            dictionary.Add(new KeyValuePair <string, string>(tokToken[0], tokToken[1]));
                        }
                    }

                    var keyValue = dictionary.First(d => d.Key.ToLower().Trim() != "path" && d.Key.ToLower().Trim() != "domain");
                    //cookieContainer.Add(new Cookie(keyValue.Key, keyValue.Value, dictionary.First(d => d.Key.ToLower().Trim() == "path").Value, dictionary.First(d => d.Key.ToLower().Trim() == "domain").Value));
                    cookieContainer.Add(new Uri(baseAddress), new Cookie(keyValue.Key, keyValue.Value));
                    //{ Domain = "DomainName" }
                    //headers.Add(keyValue.Key, keyValue.Value);
                    //cookieContainer.Add(new Cookie(keyValue.Key.ToString(), keyValue.Value.ToString()));
                }

                var handler = new HttpClientHandler()
                {
                    CookieContainer = cookieContainer
                };
                _client = new HttpClient(handler)
                {
                    BaseAddress = new Uri(baseAddress)
                };
            }
            else
            {
                _client = new HttpClient()
                {
                    BaseAddress = new Uri(baseAddress)
                };
            }

            SetHeaders(headers);
        }