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.impl.client.cache;
28  
29  import static org.mockito.Matchers.isA;
30  import static org.mockito.Matchers.same;
31  import static org.mockito.Mockito.mock;
32  import static org.mockito.Mockito.verify;
33  import static org.mockito.Mockito.when;
34  
35  import java.util.Date;
36  import java.util.HashMap;
37  
38  import org.apache.http.Header;
39  import org.apache.http.HttpResponse;
40  import org.apache.http.client.cache.HttpCacheEntry;
41  import org.apache.http.client.methods.HttpRequestWrapper;
42  import org.apache.http.message.BasicHeader;
43  import org.junit.Assert;
44  import org.junit.Before;
45  import org.junit.Test;
46  
47  @SuppressWarnings({"boxing","static-access"}) // test code
48  public class TestCachedHttpResponseGenerator {
49  
50      private HttpCacheEntry entry;
51      private HttpRequestWrapper request;
52      private CacheValidityPolicy mockValidityPolicy;
53      private CachedHttpResponseGenerator impl;
54  
55      @Before
56      public void setUp() {
57          entry = HttpTestUtils.makeCacheEntry(new HashMap<String, String>());
58          request = HttpRequestWrapper.wrap(HttpTestUtils.makeDefaultRequest());
59          mockValidityPolicy = mock(CacheValidityPolicy.class);
60          impl = new CachedHttpResponseGenerator(mockValidityPolicy);
61      }
62  
63      @Test
64      public void testResponseHasContentLength() {
65          final byte[] buf = new byte[] { 1, 2, 3, 4, 5 };
66          final HttpCacheEntry entry1 = HttpTestUtils.makeCacheEntry(buf);
67  
68          final HttpResponse response = impl.generateResponse(request, entry1);
69  
70          final Header length = response.getFirstHeader("Content-Length");
71          Assert.assertNotNull("Content-Length Header is missing", length);
72  
73          Assert.assertEquals("Content-Length does not match buffer length", buf.length, Integer
74                  .parseInt(length.getValue()));
75      }
76  
77      @Test
78      public void testContentLengthIsNotAddedWhenTransferEncodingIsPresent() {
79  
80          final Header[] hdrs = new Header[] { new BasicHeader("Transfer-Encoding", "chunked") };
81          final byte[] buf = new byte[] { 1, 2, 3, 4, 5 };
82          final HttpCacheEntry entry1 = HttpTestUtils.makeCacheEntry(hdrs, buf);
83  
84          final HttpResponse response = impl.generateResponse(request, entry1);
85  
86          final Header length = response.getFirstHeader("Content-Length");
87  
88          Assert.assertNull(length);
89      }
90  
91      @Test
92      public void testResponseMatchesCacheEntry() {
93          final HttpResponse response = impl.generateResponse(request, entry);
94  
95          Assert.assertTrue(response.containsHeader("Content-Length"));
96  
97          Assert.assertSame("HTTP", response.getProtocolVersion().getProtocol());
98          Assert.assertEquals(1, response.getProtocolVersion().getMajor());
99          Assert.assertEquals(1, response.getProtocolVersion().getMinor());
100     }
101 
102     @Test
103     public void testResponseStatusCodeMatchesCacheEntry() {
104         final HttpResponse response = impl.generateResponse(request, entry);
105 
106         Assert.assertEquals(entry.getStatusCode(), response.getStatusLine().getStatusCode());
107     }
108 
109     @Test
110     public void testAgeHeaderIsPopulatedWithCurrentAgeOfCacheEntryIfNonZero() {
111         currentAge(10L);
112 
113         final HttpResponse response = impl.generateResponse(request, entry);
114 
115         verify(mockValidityPolicy).getCurrentAgeSecs(same(entry), isA(Date.class));
116 
117         final Header ageHdr = response.getFirstHeader("Age");
118         Assert.assertNotNull(ageHdr);
119         Assert.assertEquals(10L, Long.parseLong(ageHdr.getValue()));
120     }
121 
122     @Test
123     public void testAgeHeaderIsNotPopulatedIfCurrentAgeOfCacheEntryIsZero() {
124         currentAge(0L);
125 
126         final HttpResponse response = impl.generateResponse(request, entry);
127 
128         verify(mockValidityPolicy).getCurrentAgeSecs(same(entry), isA(Date.class));
129 
130         final Header ageHdr = response.getFirstHeader("Age");
131         Assert.assertNull(ageHdr);
132     }
133 
134     @Test
135     public void testAgeHeaderIsPopulatedWithMaxAgeIfCurrentAgeTooBig() {
136         currentAge(CacheValidityPolicy.MAX_AGE + 1L);
137 
138         final HttpResponse response = impl.generateResponse(request, entry);
139 
140         verify(mockValidityPolicy).getCurrentAgeSecs(same(entry), isA(Date.class));
141 
142         final Header ageHdr = response.getFirstHeader("Age");
143         Assert.assertNotNull(ageHdr);
144         Assert.assertEquals(CacheValidityPolicy.MAX_AGE, Long.parseLong(ageHdr.getValue()));
145     }
146 
147     private void currentAge(final long sec) {
148         when(
149                 mockValidityPolicy.getCurrentAgeSecs(same(entry),
150                         isA(Date.class))).thenReturn(sec);
151     }
152 
153     @Test
154     public void testResponseContainsEntityToServeGETRequestIfEntryContainsResource() throws Exception {
155         final HttpResponse response = impl.generateResponse(request, entry);
156 
157         Assert.assertNotNull(response.getEntity());
158     }
159 
160     @Test
161     public void testResponseDoesNotContainEntityToServeHEADRequestIfEntryContainsResource() throws Exception {
162         final HttpRequestWrapper headRequest = HttpRequestWrapper.wrap(HttpTestUtils.makeDefaultHEADRequest());
163         final HttpResponse response = impl.generateResponse(headRequest, entry);
164 
165         Assert.assertNull(response.getEntity());
166     }
167 
168 }