【问题标题】:Add header to QueryRequestBuilder Graphql Hot Chocolate将标头添加到 QueryRequest Builder Graphql Hot Chocolate
【发布时间】:2022-11-14 05:11:00
【问题描述】:

我正在尝试使用 Graphql Hot Chocolate 测试我的 Web Api。我阅读了 ChilliCream https://chillicream.com/blog/2019/04/11/integration-tests 的文档,但我有疑问。一些端点使用基于 JWT 的授权,我需要在请求的标头上发送它。 (我还有其他使用标题进行某些操作的方法)

如何使用 Query RequestBuilder 发送一个或多个标头?

我会尝试使用它,我收到了 http 403

IReadOnlyQueryRequest request = QueryRequestBuilder.New()
    .SetQuery("query{ clients(where:{state:{eq:ACTIVE}}) { items{ id code date name email desc state created modify } pageInfo{ hasNextPage hasPreviousPage } totalCount } }")
    .AddProperty("enviroment", "test")
    .AddProperty("authorization", "Bearer token_value")
    .Create();
                
IExecutionResult result = await executor.ExecuteAsync(request);

【问题讨论】:

    标签: c# asp.net unit-testing graphql hotchocolate


    【解决方案1】:

    您正在测试的级别(使用 QueryRequestBuilder)与传输无关。如果您想使用 HTTP 层进行“完整”集成测试,请考虑使用 WebApplicationFactory 进行适当的 ASP.NET Core 集成测试。

    如果您只想使用 ClaimsPrincipal 测试您的代码,您可以使用 .TryAddProperty(nameof(ClaimsPrincipal), yourPrincipal),其中 yourPrincipal 是您在测试中创建的包含您的角色等的 ClaimsPrincipal。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-07
      • 2021-12-10
      • 2022-08-19
      • 2019-12-08
      • 1970-01-01
      • 2020-07-18
      • 2021-09-30
      • 2021-12-04
      相关资源
      最近更新 更多