public static bool DeleteApiKey(string serverUri, string keyId, HttpClient client)
        {
            string apiKeysUrl = $"{serverUri}/security/api-keys";
            string apiKeyUrl  = $"{apiKeysUrl}/{keyId}";

            var res = client.GetAsync(apiKeysUrl).Result;

            IEnumerable <string> values;

            res.Headers.TryGetValues(HeaderNames.XSRF_TOKEN, out values);

            if (values.Count() < 1)
            {
                throw new Exception("Can't delete Api Key");
            }

            string value = values.First();

            HttpRequestMessage message = new HttpRequestMessage(new HttpMethod("DELETE"), apiKeyUrl);

            message.Headers.Add(HeaderNames.XSRF_TOKEN, value);

            res = client.SendAsync(message).Result;

            return(Globals.Success(res));
        }
Beispiel #2
0
        public static bool Get(this HttpClient client, string uri, out string result)
        {
            HttpResponseMessage responseMessage = client.GetAsync(uri).Result;

            result = responseMessage.Content.ReadAsStringAsync().Result;
            return(Globals.Success(responseMessage));
        }
Beispiel #3
0
        public static bool Post(this HttpClient client, string uri, string body, out string result)
        {
            HttpContent         content  = new StringContent(body, Encoding.UTF8, "application/json");
            HttpResponseMessage response = client.PostAsync(uri, content).Result;

            result = response.Content.ReadAsStringAsync().Result;

            return(Globals.Success(response));
        }
        public static bool DeleteAppPool(HttpClient client, string poolUri)
        {
            if (!AppPoolExists(client, poolUri))
            {
                throw new Exception("Can't delete test site because it doesn't exist.");
            }
            HttpResponseMessage response = client.DeleteAsync(poolUri).Result;

            return(Globals.Success(response));
        }
Beispiel #5
0
        public static bool DeleteVdir(HttpClient client, string vdirUri)
        {
            if (!VdirExists(client, vdirUri))
            {
                throw new Exception("Can't delete test application because it doesn't exist.");
            }
            HttpResponseMessage response = client.DeleteAsync(vdirUri).Result;

            return(Globals.Success(response));
        }
        public void CanCommunicate(int runCount)
        {
            using (HttpClient client = ApiHttpClient.Create($"{Globals.TEST_SERVER}:{Globals.TEST_PORT}")) {
                HttpResponseMessage res = null;

                for (int i = 0; i < runCount; i++)
                {
                    res = client.GetAsync(API_URL).Result;
                    Assert.True(Globals.Success(res));
                }
            }
        }
Beispiel #7
0
        public static bool Patch(this HttpClient client, string uri, string body, out string result)
        {
            HttpContent        content        = new StringContent(body, Encoding.UTF8, "application/json");
            HttpRequestMessage requestMessage = new HttpRequestMessage(new HttpMethod("PATCH"), uri)
            {
                Content = content
            };

            HttpResponseMessage response = client.SendAsync(requestMessage).Result;

            result = response.Content.ReadAsStringAsync().Result;

            return(Globals.Success(response));
        }
        public static bool CreateSite(HttpClient client, string testSite, out JObject site)
        {
            site = null;
            HttpContent         content  = new StringContent(testSite, Encoding.UTF8, "application/json");
            HttpResponseMessage response = client.PostAsync(SITE_URL, content).Result;

            if (!Globals.Success(response))
            {
                return(false);
            }

            site = JsonConvert.DeserializeObject <JObject>(response.Content.ReadAsStringAsync().Result);

            return(true);
        }
Beispiel #9
0
        public static bool CreateVdir(HttpClient client, string virtualDirectory, out JObject result)
        {
            result = null;

            HttpContent         content  = new StringContent(virtualDirectory, Encoding.UTF8, "application/json");
            HttpResponseMessage response = client.PostAsync(VDIR_URL, content).Result;

            if (!Globals.Success(response))
            {
                return(false);
            }

            result = JsonConvert.DeserializeObject <JObject>(response.Content.ReadAsStringAsync().Result);

            return(true);
        }
        public static bool CreateAppPool(HttpClient client, string testPool, out string id)
        {
            id = null;

            HttpContent         content  = new StringContent(TEST_APP_POOL, Encoding.UTF8, "application/json");
            HttpResponseMessage response = client.PostAsync(APP_POOLS_URL, content).Result;

            if (!Globals.Success(response))
            {
                return(false);
            }

            dynamic appPool = JsonConvert.DeserializeObject <JObject>(response.Content.ReadAsStringAsync().Result);

            id = DynamicHelper.Value(appPool.id);

            return(true);
        }
Beispiel #11
0
        public void EnsureTransactionBlocks()
        {
            using (HttpClient client = ApiHttpClient.Create($"{Globals.TEST_SERVER}:{Globals.TEST_PORT}")) {
                // Ensure a site with the name of the transaction test site does not exist.
                Sites.EnsureNoSite(client, TRANSACTION_SITE_NAME);

                // Create the site we will be manipulating to test transactions
                JObject site = Sites.CreateSite(client, TRANSACTION_SITE_NAME, 50000, @"c:\sites\test_site");
                Assert.NotNull(site);

                // Create a transaction
                string res = null;
                Assert.True(client.Post(TRANSACTIONS_URL, "{}", out res));

                JObject transaction = JsonConvert.DeserializeObject <JObject>(res);

                // Try to delete the site without specifying transaction
                var response = client.DeleteAsync(Utils.Self(site)).Result;

                // Not specifying the transaction should prevent us from deleting the site
                Assert.True(!Globals.Success(response));

                // Create a request to abort the transaction
                var req = new HttpRequestMessage(new HttpMethod("PATCH"), Utils.Self(transaction));

                // Specify the current transaction in the headers
                req.Headers.Add(TRANSACTION_HEADER, transaction.Value <string>("id"));
                req.Content = new StringContent(JsonConvert.SerializeObject(new { state = "aborted" }), Encoding.UTF8, "application/json");

                // Patch the transaction to abort it
                response = client.SendAsync(req).Result;

                Assert.True(Globals.Success(response));

                // Remove the test site
                Sites.EnsureNoSite(client, TRANSACTION_SITE_NAME);
            }
        }
        public static bool TestApplicationExists(HttpClient client, string applicationUri)
        {
            HttpResponseMessage responseMessage = client.GetAsync(applicationUri).Result;

            return(Globals.Success(responseMessage));
        }
Beispiel #13
0
        public static bool VdirExists(HttpClient client, string vdirUri)
        {
            HttpResponseMessage responseMessage = client.GetAsync(vdirUri).Result;

            return(Globals.Success(responseMessage));
        }
        private static bool AppPoolExists(HttpClient client, string poolUri)
        {
            HttpResponseMessage responseMessage = client.GetAsync(poolUri).Result;

            return(Globals.Success(responseMessage));
        }
Beispiel #15
0
        public void UseTransactionManipulateSite()
        {
            using (HttpClient client = ApiHttpClient.Create($"{Globals.TEST_SERVER}:{Globals.TEST_PORT}")) {
                // Ensure a site with the name of the transaction test site does not exist.
                Sites.EnsureNoSite(client, TRANSACTION_SITE_NAME);

                // Create the site we will be manipulating to test transactions
                JObject site = Sites.CreateSite(client, TRANSACTION_SITE_NAME, 50000, @"c:\sites\test_site");
                Assert.NotNull(site);

                // Cache the value of the property we will be manipulating through a transaciton
                bool cachedAutoStart = site.Value <bool>("server_auto_start");

                // Create a transaction
                string res = null;
                Assert.True(client.Post(TRANSACTIONS_URL, "{}", out res));

                JObject transaction = JsonConvert.DeserializeObject <JObject>(res);

                // Create a request to manipulate the test site
                HttpRequestMessage req = new HttpRequestMessage(new HttpMethod("PATCH"), Utils.Self(site));

                // Add the transaction header to specify that we want to utilize the transaction in our patch request
                req.Headers.Add(TRANSACTION_HEADER, transaction.Value <string>("id"));
                site["server_auto_start"] = !site.Value <bool>("server_auto_start");
                req.Content = new StringContent(JsonConvert.SerializeObject(site), Encoding.UTF8, "application/json");

                // Patch the test site using a transaction
                var response = client.SendAsync(req).Result;

                Assert.True(Globals.Success(response));

                site = JsonConvert.DeserializeObject <JObject>(response.Content.ReadAsStringAsync().Result);

                // Check the value of the server auto start property of the test site after manipulating it through transaction
                bool transactionAutoStart = site.Value <bool>("server_auto_start");

                // Value should be different than the original value that we cached
                Assert.True(transactionAutoStart != cachedAutoStart);

                // Get the site without specifying the transaction, which means it should look the same as the original.
                Assert.True(client.Get(Utils.Self(site), out res));
                site = JsonConvert.DeserializeObject <JObject>(res);

                bool nonTransactionAutoStart = site.Value <bool>("server_auto_start");

                // Value should be the same as original value that we cached
                Assert.True(nonTransactionAutoStart == cachedAutoStart);

                // Create a request to commit the transaction
                req = new HttpRequestMessage(new HttpMethod("PATCH"), Utils.Self(transaction));

                // Specify the current transaction in the headers
                req.Headers.Add(TRANSACTION_HEADER, transaction.Value <string>("id"));
                req.Content = new StringContent(JsonConvert.SerializeObject(new { state = "committed" }), Encoding.UTF8, "application/json");

                // Patch the transaction to commit it
                response = client.SendAsync(req).Result;

                Assert.True(Globals.Success(response));

                // Get the transactions for the webserver
                Assert.True(client.Get(TRANSACTIONS_URL, out res));

                JArray transactions = JsonConvert.DeserializeObject <JObject>(res).Value <JArray>("transactions");

                // There should be no transactions after we commit ours
                Assert.True(transactions.Count == 0);

                // Get the site after committing the transaction so the server auto start should retain the manipulated value
                Assert.True(client.Get(Utils.Self(site), out res));
                site = JsonConvert.DeserializeObject <JObject>(res);

                bool commitedAutoStart = site.Value <bool>("server_auto_start");

                // Value should be different than the original value that we cached
                Assert.True(commitedAutoStart != cachedAutoStart);

                // Remove the test site we created
                Sites.EnsureNoSite(client, TRANSACTION_SITE_NAME);
            }
        }
        private static bool SiteExists(HttpClient client, string siteUri)
        {
            HttpResponseMessage responseMessage = client.GetAsync(siteUri).Result;

            return(Globals.Success(responseMessage));
        }
Beispiel #17
0
        public static bool Delete(this HttpClient client, string uri)
        {
            HttpResponseMessage response = client.DeleteAsync(uri).Result;

            return(Globals.Success(response));
        }