Esempio n. 1
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));
        }
Esempio n. 2
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);
        }
Esempio n. 3
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>");
        }
Esempio 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));
        }
Esempio n. 5
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));
        }
Esempio n. 6
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);
        }
Esempio 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;
     }
 }
Esempio 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);
        }
Esempio 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);
            }
        }
Esempio 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));
        }
Esempio 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);
            }
        }
Esempio 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());
                }
            }
        }
Esempio 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);
        }
Esempio 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;
            }
        }
Esempio n. 15
0
 public void log(string message)
 {
     ExtentTestManager.GetTest().Log(AventStack.ExtentReports.Status.Info, message);
 }