Repository navigation
Feat/pass accesstoken to request httpheaders #24
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
Merged
Merged
Changes from all commits
Commits
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
76 changes: 76 additions & 0 deletions
76
ShopifyNet.Tests/Interceptor/RequestAccessTokenInterceptorTests.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,76 @@ | ||
| using System.Net; | ||
| using System.Net.Http.Json; | ||
| using GraphQLSharp; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| namespace ShopifyNet.Tests; | ||
|
|
||
| [TestClass] | ||
| public class RequestAccessTokenInterceptorTests | ||
| { | ||
| private const string ACCESS_TOKEN_HEADER = "X-Shopify-Access-Token"; | ||
|
|
||
| /// <summary> | ||
| /// Replaces the X-Shopify-Access-Token header set by ShopifyClientOptions with ShopifyGraphQLRequest.AccessToken, when set. | ||
| /// </summary> | ||
| private class RequestAccessTokenInterceptor : IInterceptor<ShopifyGraphQLRequest, ShopifyClientOptions> | ||
| { | ||
| public Task<GraphQLResponse<TData>> InterceptRequestAsync<TData>(ShopifyGraphQLRequest request, ShopifyClientOptions options, CancellationToken cancellationToken, ExecuteRequestAsync<ShopifyGraphQLRequest, TData> executeAsync) | ||
| { | ||
| if (request.AccessToken == null) | ||
| return executeAsync(request, null, cancellationToken); | ||
|
|
||
| return executeAsync(request, headers => | ||
| { | ||
| headers.Remove(ACCESS_TOKEN_HEADER); | ||
| headers.Add(ACCESS_TOKEN_HEADER, request.AccessToken); | ||
| }, cancellationToken); | ||
| } | ||
| } | ||
|
|
||
| private static (ShopifyClient client, List<string[]> sentTokens) CreateClient() | ||
| { | ||
| var sentTokens = new List<string[]>(); | ||
| var httpClient = new TestHttpClient(msg => | ||
| { | ||
| sentTokens.Add(msg.Headers.TryGetValues(ACCESS_TOKEN_HEADER, out var values) ? values.ToArray() : []); | ||
| return new HttpResponseMessage | ||
| { | ||
| StatusCode = HttpStatusCode.OK, | ||
| Content = JsonContent.Create(new GraphQLResponse()) | ||
| }; | ||
| }); | ||
| var options = new ShopifyClientOptions("test.myshopify.com", "options-token") | ||
| { | ||
| Interceptor = new RequestAccessTokenInterceptor(), | ||
| HttpClient = httpClient, | ||
| }; | ||
| return (new ShopifyClient(options), sentTokens); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task ShouldReplaceOptionsAccessTokenWithRequestAccessToken() | ||
| { | ||
| var (client, sentTokens) = CreateClient(); | ||
|
|
||
| await client.QueryAsync(new ShopifyGraphQLRequest | ||
| { | ||
| query = "query { shop { id } }", | ||
| AccessToken = "request-token", | ||
| }); | ||
|
|
||
| Assert.AreEqual(1, sentTokens.Count); | ||
| CollectionAssert.AreEqual(new[] { "request-token" }, sentTokens[0]); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task ShouldKeepOptionsAccessTokenWhenRequestAccessTokenIsNull() | ||
| { | ||
| var (client, sentTokens) = CreateClient(); | ||
|
|
||
| await client.QueryAsync("query { shop { id } }"); | ||
|
|
||
| Assert.AreEqual(1, sentTokens.Count); | ||
| CollectionAssert.AreEqual(new[] { "options-token" }, sentTokens[0]); | ||
| } | ||
| } |
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
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
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.