Skip to content

Commit a47cae1

Browse files
committed
run websocket tests for all available serializers
1 parent a6f192b commit a47cae1

File tree

5 files changed

+87
-37
lines changed

5 files changed

+87
-37
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using GraphQL.Client.Abstractions;
6+
7+
namespace GraphQL.Client.Tests.Common.Helpers {
8+
public class AvailableJsonSerializers<TSerializerInterface> : IEnumerable<object[]> where TSerializerInterface : IGraphQLJsonSerializer {
9+
public IEnumerator<object[]> GetEnumerator() {
10+
// try to find one in the assembly and assign that
11+
var type = typeof(TSerializerInterface);
12+
return AppDomain.CurrentDomain
13+
.GetAssemblies()
14+
.SelectMany(s => s.GetTypes())
15+
.Where(p => type.IsAssignableFrom(p) && !p.IsInterface && !p.IsAbstract)
16+
.Select(serializerType => new object[]{Activator.CreateInstance(serializerType)})
17+
.GetEnumerator();
18+
}
19+
20+
IEnumerator IEnumerable.GetEnumerator() {
21+
return GetEnumerator();
22+
}
23+
}
24+
}

tests/GraphQL.Integration.Tests/GraphQL.Integration.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
</ItemGroup>
2222

2323
<ItemGroup>
24+
<ProjectReference Include="..\..\src\GraphQL.Client.Serializer.Newtonsoft\GraphQL.Client.Serializer.Newtonsoft.csproj" />
25+
<ProjectReference Include="..\..\src\GraphQL.Client.Serializer.SystemTextJson\GraphQL.Client.Serializer.SystemTextJson.csproj" />
2426
<ProjectReference Include="..\..\src\GraphQL.Client\GraphQL.Client.csproj" />
2527
<ProjectReference Include="..\IntegrationTestServer\IntegrationTestServer.csproj" />
2628
</ItemGroup>

tests/GraphQL.Integration.Tests/Helpers/WebHostHelpers.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using GraphQL.Client;
3+
using GraphQL.Client.Abstractions.Websocket;
34
using GraphQL.Client.Http;
45
using GraphQL.Client.Serializer.Newtonsoft;
56
using GraphQL.Client.Tests.Common.Helpers;
@@ -33,19 +34,20 @@ public static IWebHost CreateServer<TStartup>(int port) where TStartup : class
3334
}
3435

3536

36-
public static GraphQLHttpClient GetGraphQLClient(int port, bool requestsViaWebsocket = false)
37+
public static GraphQLHttpClient GetGraphQLClient(int port, bool requestsViaWebsocket = false, IGraphQLWebsocketJsonSerializer serializer = null)
3738
=> new GraphQLHttpClient(new GraphQLHttpClientOptions {
3839
EndPoint = new Uri($"http://localhost:{port}/graphql"),
3940
UseWebSocketForQueriesAndMutations = requestsViaWebsocket,
40-
JsonSerializer = new NewtonsoftJsonSerializer()
41+
JsonSerializer = serializer ?? new NewtonsoftJsonSerializer()
4142
});
4243

43-
public static TestServerSetup SetupTest<TStartup>(bool requestsViaWebsocket = false) where TStartup : class
44+
public static TestServerSetup SetupTest<TStartup>(bool requestsViaWebsocket = false, IGraphQLWebsocketJsonSerializer serializer = null)
45+
where TStartup : class
4446
{
4547
var port = NetworkHelpers.GetFreeTcpPortNumber();
4648
return new TestServerSetup {
4749
Server = CreateServer<TStartup>(port),
48-
Client = GetGraphQLClient(port)
50+
Client = GetGraphQLClient(port, requestsViaWebsocket, serializer)
4951
};
5052
}
5153
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace GraphQL.Integration.Tests {
2+
public class NewtonsoftWebsocketTest {
3+
4+
}
5+
}

tests/GraphQL.Integration.Tests/WebsocketTest.cs

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,23 @@
1212
using Xunit;
1313
using Xunit.Abstractions;
1414
using FluentAssertions;
15+
using GraphQL.Client.Abstractions.Websocket;
1516

