using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Net.Http; using Xunit; using Moq; using Moq.Protected; using FluentAssertions; using System.Net; using SpotifyAPI.Web; namespace Selector.Tests { public class WebHookTest { [Fact(Skip = "Not working atm")] public async Task TestHttpClientUsed() { var msg = new HttpResponseMessage(HttpStatusCode.OK); var httpHandlerMock = new Mock(); httpHandlerMock.Protected() .Setup>("SendAsync", ItExpr.IsAny(), ItExpr.IsAny()) .ReturnsAsync(msg); var watcherMock = new Mock(); watcherMock.SetupAdd(w => w.ItemChange += It.IsAny>()); watcherMock.SetupRemove(w => w.ItemChange -= It.IsAny>()); var link = "https://link"; var content = new StringContent(""); var config = new WebHookConfig() { Url = link, Content = content, }; var http = new HttpClient(httpHandlerMock.Object); var webHook = new WebHook(watcherMock.Object, http, config); webHook.Subscribe(); watcherMock.Raise(w => w.ItemChange += null, this, new ListeningChangeEventArgs()); await Task.Delay(100); httpHandlerMock.Protected().Verify>("SendAsync", Times.Once(), ItExpr.IsAny(), ItExpr.IsAny()); } [Theory] [InlineData(200, true, true)] [InlineData(404, true, false)] [InlineData(500, true, false)] public async Task TestEventFiring(int code, bool predicate, bool successful) { var msg = new HttpResponseMessage(Enum.Parse(code.ToString())); var httpHandlerMock = new Mock(); httpHandlerMock.Protected() .Setup>("SendAsync", ItExpr.IsAny(), ItExpr.IsAny()) .ReturnsAsync(msg); var watcherMock = new Mock(); var link = "https://link"; var content = new StringContent(""); var config = new WebHookConfig() { Url = link, Content = content, }; var http = new HttpClient(httpHandlerMock.Object); bool predicateEvent = false, successfulEvent = false, failedEvent = false; var webHook = new WebHook(watcherMock.Object, http, config); webHook.PredicatePass += (o, e) => { predicateEvent = predicate; }; webHook.SuccessfulRequest += (o, e) => { successfulEvent = successful; }; webHook.FailedRequest += (o, e) => { failedEvent = !successful; }; await webHook.AsyncCallback(ListeningChangeEventArgs.From(new (), new (), new())); predicateEvent.Should().Be(predicate); successfulEvent.Should().Be(successful); failedEvent.Should().Be(!successful); } } }