简体   繁体   中英

Spring Cloud Gateway Custom Filter : WebClient.create().post() causes hanging when testing

So I've created a custom filter that, when accessed, will create a webflux client and post to a predetermined url. This seems to work fine when running, but when testing this code the test is hanging (until I cancel the test). So I feel there is a possible memory leak on top of not being able to complete the test to make sure this route is working properly. If I switch the WebClient method to get() then a resulting test of the filter works fine. Something with a post() I am not sure what is missing.

@Component
class ProxyGatewayFilterFactory: AbstractGatewayFilterFactory<ProxyGatewayFilterFactory.Params>(Params::class.java) {
    override fun apply(params: Params): GatewayFilter {
        return OrderedGatewayFilter(
                GatewayFilter { exchange, chain ->
                    exchange.request.mutate().header("test","test1").build()
                    WebClient.create().post()
                            .uri(params.proxyBasePath)
                            .body(BodyInserters.fromDataBuffers(exchange.request.body))
                            .headers { it.addAll(exchange.request.headers) }
                            .exchange()
                            .flatMap {
                                println("the POST statusCode is "+it.statusCode())
                                Mono.just(it.statusCode().is2xxSuccessful)
                            }
                            .map {
                                exchange.request.mutate().header("test", "test2").build()
                                println("exchange request uri is " + exchange.request.uri)
                                println("exchange response statusCode is "+ exchange.response.statusCode)
                                exchange
                            }
                            .flatMap(chain::filter)
                }, params.order)
}

Taken from the documentation, if using exchange you have an obligation to consume the body.

Unlike retrieve(), when using exchange(), it is the responsibility of the application to consume any response content regardless of the scenario (success, error, unexpected data, etc). Not doing so can cause a memory leak. The Javadoc for ClientResponse lists all the available options for consuming the body. Generally prefer using retrieve() unless you have a good reason for using exchange() which does allow to check the response status and headers before deciding how to or if to consume the response.

Spring framework 5.2.9 Webclient

This api has been changed in the latest version of the spring framework 5.3.0 now spring will force you to consume the body, because developers didn't actually read the docs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM