Do not decompress HTTP responses when compression disabled
This commit refines changes made in gh-35225 so as to not decompress HTTP responses if decompression support is not enabled. Closes gh-35225
This commit is contained in:
		
							parent
							
								
									441b14b0c1
								
							
						
					
					
						commit
						9979de99fd
					
				|  | @ -112,7 +112,7 @@ class JdkClientHttpRequest extends AbstractStreamingClientHttpRequest { | |||
| 		TimeoutHandler timeoutHandler = null; | ||||
| 		try { | ||||
| 			HttpRequest request = buildRequest(headers, body); | ||||
| 			responseFuture = this.httpClient.sendAsync(request, new DecompressingBodyHandler()); | ||||
| 			responseFuture = this.httpClient.sendAsync(request, this.compression ? new DecompressingBodyHandler() : HttpResponse.BodyHandlers.ofInputStream()); | ||||
| 
 | ||||
| 			if (this.timeout != null) { | ||||
| 				timeoutHandler = new TimeoutHandler(responseFuture, this.timeout); | ||||
|  |  | |||
|  | @ -116,29 +116,26 @@ public abstract class AbstractMockWebServerTests { | |||
| 					String encoding = request.getTarget().replace("/compress/",""); | ||||
| 					String requestBody = request.getBody().utf8(); | ||||
| 					ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||||
| 					if (encoding.equals("gzip")) { | ||||
| 						try(GZIPOutputStream gzipOutputStream = new GZIPOutputStream(outputStream)) { | ||||
| 							gzipOutputStream.write(requestBody.getBytes()); | ||||
| 							gzipOutputStream.flush(); | ||||
| 						} | ||||
| 					} | ||||
| 					else if(encoding.equals("deflate")) { | ||||
| 					if(encoding.equals("deflate")) { | ||||
| 							try(DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(outputStream)) { | ||||
| 							deflaterOutputStream.write(requestBody.getBytes()); | ||||
| 							deflaterOutputStream.flush(); | ||||
| 						} | ||||
| 					} | ||||
| 					// compress anyway with gzip | ||||
| 					else { | ||||
| 						outputStream.write(requestBody.getBytes()); | ||||
| 						encoding = "gzip"; | ||||
| 						try(GZIPOutputStream gzipOutputStream = new GZIPOutputStream(outputStream)) { | ||||
| 							gzipOutputStream.write(requestBody.getBytes()); | ||||
| 							gzipOutputStream.flush(); | ||||
| 						} | ||||
| 					} | ||||
| 					Buffer buffer = new Buffer(); | ||||
| 					buffer.write(outputStream.toByteArray()); | ||||
| 					MockResponse.Builder builder = new MockResponse.Builder() | ||||
| 							.body(buffer) | ||||
| 							.code(200); | ||||
| 					if (!encoding.isEmpty()) { | ||||
| 					builder.setHeader(HttpHeaders.CONTENT_ENCODING, encoding); | ||||
| 					} | ||||
| 					return builder.build(); | ||||
| 				} | ||||
| 				return new MockResponse.Builder().code(404).build(); | ||||
|  |  | |||
|  | @ -110,12 +110,18 @@ class JdkClientHttpRequestFactoryTests extends AbstractHttpRequestFactoryTests { | |||
| 	@Test | ||||
| 	void compressionDisabled() throws IOException { | ||||
| 		URI uri = URI.create(baseUrl + "/compress/"); | ||||
| 		if (this.factory instanceof JdkClientHttpRequestFactory jdkClientHttpRequestFactory) { | ||||
| 			jdkClientHttpRequestFactory.enableCompression(false); | ||||
| 		} | ||||
| 		ClientHttpRequest request = this.factory.createRequest(uri, HttpMethod.POST); | ||||
| 		StreamUtils.copy("Payload to compress", StandardCharsets.UTF_8, request.getBody()); | ||||
| 		try (ClientHttpResponse response = request.execute()) { | ||||
| 		assertThat(request.getHeaders().containsHeader("Accept-Encoding")).isFalse(); | ||||
| 			assertThat(response.getStatusCode()).as("Invalid response status").isEqualTo(HttpStatus.OK); | ||||
| 			assertThat(response.getHeaders().containsHeader("Content-Encoding")).isFalse(); | ||||
| 			assertThat(response.getBody()).as("Invalid request body").hasContent("Payload to compress"); | ||||
| 			assertThat(response.getHeaders().containsHeader("Content-Encoding")).isTrue(); | ||||
| 			assertThat(StreamUtils.copyToString(response.getBody(), StandardCharsets.UTF_8)) | ||||
| 					.as("Body should not be decompressed") | ||||
| 					.doesNotContain("Payload to compress"); | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue