-
Notifications
You must be signed in to change notification settings - Fork 133
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
Add TestServer support for GraphQLHttpClient #354 #357
Open
andreyleskov
wants to merge
3
commits into
graphql-dotnet:master
Choose a base branch
from
andreyleskov:test-server-support#354
base: master
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 all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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
15 changes: 15 additions & 0 deletions
15
src/GraphQL.Client.Abstractions.Websocket/IWebSocketFactory.cs
This file contains 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,15 @@ | ||
using System; | ||
using System.Net.WebSockets; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace GraphQL.Client.Abstractions.Websocket | ||
{ | ||
/// <summary> | ||
/// creates and returns a configured and connected <see cref="WebSocket"/> instance | ||
/// </summary> | ||
public interface IWebSocketFactory | ||
{ | ||
Task<WebSocket> ConnectAsync(Uri webSocketUri, CancellationToken cancellationToken); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/GraphQL.Client.TestHost/GraphQL.Client.TestHost.csproj
This file contains 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,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net461;netstandard2.1</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\GraphQL.Client\GraphQL.Client.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.2.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains 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,13 @@ | ||
using System; | ||
using GraphQL.Client.Abstractions.Websocket; | ||
using GraphQL.Client.Http; | ||
using Microsoft.AspNetCore.TestHost; | ||
|
||
namespace GraphQL.Client.TestHost | ||
{ | ||
public static class TestServerExtensions | ||
{ | ||
public static GraphQLHttpClient CreateGraphQLHttpClient(this TestServer testServer, GraphQLHttpClientOptions options, IGraphQLWebsocketJsonSerializer serializer) | ||
=> new GraphQLHttpClient(options, serializer, testServer.CreateClient(), new TestServerWebSocketFactory(testServer)); | ||
} | ||
} |
This file contains 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,27 @@ | ||
using System; | ||
using System.Net.WebSockets; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using GraphQL.Client.Abstractions.Websocket; | ||
using GraphQL.Client.Http.Websocket; | ||
using Microsoft.AspNetCore.TestHost; | ||
|
||
namespace GraphQL.Client.TestHost | ||
{ | ||
public class TestServerWebSocketFactory: IWebSocketFactory | ||
{ | ||
private readonly WebSocketClient _webSocketClient; | ||
|
||
public TestServerWebSocketFactory(TestServer testServer) | ||
{ | ||
_webSocketClient = testServer.CreateWebSocketClient(); | ||
_webSocketClient.ConfigureRequest = r => | ||
{ | ||
r.Headers["Sec-WebSocket-Protocol"] = "graphql-ws"; | ||
}; | ||
} | ||
|
||
public Task<WebSocket> ConnectAsync(Uri webSocketUri, CancellationToken cancellationToken) | ||
=> _webSocketClient.ConnectAsync(webSocketUri, cancellationToken); | ||
} | ||
} |
This file contains 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 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,65 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Net.Http; | ||
using System.Net.WebSockets; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using GraphQL.Client.Abstractions.Websocket; | ||
|
||
namespace GraphQL.Client.Http.Websocket | ||
{ | ||
#if NETSTANDARD | ||
/// <summary> | ||
/// Default web socket factory for netstandard2.0 | ||
/// </summary> | ||
public class ClientWebSocketFactory : IWebSocketFactory | ||
{ | ||
private readonly GraphQLHttpClientOptions _options; | ||
|
||
public ClientWebSocketFactory(GraphQLHttpClientOptions options) | ||
{ | ||
_options = options; | ||
} | ||
|
||
public async Task<WebSocket> ConnectAsync(Uri webSocketUri, CancellationToken cancellationToken) | ||
{ | ||
var webSocket = new ClientWebSocket(); | ||
webSocket.Options.AddSubProtocol("graphql-ws"); | ||
|
||
// the following properties are not supported in Blazor WebAssembly and throw a PlatformNotSupportedException error when accessed | ||
try | ||
{ | ||
webSocket.Options.ClientCertificates = ((HttpClientHandler) _options.HttpMessageHandler).ClientCertificates; | ||
} | ||
catch (NotImplementedException) | ||
{ | ||
Debug.WriteLine("property 'ClientWebSocketOptions.ClientCertificates' not implemented by current platform"); | ||
} | ||
catch (PlatformNotSupportedException) | ||
{ | ||
Debug.WriteLine("property 'ClientWebSocketOptions.ClientCertificates' not supported by current platform"); | ||
} | ||
|
||
try | ||
{ | ||
webSocket.Options.UseDefaultCredentials = | ||
((HttpClientHandler) _options.HttpMessageHandler).UseDefaultCredentials; | ||
} | ||
catch (NotImplementedException) | ||
{ | ||
Debug.WriteLine("property 'ClientWebSocketOptions.UseDefaultCredentials' not implemented by current platform"); | ||
} | ||
catch (PlatformNotSupportedException) | ||
{ | ||
Debug.WriteLine("Property 'ClientWebSocketOptions.UseDefaultCredentials' not supported by current platform"); | ||
} | ||
|
||
_options.ConfigureWebsocketOptions(webSocket.Options); | ||
|
||
Debug.WriteLine($"opening websocket {webSocket.GetHashCode()} (thread {Thread.CurrentThread.ManagedThreadId})"); | ||
await webSocket.ConnectAsync(webSocketUri, cancellationToken); | ||
return webSocket; | ||
} | ||
} | ||
#endif | ||
} |
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rose-a @andreyleskov regarding 4. Tests stability:
I recommend you ALWAYS use
ConfigureAwait(false)
in library code especially when using XUnit package as a test framework because ofXunit.Sdk.MaxConcurrencySyncContext
. In my casemaximumConcurrencyLevel
was 6 and some tests randomly failed. So please look through entire codebase and addConfigureAwait(false)
to awaitable calls. Perhaps it will help.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for the comment, it makes sense from my point of view. This PR is following the existing await style and focuses on just WebSockets. I assume repository owners prefer to do repository-wide changes as await style in a dedicated PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
im on to this in in #370...