public async Task ProcessActionTimeoutShouldSucceed() { // Arrange var settings = new ProcessHttpSettings { Uri = new Uri("https://blip.ai"), Method = HttpMethod.Post.ToString(), Body = "{\"plan\":\"Premium\",\"details\":{\"address\": \"Rua X\"}}", Headers = new Dictionary <string, string>() { { "Content-Type", "application/json" }, { "Authorization", "Key askçjdhaklsdghasklgdasd=" } }, ResponseBodyVariable = "httpResultBody", ResponseStatusVariable = "httpResultStatus", }; var target = GetTarget(); HttpClient.SendAsync(Arg.Any <HttpRequestMessage>(), Arg.Any <CancellationToken>()) .Returns(async token => { await Task.Delay(TimeSpan.FromMilliseconds(20), token.Arg <CancellationToken>()); return(Substitute.For <HttpResponseMessage>()); }); // Act using (var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(10))) await target.ExecuteAsync(Context, JObject.FromObject(settings), cts.Token); //Assert await Context.DidNotReceive().SetVariableAsync(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <CancellationToken>()); }
public async Task ProcessAction_CheckConfigurationVariableValues(string botIdentifierVariableValue, bool expectedResult) { // Arrange const string userIdentity = "*****@*****.**"; const string botIdentity = "*****@*****.**"; const string botIdentifierConfigVariableName = "processHttpAddBotIdentityToRequestHeader"; Context.Flow.Configuration.Add(botIdentifierConfigVariableName, botIdentifierVariableValue); Context.UserIdentity.Returns(Identity.Parse(userIdentity)); Context.OwnerIdentity.Returns(Identity.Parse(botIdentity)); var settings = new ProcessHttpSettings { Uri = new Uri("https://blip.ai"), Method = HttpMethod.Post.ToString(), Body = "{\"plan\":\"Premium\",\"details\":{\"address\": \"Rua X\"}}", Headers = new Dictionary <string, string>() { { "Content-Type", "application/json" }, { "Authorization", "Key askçjdhaklsdghasklgdasd=" } }, ResponseBodyVariable = "httpResultBody", ResponseStatusVariable = "httpResultStatus", }; var target = GetTarget(); var httpResponseMessage = new HttpResponseMessage() { StatusCode = HttpStatusCode.Accepted, Content = new StringContent("Some result") }; HttpRequestMessage requestMessage = null; HttpClient .SendAsync(Arg.Do <HttpRequestMessage>(m => requestMessage = m), Arg.Any <CancellationToken>()) .ReturnsForAnyArgs(httpResponseMessage); // Act await target.ExecuteAsync(Context, JObject.FromObject(settings), CancellationToken); // Assert requestMessage.Headers.Contains("X-Blip-Bot").ShouldBe(expectedResult); if (expectedResult) { requestMessage.Headers.GetValues("X-Blip-Bot").First().ShouldBe(botIdentity); } await HttpClient.Received(1).SendAsync( Arg.Is <HttpRequestMessage>( h => h.RequestUri.Equals(settings.Uri)), Arg.Any <CancellationToken>()); }
public async Task ProcessPostActionShouldSucceed() { //Arrange var settings = new ProcessHttpSettings { Uri = new Uri("https://blip.ai"), Method = HttpMethod.Post.ToString(), Body = "{\"plan\":\"Premium\",\"details\":{\"address\": \"Rua X\"}}", Headers = new Dictionary <string, string>() { { "Content-Type", "application/json" }, { "Authorization", "Key askçjdhaklsdghasklgdasd=" } }, ResponseBodyVariable = "httpResultBody", ResponseStatusVariable = "httpResultStatus", }; var target = GetTarget(); var httpResponseMessage = new HttpResponseMessage() { StatusCode = HttpStatusCode.Accepted, Content = new StringContent("Some result") }; HttpClient.SendAsync(Arg.Any <HttpRequestMessage>(), CancellationToken).Returns(httpResponseMessage); //Act await target.ExecuteAsync(Context, JObject.FromObject(settings), CancellationToken); //Assert await HttpClient.Received(1).SendAsync( Arg.Is <HttpRequestMessage>( h => h.RequestUri.Equals(settings.Uri)), CancellationToken); await Context.Received(1).SetVariableAsync(settings.ResponseStatusVariable, ((int)HttpStatusCode.Accepted).ToString(), CancellationToken); await Context.Received(1).SetVariableAsync(settings.ResponseBodyVariable, "Some result", CancellationToken); }
public async Task ProcessPostActionWithoutValidSettingsShouldFailed() { // Arrange var settings = new ProcessHttpSettings { Method = HttpMethod.Post.ToString(), Body = "{\"plan\":\"Premium\",\"details\":{\"address\": \"Rua X\"}}", Headers = new Dictionary <string, string>() { { "Content-Type", "application/json" }, { "Authorization", "Key askçjdhaklsdghasklgdasd=" } }, }; var target = GetTarget(); var httpResponseMessage = new HttpResponseMessage() { StatusCode = HttpStatusCode.InternalServerError, Content = new StringContent("Error") }; HttpClient.SendAsync(Arg.Any <HttpRequestMessage>(), Arg.Any <CancellationToken>()).Returns(httpResponseMessage); // Act try { await target.ExecuteAsync(Context, JObject.FromObject(settings), CancellationToken); throw new Exception(); } catch (ValidationException exception) { // Assert await HttpClient.DidNotReceive().SendAsync( Arg.Is <HttpRequestMessage>( h => h.RequestUri.Equals(settings.Uri)), Arg.Any <CancellationToken>()); } }