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.Assert;
47  import org.junit.Before;
48  import org.junit.Test;
49  import org.junit.runner.RunWith;
50  import org.mockito.Mock;
51  import org.mockito.Mockito;
52  import org.mockito.junit.MockitoJUnitRunner;
53  
54  @RunWith(MockitoJUnitRunner.class)
55  public class TestContentCompressionExec {
56  
57      @Mock
58      private ExecRuntime execRuntime;
59      @Mock
60      private ExecChain execChain;
61      @Mock
62      private ClassicHttpRequest originaRequest;
63  
64      private HttpClientContext context;
65      private HttpHost host;
66      private ExecChain.Scope scope;
67      private ContentCompressionExec impl;
68  
69      @Before
70      public void setup() {
71          host = new HttpHost("somehost", 80);
72          context = HttpClientContext.create();
73          scope = new ExecChain.Scope("test", new HttpRoute(host), originaRequest, execRuntime, context);
74          impl = new ContentCompressionExec();
75      }
76  
77  
78      @Test
79      public void testContentEncodingNoEntity() throws Exception {
80          final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, host, "/");
81          final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
82  
83          Mockito.when(execChain.proceed(request, scope)).thenReturn(response);
84  
85          impl.execute(request, scope, execChain);
86  
87          final HttpEntity entity = response.getEntity();
88          Assert.assertNull(entity);
89      }
90  
91      @Test
92      public void testNoContentEncoding() throws Exception {
93          final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, host, "/");
94          final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
95          final StringEntity original = new StringEntity("plain stuff");
96          response.setEntity(original);
97  
98          Mockito.when(execChain.proceed(request, scope)).thenReturn(response);
99  
100         impl.execute(request, scope, execChain);
101 
102         final HttpEntity entity = response.getEntity();
103         Assert.assertNotNull(entity);
104         Assert.assertTrue(entity instanceof StringEntity);
105     }
106 
107     @Test
108     public void testGzipContentEncoding() throws Exception {
109         final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, host, "/");
110         final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
111         final HttpEntity original = EntityBuilder.create().setText("encoded stuff").setContentEncoding("GZip").build();
112         response.setEntity(original);
113 
114         Mockito.when(execChain.proceed(request, scope)).thenReturn(response);
115 
116         impl.execute(request, scope, execChain);
117 
118         final HttpEntity entity = response.getEntity();
119         Assert.assertNotNull(entity);
120         Assert.assertTrue(entity instanceof DecompressingEntity);
121     }
122 
123     @Test
124     public void testGzipContentEncodingZeroLength() throws Exception {
125         final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, host, "/");
126         final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
127         final HttpEntity original = EntityBuilder.create().setText("").setContentEncoding("GZip").build();
128         response.setEntity(original);
129 
130         Mockito.when(execChain.proceed(request, scope)).thenReturn(response);
131 
132         impl.execute(request, scope, execChain);
133 
134         final HttpEntity entity = response.getEntity();
135         Assert.assertNotNull(entity);
136         Assert.assertTrue(entity instanceof StringEntity);
137     }
138 
139     @Test
140     public void testXGzipContentEncoding() throws Exception {
141         final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, host, "/");
142         final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
143         final HttpEntity original = EntityBuilder.create().setText("encoded stuff").setContentEncoding("x-gzip").build();
144         response.setEntity(original);
145 
146         Mockito.when(execChain.proceed(request, scope)).thenReturn(response);
147 
148         impl.execute(request, scope, execChain);
149 
150         final HttpEntity entity = response.getEntity();
151         Assert.assertNotNull(entity);
152         Assert.assertTrue(entity instanceof DecompressingEntity);
153     }
154 
155     @Test
156     public void testDeflateContentEncoding() throws Exception {
157         final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, host, "/");
158         final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
159         final HttpEntity original = EntityBuilder.create().setText("encoded stuff").setContentEncoding("deFlaTe").build();
160         response.setEntity(original);
161 
162         Mockito.when(execChain.proceed(request, scope)).thenReturn(response);
163 
164         impl.execute(request, scope, execChain);
165 
166         final HttpEntity entity = response.getEntity();
167         Assert.assertNotNull(entity);
168         Assert.assertTrue(entity instanceof DecompressingEntity);
169     }
170 
171     @Test
172     public void testIdentityContentEncoding() throws Exception {
173         final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, host, "/");
174         final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
175         final HttpEntity original = EntityBuilder.create().setText("encoded stuff").setContentEncoding("identity").build();
176         response.setEntity(original);
177 
178         Mockito.when(execChain.proceed(request, scope)).thenReturn(response);
179 
180         impl.execute(request, scope, execChain);
181 
182         final HttpEntity entity = response.getEntity();
183         Assert.assertNotNull(entity);
184         Assert.assertTrue(entity instanceof StringEntity);
185     }
186 
187     @Test(expected=HttpException.class)
188     public void testUnknownContentEncoding() throws Exception {
189         final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, host, "/");
190         final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
191         final HttpEntity original = EntityBuilder.create().setText("encoded stuff").setContentEncoding("whatever").build();
192         response.setEntity(original);
193 
194         impl = new ContentCompressionExec(false);
195 
196         Mockito.when(execChain.proceed(request, scope)).thenReturn(response);
197 
198         impl.execute(request, scope, execChain);
199     }
200 
201     @Test
202     public void testContentEncodingRequestParameter() throws Exception {
203         final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, host, "/");
204         final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
205         final HttpEntity original = EntityBuilder.create().setText("encoded stuff").setContentEncoding("GZip").build();
206         response.setEntity(original);
207 
208         final RequestConfig config = RequestConfig.custom()
209                 .setContentCompressionEnabled(false)
210                 .build();
211 
212         context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
213 
214         Mockito.when(execChain.proceed(request, scope)).thenReturn(response);
215 
216         impl.execute(request, scope, execChain);
217 
218         final HttpEntity entity = response.getEntity();
219         Assert.assertNotNull(entity);
220         Assert.assertFalse(entity instanceof GzipDecompressingEntity);
221     }
222 
223 }