Skip to content

Commit

Permalink
Filter expected 404 response for close connection REST Api (#1794)
Browse files Browse the repository at this point in the history
  • Loading branch information
Y-Sindo authored Jun 14, 2023
1 parent aa3ddb5 commit 2e19391
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Microsoft.Azure.SignalR.Common/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public static class Headers

public static class ErrorCodes
{
public const string WarningConnectionsNotExisted = "Warning.Connection.NotExisted";
public const string WarningConnectionNotExisted = "Warning.Connection.NotExisted";
public const string WarningUserNotExisted = "Warning.User.NotExisted";
public const string WarningGroupNotExisted = "Warning.Group.NotExisted";
public const string InfoUserNotInGroup = "Info.User.NotInGroup";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public async Task CloseConnectionAsync(string connectionId, string reason, Cance
throw new ArgumentException(NullOrEmptyStringErrorMessage, nameof(connectionId));
}
var api = await _restApiProvider.GetCloseConnectionEndpointAsync(_appName, _hubName, connectionId, reason);
await _restClient.SendAsync(api, HttpMethod.Delete, _productInfo, handleExpectedResponseAsync: null, cancellationToken: cancellationToken);
await _restClient.SendAsync(api, HttpMethod.Delete, _productInfo, handleExpectedResponse: static response => FilterExpectedResponse(response, ErrorCodes.WarningConnectionNotExisted), cancellationToken: cancellationToken);
}

private static void ValidateUserIdAndGroupName(string userId, string groupName)
Expand All @@ -275,7 +275,7 @@ public async Task<bool> ConnectionExistsAsync(string connectionId, CancellationT
await _restClient.SendAsync(api, HttpMethod.Head, _productInfo, handleExpectedResponse: response =>
{
exists = response.StatusCode == HttpStatusCode.OK;
return FilterExpectedResponse(response, ErrorCodes.WarningConnectionsNotExisted);
return FilterExpectedResponse(response, ErrorCodes.WarningConnectionNotExisted);
}, cancellationToken: cancellationToken);
return exists;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,98 @@ public async Task AddNonexistentConnectionToGroupRestApiTest()
await context.Groups.AddToGroupAsync(Guid.NewGuid().ToString(), "group");
}

[ConditionalFact]
[SkipIfConnectionStringNotPresent]
public async Task CloseNonexistentConnectionToGroupRestApiTest()
{
using var serviceManager = new ServiceManagerBuilder().WithOptions(o =>
{
o.ConnectionString = TestConfiguration.Instance.ConnectionString;
o.ServiceTransportType = ServiceTransportType.Transient;
}).BuildServiceManager();
using var context = await serviceManager.CreateHubContextAsync(HubName, default);
await context.ClientManager.CloseConnectionAsync(Guid.NewGuid().ToString());
}

[ConditionalFact]
[SkipIfConnectionStringNotPresent]
public async Task RemoveNonexistentConnectionFromGroupRestApiTest()
{
using var serviceManager = new ServiceManagerBuilder().WithOptions(o =>
{
o.ConnectionString = TestConfiguration.Instance.ConnectionString;
o.ServiceTransportType = ServiceTransportType.Transient;
}).BuildServiceManager();
using var context = await serviceManager.CreateHubContextAsync(HubName, default);
await context.Groups.RemoveFromGroupAsync(Guid.NewGuid().ToString(), "group");
}

[ConditionalFact]
[SkipIfConnectionStringNotPresent]
public async Task RemoveNonexistentConnectionFromAllGroupsRestApiTest()
{
using var serviceManager = new ServiceManagerBuilder().WithOptions(o =>
{
o.ConnectionString = TestConfiguration.Instance.ConnectionString;
o.ServiceTransportType = ServiceTransportType.Transient;
}).BuildServiceManager();
using var context = await serviceManager.CreateHubContextAsync(HubName, default);
await context.Groups.RemoveFromAllGroupsAsync(Guid.NewGuid().ToString());
}

[ConditionalFact]
[SkipIfConnectionStringNotPresent]
public async Task AddNonexistentUserToGroupRestApiTest()
{
using var serviceManager = new ServiceManagerBuilder().WithOptions(o =>
{
o.ConnectionString = TestConfiguration.Instance.ConnectionString;
o.ServiceTransportType = ServiceTransportType.Transient;
}).BuildServiceManager();
using var context = await serviceManager.CreateHubContextAsync(HubName, default);
await context.UserGroups.AddToGroupAsync(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
}

[ConditionalFact]
[SkipIfConnectionStringNotPresent]
public async Task AddNonexistentUserToGroupWithTTLRestApiTest()
{
using var serviceManager = new ServiceManagerBuilder().WithOptions(o =>
{
o.ConnectionString = TestConfiguration.Instance.ConnectionString;
o.ServiceTransportType = ServiceTransportType.Transient;
}).BuildServiceManager();
using var context = await serviceManager.CreateHubContextAsync(HubName, default);
await context.UserGroups.AddToGroupAsync(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), TimeSpan.Zero);
await context.UserGroups.AddToGroupAsync(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), TimeSpan.FromSeconds(1));
}

[ConditionalFact]
[SkipIfConnectionStringNotPresent]
public async Task RemoveNonexistentUserFromGroupRestApiTest()
{
using var serviceManager = new ServiceManagerBuilder().WithOptions(o =>
{
o.ConnectionString = TestConfiguration.Instance.ConnectionString;
o.ServiceTransportType = ServiceTransportType.Transient;
}).BuildServiceManager();
using var context = await serviceManager.CreateHubContextAsync(HubName, default);
await context.UserGroups.RemoveFromGroupAsync(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
}

[ConditionalFact]
[SkipIfConnectionStringNotPresent]
public async Task RemoveNonexistentUserFromAllGroupsRestApiTest()
{
using var serviceManager = new ServiceManagerBuilder().WithOptions(o =>
{
o.ConnectionString = TestConfiguration.Instance.ConnectionString;
o.ServiceTransportType = ServiceTransportType.Transient;
}).BuildServiceManager();
using var context = await serviceManager.CreateHubContextAsync(HubName, default);
await context.UserGroups.RemoveFromAllGroupsAsync(Guid.NewGuid().ToString());
}

private static IDictionary<string, List<string>> GenerateUserGroupDict(IList<string> userNames, IList<string> groupNames)
{
return (from i in Enumerable.Range(0, userNames.Count)
Expand Down

0 comments on commit 2e19391

Please sign in to comment.