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.http.client.protocol;
28  
29  import org.apache.http.HttpEntity;
30  import org.apache.http.HttpException;
31  import org.apache.http.HttpResponse;
32  import org.apache.http.HttpResponseInterceptor;
33  import org.apache.http.HttpVersion;
34  import org.apache.http.client.config.RequestConfig;
35  import org.apache.http.client.entity.DecompressingEntity;
36  import org.apache.http.client.entity.GzipDecompressingEntity;
37  import org.apache.http.entity.StringEntity;
38  import org.apache.http.message.BasicHttpResponse;
39  import org.apache.http.protocol.BasicHttpContext;
40  import org.apache.http.protocol.HttpContext;
41  import org.junit.Assert;
42  import org.junit.Test;
43  
44  public class TestResponseContentEncoding {
45  
46      @Test
47      public void testContentEncodingNoEntity() throws Exception {
48          final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
49          final HttpContext context = new BasicHttpContext();
50  
51          final HttpResponseInterceptor interceptor = new ResponseContentEncoding();
52          interceptor.process(response, context);
53          final HttpEntity entity = response.getEntity();
54          Assert.assertNull(entity);
55      }
56  
57      @Test
58      public void testNoContentEncoding() throws Exception {
59          final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
60          final StringEntity original = new StringEntity("plain stuff");
61          response.setEntity(original);
62          final HttpContext context = new BasicHttpContext();
63  
64          final HttpResponseInterceptor interceptor = new ResponseContentEncoding();
65          interceptor.process(response, context);
66          final HttpEntity entity = response.getEntity();
67          Assert.assertNotNull(entity);
68          Assert.assertTrue(entity instanceof StringEntity);
69      }
70  
71      @Test
72      public void testGzipContentEncoding() throws Exception {
73          final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
74          final StringEntity original = new StringEntity("encoded stuff");
75          original.setContentEncoding("GZip");
76          response.setEntity(original);
77          final HttpContext context = new BasicHttpContext();
78  
79          final HttpResponseInterceptor interceptor = new ResponseContentEncoding();
80          interceptor.process(response, context);
81          final HttpEntity entity = response.getEntity();
82          Assert.assertNotNull(entity);
83          Assert.assertTrue(entity instanceof DecompressingEntity);
84      }
85  
86      @Test
87      public void testGzipContentEncodingZeroLength() throws Exception {
88          final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
89          final StringEntity original = new StringEntity("");
90          original.setContentEncoding("GZip");
91          response.setEntity(original);
92          final HttpContext context = new BasicHttpContext();
93  
94          final HttpResponseInterceptor interceptor = new ResponseContentEncoding();
95          interceptor.process(response, context);
96          final HttpEntity entity = response.getEntity();
97          Assert.assertNotNull(entity);
98          Assert.assertTrue(entity instanceof StringEntity);
99      }
100 
101     @Test
102     public void testXGzipContentEncoding() throws Exception {
103         final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
104         final StringEntity original = new StringEntity("encoded stuff");
105         original.setContentEncoding("x-gzip");
106         response.setEntity(original);
107         final HttpContext context = new BasicHttpContext();
108 
109         final HttpResponseInterceptor interceptor = new ResponseContentEncoding();
110         interceptor.process(response, context);
111         final HttpEntity entity = response.getEntity();
112         Assert.assertNotNull(entity);
113         Assert.assertTrue(entity instanceof DecompressingEntity);
114     }
115 
116     @Test
117     public void testDeflateContentEncoding() throws Exception {
118         final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
119         final StringEntity original = new StringEntity("encoded stuff");
120         original.setContentEncoding("deFlaTe");
121         response.setEntity(original);
122         final HttpContext context = new BasicHttpContext();
123 
124         final HttpResponseInterceptor interceptor = new ResponseContentEncoding();
125         interceptor.process(response, context);
126         final HttpEntity entity = response.getEntity();
127         Assert.assertNotNull(entity);
128         Assert.assertTrue(entity instanceof DecompressingEntity);
129     }
130 
131     @Test
132     public void testIdentityContentEncoding() throws Exception {
133         final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
134         final StringEntity original = new StringEntity("encoded stuff");
135         original.setContentEncoding("identity");
136         response.setEntity(original);
137         final HttpContext context = new BasicHttpContext();
138 
139         final HttpResponseInterceptor interceptor = new ResponseContentEncoding();
140         interceptor.process(response, context);
141         final HttpEntity entity = response.getEntity();
142         Assert.assertNotNull(entity);
143         Assert.assertTrue(entity instanceof StringEntity);
144     }
145 
146     @Test(expected=HttpException.class)
147     public void testUnknownContentEncoding() throws Exception {
148         final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
149         final StringEntity original = new StringEntity("encoded stuff");
150         original.setContentEncoding("whatever");
151         response.setEntity(original);
152         final HttpContext context = new BasicHttpContext();
153 
154         final HttpResponseInterceptor interceptor = new ResponseContentEncoding(false);
155         interceptor.process(response, context);
156     }
157 
158     @Test
159     public void testContentEncodingRequestParameter() throws Exception {
160         final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
161         final StringEntity original = new StringEntity("encoded stuff");
162         original.setContentEncoding("GZip");
163         response.setEntity(original);
164 
165         final RequestConfig config = RequestConfig.custom()
166                 .setDecompressionEnabled(false)
167                 .build();
168 
169         final HttpContext context = new BasicHttpContext();
170         context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
171 
172         final HttpResponseInterceptor interceptor = new ResponseContentEncoding();
173         interceptor.process(response, context);
174         final HttpEntity entity = response.getEntity();
175         Assert.assertNotNull(entity);
176         Assert.assertFalse(entity instanceof GzipDecompressingEntity);
177     }
178 
179 }