-
Notifications
You must be signed in to change notification settings - Fork 110
Add client invocation for RestHublifetimeManager #2206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TD2106
wants to merge
10
commits into
Azure:dev
Choose a base branch
from
TD2106:duy/transient-ci
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ab1dbcc
Add client invocation for transient management mode
TD2106 b0f2622
Merge branch 'dev' into duy/transient-ci
TD2106 4ae742a
update to follow new response structure
TD2106 13d6eda
update error throwing
TD2106 6e91cd5
Merge branch 'dev' into duy/transient-ci
TD2106 ccd74e2
update test
TD2106 894fa29
update deserialization
TD2106 93b3e76
fix bug
TD2106 8094a28
update
TD2106 cc6e1ac
cleanup
TD2106 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
250 changes: 250 additions & 0 deletions
250
test/Microsoft.Azure.SignalR.Management.Tests/RestHubLifetimeManagerFacts.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,250 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| using System; | ||
| using System.Linq; | ||
| using System.Net; | ||
| using System.Net.Http; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.AspNetCore.SignalR; | ||
| using Microsoft.Azure.SignalR.Common; | ||
| using Microsoft.Azure.SignalR.Tests.Common; | ||
| using Moq; | ||
| using Moq.Protected; | ||
| using Xunit; | ||
|
|
||
| #nullable enable | ||
|
|
||
| namespace Microsoft.Azure.SignalR.Management.Tests | ||
| { | ||
| public class RestHubLifetimeManagerFacts | ||
| { | ||
| #if NET7_0_OR_GREATER | ||
| private readonly Mock<IHttpClientFactory> _httpClientFactoryMock; | ||
| private readonly HttpClient _httpClient; | ||
| private readonly string _hubName = "TestHub"; | ||
| private readonly string _appName = "TestApp"; | ||
| private readonly RestHubLifetimeManager<TestHub> _manager; | ||
|
|
||
| private readonly Mock<HttpMessageHandler> _httpMessageHandlerMock; | ||
|
|
||
| public RestHubLifetimeManagerFacts() | ||
| { | ||
| _httpMessageHandlerMock = new Mock<HttpMessageHandler>(); | ||
|
|
||
| _httpClient = new HttpClient(_httpMessageHandlerMock.Object); | ||
|
|
||
| _httpClientFactoryMock = new Mock<IHttpClientFactory>(); | ||
| _httpClientFactoryMock | ||
| .Setup(f => f.CreateClient(It.IsAny<string>())) | ||
| .Returns(_httpClient); | ||
|
|
||
| var restClient = new RestClient(_httpClientFactoryMock.Object); | ||
|
|
||
| _manager = new RestHubLifetimeManager<TestHub>( | ||
| _hubName, | ||
| new(FakeEndpointUtils.GetFakeConnectionString(1).First()), | ||
| _appName, | ||
| restClient | ||
| ); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task InvokeConnectionAsync_NullMethodName_ThrowsArgumentException() | ||
| { | ||
| string? methodName = null; | ||
| var connectionId = "connection1"; | ||
| var args = Array.Empty<object>(); | ||
|
|
||
| var exception = await Assert.ThrowsAsync<ArgumentException>( | ||
| async () => await _manager.InvokeConnectionAsync<string>(connectionId, methodName!, args)); | ||
|
|
||
| Assert.Equal("methodName", exception.ParamName); | ||
|
|
||
| methodName = ""; | ||
| exception = await Assert.ThrowsAsync<ArgumentException>( | ||
| async () => await _manager.InvokeConnectionAsync<string>(connectionId, methodName, args)); | ||
| Assert.Equal("methodName", exception.ParamName); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task InvokeConnectionAsync_NullConnectionId_ThrowsArgumentException() | ||
| { | ||
| var methodName = "testMethod"; | ||
| string? connectionId = null; | ||
| var args = Array.Empty<object>(); | ||
|
|
||
| var exception = await Assert.ThrowsAsync<ArgumentException>( | ||
| async () => await _manager.InvokeConnectionAsync<string>(connectionId!, methodName, args)); | ||
|
|
||
| Assert.Equal("connectionId", exception.ParamName); | ||
|
|
||
| connectionId = ""; | ||
| exception = await Assert.ThrowsAsync<ArgumentException>( | ||
| async () => await _manager.InvokeConnectionAsync<string>(connectionId, methodName, args)); | ||
| Assert.Equal("connectionId", exception.ParamName); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task InvokeConnectionAsync_WithStringResult_ReturnsDeserializedValue() | ||
| { | ||
| // Arrange | ||
| var connectionId = "connection1"; | ||
| var methodName = "getUsername"; | ||
| var args = new object?[] { 42, "test-param", true }; | ||
| var expectedResult = "John Doe"; | ||
|
|
||
| var jsonResponse = $"{{\"result\":\"{expectedResult}\"}}"; | ||
|
|
||
| _httpMessageHandlerMock | ||
| .Protected() | ||
| .Setup<Task<HttpResponseMessage>>( | ||
| "SendAsync", | ||
| ItExpr.IsAny<HttpRequestMessage>(), | ||
| ItExpr.IsAny<CancellationToken>() | ||
| ) | ||
| .ReturnsAsync(() => new HttpResponseMessage(HttpStatusCode.OK) | ||
| { | ||
| Content = new StringContent(jsonResponse) | ||
| }); | ||
|
|
||
| // Act | ||
| var result = await _manager.InvokeConnectionAsync<string>(connectionId, methodName, args); | ||
|
|
||
| // Assert | ||
| Assert.Equal(expectedResult, result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task InvokeConnectionAsync_WithComplexObjectResult_ReturnsDeserializedObject() | ||
| { | ||
| // Arrange | ||
| var connectionId = "connection1"; | ||
| var methodName = "getUserProfile"; | ||
| var args = new object?[] { "userId123", new { filter = "personal" } }; | ||
|
|
||
| var jsonResponse = @"{""result"":{""id"":123,""name"":""Jane Doe"",""active"":true,""roles"":[""user"",""admin""]}}"; | ||
|
|
||
| _httpMessageHandlerMock | ||
| .Protected() | ||
| .Setup<Task<HttpResponseMessage>>( | ||
| "SendAsync", | ||
| ItExpr.IsAny<HttpRequestMessage>(), | ||
| ItExpr.IsAny<CancellationToken>() | ||
| ) | ||
| .ReturnsAsync(() => new HttpResponseMessage(HttpStatusCode.OK) | ||
| { | ||
| Content = new StringContent(jsonResponse) | ||
| }); | ||
|
|
||
| // Act | ||
| var result = await _manager.InvokeConnectionAsync<UserProfile>(connectionId, methodName, args); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(result); | ||
| Assert.Equal(123, result.id); | ||
| Assert.Equal("Jane Doe", result.name); | ||
| Assert.True(result.active); | ||
| Assert.Equal(2, result.roles.Length); | ||
| Assert.Contains("admin", result.roles); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task InvokeConnectionAsync_WithErrorResponse_ThrowsHubException() | ||
| { | ||
| // Arrange | ||
| var connectionId = "connection1"; | ||
| var methodName = "getError"; | ||
| var args = Array.Empty<object>(); | ||
| var errorMessage = "Connection does not exist"; | ||
|
|
||
| _httpMessageHandlerMock | ||
| .Protected() | ||
| .Setup<Task<HttpResponseMessage>>( | ||
| "SendAsync", | ||
| ItExpr.IsAny<HttpRequestMessage>(), | ||
| ItExpr.IsAny<CancellationToken>() | ||
| ) | ||
| .ReturnsAsync(() => new HttpResponseMessage(HttpStatusCode.NotFound) | ||
| { | ||
| Content = new StringContent(errorMessage) | ||
| }); | ||
|
|
||
| // Act & Assert | ||
| var exception = await Assert.ThrowsAsync<AzureSignalRInaccessibleEndpointException>( | ||
| async () => await _manager.InvokeConnectionAsync<string>(connectionId, methodName, args)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task InvokeConnectionAsync_WithMissingResultNode_ThrowsHubException() | ||
| { | ||
| // Arrange | ||
| var connectionId = "connection1"; | ||
| var methodName = "getIncompleteData"; | ||
| var args = Array.Empty<object>(); | ||
|
|
||
| // JSON missing the required result node | ||
| var incompleteJsonResponse = "{\"jsonObject\":{}}"; | ||
|
|
||
| _httpMessageHandlerMock | ||
| .Protected() | ||
| .Setup<Task<HttpResponseMessage>>( | ||
| "SendAsync", | ||
| ItExpr.IsAny<HttpRequestMessage>(), | ||
| ItExpr.IsAny<CancellationToken>() | ||
| ) | ||
| .ReturnsAsync(() => new HttpResponseMessage(HttpStatusCode.OK) | ||
| { | ||
| Content = new StringContent(incompleteJsonResponse) | ||
| }); | ||
|
|
||
| // Act & Assert | ||
| var exception = await Assert.ThrowsAsync<HubException>( | ||
| async () => await _manager.InvokeConnectionAsync<string>(connectionId, methodName, args)); | ||
|
|
||
| Assert.Contains("Result not found in JSON response", exception.Message); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task InvokeConnectionAsync_WithMissingJsonObjectNode_ThrowsHubException() | ||
| { | ||
| // Arrange | ||
| var connectionId = "connection1"; | ||
| var methodName = "getIncompleteData"; | ||
| var args = Array.Empty<object>(); | ||
|
|
||
| // JSON missing the required jsonObject node | ||
| var incompleteJsonResponse = "{}"; | ||
|
|
||
| _httpMessageHandlerMock | ||
| .Protected() | ||
| .Setup<Task<HttpResponseMessage>>( | ||
| "SendAsync", | ||
| ItExpr.IsAny<HttpRequestMessage>(), | ||
| ItExpr.IsAny<CancellationToken>() | ||
| ) | ||
| .ReturnsAsync(() => new HttpResponseMessage(HttpStatusCode.OK) | ||
| { | ||
| Content = new StringContent(incompleteJsonResponse) | ||
| }); | ||
|
|
||
| // Act & Assert | ||
| var exception = await Assert.ThrowsAsync<HubException>( | ||
| async () => await _manager.InvokeConnectionAsync<string>(connectionId, methodName, args)); | ||
|
|
||
| Assert.Contains("Result not found in JSON response", exception.Message); | ||
| } | ||
| #endif | ||
|
|
||
| public class TestHub : Hub { } | ||
|
|
||
| public class UserProfile | ||
| { | ||
| public int id { get; set; } | ||
| public string name { get; set; } = string.Empty; | ||
| public bool active { get; set; } | ||
| public string[] roles { get; set; } = Array.Empty<string>(); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.