Skip to content
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

Allow clients to assert the webTestClient response status #778

Closed
ivangfr opened this issue Aug 20, 2023 · 2 comments
Closed

Allow clients to assert the webTestClient response status #778

ivangfr opened this issue Aug 20, 2023 · 2 comments
Labels
in: test Issues related to the test module status: declined A suggestion or change that we don't feel we should currently apply

Comments

@ivangfr
Copy link

ivangfr commented Aug 20, 2023

Hi! I have a Spring Boot that exposes a GraphQL API, and it's secured with LDAP. Now, I am implementing some test cases using HttpGraphQlTester.

The problem I am facing is when I inform invalid credentials.

For instance, let's suppose that a valid user has the username app-user and password 123.

All good when I call

GraphQlTester.Response response = tester
        .mutate()
        .headers(headers -> headers.setBasicAuth("app-user", "123"))
        .build()
        .document({...})
        .execute();

I get 200 and the response;

The same happens when I do not inform the credentials

GraphQlTester.Response response = tester
        .document({...})
        .execute();

I get 200 and the error in my response;

However, when informing invalid credentials

GraphQlTester.Response response = tester
        .mutate()
        .headers(headers -> headers.setBasicAuth("app-user-2", "123"))
        .build()
        .document({...})
        .execute();

I get 401 and the test fails.

Checking the library code, I've seen that we are asserting the webClientTest response to be .expectStatus().isOk().

final class WebTestClientTransport implements GraphQlTransport {
	...
	@Override
	public Mono<GraphQlResponse> execute(GraphQlRequest request) {

		Map<String, Object> responseMap = this.webTestClient.post()
				.contentType(MediaType.APPLICATION_JSON)
				.accept(MediaType.APPLICATION_JSON)
				.bodyValue(request.toMap())
				.exchange()
				.expectStatus().isOk()
				.expectHeader().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)
				.expectBody(MAP_TYPE)
				.returnResult()
				.getResponseBody();
		...
	}
	...
}

Is it (or would it be) possible to not assert inside the library and let the clients assert in their test cases?

Best regards,

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Aug 20, 2023
@bclozel
Copy link
Member

bclozel commented Aug 28, 2023

Valid GraphQL responses must have the expected content-type and, have a 200 OK response status and a valid response body. If you're trying to test cases where transport-specific features are involved, using a WebTestClient is more appropriate.

@bclozel bclozel closed this as not planned Won't fix, can't repro, duplicate, stale Aug 28, 2023
@bclozel bclozel added status: declined A suggestion or change that we don't feel we should currently apply in: test Issues related to the test module and removed status: waiting-for-triage An issue we've not yet triaged labels Aug 28, 2023
@rstoyanchev
Copy link
Contributor

Indeed the 401 occurs at the transport level before it even gets to the GraphQL handler, and using a regular HTTP request with an HTTP test client is a good fit for the scenario.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test Issues related to the test module status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

No branches or pull requests

4 participants