Ejemplo n.º 1
0
        public void RemoveWebHookShouldSucceed()
        {
            WebHook[] inputWebHooks = new WebHook[]
            {
                new WebHook(HookEventTypes.PostDeployment, "http://www.gothere.com/aaabbbbbbaaa"),
                new WebHook(HookEventTypes.PostDeployment, "http://www.gothere.com/aaabbbbbbaaa2"),
                new WebHook(HookEventTypes.PostDeployment, "http://www.gothere.com/aaabbbbbbaaa3")
            };

            AddWebHooks(inputWebHooks);

            WebHook webHookToRemove = _webHooksManager.WebHooks.First(h => h.HookAddress == "http://www.gothere.com/aaabbbbbbaaa2");
            _webHooksManager.RemoveWebHook(webHookToRemove.Id);

            WebHook[] expectedWebHooks = new WebHook[]
            {
                new WebHook(HookEventTypes.PostDeployment, "http://www.gothere.com/aaabbbbbbaaa"),
                new WebHook(HookEventTypes.PostDeployment, "http://www.gothere.com/aaabbbbbbaaa3"),
            };
            AssertWebHooks(expectedWebHooks);

            webHookToRemove = _webHooksManager.WebHooks.First(h => h.HookAddress == "http://www.gothere.com/aaabbbbbbaaa");
            _webHooksManager.RemoveWebHook(webHookToRemove.Id);
            expectedWebHooks = new WebHook[]
            {
                new WebHook(HookEventTypes.PostDeployment, "http://www.gothere.com/aaabbbbbbaaa3")
            };
            AssertWebHooks(expectedWebHooks);
        }
Ejemplo n.º 2
0
        public void AddWebHookShouldSucceed()
        {
            WebHook[] inputWebHooks = new WebHook[]
            {
                new WebHook(HookEventTypes.PostDeployment, "http://www.gothere.com/aaabbbbbbaaa")
            };

            WebHook[] expectedWebHooks = inputWebHooks;

            IEnumerable<WebHook> webHooksAdded = AddWebHooks(inputWebHooks);

            AssertWebHooks(expectedWebHooks);

            AssertWebHook(expectedWebHooks[0], webHooksAdded.First());
        }
Ejemplo n.º 3
0
 public HttpResponseMessage Subscribe(WebHook webHook)
 {
     try
     {
         WebHook webHookAdded = _hooksManager.AddWebHook(webHook);
         return Request.CreateResponse(HttpStatusCode.Created, webHookAdded);
     }
     catch (ConflictException)
     {
         _tracer.Trace("Web hook with address {0} already exists".FormatCurrentCulture(webHook.HookAddress));
         return Request.CreateResponse(HttpStatusCode.Conflict);
     }
     catch (LockOperationException)
     {
         _tracer.Trace("Failed to acquire lock while subscribing {0}".FormatCurrentCulture(webHook.HookAddress));
         return Request.CreateResponse(HttpStatusCode.InternalServerError);
     }
 }
Ejemplo n.º 4
0
        public WebHook AddWebHook(WebHook webHook)
        {
            using (_tracer.Step("WebHooksManager.AddWebHook"))
            {
                if (!Uri.IsWellFormedUriString(webHook.HookAddress, UriKind.RelativeOrAbsolute))
                {
                    throw new FormatException(Resources.Error_InvalidHookAddress.FormatCurrentCulture(webHook.HookAddress));
                }

                WebHook createdWebHook = null;

                bool lockAcquired = _hooksLock.TryLockOperation(() =>
                {
                    var webHooks = new List<WebHook>(ReadWebHooksFromFile());
                    WebHook existingWebHook = webHooks.FirstOrDefault(h => String.Equals(h.HookAddress, webHook.HookAddress, StringComparison.OrdinalIgnoreCase));

                    if (existingWebHook == null)
                    {
                        // if web hook doesn't exist (by address) then add it
                        createdWebHook = new WebHook(webHook.HookEventType, webHook.HookAddress, id: DateTime.UtcNow.Ticks.ToString(), insecureSsl: webHook.InsecureSsl);
                        webHooks.Add(createdWebHook);
                        SaveHooksToFile(webHooks);

                        _tracer.Trace("Added web hook: type - {0}, address - {1}", createdWebHook.HookEventType, createdWebHook.HookAddress);
                    }
                    else if (String.Equals(webHook.HookEventType, existingWebHook.HookEventType, StringComparison.OrdinalIgnoreCase))
                    {
                        // if web hook exist with the same hook event type, return the existing one
                        createdWebHook = existingWebHook;
                    }
                    else
                    {
                        // if web hook exists but with a different hook event type then throw a conflict exception
                        throw new ConflictException();
                    }
                }, LockTimeout);

                VerifyLockAcquired(lockAcquired);

                return createdWebHook;
            }
        }
