View | Details | Raw Unified | Return to bug 58079
Collapse All | Expand All

(-)a/src/protocol/http/org/apache/jmeter/protocol/http/control/CacheManager.java (-3 / +15 lines)
Lines 122-128 public class CacheManager extends ConfigTestElement implements TestStateListener Link Here
122
     * @param res result
122
     * @param res result
123
     */
123
     */
124
    public void saveDetails(URLConnection conn, HTTPSampleResult res){
124
    public void saveDetails(URLConnection conn, HTTPSampleResult res){
125
        if (isCacheable(res)){
125
        if (isCacheable(res) && !hasVaryHeader(conn)){
126
            String lastModified = conn.getHeaderField(HTTPConstants.LAST_MODIFIED);
126
            String lastModified = conn.getHeaderField(HTTPConstants.LAST_MODIFIED);
127
            String expires = conn.getHeaderField(HTTPConstants.EXPIRES);
127
            String expires = conn.getHeaderField(HTTPConstants.EXPIRES);
128
            String etag = conn.getHeaderField(HTTPConstants.ETAG);
128
            String etag = conn.getHeaderField(HTTPConstants.ETAG);
Lines 133-138 public class CacheManager extends ConfigTestElement implements TestStateListener Link Here
133
        }
133
        }
134
    }
134
    }
135
135
136
    private boolean hasVaryHeader(URLConnection conn) {
137
        return conn.getHeaderField(HTTPConstants.VARY) != null;
138
    }
139
136
    /**
140
    /**
137
     * Save the Last-Modified, Etag, and Expires headers if the result is
141
     * Save the Last-Modified, Etag, and Expires headers if the result is
138
     * cacheable. Version for Commons HttpClient implementation.
142
     * cacheable. Version for Commons HttpClient implementation.
Lines 145-151 public class CacheManager extends ConfigTestElement implements TestStateListener Link Here
145
     *             if extraction of the the uri from <code>method</code> fails
149
     *             if extraction of the the uri from <code>method</code> fails
146
     */
150
     */
147
    public void saveDetails(HttpMethod method, HTTPSampleResult res) throws URIException{
151
    public void saveDetails(HttpMethod method, HTTPSampleResult res) throws URIException{
148
        if (isCacheable(res)){
152
        if (isCacheable(res) && !hasVaryHeader(method)){
149
            String lastModified = getHeader(method ,HTTPConstants.LAST_MODIFIED);
153
            String lastModified = getHeader(method ,HTTPConstants.LAST_MODIFIED);
150
            String expires = getHeader(method ,HTTPConstants.EXPIRES);
154
            String expires = getHeader(method ,HTTPConstants.EXPIRES);
151
            String etag = getHeader(method ,HTTPConstants.ETAG);
155
            String etag = getHeader(method ,HTTPConstants.ETAG);
Lines 156-161 public class CacheManager extends ConfigTestElement implements TestStateListener Link Here
156
        }
160
        }
157
    }
161
    }
158
162
163
    private boolean hasVaryHeader(HttpMethod method) {
164
        return getHeader(method, HTTPConstants.VARY) != null;
165
    }
166
159
    /**
167
    /**
160
     * Save the Last-Modified, Etag, and Expires headers if the result is
168
     * Save the Last-Modified, Etag, and Expires headers if the result is
161
     * cacheable. Version for Apache HttpClient implementation.
169
     * cacheable. Version for Apache HttpClient implementation.
Lines 166-172 public class CacheManager extends ConfigTestElement implements TestStateListener Link Here
166
     *            result to decide if result is cacheable
174
     *            result to decide if result is cacheable
167
     */
175
     */
