-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/make automatic header optional (#542)
* Make UserAgent request header optional. * Changed to configurable user agent header. * Update src/GraphQL.Client/GraphQLHttpClientOptions.cs Co-authored-by: Alexander Rose <[email protected]> * Update src/GraphQL.Client/GraphQLHttpRequest.cs Co-authored-by: Alexander Rose <[email protected]> * Update src/GraphQL.Client/GraphQLHttpRequest.cs Co-authored-by: Alexander Rose <[email protected]> * Update src/GraphQL.Client/GraphQLHttpRequest.cs Co-authored-by: Alexander Rose <[email protected]> * Update src/GraphQL.Client/GraphQLHttpRequest.cs * refactor test helpers to provide access to GraphQLHttpClientOptions when creating the test client * test user agent header --------- Co-authored-by: Jesse <[email protected]> Co-authored-by: Ivan Maximov <[email protected]>
- Loading branch information
1 parent
4430c20
commit 9f7d593
Showing
11 changed files
with
145 additions
and
61 deletions.
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
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
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
12 changes: 12 additions & 0 deletions
12
tests/GraphQL.Client.Tests.Common/Properties/launchSettings.json
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,12 @@ | ||
{ | ||
"profiles": { | ||
"GraphQL.Client.Tests.Common": { | ||
"commandName": "Project", | ||
"launchBrowser": true, | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
}, | ||
"applicationUrl": "http://localhost:59034" | ||
} | ||
} | ||
} |
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
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,68 @@ | ||
using System.Net.Http.Headers; | ||
using FluentAssertions; | ||
using GraphQL.Client.Abstractions; | ||
using GraphQL.Client.Http; | ||
using GraphQL.Integration.Tests.Helpers; | ||
using Xunit; | ||
|
||
namespace GraphQL.Integration.Tests; | ||
|
||
public class UserAgentHeaderTests : IAsyncLifetime, IClassFixture<SystemTextJsonAutoNegotiateServerTestFixture> | ||
{ | ||
private readonly IntegrationServerTestFixture Fixture; | ||
private GraphQLHttpClient? ChatClient; | ||
|
||
public UserAgentHeaderTests(SystemTextJsonAutoNegotiateServerTestFixture fixture) | ||
{ | ||
Fixture = fixture; | ||
} | ||
|
||
public async Task InitializeAsync() => await Fixture.CreateServer().ConfigureAwait(false); | ||
|
||
public Task DisposeAsync() | ||
{ | ||
ChatClient?.Dispose(); | ||
return Task.CompletedTask; | ||
} | ||
|
||
[Fact] | ||
public async void Can_set_custom_user_agent() | ||
{ | ||
const string userAgent = "CustomUserAgent"; | ||
ChatClient = Fixture.GetChatClient(options => options.DefaultUserAgentRequestHeader = ProductInfoHeaderValue.Parse(userAgent)); | ||
|
||
var graphQLRequest = new GraphQLRequest("query clientUserAgent { clientUserAgent }"); | ||
var response = await ChatClient.SendQueryAsync(graphQLRequest, () => new { clientUserAgent = string.Empty }).ConfigureAwait(false); | ||
|
||
response.Errors.Should().BeNull(); | ||
response.Data.clientUserAgent.Should().Be(userAgent); | ||
} | ||
|
||
[Fact] | ||
public async void Default_user_agent_is_set_as_expected() | ||
{ | ||
string? expectedUserAgent = new ProductInfoHeaderValue( | ||
typeof(GraphQLHttpClient).Assembly.GetName().Name, | ||
typeof(GraphQLHttpClient).Assembly.GetName().Version.ToString()).ToString(); | ||
|
||
ChatClient = Fixture.GetChatClient(); | ||
|
||
var graphQLRequest = new GraphQLRequest("query clientUserAgent { clientUserAgent }"); | ||
var response = await ChatClient.SendQueryAsync(graphQLRequest, () => new { clientUserAgent = string.Empty }).ConfigureAwait(false); | ||
|
||
response.Errors.Should().BeNull(); | ||
response.Data.clientUserAgent.Should().Be(expectedUserAgent); | ||
} | ||
|
||
[Fact] | ||
public async void No_Default_user_agent_if_set_to_null() | ||
{ | ||
ChatClient = Fixture.GetChatClient(options => options.DefaultUserAgentRequestHeader = null); | ||
|
||
var graphQLRequest = new GraphQLRequest("query clientUserAgent { clientUserAgent }"); | ||
var response = await ChatClient.SendQueryAsync(graphQLRequest, () => new { clientUserAgent = string.Empty }).ConfigureAwait(false); | ||
|
||
response.Errors.Should().HaveCount(1); | ||
response.Errors[0].Message.Should().Be("user agent header not set"); | ||
} | ||
} |
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