Ejemplo n.º 5
0
 private static async Task VerifyWebHook(ApplicationManager hookAppManager, WebHook expectedWebHook)
 {
     TestTracer.Trace("Verify web hook");
     WebHook actualWebHooks = await hookAppManager.WebHooksManager.GetWebHookAsync(expectedWebHook.Id);
     Assert.Equal(expectedWebHook.HookAddress, actualWebHooks.HookAddress);
     Assert.Equal(expectedWebHook.HookEventType, actualWebHooks.HookEventType);
 }
Ejemplo n.º 6
0
        private static HttpClient CreateHttpClient(WebHook webHook)
        {
            var webRequestHandler = new WebRequestHandler();

            var hookAddress = new Uri(webHook.HookAddress);
            string userInfo = hookAddress.UserInfo;
            if (userInfo != null)
            {
                string[] userInfos = userInfo.Split(':');
                if (userInfos.Length == 2)
                {
                    webRequestHandler.Credentials = new NetworkCredential(userInfos[0], userInfos[1]);
                }
            }

            if (webHook.InsecureSsl)
            {
                webRequestHandler.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
            }

            return new HttpClient(webRequestHandler)
            {
                Timeout = TimeSpan.FromSeconds(10)
            };
        }
Ejemplo n.º 7
0
        private async Task PublishToHookAsync(WebHook webHook, string jsonString)
        {
            using (HttpClient httpClient = CreateHttpClient(webHook))
            {
                using (var content = new StringContent(jsonString, System.Text.Encoding.UTF8, "application/json"))
                {
                    try
                    {
                        _tracer.Trace("Publish {0}#{1} to address - {2}, json - {3}, insecure - {4}", webHook.HookEventType, webHook.Id, webHook.HookAddress, jsonString, webHook.InsecureSsl);

                        webHook.LastPublishDate = DateTime.UtcNow;
                        webHook.LastContext = jsonString;

                        using (HttpResponseMessage response = await httpClient.PostAsync(webHook.HookAddress, content))
                        {
                            _tracer.Trace("Publish {0}#{1} to address - {2}, response - {3}", webHook.HookEventType, webHook.Id, webHook.HookAddress, response.StatusCode);

                            // Handle 410 responses by removing the web hook
                            if (response.StatusCode == HttpStatusCode.Gone)
                            {
                                RemoveWebHookNotUnderLock(webHook.Id);
                            }

                            webHook.LastPublishStatus = response.StatusCode.ToString();
                            webHook.LastPublishReason = response.ReasonPhrase;
                        }
                    }
                    catch (Exception ex)
                    {
                        _tracer.Trace("Error while publishing hook - {0}#{1}, to address - {1}", webHook.HookEventType, webHook.Id, webHook.HookAddress);
                        _tracer.TraceError(ex);

                        webHook.LastPublishStatus = "Failure";
                        webHook.LastPublishReason = ex.Message;
                    }
                }
            }
        }
Ejemplo n.º 8
0
        private static HttpClient CreateHttpClient(WebHook webHook)
        {
            HttpClient httpClient = null;

            if (webHook.InsecureSsl)
            {
                var webRequestHandler = new WebRequestHandler()
                {
                    ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true
                };

                httpClient = new HttpClient(webRequestHandler);
            }
            else
            {
                httpClient = new HttpClient();
            }

            httpClient.Timeout = TimeSpan.FromSeconds(10);

            return httpClient;
        }
