View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.hc.client5.http.impl.classic;
28  
29  import org.apache.hc.client5.http.HttpRoute;
30  import org.apache.hc.client5.http.classic.ExecChain;
31  import org.apache.hc.client5.http.classic.ExecRuntime;
32  import org.apache.hc.client5.http.config.RequestConfig;
33  import org.apache.hc.client5.http.entity.DecompressingEntity;
34  import org.apache.hc.client5.http.entity.EntityBuilder;
35  import org.apache.hc.client5.http.entity.GzipDecompressingEntity;
36  import org.apache.hc.client5.http.protocol.HttpClientContext;
37  import org.apache.hc.core5.http.ClassicHttpRequest;
38  import org.apache.hc.core5.http.ClassicHttpResponse;
39  import org.apache.hc.core5.http.HttpEntity;
40  import org.apache.hc.core5.http.HttpException;
41  import org.apache.hc.core5.http.HttpHost;
42  import org.apache.hc.core5.http.Method;
43  import org.apache.hc.core5.http.io.entity.StringEntity;
44  import org.apache.hc.core5.http.message.BasicClassicHttpRequest;
45  import org.apache.hc.core5.http.message.BasicClassicHttpResponse;
46  import org.junit.jupiter.api.Assertions;
47  import org.junit.jupiter.api.BeforeEach;
48  import org.junit.jupiter.api.Test;
49  import org.mockito.Mock;
50  import org.mockito.Mockito;
51  import org.mockito.MockitoAnnotations;
52  
53  public class TestContentCompressionExec {
54  
55      @Mock
56      private ExecRuntime execRuntime;
57      @Mock
58      private ExecChain execChain;
59      @Mock
60      private ClassicHttpRequest originaRequest;
61  
62      private HttpClientContext context;
63      private HttpHost host;
64      private ExecChain.Scope scope;
65      private ContentCompressionExec impl;
66  
67      @BeforeEach
68      public void setup() {
69          MockitoAnnotations.openMocks(this);
70          host = new HttpHost("somehost", 80);
71          context = HttpClientContext.create();
72          scope = new ExecChain.Scope("test", new HttpRoute(host), originaRequest, execRuntime, context);
73          impl = new ContentCompressionExec();
74      }
75  
76  
77      @Test
78      public void testContentEncodingNoEntity() throws Exception {
79          final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, host, "/");
80          final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
81  
82          Mockito.when(execChain.proceed(request, scope)).thenReturn(response);
83  
84          impl.execute(request, scope, execChain);
85  
86          final HttpEntity entity = response.getEntity();
87          Assertions.assertNull(entity);
88      }
89  
90      @Test
91      public void testNoContentEncoding() throws Exception {
92          final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, host, "/");
93          final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
94          final StringEntity original = new StringEntity("plain stuff");
95          response.setEntity(original);
96  
97          Mockito.when(execChain.proceed(request, scope)).thenReturn(response);
98  
99          impl.execute(request, scope, execChain);
100 
101         final HttpEntity entity = response.getEntity();
102         Assertions.assertNotNull(entity);
103         Assertions.assertTrue(entity instanceof StringEntity);
104     }
105 
106     @Test
107     public void testGzipContentEncoding() throws Exception {
108         final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, host, "/");
109         final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
110         final HttpEntity original = EntityBuilder.create().setText("encoded stuff").setContentEncoding("GZip").build();
111         response.setEntity(original);
112 
113         Mockito.when(execChain.proceed(request, scope)).thenReturn(response);
114 
115         impl.execute(request, scope, execChain);
116 
117         final HttpEntity entity = response.getEntity();
118         Assertions.assertNotNull(entity);
119         Assertions.assertTrue(entity instanceof DecompressingEntity);
120     }
121 
122     @Test
123     public void testGzipContentEncodingZeroLength() throws Exception {
124         final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, host, "/");
125         final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
126         final HttpEntity original = EntityBuilder.create().setText("").setContentEncoding("GZip").build();
127         response.setEntity(original);
128 
129         Mockito.when(execChain.proceed(request, scope)).thenReturn(response);
130 
131         impl.execute(request, scope, execChain);
132 
133         final HttpEntity entity = response.getEntity();
134         Assertions.assertNotNull(entity);
135         Assertions.assertTrue(entity instanceof StringEntity);
136     }
137 
138     @Test
139     public void testXGzipContentEncoding() throws Exception {
140         final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, host, "/");
141         final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
142         final HttpEntity original = EntityBuilder.create().setText("encoded stuff").setContentEncoding("x-gzip").build();
143         response.setEntity(original);
144 
145         Mockito.when(execChain.proceed(request, scope)).thenReturn(response);
146 
147         impl.execute(request, scope, execChain);
148 
149         final HttpEntity entity = response.getEntity();
150         Assertions.assertNotNull(entity);
151         Assertions.assertTrue(entity instanceof DecompressingEntity);
152     }
153 
154     @Test
155     public void testDeflateContentEncoding() throws Exception {
156         final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, host, "/");
157         final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
158         final HttpEntity original = EntityBuilder.create().setText("encoded stuff").setContentEncoding("deFlaTe").build();
159         response.setEntity(original);
160 
161         Mockito.when(execChain.proceed(request, scope)).thenReturn(response);
162 
163         impl.execute(request, scope, execChain);
164 
165         final HttpEntity entity = response.getEntity();
166         Assertions.assertNotNull(entity);
167         Assertions.assertTrue(entity instanceof DecompressingEntity);
168     }
169 
170     @Test
171     public void testIdentityContentEncoding() throws Exception {
172         final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, host, "/");
173         final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
174         final HttpEntity original = EntityBuilder.create().setText("encoded stuff").setContentEncoding("identity").build();
175         response.setEntity(original);
176 
177         Mockito.when(execChain.proceed(request, scope)).thenReturn(response);
178 
179         impl.execute(request, scope, execChain);
180 
181         final HttpEntity entity = response.getEntity();
182         Assertions.assertNotNull(entity);
183         Assertions.assertTrue(entity instanceof StringEntity);
184     }
185 
186     @Test
187     public void testBrotliContentEncoding() throws Exception {
188         final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, host, "/");
189         final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
190         final HttpEntity original = EntityBuilder.create().setText("encoded stuff").setContentEncoding("br").build();
191         response.setEntity(original);
192 
193         Mockito.when(execChain.proceed(request, scope)).thenReturn(response);
194 
195         impl.execute(request, scope, execChain);
196 
197         final HttpEntity entity = response.getEntity();
198         Assertions.assertNotNull(entity);
199         Assertions.assertTrue(entity instanceof DecompressingEntity);
200     }
201 
202     @Test
203     public void testUnknownContentEncoding() throws Exception {
204         final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, host, "/");
205         final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
206         final HttpEntity original = EntityBuilder.create().setText("encoded stuff").setContentEncoding("whatever").build();
207         response.setEntity(original);
208 
209         impl = new ContentCompressionExec(false);
210 
211         Mockito.when(execChain.proceed(request, scope)).thenReturn(response);
212 
213         Assertions.assertThrows(HttpException.class, () ->
214                 impl.execute(request, scope, execChain));
215     }
216 
217     @Test
218     public void testContentEncodingRequestParameter() throws Exception {
219         final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, host, "/");
220         final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
221         final HttpEntity original = EntityBuilder.create().setText("encoded stuff").setContentEncoding("GZip").build();
222         response.setEntity(original);
223 
224         final RequestConfig config = RequestConfig.custom()
225                 .setContentCompressionEnabled(false)
226                 .build();
227 
228         context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
229 
230         Mockito.when(execChain.proceed(request, scope)).thenReturn(response);
231 
232         impl.execute(request, scope, execChain);
233 
234         final HttpEntity entity = response.getEntity();
235         Assertions.assertNotNull(entity);
236         Assertions.assertFalse(entity instanceof GzipDecompressingEntity);
237     }
238 
239 }