-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathRestHubLifetimeManagerFacts.cs
More file actions
250 lines (208 loc) · 9.12 KB
/
Copy pathRestHubLifetimeManagerFacts.cs
File metadata and controls
250 lines (208 loc) · 9.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
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>();
}
}
}