Allan
Allan

Reputation: 45

Add header to QueryRequestBuilder Graphql Hot Chocolate

I'm trying to testing my web Api with Graphql Hot Chocolate. I read documentation of ChilliCream https://chillicream.com/blog/2019/04/11/integration-tests but I have a doubt. Some endpoints use Authorization based of JWT, and I need to send it on a header in my request. (I have other methods that use headers for some action)

How can I send one or more headers using QueryRequestBuilder?

I'll tried to use this, I received 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);

Upvotes: 1

Views: 1378

Answers (1)

Tobias Tengler
Tobias Tengler

Reputation: 7454

The level you are testing on (with the QueryRequestBuilder) is transport-agnostic. If you want to do "full" integration testing with the HTTP layer, look into doing proper ASP.NET Core integration tests using WebApplicationFactory.

If you just want to test your code using a ClaimsPrincipal you can do .TryAddProperty(nameof(ClaimsPrincipal), yourPrincipal), where yourPrincipal is a ClaimsPrincipal you created in the Test containing your roles, etc.

EDIT: In newer version you can also just do .SetUser(yourPrincipal).

Upvotes: 3

Related Questions