diff --git a/spring-web/src/main/java/org/springframework/http/client/reactive/AbstractClientHttpRequest.java b/spring-web/src/main/java/org/springframework/http/client/reactive/AbstractClientHttpRequest.java index cc0d7230c7..622e6bd6f4 100644 --- a/spring-web/src/main/java/org/springframework/http/client/reactive/AbstractClientHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/client/reactive/AbstractClientHttpRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -118,12 +118,12 @@ public abstract class AbstractClientHttpRequest implements ClientHttpRequest { return Mono.empty(); } - this.commitActions.add(() -> { - applyHeaders(); - applyCookies(); - this.state.set(State.COMMITTED); - return Mono.empty(); - }); + this.commitActions.add(() -> + Mono.fromRunnable(() -> { + applyHeaders(); + applyCookies(); + this.state.set(State.COMMITTED); + })); if (writeAction != null) { this.commitActions.add(writeAction); @@ -132,7 +132,7 @@ public abstract class AbstractClientHttpRequest implements ClientHttpRequest { List> actions = this.commitActions.stream() .map(Supplier::get).collect(Collectors.toList()); - return Mono.fromDirect(Flux.concat(actions)); + return Flux.concat(actions).then(); } diff --git a/spring-web/src/main/java/org/springframework/http/codec/EncoderHttpMessageWriter.java b/spring-web/src/main/java/org/springframework/http/codec/EncoderHttpMessageWriter.java index 4157328fcc..1541fc6927 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/EncoderHttpMessageWriter.java +++ b/spring-web/src/main/java/org/springframework/http/codec/EncoderHttpMessageWriter.java @@ -103,11 +103,14 @@ public class EncoderHttpMessageWriter implements HttpMessageWriter { Flux body = this.encoder.encode( inputStream, message.bufferFactory(), elementType, contentType, hints); - // Response is not committed until the first signal... if (inputStream instanceof Mono) { HttpHeaders headers = message.getHeaders(); if (headers.getContentLength() < 0 && !headers.containsKey(HttpHeaders.TRANSFER_ENCODING)) { - body = body.doOnNext(data -> headers.setContentLength(data.readableByteCount())); + return Mono.from(body) + .flatMap(dataBuffer -> { + headers.setContentLength(dataBuffer.readableByteCount()); + return message.writeWith(Mono.just(dataBuffer)); + }); } } diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/BodyInsertersTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/BodyInsertersTests.java index 40c51f98e4..cbacb6c8dd 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/BodyInsertersTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/BodyInsertersTests.java @@ -340,10 +340,12 @@ public class BodyInsertersTests { String content = new String(resultBytes, StandardCharsets.UTF_8); assertThat(content, containsString("Content-Disposition: form-data; name=\"name\"\r\n" + "Content-Type: text/plain;charset=UTF-8\r\n" + + "Content-Length: 6\r\n" + "\r\n" + "value1")); assertThat(content, containsString("Content-Disposition: form-data; name=\"name\"\r\n" + "Content-Type: text/plain;charset=UTF-8\r\n" + + "Content-Length: 6\r\n" + "\r\n" + "value2")); }) diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientIntegrationTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientIntegrationTests.java index d3de996088..86e2ee228b 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientIntegrationTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientIntegrationTests.java @@ -317,7 +317,7 @@ public class WebClientIntegrationTests { expectRequest(request -> { assertEquals("/pojo/capitalize", request.getPath()); assertEquals("{\"foo\":\"foofoo\",\"bar\":\"barbar\"}", request.getBody().readUtf8()); - assertEquals("chunked", request.getHeader(HttpHeaders.TRANSFER_ENCODING)); + assertEquals("31", request.getHeader(HttpHeaders.CONTENT_LENGTH)); assertEquals("application/json", request.getHeader(HttpHeaders.ACCEPT)); assertEquals("application/json", request.getHeader(HttpHeaders.CONTENT_TYPE)); });