spring-projects / spring-graphql

Spring Integration for GraphQL
https://spring.io/projects/spring-graphql
Apache License 2.0
1.52k stars 298 forks source link

subscription integration test fails with web starter #390

Closed Thinkenterprise closed 2 years ago

Thinkenterprise commented 2 years ago

I wrote the following test

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class TestWebRouteSubscription {

    @Value("ws://localhost:${local.server.port}${spring.graphql.websocket.path}")
    private String baseUrl;

    @Autowired
    private GraphQlTester webGraphQlTester;

    @BeforeEach
    void setUp() {
        URI url = URI.create(baseUrl);
        this.webGraphQlTester = WebSocketGraphQlTester.builder(url, new ReactorNettyWebSocketClient()).build();
    }

    @Test
    @DisplayName("Test register Route Creted Subscription")
    public void registerRouteCreated() {

        Flux<Route> result = webGraphQlTester.documentName("registerRouteCreated").executeSubscription()
                .toFlux("registerRouteCreated", Route.class);
        StepVerifier verifier = StepVerifier.create(result).expectNextCount(1L).thenCancel().verifyLater();
        webGraphQlTester.documentName("createRoute").variable("flightNumber", "LH7977").execute()
                .path("createRoute.flightNumber").entity(String.class).isEqualTo("LH7977");

        verifier.verify();
    }
}

For the test I have the following dependency active

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-graphql</artifactId>
        </dependency>
        <!-- Database Feature Dependencies -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
        <!-- Test Feature Dependencies -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.graphql</groupId>
            <artifactId>spring-graphql-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>

In this setting, the test runs and works!!

If I change the first dependency from

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

to

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

the test fails. The error message is:

org.springframework.graphql.client.GraphQlTransportException: GraphQlTransport error: Invalid handshake response getStatus: 405 ; nested exception is io.netty.handler.codec.http.websocketx.WebSocketClientHandshakeException: Invalid handshake response getStatus: 405 

In both cases web and weblux a websocket configuration is active? I cant understand this problem?

bclozel commented 2 years ago

spring-boot-starter-webflux has both HTTP and WebSocket support, whereas spring-boot-starter-web only ships HTTP support. What happens if you also add spring-boot-starter-websocket to your application?

Thinkenterprise commented 2 years ago

Yes of course you were right. I opened the issue too quickly, sorry

bclozel commented 2 years ago

No worries, thanks for testing before the release, this is helping!

sgrannan commented 7 months ago

@Thinkenterprise do you mind explaining what the problem was and what you did to solve it? Mostly for people like me who come here with the exact same setup and issue but don't have the same understanding?

I am using the following dependencies, including web in place of webflux (except for testing) but I DO include spring-boot-starter-websocket and -security and yet I still receive error like you generated it, originally using web.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-graphql</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- Database Feature Dependencies -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
</dependency>
<!-- Test Feature Dependencies -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework.graphql</groupId>
    <artifactId>spring-graphql-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>io.projectreactor</groupId>
    <artifactId>reactor-test</artifactId>
    <scope>test</scope>
</dependency>