Author: sebb Date: Tue Nov 17 19:27:50 2009 New Revision: 881451 URL: http://svn.apache.org/viewvc?rev=881451&view=rev Log: Bug 48153 - Support for Cache-Control and Expires headers
Modified: jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/control/CacheManager.java jakarta/jmeter/trunk/test/src/org/apache/jmeter/protocol/http/control/TestCacheManager.java jakarta/jmeter/trunk/xdocs/changes.xml Modified: jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/control/CacheManager.java URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/control/CacheManager.java?rev=881451&r1=881450&r2=881451&view=diff ============================================================================== --- jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/control/CacheManager.java (original) +++ jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/control/CacheManager.java Tue Nov 17 19:27:50 2009 @@ -108,7 +108,8 @@ String expires = conn.getHeaderField(HTTPConstantsInterface.EXPIRES); String etag = conn.getHeaderField(HTTPConstantsInterface.ETAG); String url = conn.getURL().toString(); - setCache(lastModified, expires, etag, url); + String cacheControl = conn.getHeaderField(HTTPConstantsInterface.CACHE_CONTROL); + setCache(lastModified, cacheControl, expires, etag, url); } } @@ -124,24 +125,32 @@ String expires = getHeader(method ,HTTPConstantsInterface.EXPIRES); String etag = getHeader(method ,HTTPConstantsInterface.ETAG); String url = method.getURI().toString(); - setCache(lastModified, expires, etag, url); + String cacheControl = getHeader(method, HTTPConstantsInterface.CACHE_CONTROL); + setCache(lastModified, cacheControl, expires, etag, url); } } // helper method to save the cache entry - private void setCache(String lastModified, String expires, String etag, String url) { + private void setCache(String lastModified, String cacheControl, String expires, String etag, String url) { if (log.isDebugEnabled()){ - log.debug("SET(both) "+url + " " + lastModified + " " + " " + expires + " " + etag); + log.debug("SET(both) "+url + " " + cacheControl + " " + lastModified + " " + " " + expires + " " + etag); } Date expiresDate = null; // i.e. not using Expires - if (expires != null && useExpires) {// Check that the header is present and we are processing Expires - try { - expiresDate = DateUtil.parseDate(expires); - } catch (DateParseException e) { - if (log.isDebugEnabled()){ - log.debug("Unable to parse Expires: '"+expires+"' "+e); + if (useExpires) {// Check that we are processing Expires/CacheControl + final String MAX_AGE = "max-age="; + // TODO - check for other CacheControl attributes? + if (cacheControl != null && cacheControl.contains("public") && cacheControl.contains(MAX_AGE)) { + long maxAge = Long.parseLong(cacheControl.substring(cacheControl.indexOf(MAX_AGE)+MAX_AGE.length()))*1000; + expiresDate=new Date(System.currentTimeMillis()+maxAge); + } else if (expires != null) { + try { + expiresDate = DateUtil.parseDate(expires); + } catch (DateParseException e) { + if (log.isDebugEnabled()){ + log.debug("Unable to parse Expires: '"+expires+"' "+e); + } + expiresDate = new Date(0L); // invalid dates must be treated as expired } - expiresDate = new Date(0L); // invalid dates must be treated as expired } } getCache().put(url, new CacheEntry(lastModified, expiresDate, etag)); @@ -228,8 +237,10 @@ log.debug("Expires= " + expiresDate + " (Valid)"); } return true; - } else if (log.isDebugEnabled()){ - log.debug("Expires= " + expiresDate + " (Expired)"); + } else { + if (log.isDebugEnabled()){ + log.debug("Expires= " + expiresDate + " (Expired)"); + } } } } Modified: jakarta/jmeter/trunk/test/src/org/apache/jmeter/protocol/http/control/TestCacheManager.java URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/test/src/org/apache/jmeter/protocol/http/control/TestCacheManager.java?rev=881451&r1=881450&r2=881451&view=diff ============================================================================== --- jakarta/jmeter/trunk/test/src/org/apache/jmeter/protocol/http/control/TestCacheManager.java (original) +++ jakarta/jmeter/trunk/test/src/org/apache/jmeter/protocol/http/control/TestCacheManager.java Tue Nov 17 19:27:50 2009 @@ -57,12 +57,19 @@ public void connect() throws IOException { } + private String expires = null; + private String cacheControl = null; + @Override public String getHeaderField(String name) { if (HTTPConstantsInterface.LAST_MODIFIED.equals(name)) { return currentTimeInGMT; } else if (HTTPConstantsInterface.ETAG.equals(name)) { return EXPECTED_ETAG; + } else if (HTTPConstantsInterface.EXPIRES.equals(name)){ + return expires; + } else if (HTTPConstantsInterface.CACHE_CONTROL.equals(name)){ + return cacheControl; } return super.getHeaderField(name); } @@ -73,8 +80,10 @@ } private class HttpMethodStub extends PostMethod { - Header lastModifiedHeader; - Header etagHeader; + private Header lastModifiedHeader; + private Header etagHeader; + private String expires; + private String cacheControl; HttpMethodStub() { this.lastModifiedHeader = new Header(HTTPConstantsInterface.LAST_MODIFIED, currentTimeInGMT); @@ -87,6 +96,10 @@ return this.lastModifiedHeader; } else if (HTTPConstantsInterface.ETAG.equals(headerName)) { return this.etagHeader; + } else if (HTTPConstantsInterface.EXPIRES.equals(headerName)) { + return expires == null ? null : new Header(HTTPConstantsInterface.EXPIRES, expires); + } else if (HTTPConstantsInterface.CACHE_CONTROL.equals(headerName)) { + return cacheControl == null ? null : new Header(HTTPConstantsInterface.CACHE_CONTROL, cacheControl); } return null; } @@ -128,24 +141,30 @@ private URLConnection urlConnection; private HttpMethod httpMethod; private HttpURLConnection httpUrlConnection; + private SampleResult sampleResultOK; public TestCacheManager(String name) { super(name); } + private String makeDate(Date d){ + SimpleDateFormat simpleDateFormat = new SimpleDateFormat(); + simpleDateFormat.setTimeZone(new SimpleTimeZone(0, "GMT")); + simpleDateFormat.applyPattern("EEE, dd MMM yyyy HH:mm:ss z"); + return simpleDateFormat.format(d); + } + @Override public void setUp() throws Exception { super.setUp(); this.cacheManager = new CacheManager(); - SimpleDateFormat simpleDateFormat = new SimpleDateFormat(); - simpleDateFormat.setTimeZone(new SimpleTimeZone(0, "GMT")); - simpleDateFormat.applyPattern("EEE, dd MMM yyyy HH:mm:ss z"); - this.currentTimeInGMT = simpleDateFormat.format(new Date()); + this.currentTimeInGMT = makeDate(new Date()); this.uri = new URI(LOCAL_HOST, false); this.url = new URL(LOCAL_HOST); this.urlConnection = new URLConnectionStub(this.url.openConnection()); this.httpMethod = new HttpMethodStub(); this.httpUrlConnection = new HttpURLConnectionStub(this.httpMethod, this.url); + this.sampleResultOK = getSampleResultWithSpecifiedResponseCode("200"); } @Override @@ -157,9 +176,55 @@ this.uri = null; this.cacheManager = null; this.currentTimeInGMT = null; + this.sampleResultOK = null; super.tearDown(); } + public void testExpiresJava() throws Exception{ + this.cacheManager.setUseExpires(true); + this.cacheManager.testIterationStart(null); + assertNull("Should not find entry",getThreadCacheEntry(LOCAL_HOST)); + assertFalse("Should not find valid entry",this.cacheManager.inCache(url)); + ((URLConnectionStub)urlConnection).expires=makeDate(new Date(System.currentTimeMillis()+2000)); + this.cacheManager.saveDetails(this.urlConnection, sampleResultOK); + assertNotNull("Should find entry",getThreadCacheEntry(LOCAL_HOST)); + assertTrue("Should find valid entry",this.cacheManager.inCache(url)); + } + + public void testNoExpiresJava() throws Exception{ + this.cacheManager.setUseExpires(false); + this.cacheManager.testIterationStart(null); + assertNull("Should not find entry",getThreadCacheEntry(LOCAL_HOST)); + assertFalse("Should not find valid entry",this.cacheManager.inCache(url)); + ((URLConnectionStub)urlConnection).expires=makeDate(new Date(System.currentTimeMillis()+2000)); + this.cacheManager.saveDetails(this.urlConnection, sampleResultOK); + assertNotNull("Should find entry",getThreadCacheEntry(LOCAL_HOST)); + assertFalse("Should not find valid entry",this.cacheManager.inCache(url)); + } + + public void testExpiresHttpClient() throws Exception{ + this.cacheManager.setUseExpires(true); + this.cacheManager.testIterationStart(null); + assertNull("Should not find entry",getThreadCacheEntry(LOCAL_HOST)); + assertFalse("Should not find valid entry",this.cacheManager.inCache(url)); + ((HttpMethodStub)httpMethod).expires=makeDate(new Date(System.currentTimeMillis()+2000)); + this.cacheManager.saveDetails(httpMethod, sampleResultOK); + assertNotNull("Should find entry",getThreadCacheEntry(LOCAL_HOST)); + assertTrue("Should find valid entry",this.cacheManager.inCache(url)); + } + + public void testCacheHttpClient() throws Exception{ + this.cacheManager.setUseExpires(true); + this.cacheManager.testIterationStart(null); + assertNull("Should not find entry",getThreadCacheEntry(LOCAL_HOST)); + assertFalse("Should not find valid entry",this.cacheManager.inCache(url)); + ((HttpMethodStub)httpMethod).expires=makeDate(new Date(System.currentTimeMillis())); + ((HttpMethodStub)httpMethod).cacheControl="public, max-age=10"; + this.cacheManager.saveDetails(httpMethod, sampleResultOK); + assertNotNull("Should find entry",getThreadCacheEntry(LOCAL_HOST)); + assertTrue("Should find valid entry",this.cacheManager.inCache(url)); + } + public void testGetClearEachIteration() throws Exception { assertFalse("Should default not to clear after each iteration.", this.cacheManager.getClearEachIteration()); this.cacheManager.setClearEachIteration(true); Modified: jakarta/jmeter/trunk/xdocs/changes.xml URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/xdocs/changes.xml?rev=881451&r1=881450&r2=881451&view=diff ============================================================================== --- jakarta/jmeter/trunk/xdocs/changes.xml (original) +++ jakarta/jmeter/trunk/xdocs/changes.xml Tue Nov 17 19:27:50 2009 @@ -117,6 +117,7 @@ <li>Bug 47622 - enable recording of HTTPS sessions</li> <li>Allow Proxy Server to be specified on HTTP Sampler GUI and HTTP Config GUI</li> <li>Bug 47461 - Update Cache Manager to handle Expires HTTP header</li> +<li>Bug 48153 - Support for Cache-Control and Expires headers</li> </ul> <h3>Other samplers</h3> --------------------------------------------------------------------- To unsubscribe, e-mail: jmeter-dev-unsubscr...@jakarta.apache.org For additional commands, e-mail: jmeter-dev-h...@jakarta.apache.org