Ejemplo n.º 9
0
        public void AddSameAddressWebHookWithDifferentHookEventTypeShouldThrowConflictException()
        {
            WebHook[] inputWebHooks = new WebHook[]
            {
                new WebHook(HookEventTypes.PostDeployment, "http://www.gothere.com/aaabbbbbbaaa"),
                new WebHook(HookEventTypes.PostDeployment, "http://www.gothere.com/aaabbbbbbaaa2"),
                new WebHook(HookEventTypes.PostDeployment + "_NewEvent", "http://www.gothere.com/aaabbbbbbaaa"),
            };

            WebHook[] expectedWebHooks = new WebHook[]
            {
                new WebHook(HookEventTypes.PostDeployment, "http://www.gothere.com/aaabbbbbbaaa"),
                new WebHook(HookEventTypes.PostDeployment, "http://www.gothere.com/aaabbbbbbaaa2"),
            };

            Assert.Throws<ConflictException>(() => AddWebHooks(inputWebHooks));

            AssertWebHooks(expectedWebHooks);
        }
Ejemplo n.º 10
0
        public void Add3WebHooksShouldSucceed()
        {
            WebHook[] inputWebHooks = new WebHook[]
            {
                new WebHook(HookEventTypes.PostDeployment, "http://www.gothere.com/aaabbbbbbaaa"),
                new WebHook(HookEventTypes.PostDeployment, "http://www.gothere.com/aaabbbbbbaaa2", "111", insecureSsl: false),
                new WebHook(HookEventTypes.PostDeployment, "http://www.gothere.com/aaabbbbbbaaa3", "111", insecureSsl: true)
            };

            WebHook[] expectedWebHooks = inputWebHooks;

            AddWebHooks(inputWebHooks);

            AssertWebHooks(expectedWebHooks);
        }
Ejemplo n.º 11
0
 private static void AssertWebHook(WebHook expectedWebHook, WebHook actualWebHook)
 {
     Assert.Equal(expectedWebHook.HookEventType, actualWebHook.HookEventType);
     Assert.Equal(expectedWebHook.HookAddress, actualWebHook.HookAddress);
     Assert.Equal(expectedWebHook.InsecureSsl, actualWebHook.InsecureSsl);
     Assert.True(!String.IsNullOrEmpty(actualWebHook.Id), "Received empty id for web hook");
 }
Ejemplo n.º 12
0
        private IEnumerable<WebHook> AddWebHooks(WebHook[] webHooks)
        {
            List<WebHook> webHooksAdded = new List<WebHook>();

            foreach (var webHook in webHooks)
            {
                WebHook webHookAdded = _webHooksManager.AddWebHook(webHook);
                webHooksAdded.Add(webHookAdded);
                Thread.Sleep(1);
            }

            return webHooksAdded;
        }
Ejemplo n.º 13
0
 private void AssertWebHooks(WebHook[] expectedWebHooks)
 {
     IEnumerable<WebHook> webHooks = _webHooksManager.WebHooks;
     Assert.Equal(expectedWebHooks.Length, webHooks.Count());
     for (int i = 0; i < expectedWebHooks.Length; i++)
     {
         WebHook expectedWebHook = expectedWebHooks[i];
         WebHook actualWebHook = webHooks.Single(w => String.Equals(w.HookAddress, expectedWebHook.HookAddress, StringComparison.OrdinalIgnoreCase));
         AssertWebHook(expectedWebHooks[i], actualWebHook);
         AssertWebHook(expectedWebHooks[i], _webHooksManager.GetWebHook(actualWebHook.Id));
     }
 }
Ejemplo n.º 14
0
        public void AddSameAddressWebHookWithSameHookEventTypeShouldSucceed()
        {
            WebHook[] inputWebHooks = new WebHook[]
            {
                new WebHook(HookEventTypes.PostDeployment, "http://www.gothere.com/aaabbbbbbaaa"),
                new WebHook(HookEventTypes.PostDeployment, "http://www.gothere.com/aaabbbbbbaaa2"),
                new WebHook(HookEventTypes.PostDeployment, "http://www.gothere.com/aaabbbbbbaaa"),
            };

            WebHook[] expectedWebHooks = new WebHook[]
            {
                new WebHook(HookEventTypes.PostDeployment, "http://www.gothere.com/aaabbbbbbaaa"),
                new WebHook(HookEventTypes.PostDeployment, "http://www.gothere.com/aaabbbbbbaaa2"),
            };

            AddWebHooks(inputWebHooks);

            AssertWebHooks(expectedWebHooks);
        }