1617
namespace GraphQL.Integration.Tests {
1718
public class WebsocketTest {
1819
private readonly ITestOutputHelper output;
19-
2020
private static IWebHost CreateServer(int port) => WebHostHelpers.CreateServer<StartupChat>(port);
2121

2222
public WebsocketTest(ITestOutputHelper output) {
2323
this.output = output;
2424
}
2525

26-
[Fact]
27-
public async void AssertTestingHarness() {
26+
[Theory]
27+
[ClassData(typeof(AvailableJsonSerializers<IGraphQLWebsocketJsonSerializer>))]
28+
public async void AssertTestingHarness(IGraphQLWebsocketJsonSerializer serializer) {
2829
var port = NetworkHelpers.GetFreeTcpPortNumber();
2930
using (CreateServer(port)) {
30-
var client = WebHostHelpers.GetGraphQLClient(port);
31+
var client = WebHostHelpers.GetGraphQLClient(port, serializer: serializer);
3132

3233
const string message = "some random testing message";
3334
var response = await client.AddMessageAsync(message).ConfigureAwait(false);
@@ -36,23 +37,27 @@ public async void AssertTestingHarness() {
3637
}
3738
}
3839

39-
[Fact]
40-
public async void CanSendRequestViaWebsocket() {
40+
41+
[Theory]
42+
[ClassData(typeof(AvailableJsonSerializers<IGraphQLWebsocketJsonSerializer>))]
43+
public async void CanSendRequestViaWebsocket(IGraphQLWebsocketJsonSerializer serializer) {
4144
var port = NetworkHelpers.GetFreeTcpPortNumber();
4245
using (CreateServer(port)) {
43-
var client = WebHostHelpers.GetGraphQLClient(port, true);
46+
var client = WebHostHelpers.GetGraphQLClient(port, true, serializer);
4447
const string message = "some random testing message";
4548
var response = await client.AddMessageAsync(message).ConfigureAwait(false);
4649

4750
Assert.Equal(message, response.Data.AddMessage.Content);
4851
}
4952
}
5053

51-
[Fact]
52-
public async void CanHandleRequestErrorViaWebsocket() {
54+
55+
[Theory]
56+
[ClassData(typeof(AvailableJsonSerializers<IGraphQLWebsocketJsonSerializer>))]
57+
public async void CanHandleRequestErrorViaWebsocket(IGraphQLWebsocketJsonSerializer serializer) {
5358
var port = NetworkHelpers.GetFreeTcpPortNumber();
5459
using (CreateServer(port)) {
55-
var client = WebHostHelpers.GetGraphQLClient(port, true);
60+
var client = WebHostHelpers.GetGraphQLClient(port, true, serializer);
5661
var response = await client.SendQueryAsync<object>("this query is formatted quite badly").ConfigureAwait(false);
5762

5863
Assert.Single(response.Errors);
@@ -68,11 +73,13 @@ public async void CanHandleRequestErrorViaWebsocket() {
6873

6974
private readonly GraphQLRequest SubscriptionRequest = new GraphQLRequest(SubscriptionQuery);
7075

71-
[Fact]
72-
public async void CanCreateObservableSubscription() {
76+
77+
[Theory]
78+
[ClassData(typeof(AvailableJsonSerializers<IGraphQLWebsocketJsonSerializer>))]
79+
public async void CanCreateObservableSubscription(IGraphQLWebsocketJsonSerializer serializer) {
7380
var port = NetworkHelpers.GetFreeTcpPortNumber();
7481
using (CreateServer(port)) {
75-
var client = WebHostHelpers.GetGraphQLClient(port);
82+
var client = WebHostHelpers.GetGraphQLClient(port, serializer: serializer);
7683
await client.InitializeWebsocketConnection();
7784

7885
Debug.WriteLine("creating subscription stream");
@@ -108,11 +115,13 @@ public class MessageAddedContent {
108115
}
109116
}
110117

111-
[Fact]
112-
public async void CanReconnectWithSameObservable() {
118+
119+
[Theory]
120+
[ClassData(typeof(AvailableJsonSerializers<IGraphQLWebsocketJsonSerializer>))]
121+
public async void CanReconnectWithSameObservable(IGraphQLWebsocketJsonSerializer serializer) {
113122
var port = NetworkHelpers.GetFreeTcpPortNumber();
114123
using (CreateServer(port)) {
115-
var client = WebHostHelpers.GetGraphQLClient(port);
124+
var client = WebHostHelpers.GetGraphQLClient(port, serializer: serializer);
116125
await client.InitializeWebsocketConnection();
117126

118127
Debug.WriteLine("creating subscription stream");
@@ -175,18 +184,22 @@ public class UserJoinedContent {
175184

176185
private readonly GraphQLRequest SubscriptionRequest2 = new GraphQLRequest(SubscriptionQuery2);
177186

178-
[Fact]
179-
public async void CanConnectTwoSubscriptionsSimultaneously() {
187+
188+
[Theory]
189+
[ClassData(typeof(AvailableJsonSerializers<IGraphQLWebsocketJsonSerializer>))]
190+
public async void CanConnectTwoSubscriptionsSimultaneously(IGraphQLWebsocketJsonSerializer serializer) {
180191
var port = NetworkHelpers.GetFreeTcpPortNumber();
181192
var callbackTester = new CallbackTester<Exception>();
182193
var callbackTester2 = new CallbackTester<Exception>();
183194
using (CreateServer(port)) {
184-
var client = WebHostHelpers.GetGraphQLClient(port);
195+
var client = WebHostHelpers.GetGraphQLClient(port, serializer: serializer);
185196
await client.InitializeWebsocketConnection();
186197

187198
Debug.WriteLine("creating subscription stream");
188-
IObservable<GraphQLResponse<MessageAddedSubscriptionResult>> observable1 = client.CreateSubscriptionStream<MessageAddedSubscriptionResult>(SubscriptionRequest, callbackTester.Callback);
189-
IObservable<GraphQLResponse<UserJoinedSubscriptionResult>> observable2 = client.CreateSubscriptionStream<UserJoinedSubscriptionResult>(SubscriptionRequest2, callbackTester2.Callback);
199+
IObservable<GraphQLResponse<MessageAddedSubscriptionResult>> observable1 =
200+
client.CreateSubscriptionStream<MessageAddedSubscriptionResult>(SubscriptionRequest, callbackTester.Callback);
201+
IObservable<GraphQLResponse<UserJoinedSubscriptionResult>> observable2 =
202+
client.CreateSubscriptionStream<UserJoinedSubscriptionResult>(SubscriptionRequest2, callbackTester2.Callback);
190203

191204
Debug.WriteLine("subscribing...");
192205
var tester = observable1.Monitor();
@@ -197,9 +210,7 @@ public async void CanConnectTwoSubscriptionsSimultaneously() {
197210
response.Data.AddMessage.Content.Should().Be(message1);
198211
tester.Should().HaveReceivedPayload()
199212
.Which.Data.MessageAdded.Content.Should().Be(message1);
200-
201-
await Task.Delay(500); // ToDo: can be removed after https://github.com/graphql-dotnet/server/pull/199 was merged and released
202-
213+
203214
var joinResponse = await client.JoinDeveloperUser().ConfigureAwait(false);
204215
joinResponse.Data.Join.DisplayName.Should().Be("developer", "because that's the display name of user \"1\"");
205216

@@ -222,13 +233,15 @@ public async void CanConnectTwoSubscriptionsSimultaneously() {
222233
}
223234
}
224235

225-
[Fact]
226-
public async void CanHandleConnectionTimeout() {
236+
237+
[Theory]
238+
[ClassData(typeof(AvailableJsonSerializers<IGraphQLWebsocketJsonSerializer>))]
239+
public async void CanHandleConnectionTimeout(IGraphQLWebsocketJsonSerializer serializer) {
227240
var port = NetworkHelpers.GetFreeTcpPortNumber();
228241
var server = CreateServer(port);
229242
var callbackTester = new CallbackTester<Exception>();
230243

231-
var client = WebHostHelpers.GetGraphQLClient(port);
244+
var client = WebHostHelpers.GetGraphQLClient(port, serializer: serializer);
232245
await client.InitializeWebsocketConnection();
233246
Debug.WriteLine("creating subscription stream");
234247
IObservable<GraphQLResponse<MessageAddedSubscriptionResult>> observable = client.CreateSubscriptionStream<MessageAddedSubscriptionResult>(SubscriptionRequest, callbackTester.Callback);
@@ -263,11 +276,13 @@ public async void CanHandleConnectionTimeout() {
263276
server.Dispose();
264277
}
265278

266-
[Fact]
267-
public async void CanHandleSubscriptionError() {
279+
280+
[Theory]
281+
[ClassData(typeof(AvailableJsonSerializers<IGraphQLWebsocketJsonSerializer>))]
282+
public async void CanHandleSubscriptionError(IGraphQLWebsocketJsonSerializer serializer) {
268283
var port = NetworkHelpers.GetFreeTcpPortNumber();
269284
using (CreateServer(port)) {
270-
var client = WebHostHelpers.GetGraphQLClient(port);
285+
var client = WebHostHelpers.GetGraphQLClient(port, serializer: serializer);
271286
await client.InitializeWebsocketConnection();
272287
Debug.WriteLine("creating subscription stream");
273288
IObservable<GraphQLResponse<object>> observable = client.CreateSubscriptionStream<object>(
@@ -289,14 +304,16 @@ public async void CanHandleSubscriptionError() {
289304
}
290305
}
291306

292-
[Fact]
293-
public async void CanHandleQueryErrorInSubscription() {
307+
308+
[Theory]
309+
[ClassData(typeof(AvailableJsonSerializers<IGraphQLWebsocketJsonSerializer>))]
310+
public async void CanHandleQueryErrorInSubscription(IGraphQLWebsocketJsonSerializer serializer) {
294311
var port = NetworkHelpers.GetFreeTcpPortNumber();
295312
using (CreateServer(port)) {
296313

297314
var test = new GraphQLRequest("tset", new { test = "blaa" });
298315

299-
var client = WebHostHelpers.GetGraphQLClient(port);
316+
var client = WebHostHelpers.GetGraphQLClient(port, serializer: serializer);
300317
await client.InitializeWebsocketConnection();
301318
Debug.WriteLine("creating subscription stream");
302319
IObservable<GraphQLResponse<object>> observable = client.CreateSubscriptionStream<object>(

0 commit comments

Comments
 (0)