Skip to content

Commit

Permalink
refactor: clean messages tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tr00d committed Sep 3, 2024
1 parent a46c451 commit e5218d6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
3 changes: 2 additions & 1 deletion Vonage.Test/Messages/Rcs/RcsMessagesTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#region
using System;
using System.Threading.Tasks;
using Vonage.Common.Monads;
using Vonage.Messages;
using Vonage.Messages.Rcs;
using Vonage.Request;
Expand Down Expand Up @@ -107,7 +108,7 @@ public async Task SendRcsVideoAsyncReturnsOk()
[Fact]
public async Task UpdateAsyncReturnsOk()
{
this.Setup(this.expectedUri, "", this.helper.GetRequestJson());
this.Setup(this.expectedUri, Maybe<string>.None, this.helper.GetRequestJson());
await this.client.MessagesClient.UpdateAsync(RcsUpdateMessageRequest.Build("ID-123"));
}

Expand Down
3 changes: 2 additions & 1 deletion Vonage.Test/Messages/WhatsApp/WhatsAppMessagesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using FluentAssertions;
using Vonage.Common.Monads;
using Vonage.Messages;
using Vonage.Messages.WhatsApp;
using Vonage.Messages.WhatsApp.ProductMessages.MultipleItems;
Expand Down Expand Up @@ -515,7 +516,7 @@ public async Task SendWhatsAppVideoAsyncReturnsOkWithContext()
[Fact]
public async Task UpdateAsyncReturnsOk()
{
this.Setup(this.expectedUri, "", this.helper.GetRequestJson());
this.Setup(this.expectedUri, Maybe<string>.None, this.helper.GetRequestJson());
await this.BuildVonageClient(Credentials.FromAppIdAndPrivateKey(this.AppId, this.PrivateKey))
.MessagesClient.UpdateAsync(WhatsAppUpdateMessageRequest.Build("ID-123"));
}
Expand Down
21 changes: 12 additions & 9 deletions Vonage.Test/TestBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
#region
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
Expand All @@ -12,10 +13,12 @@
using Moq;
using Moq.Protected;
using Vonage.Common;
using Vonage.Common.Monads;
using Vonage.Request;
using Vonage.Test.Common.TestHelpers;
using Xunit;
using TimeProvider = Vonage.Common.TimeProvider;
#endregion

namespace Vonage.Test
{
Expand Down Expand Up @@ -73,17 +76,21 @@ private static string GetAssemblyDirectory()
return Path.GetDirectoryName(path);
}

protected void Setup(string uri, string responseContent, string requestContent = null,
protected void Setup(string uri, Maybe<string> responseContent, string requestContent = null,
HttpStatusCode expectedCode = HttpStatusCode.OK) =>
this.Setup(uri, new StringContent(responseContent, Encoding.UTF8, "application/json"), expectedCode,
this.Setup(uri,
responseContent.Map<HttpContent>(content =>
new StringContent(content, Encoding.UTF8, "application/json")), expectedCode,
requestContent);

protected void Setup(string uri, byte[] responseContent, HttpStatusCode expectedCode = HttpStatusCode.OK) =>
this.Setup(uri, new StreamContent(new MemoryStream(responseContent)), expectedCode);

private void Setup(string uri, HttpContent httpContent, HttpStatusCode expectedCode,
private void Setup(string uri, Maybe<HttpContent> httpContent, HttpStatusCode expectedCode,
string requestContent = null)
{
var expectedResponse = new HttpResponseMessage(expectedCode);
httpContent.IfSome(some => expectedResponse.Content = some);
var mockHandler = new Mock<HttpMessageHandler>(MockBehavior.Strict);
mockHandler
.Protected()
Expand All @@ -99,11 +106,7 @@ private void Setup(string uri, HttpContent httpContent, HttpStatusCode expectedC
var actualContent = actualHttpRequestMessage.Content.ReadAsStringAsync().Result;
Assert.Equal(requestContent, actualContent, StringComparer.OrdinalIgnoreCase);
})
.ReturnsAsync(new HttpResponseMessage
{
StatusCode = expectedCode,
Content = httpContent,
})
.ReturnsAsync(expectedResponse)
.Verifiable();
this.configuration.ClientHandler = mockHandler.Object;
}
Expand Down
11 changes: 7 additions & 4 deletions Vonage.Test/VoiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
using System;
#region
using System;
using System.Globalization;
using System.Net;
using System.Threading.Tasks;
using AutoFixture;
using FluentAssertions;
using Vonage.Common.Exceptions;
using Vonage.Common.Monads;
using Vonage.Voice;
using Vonage.Voice.Nccos;
using Vonage.Voice.Nccos.Endpoints;
using Xunit;
#endregion

namespace Vonage.Test;

Expand Down Expand Up @@ -456,7 +459,7 @@ public async Task UpdateCallAsync()
{
var uuid = this.fixture.Create<Guid>().ToString();
var request = new CallEditCommand {Action = CallEditCommand.ActionType.earmuff};
this.Setup($"{BaseUri}/{uuid}", string.Empty, this.GetRequestJson());
this.Setup($"{BaseUri}/{uuid}", Maybe<string>.None, this.GetRequestJson());
var response = await this.client.VoiceClient.UpdateCallAsync(uuid, request);
Assert.True(response);
}
Expand All @@ -470,7 +473,7 @@ public async Task UpdateCallWithCredentials()
Destination = new Destination {Type = "ncco", Url = new[] {"https://example.com/ncco.json"}},
Action = CallEditCommand.ActionType.transfer,
};
this.Setup($"{BaseUri}/{uuid}", string.Empty, this.GetRequestJson());
this.Setup($"{BaseUri}/{uuid}", Maybe<string>.None, this.GetRequestJson());
var response =
await this.client.VoiceClient.UpdateCallAsync(uuid, request,
this.BuildCredentialsForBearerAuthentication());
Expand All @@ -486,7 +489,7 @@ public async Task UpdateCallWithInlineNcco()
Destination = new Destination {Type = "ncco", Ncco = new Ncco(new TalkAction {Text = "hello world"})},
Action = CallEditCommand.ActionType.transfer,
};
this.Setup($"{BaseUri}/{uuid}", string.Empty, this.GetRequestJson());
this.Setup($"{BaseUri}/{uuid}", Maybe<string>.None, this.GetRequestJson());
Assert.True(await this.client.VoiceClient.UpdateCallAsync(uuid, request));
}

Expand Down

0 comments on commit e5218d6

Please sign in to comment.