Skip to content

Commit f494b79

Browse files
AXIS2-6101 Add unit tests for HC5 5.6+ content encoding behavior
4 tests covering the double-decompression fix: - testDecompressedEntityWithGzipHeader: HC5 5.6+ scenario where Content-Encoding header says "gzip" but entity encoding is null (already decompressed). Verifies Axis2 does NOT re-decompress. - testEntityWithGzipContentEncoding: entity encoding says "gzip" (pre-5.6 behavior or decompression disabled). Axis2 should decompress. - testNoContentEncoding: no encoding at all (common case). - testIdentityContentEncoding: "identity" encoding treated as no encoding. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent e9c1a0f commit f494b79

1 file changed

Lines changed: 99 additions & 0 deletions

File tree

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.axis2.transport.http;
20+
21+
import junit.framework.TestCase;
22+
import org.apache.axis2.kernel.http.HTTPConstants;
23+
import org.apache.hc.core5.http.ContentType;
24+
import org.apache.hc.core5.http.io.entity.StringEntity;
25+
import org.apache.hc.core5.http.message.BasicClassicHttpResponse;
26+
27+
/**
28+
* Tests for AXIS2-6101: HttpClient 5.6+ double gzip decompression.
29+
*
30+
* <p>Starting with HC5 5.6, ContentCompressionExec decompresses gzip
31+
* responses but no longer removes the Content-Encoding header. Axis2
32+
* must check the entity's content encoding (which reflects the actual
33+
* state after decompression) rather than the response header.
34+
*/
35+
public class ContentEncodingTest extends TestCase {
36+
37+
/**
38+
* Simulates HC5 5.6+ behavior: Content-Encoding header says "gzip"
39+
* but the entity has no content encoding (already decompressed).
40+
* Axis2 must NOT attempt to decompress again.
41+
*/
42+
public void testDecompressedEntityWithGzipHeader() throws Exception {
43+
BasicClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
44+
// HC5 5.6+ leaves the header but the entity is already decompressed
45+
response.setHeader(HTTPConstants.HEADER_CONTENT_ENCODING, HTTPConstants.COMPRESSION_GZIP);
46+
StringEntity entity = new StringEntity("already decompressed content");
47+
// StringEntity.getContentEncoding() returns null — no encoding on the entity
48+
response.setEntity(entity);
49+
50+
// Verify the header still says gzip (this is what the old code checked)
51+
assertEquals("gzip", response.getHeader(HTTPConstants.HEADER_CONTENT_ENCODING).getValue());
52+
53+
// Verify the entity's content encoding is null (this is what the fix checks)
54+
assertNull("Entity content encoding should be null after HC5 decompression",
55+
entity.getContentEncoding());
56+
}
57+
58+
/**
59+
* Simulates pre-HC5-5.6 behavior OR a server that sends gzip without
60+
* HC5 decompression (e.g., decompression disabled). The entity's content
61+
* encoding says "gzip", so Axis2 should decompress.
62+
*/
63+
public void testEntityWithGzipContentEncoding() throws Exception {
64+
BasicClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
65+
response.setHeader(HTTPConstants.HEADER_CONTENT_ENCODING, HTTPConstants.COMPRESSION_GZIP);
66+
StringEntity entity = new StringEntity("compressed content", ContentType.DEFAULT_TEXT, "gzip", false);
67+
response.setEntity(entity);
68+
69+
// Both the header and the entity say gzip — Axis2 should decompress
70+
assertEquals("gzip", response.getHeader(HTTPConstants.HEADER_CONTENT_ENCODING).getValue());
71+
assertEquals("gzip", entity.getContentEncoding());
72+
}
73+
74+
/**
75+
* No content encoding at all — the common case for uncompressed responses.
76+
*/
77+
public void testNoContentEncoding() throws Exception {
78+
BasicClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
79+
StringEntity entity = new StringEntity("plain content");
80+
response.setEntity(entity);
81+
82+
// No Content-Encoding header
83+
assertNull(response.getHeader(HTTPConstants.HEADER_CONTENT_ENCODING));
84+
// No entity content encoding
85+
assertNull(entity.getContentEncoding());
86+
}
87+
88+
/**
89+
* Identity content encoding — should be treated as no encoding.
90+
*/
91+
public void testIdentityContentEncoding() throws Exception {
92+
BasicClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
93+
StringEntity entity = new StringEntity("identity content", ContentType.DEFAULT_TEXT, "identity", false);
94+
response.setEntity(entity);
95+
96+
assertEquals("identity", entity.getContentEncoding());
97+
// Axis2 should ignore "identity" encoding (pass through without decompression)
98+
}
99+
}

0 commit comments

Comments
 (0)