168
    public void saveDetails(HttpResponse method, HTTPSampleResult res) {
176
    public void saveDetails(HttpResponse method, HTTPSampleResult res) {
169
        if (isCacheable(res)){
177
        if (isCacheable(res) && !hasVaryHeader(method)){
170
            String lastModified = getHeader(method ,HTTPConstants.LAST_MODIFIED);
178
            String lastModified = getHeader(method ,HTTPConstants.LAST_MODIFIED);
171
            String expires = getHeader(method ,HTTPConstants.EXPIRES);
179
            String expires = getHeader(method ,HTTPConstants.EXPIRES);
172
            String etag = getHeader(method ,HTTPConstants.ETAG);
180
            String etag = getHeader(method ,HTTPConstants.ETAG);
Lines 176-181 public class CacheManager extends ConfigTestElement implements TestStateListener Link Here
176
        }
184
        }
177
    }
185
    }
178
186
187
    private boolean hasVaryHeader(HttpResponse method) {
188
        return getHeader(method, HTTPConstants.VARY) != null;
189
    }
190
179
    // helper method to save the cache entry
191
    // helper method to save the cache entry
180
    private void setCache(String lastModified, String cacheControl, String expires, String etag, String url, String date) {
192
    private void setCache(String lastModified, String cacheControl, String expires, String etag, String url, String date) {
181
        if (log.isDebugEnabled()){
193
        if (log.isDebugEnabled()){
(-)a/src/protocol/http/org/apache/jmeter/protocol/http/util/HTTPConstantsInterface.java (-1 / +2 lines)
Lines 75-80 public interface HTTPConstantsInterface { Link Here
75
    String LAST_MODIFIED = "Last-Modified"; // $NON-NLS-1$
75
    String LAST_MODIFIED = "Last-Modified"; // $NON-NLS-1$
76
    String EXPIRES = "Expires"; // $NON-NLS-1$
76
    String EXPIRES = "Expires"; // $NON-NLS-1$
77
    String CACHE_CONTROL = "Cache-Control";  //e.g. public, max-age=259200
77
    String CACHE_CONTROL = "Cache-Control";  //e.g. public, max-age=259200
78
    String DATE = "Date";  //e.g. Date Header of response 
78
    String DATE = "Date";  //e.g. Date Header of response
79
    String VARY = "Vary"; // $NON-NLS-1$
79
80
80
}
81
}
(-)a/test/src/org/apache/jmeter/protocol/http/control/TestCacheManager.java (-2 / +35 lines)
Lines 73-78 public class TestCacheManager extends JMeterTestCase { Link Here
73
                return cacheControl;
73
                return cacheControl;
74
            } else if (HTTPConstants.DATE.equals(name)){
74
            } else if (HTTPConstants.DATE.equals(name)){
75
                return currentTimeInGMT;
75
                return currentTimeInGMT;
76
            } else if (HTTPConstants.VARY.equals(name)) {
77
                return vary;
76
            }
78
            }
77
            return super.getHeaderField(name);
79
            return super.getHeaderField(name);
78
        }
80
        }
Lines 105-112 public class TestCacheManager extends JMeterTestCase { Link Here
105
                return expires == null ? null : new Header(HTTPConstants.EXPIRES, expires);
107
                return expires == null ? null : new Header(HTTPConstants.EXPIRES, expires);
106
            } else if (HTTPConstants.CACHE_CONTROL.equals(headerName)) {
108
            } else if (HTTPConstants.CACHE_CONTROL.equals(headerName)) {
107
                return cacheControl == null ? null : new Header(HTTPConstants.CACHE_CONTROL, cacheControl);
109
                return cacheControl == null ? null : new Header(HTTPConstants.CACHE_CONTROL, cacheControl);
108
            } if (HTTPConstants.DATE.equals(headerName)) {
110
            } else if (HTTPConstants.DATE.equals(headerName)) {
109
                return this.dateHeader;
111
                return this.dateHeader;
112
            } else if (HTTPConstants.VARY.equals(headerName)) {
113
                return vary == null ? null : new Header(HTTPConstants.VARY, vary);
110
            }
114
            }
111
            return null;
115
            return null;
112
        }
116
        }
Lines 144-149 public class TestCacheManager extends JMeterTestCase { Link Here
144
    private static final TimeZone GMT = TimeZone.getTimeZone("GMT");
148
    private static final TimeZone GMT = TimeZone.getTimeZone("GMT");
145
    private CacheManager cacheManager;
149
    private CacheManager cacheManager;
146
    private String currentTimeInGMT;
150
    private String currentTimeInGMT;
151
    private String vary = null;
147
    private URL url;
152
    private URL url;
148
    private URI uri;
153
    private URI uri;
149
    private URLConnection urlConnection;
154
    private URLConnection urlConnection;
Lines 228-233 public class TestCacheManager extends JMeterTestCase { Link Here
228
        assertFalse("Should not find valid entry",this.cacheManager.inCache(url));
233
        assertFalse("Should not find valid entry",this.cacheManager.inCache(url));
229
    }
234
    }
230
235
236
    public void testCacheVaryJava() throws Exception{
237
        this.cacheManager.setUseExpires(true);
238
        this.cacheManager.testIterationStart(null);
239
        assertNull("Should not find entry",getThreadCacheEntry(LOCAL_HOST));
240
        assertFalse("Should not find valid entry",this.cacheManager.inCache(url));
241
        ((URLConnectionStub)urlConnection).expires=makeDate(new Date(System.currentTimeMillis()));
242
        ((URLConnectionStub)urlConnection).cacheControl="public, max-age=5";
243
        this.vary="Accept-Encoding";
244
        this.cacheManager.saveDetails(this.urlConnection, sampleResultOK);
245
        this.vary=null;
246
        assertNull("Should not find entry",getThreadCacheEntry(LOCAL_HOST));
247
        assertFalse("Should not find valid entry",this.cacheManager.inCache(url));
248
    }
249
231
    public void testExpiresHttpClient() throws Exception{
250
    public void testExpiresHttpClient() throws Exception{
232
        this.cacheManager.setUseExpires(true);
251
        this.cacheManager.setUseExpires(true);
233
        this.cacheManager.testIterationStart(null);
252
        this.cacheManager.testIterationStart(null);
Lines 257-263 public class TestCacheManager extends JMeterTestCase { Link Here
257
        assertNotNull("Should find entry",getThreadCacheEntry(LOCAL_HOST));
276
        assertNotNull("Should find entry",getThreadCacheEntry(LOCAL_HOST));
258
        assertFalse("Should not find valid entry",this.cacheManager.inCache(url));
277
        assertFalse("Should not find valid entry",this.cacheManager.inCache(url));
259
    }
278
    }
260
    
279
280
    public void testCacheVaryHttpClient() throws Exception{
281
        this.cacheManager.setUseExpires(true);
282
        this.cacheManager.testIterationStart(null);
283
        assertNull("Should not find entry",getThreadCacheEntry(LOCAL_HOST));
284
        assertFalse("Should not find valid entry",this.cacheManager.inCache(url));
285
        ((HttpMethodStub)httpMethod).expires=makeDate(new Date(System.currentTimeMillis()));
286
        ((HttpMethodStub)httpMethod).cacheControl="public, max-age=5";
287
        this.vary = "Something";
288
        this.cacheManager.saveDetails(httpMethod, sampleResultOK);
289
        assertNull("Should not find entry",getThreadCacheEntry(LOCAL_HOST));
290
        assertFalse("Should not find valid entry",this.cacheManager.inCache(url));
291
        this.vary = null;
292
    }
293
261
    public void testCacheHttpClientHEAD() throws Exception{
294
    public void testCacheHttpClientHEAD() throws Exception{
262
        this.cacheManager.setUseExpires(true);
295
        this.cacheManager.setUseExpires(true);
263
        this.cacheManager.testIterationStart(null);
296
        this.cacheManager.testIterationStart(null);

Return to bug 58079