abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r434117 - in /incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client: JettyTest.java JettyUtil.java TestSuite.java app/AppTest.java cache/CacheTest.java
Date Wed, 23 Aug 2006 18:24:12 GMT
Author: jmsnell
Date: Wed Aug 23 11:24:11 2006
New Revision: 434117

URL: http://svn.apache.org/viewvc?rev=434117&view=rev
Log:
Fix the client test suite.

there were several issues here.

 * Change all of the requests so that keep-alive connections are not used.  The jetty
   server hangs on shutdown until all open connections timeout, using Connection: close
   resolves the problem.

 * It appears that in some cases, Jetty will actually use the same writer / outputstream
   for different requests, causing the output from the previous request to be sent to 
   subsequent requests!!  This looks like a bug, but we're able to work around it by
   explicitly closing the writer in the servlet after we write to it.

 * The way I was starting and stopping the server was just wrong.  I change it, but this
   change currently means that the only reliable way to run the client tests is via the
   TestSuite class.  That should be ok for now, but we'll want to revisit to see if we 
   can make this better.

 * Commented out the Auth Cache test.  It's testing incorrect behavior.  I need to fix 
   up the cache code so that authenticated requests are properly handled.  Specifically,
   authenticated requests must only be cached if the response is explicitly marked as 
   being cacheable (e.g. using Cache-Control: public)

Modified:
    incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/JettyTest.java
    incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/JettyUtil.java
    incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/TestSuite.java
    incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/app/AppTest.java
    incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/cache/CacheTest.java

Modified: incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/JettyTest.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/JettyTest.java?rev=434117&r1=434116&r2=434117&view=diff
==============================================================================
--- incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/JettyTest.java
(original)
+++ incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/JettyTest.java
Wed Aug 23 11:24:11 2006
@@ -17,40 +17,33 @@
 */
 package org.apache.abdera.test.client;
 
-import org.mortbay.jetty.servlet.ServletHandler;
-
 import junit.framework.TestCase;
 
 public abstract class JettyTest extends TestCase {
   
-  protected int numtests = 0;
-  protected int testsrun = 0;
-  
-  protected JettyTest(int numtests) {
-    this.numtests = numtests;
-  }
+  protected JettyTest() {}
   
-  protected static ServletHandler getServletHandler(String... servletMappings) {
-    ServletHandler handler = new ServletHandler();
+  protected static void getServletHandler(String... servletMappings) {
     for (int n = 0; n < servletMappings.length; n = n + 2) {
       String name = servletMappings[n];
       String root = servletMappings[n+1];
-      handler.addServletWithMapping(name, root);
+      JettyUtil.addServlet(name, root);
     }
-    try {
-      JettyUtil.addHandler(handler);
-    } catch (Exception e) {}
-    return handler;
   }
     
-  protected abstract ServletHandler getServletHandler();
+  protected abstract void getServletHandler();
   
   protected String getBase() {
     return "http://localhost:" + JettyUtil.getPort();
   }
   
+  @Override
+  protected void setUp() throws Exception {
+    getServletHandler();
+    JettyUtil.start();
+  }
+
   public void tearDown() throws Exception {
-    if (++testsrun == numtests)
-      JettyUtil.removeHandler(getServletHandler());
+    //JettyUtil.stop();
   }
 }

Modified: incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/JettyUtil.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/JettyUtil.java?rev=434117&r1=434116&r2=434117&view=diff
==============================================================================
--- incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/JettyUtil.java
(original)
+++ incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/JettyUtil.java
Wed Aug 23 11:24:11 2006
@@ -17,13 +17,9 @@
 */
 package org.apache.abdera.test.client;
 
-import javax.servlet.http.HttpServlet;
-
 import org.mortbay.jetty.Connector;
-import org.mortbay.jetty.Handler;
 import org.mortbay.jetty.Server;
 import org.mortbay.jetty.bio.SocketConnector;
-import org.mortbay.jetty.handler.HandlerWrapper;
 import org.mortbay.jetty.servlet.ServletHandler;
 
 public class JettyUtil {
@@ -32,6 +28,7 @@
   
   private static int PORT = 8080;
   private static Server server = null;
+  private static ServletHandler handler = null;
   
   public static int getPort() {
     if (System.getProperty(PORT_PROP) != null) {
@@ -45,59 +42,31 @@
     Connector connector = new SocketConnector();
     connector.setPort(getPort());
     server.setConnectors(new Connector[]{connector});
-    server.setHandler(new HandlerWrapper());
-    server.start();
+    handler = new ServletHandler();
+    server.setHandler(handler);
   }
   
-  public static void addHandler(ServletHandler handler) throws Exception {
-    if (server == null) initServer();
-    if (hasHandler(handler)) return;
-    server.addHandler(handler);
+  public static void addServlet(String path, String _class) {
+    try {
+      if (server == null) initServer();
+    } catch (Exception e) {}
+    handler.addServletWithMapping(path, _class);
   }
   
-  private static boolean hasHandler(ServletHandler handler) throws Exception {
-    if (server == null || server.getHandlers() == null) return false;
-    for (Handler h : server.getHandlers()) {
-      if (h.equals(handler)) return true;
-    }
-    return false;
+  public static void start() throws Exception {
+    if (server == null) initServer();
+    if (server.isRunning()) return;
+    server.start();
   }
   
-  public static void removeHandler(ServletHandler handler) throws Exception {
+  public static void stop() throws Exception {
     if (server == null) return;
-    if (handler.isRunning()) handler.stop();
-    server.removeHandler(handler);
-    if (server.getHandlers().length == 1) {
-      server.removeHandler(server.getHandler());
-      server.stop();
-      server = null;
-    }
+    server.stop();
+    server = null;
   }
   
   public static boolean isRunning() {
     return (server != null);
   }
   
-  public static void main(String[] args) throws Exception {
-    
-    ServletHandler handler1 = new ServletHandler();
-    handler1.addServletWithMapping(TestServlet.class, "/foo");
-    System.out.println(JettyUtil.isRunning());
-    JettyUtil.addHandler(handler1);
-    System.out.println(JettyUtil.isRunning());
-    
-    ServletHandler handler2 = new ServletHandler();
-    handler2.addServletWithMapping(TestServlet.class, "/bar");
-    System.out.println(JettyUtil.isRunning());
-    JettyUtil.addHandler(handler2);
-    System.out.println(JettyUtil.isRunning());
-    
-    JettyUtil.removeHandler(handler1);
-    System.out.println(JettyUtil.isRunning());
-    JettyUtil.removeHandler(handler2);
-    System.out.println(JettyUtil.isRunning());
-  }
-  
-  @SuppressWarnings("serial")
-  private static class TestServlet extends HttpServlet {}
 }

Modified: incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/TestSuite.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/TestSuite.java?rev=434117&r1=434116&r2=434117&view=diff
==============================================================================
--- incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/TestSuite.java
(original)
+++ incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/TestSuite.java
Wed Aug 23 11:24:11 2006
@@ -24,11 +24,14 @@
   public static void main(String[] args)
   {
     junit.textui.TestRunner.run(new TestSuite());
+    try {
+      JettyUtil.stop();
+    } catch (Exception e) {}
   }
 
   public TestSuite()
   {
-    //addTestSuite(CacheTest.class);
+    addTestSuite(CacheTest.class);
     addTestSuite(AppTest.class);
   }
 }

Modified: incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/app/AppTest.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/app/AppTest.java?rev=434117&r1=434116&r2=434117&view=diff
==============================================================================
--- incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/app/AppTest.java
(original)
+++ incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/app/AppTest.java
Wed Aug 23 11:24:11 2006
@@ -48,7 +48,6 @@
 import org.apache.abdera.protocol.client.Response;
 import org.apache.abdera.test.client.JettyTest;
 import org.apache.abdera.util.MimeTypeHelper;
-import org.mortbay.jetty.servlet.ServletHandler;
 
 /**
  * Test to make sure that we can operate as a simple APP client
@@ -66,12 +65,6 @@
     return abdera.getParser();
   }
   
-  private static ServletHandler handler = 
-    JettyTest.getServletHandler(
-      ServiceDocumentServlet.class.getName(), "/service",
-      CollectionServlet.class.getName(), "/collections/*"
-    );
-  
   private static AppTest INSTANCE = null;
   
   private static Document<Service> init_service_document(String base) {
@@ -109,9 +102,10 @@
       HttpServletRequest request, 
       HttpServletResponse response) 
         throws ServletException, IOException {
-      response.setStatus(HttpServletResponse.SC_OK);
-      response.setContentType("application/atomserv+xml; charset=utf-8");
-      service.writeTo(response.getOutputStream());
+
+        response.setStatus(HttpServletResponse.SC_OK);
+        response.setContentType("application/atomserv+xml; charset=utf-8");
+        service.writeTo(response.getOutputStream());
     }
   }
   
@@ -325,22 +319,25 @@
   }
   
   public AppTest() {
-    super(1);
     AppTest.INSTANCE = this;
   }
   
-  protected ServletHandler getServletHandler() {
-    return AppTest.handler;
+  protected void getServletHandler() {
+      getServletHandler(
+        ServiceDocumentServlet.class.getName(), "/service",
+        CollectionServlet.class.getName(), "/collections/*"
+      );
   }  
   
   public void testAppClient() throws Exception {
     
     Client client = new CommonsClient();
     Entry entry = getFactory().newEntry();
-    
+    RequestOptions options = client.getDefaultRequestOptions();
+    options.setHeader("Connection", "close");
     // do the introspection step
-    Response response = client.get("http://localhost:8080/service");
-    assertEquals(200,response.getStatus());
+    Response response = client.get("http://localhost:8080/service",options);
+    assertEquals(response.getStatus(),200);
     Document<Service> service_doc = response.getDocument();
     assertNotNull(service_doc);
     assertEquals(1,service_doc.getRoot().getWorkspaces().size());
@@ -354,7 +351,7 @@
     String col_uri = getBase() + "/collections/entries";
     
     // post a new entry
-    response = client.post(col_uri, entry);
+    response = client.post(col_uri, entry, options);
     assertEquals(201, response.getStatus());
     assertNotNull(response.getLocation());
     assertNotNull(response.getContentLocation());
@@ -362,13 +359,13 @@
     String self_uri = response.getLocation();
     
     // get the collection to see if our entry is there
-    response = client.get(col_uri);
+    response = client.get(col_uri, options);
     assertEquals(200, response.getStatus());
     Document<Feed> feed_doc = response.getDocument();
     assertEquals(feed_doc.getRoot().getEntries().size(), 1);
     
     // get the entry to see if we can get it
-    response = client.get(self_uri);
+    response = client.get(self_uri, options);
     assertEquals(200, response.getStatus());
     Document<Entry> entry_doc = response.getDocument();
     assertEquals(entry_doc.getRoot().getId().toString(), self_uri); // this isn't always
true in real life, but for our tests they are the same
@@ -382,21 +379,21 @@
     entry.setTitle("New title");
     
     // submit the changed entry back to the server
-    response = client.put(edit_uri, entry);
+    response = client.put(edit_uri, entry, options);
     assertEquals(204, response.getStatus());
 
     // check to see if the entry was modified properly
-    response = client.get(self_uri);
+    response = client.get(self_uri, options);
     assertEquals(200, response.getStatus());
     entry_doc = response.getDocument();
     assertEquals(entry_doc.getRoot().getTitle(), "New title");
     
     // delete the entry
-    response = client.delete(edit_uri);
+    response = client.delete(edit_uri, options);
     assertEquals(204, response.getStatus());
     
     // is it gone?
-    response = client.get(self_uri);
+    response = client.get(self_uri, options);
     assertEquals(404, response.getStatus());
     
     // YAY! We're a working APP client
@@ -404,8 +401,9 @@
     // Now let's try to do a media post
     
     // Post the media resource
-    RequestOptions options = new RequestOptions();
+    options = client.getDefaultRequestOptions();
     options.setContentType("text/plain");
+    options.setHeader("Connection", "close");
     response = client.post(col_uri, new ByteArrayInputStream("test".getBytes()), options);
     assertEquals(201, response.getStatus());
     assertNotNull(response.getLocation());
@@ -414,7 +412,9 @@
     self_uri = response.getLocation();
 
     // was an entry created?
-    response = client.get(self_uri);
+    options = client.getDefaultRequestOptions();
+    options.setHeader("Connection", "close");
+    response = client.get(self_uri, options);
     assertEquals(200, response.getStatus());
     entry_doc = response.getDocument();
     assertEquals(entry_doc.getRoot().getId().toString(), self_uri); // this isn't always
true in real life, but for our tests they are the same
@@ -430,7 +430,10 @@
     entry.setTitle("New title");
     
     // submit the changes
-    response = client.put(edit_uri, entry);
+    options = client.getDefaultRequestOptions();
+    options.setContentType("application/atom+xml");
+    options.setHeader("Connection", "close");
+    response = client.put(edit_uri, entry, options);
     assertEquals(204, response.getStatus());
 
     // get the media resource
@@ -440,21 +443,24 @@
     assertEquals(mediavalue,"test");
     
     // edit the media resource
+    options = client.getDefaultRequestOptions();
+    options.setHeader("Connection", "close");
+    options.setContentType("text/plain");
     response = client.put(edit_media, new ByteArrayInputStream("TEST".getBytes()), options);
     assertEquals(204, response.getStatus());
 
     // was the resource changed?
-    response = client.get(media);
+    response = client.get(media, options);
     assertEquals(200, response.getStatus());
     mediavalue = read(response.getInputStream());
     assertEquals(mediavalue,"TEST");
     
     // delete the entry
-    response = client.delete(edit_uri);
+    response = client.delete(edit_uri, options);
     assertEquals(204, response.getStatus());
     
     // is the entry gone?
-    response = client.get(self_uri);
+    response = client.get(self_uri, options);
     assertEquals(404, response.getStatus());
 
     // is the media resource gone?

Modified: incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/cache/CacheTest.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/cache/CacheTest.java?rev=434117&r1=434116&r2=434117&view=diff
==============================================================================
--- incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/cache/CacheTest.java
(original)
+++ incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/cache/CacheTest.java
Wed Aug 23 11:24:11 2006
@@ -49,14 +49,6 @@
  */
 @SuppressWarnings("serial")
 public class CacheTest extends JettyTest {
-
-  private static ServletHandler handler = 
-    JettyTest.getServletHandler(
-      "org.apache.abdera.test.client.cache.CacheTest$CheckCacheInvalidateServlet","/check_cache_invalidate",
-      "org.apache.abdera.test.client.cache.CacheTest$NoCacheServlet", "/no_cache",
-      "org.apache.abdera.test.client.cache.CacheTest$AuthServlet", "/auth",
-      "org.apache.abdera.test.client.cache.CacheTest$CheckMustRevalidateServlet", "/must_revalidate"
-    );
   
   private static String CHECK_CACHE_INVALIDATE;
   private static String CHECK_NO_CACHE;
@@ -64,7 +56,6 @@
   private static String CHECK_MUST_REVALIDATE;
   
   public CacheTest() {
-    super(11);
     String base = getBase();
     CHECK_CACHE_INVALIDATE = base + "/check_cache_invalidate";
     CHECK_NO_CACHE = base + "/no_cache";
@@ -72,8 +63,13 @@
     CHECK_MUST_REVALIDATE = base + "/must_revalidate";
   }
   
-  protected ServletHandler getServletHandler() {
-    return CacheTest.handler;
+  protected void getServletHandler() {
+    getServletHandler(
+        "org.apache.abdera.test.client.cache.CacheTest$CheckCacheInvalidateServlet","/check_cache_invalidate",
+        "org.apache.abdera.test.client.cache.CacheTest$NoCacheServlet", "/no_cache",
+        "org.apache.abdera.test.client.cache.CacheTest$AuthServlet", "/auth",
+        "org.apache.abdera.test.client.cache.CacheTest$CheckMustRevalidateServlet", "/must_revalidate"
+      );
   }
   
   public static class CheckMustRevalidateServlet extends HttpServlet {
@@ -83,18 +79,20 @@
       {
       String reqnum = request.getHeader("X-Reqnum");
       int req = Integer.parseInt(reqnum);
-      response.setContentType("text/plain");
       if (req == 1) {
         response.setStatus(HttpServletResponse.SC_OK);
+        response.setContentType("text/plain");
         response.setHeader("Cache-Control", "must-revalidate");
         response.setDateHeader("Date", System.currentTimeMillis());      
         response.getWriter().println(reqnum);
+        response.getWriter().close();
       } else if (req == 2) {
         response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
+        response.setContentType("text/plain");
         response.setDateHeader("Date", System.currentTimeMillis());
         return;
       } else if (req == 3) {
-        response.sendError(HttpServletResponse.SC_NOT_FOUND);
+        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
         response.setDateHeader("Date", System.currentTimeMillis());
         return;
       }
@@ -107,14 +105,16 @@
       throws ServletException, IOException
     {
       String reqnum = request.getHeader("X-Reqnum");
-
-      response.setContentType("text/plain");
       response.setStatus(HttpServletResponse.SC_OK);
-      response.setHeader("Cache-Control", "max-age=60");
       response.setDateHeader("Date", System.currentTimeMillis());
-
+      response.setContentType("text/plain");
+      response.setHeader("Cache-Control", "max-age=60");
       response.getWriter().println(reqnum);
+      response.getWriter().close();
     }
+    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {}
+    protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {}
+    protected void doPut(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {}
   }
 
   public static class NoCacheServlet extends HttpServlet {
@@ -135,6 +135,7 @@
       response.setDateHeader("Date", System.currentTimeMillis());
 
       response.getWriter().println(reqnum);
+      response.getWriter().close();
     }
   }
   
@@ -145,7 +146,6 @@
     {
       String reqnum = request.getHeader("X-Reqnum");
       int num = Integer.parseInt(reqnum);
-      response.setContentType("text/plain");
       switch (num) {
         case 1: response.setStatus(HttpServletResponse.SC_OK); break;
         case 2: response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); break;
@@ -153,7 +153,9 @@
           response.setStatus(HttpServletResponse.SC_OK); break;
       }
       response.setDateHeader("Date", System.currentTimeMillis());
+      response.setContentType("text/plain");
       response.getWriter().println(reqnum);
+      response.getWriter().close();
     }
   }
   
@@ -164,109 +166,108 @@
   private static final int DELETE = 4;
   private static final int PUT = 5;
   
-  public static void testRequestNoStore() throws Exception {
+  public void testRequestNoStore() throws Exception {
     _requestCacheInvalidation(NOSTORE);
   }
 
-  public static void testRequestNoCache() throws Exception {
+  public void testRequestNoCache() throws Exception {
     _requestCacheInvalidation(NOCACHE);    
   }
   
-  public static void testRequestMaxAge0() throws Exception {
+  public void testRequestMaxAge0() throws Exception {
     _requestCacheInvalidation(MAXAGE0);
   }
 
-  public static void testResponseNoStore() throws Exception {
+  public void testResponseNoStore() throws Exception {
     _responseNoCache(NOSTORE);
   }
 
-  public static void testResponseNoCache() throws Exception {
+  public void testResponseNoCache() throws Exception {
     _responseNoCache(NOCACHE);
   }
   
-  public static void testResponseMaxAge0() throws Exception {
+  public void testResponseMaxAge0() throws Exception {
     _responseNoCache(MAXAGE0);
   }
   
-  public static void testPostInvalidates() throws Exception {
+  public void testPostInvalidates() throws Exception {
     _methodInvalidates(POST);
   }
 
-  public static void testPutInvalidates() throws Exception {
+  public void testPutInvalidates() throws Exception {
     _methodInvalidates(PUT);
   }
   
-  public static void testDeleteInvalidates() throws Exception {
+  public void testDeleteInvalidates() throws Exception {
     _methodInvalidates(DELETE);
   }
   
-  public static void testAuthForcesRevalidation() throws Exception {
-    
-    // the revalidatewithauth mechanism allows us to revalidate the 
-    // cache when authentication is used, meaning that we'll only
-    // be served data from the cache if our authentication credentials
-    // are valid.
-    
-    // unfortunately, this only works for preemptive auth and auth using
-    // the RequestOptions.setAuthorization method.  I'm not quite sure
-    // yet how to plug into httpclient's built in auth mechanisms to 
-    // ensure we can invalidate the cache.    
-    
-    Client client = new CommonsClient();
-    client.usePreemptiveAuthentication(true);
-    client.addCredentials(CHECK_AUTH, null, null, new UsernamePasswordCredentials("james","snell"));
-    RequestOptions options = client.getDefaultRequestOptions();
-    options.setRevalidateWithAuth(true);
-    options.setHeader("x-reqnum", "1");
-    Response response = client.get(CHECK_AUTH, options);
-  
-    // first request works as expected. fills the cache
-    String resp1 = getResponse(response.getInputStream());
-    assertEquals(resp1, "1");
-
-    // second request uses authentication, should force revalidation of the cache
-    options.setHeader("x-reqnum", "2");
-    response = client.get(CHECK_AUTH, options);
-  
-    resp1 = getResponse(response.getInputStream());
-    assertEquals(response.getStatus(), HttpServletResponse.SC_UNAUTHORIZED);
-    assertEquals(resp1, "2");
-
-    // third request does not use authentication, but since the previous request
-    // resulted in an "unauthorized" response, the cache needs to be refilled
-    options.setHeader("x-reqnum", "3");
-    client.usePreemptiveAuthentication(false);
-    response = client.get(CHECK_AUTH, options);
-  
-    resp1 = getResponse(response.getInputStream());
-    assertEquals(response.getStatus(), HttpServletResponse.SC_OK);
-    assertEquals(resp1, "3");  
-
-    // fourth request does not use authentication, will pull from the cache
-    options.setHeader("x-reqnum", "4");
-    response = client.get(CHECK_AUTH, options);
-  
-    resp1 = getResponse(response.getInputStream());
-    assertEquals(response.getStatus(), HttpServletResponse.SC_OK);
-    assertEquals(resp1, "3");  
-    
-    // fifth request uses authentication, will force revalidation
-    options.setAuthorization("Basic amFtZXM6c25lbGw=");
-    options.setHeader("x-reqnum", "5");
-    response = client.get(CHECK_AUTH, options);
+  public void testAuthForcesRevalidation() throws Exception {
     
-    resp1 = getResponse(response.getInputStream());
-    assertEquals(response.getStatus(), HttpServletResponse.SC_OK);
-    assertEquals(resp1, "5");
+//TODO: Actually need to rethink this.  Responses to authenticated requests 
+//      should never be cached unless the resource is explicitly marked as
+//      being cacheable (e.g. using Cache-Control: public). So this test 
+//      was testing incorrect behavior. 
+    
+//    Client client = new CommonsClient();
+//    client.usePreemptiveAuthentication(true);
+//    client.addCredentials(CHECK_AUTH, null, null, new UsernamePasswordCredentials("james","snell"));
+//    RequestOptions options = client.getDefaultRequestOptions();
+//    options.setHeader("Connection", "close");
+//    options.setRevalidateWithAuth(true);
+//    options.setHeader("x-reqnum", "1");
+//    Response response = client.get(CHECK_AUTH, options);
+//  
+//    // first request works as expected. fills the cache
+//    String resp1 = getResponse(response);
+//    assertEquals(resp1, "1");
+//
+//    // second request uses authentication, should force revalidation of the cache
+//    options.setHeader("x-reqnum", "2");
+//    response = client.get(CHECK_AUTH, options);
+//  
+//    resp1 = getResponse(response);
+//    assertEquals(response.getStatus(), HttpServletResponse.SC_UNAUTHORIZED);
+//    assertEquals(resp1, "2");
+//
+//    // third request does not use authentication, but since the previous request
+//    // resulted in an "unauthorized" response, the cache needs to be refilled
+//    options.setHeader("x-reqnum", "3");
+//    client.usePreemptiveAuthentication(false);
+//    response = client.get(CHECK_AUTH, options);
+//  
+//    resp1 = getResponse(response);
+//    assertEquals(response.getStatus(), HttpServletResponse.SC_OK);
+//    assertEquals(resp1, "3");  
+//
+//    // fourth request does not use authentication, will pull from the cache
+//    options = client.getDefaultRequestOptions();
+//    options.setHeader("x-reqnum", "4");
+//    client.usePreemptiveAuthentication(false);
+//    response = client.get(CHECK_AUTH, options);
+//  
+//    resp1 = getResponse(response);
+//    assertEquals(response.getStatus(), HttpServletResponse.SC_OK);
+//    assertEquals(resp1, "3");  
+//    
+//    // fifth request uses authentication, will force revalidation
+//    options.setAuthorization("Basic amFtZXM6c25lbGw=");
+//    options.setHeader("x-reqnum", "5");
+//    response = client.get(CHECK_AUTH, options);
+//    
+//    resp1 = getResponse(response);
+//    assertEquals(response.getStatus(), HttpServletResponse.SC_OK);
+//    assertEquals(resp1, "5");
   }
   
-  public static void testResponseMustRevalidate() throws Exception {
+  public void testResponseMustRevalidate() throws Exception {
     Client client = new CommonsClient();
     RequestOptions options = client.getDefaultRequestOptions();
+    options.setHeader("Connection", "close");
     options.setHeader("x-reqnum", "1");
     Response response = client.get(CHECK_MUST_REVALIDATE, options);
   
-    String resp1 = getResponse(response.getInputStream());
+    String resp1 = getResponse(response);
     assertEquals(resp1, "1");
     
     // Should be revalidated and use the cache
@@ -274,62 +275,68 @@
     response = client.get(CHECK_MUST_REVALIDATE, options);
     assertTrue(response instanceof CachedResponse);
     
-    String resp2 = getResponse(response.getInputStream());
+    String resp2 = getResponse(response);
     assertEquals(resp2, "1");
     
     // Should be revalidated and return a 404
     options.setHeader("x-reqnum", "3");
     response = client.get(CHECK_MUST_REVALIDATE, options);  
     assertEquals(response.getStatus(), 404);
+    response.release();
 
   }
   
-  private static void _methodInvalidates(int type) throws Exception {
+  private void _methodInvalidates(int type) throws Exception {
     
     Client client = new CommonsClient();
     RequestOptions options = client.getDefaultRequestOptions();
+    options.setHeader("Connection", "close");
     options.setHeader("x-reqnum", "1");
     Response response = client.get(CHECK_CACHE_INVALIDATE, options);
   
-    String resp1 = getResponse(response.getInputStream());
+    String resp1 = getResponse(response);
+    response.release();
     assertEquals(resp1, "1");
     
     // calling a method that could change state on the server should invalidate the cache
     options.setHeader("x-reqnum", "2");
     switch(type) {
       case POST:  
-        client.post(
+        response = client.post(
           CHECK_CACHE_INVALIDATE, 
           new ByteArrayInputStream("".getBytes()), 
           options);
         break;
       case PUT:
-        client.put(
+        response = client.put(
           CHECK_CACHE_INVALIDATE, 
           new ByteArrayInputStream("".getBytes()), 
           options);
         break;
       case DELETE:
-        client.delete(
+        response = client.delete(
           CHECK_CACHE_INVALIDATE, 
           options);
         break;
     }
+    response.release();
     
     options.setHeader("x-reqnum", "3");
     response = client.get(CHECK_CACHE_INVALIDATE, options);
   
-    resp1 = getResponse(response.getInputStream());
+    resp1 = getResponse(response);
+    response.release();
     assertEquals(resp1, "3");
   }
   
-  private static void _requestCacheInvalidation(int type) throws Exception {
+  private void _requestCacheInvalidation(int type) throws Exception {
     
     Client client = new CommonsClient();
     RequestOptions options = client.getDefaultRequestOptions();
+    options.setHeader("Connection", "close");
     options.setHeader("x-reqnum", "1");
     Response response = client.get(CHECK_CACHE_INVALIDATE, options);  
-    String resp1 = getResponse(response.getInputStream());
+    String resp1 = getResponse(response);
     assertEquals(resp1, "1");
     
     // Should not use the cache
@@ -341,7 +348,7 @@
     }
     response = client.get(CHECK_CACHE_INVALIDATE, options);
   
-    String resp2 = getResponse(response.getInputStream());
+    String resp2 = getResponse(response);
     assertEquals(resp2, "2");
     
     // Should use the cache
@@ -353,42 +360,45 @@
     }
     response = client.get(CHECK_CACHE_INVALIDATE, options);
   
-    String resp3 = getResponse(response.getInputStream());
+    String resp3 = getResponse(response);
     assertEquals(resp3, "2");
   }
   
-  private static void _responseNoCache(int type) throws Exception {
+  private void _responseNoCache(int type) throws Exception {
     
     Client client = new CommonsClient();
     RequestOptions options = client.getDefaultRequestOptions();
+    options.setHeader("Connection", "close");
     options.setHeader("x-reqnum", "1");
     options.setHeader("x-reqtest", String.valueOf(type));
     Response response = client.get(CHECK_NO_CACHE, options);
   
-    String resp1 = getResponse(response.getInputStream());
+    String resp1 = getResponse(response);
     assertEquals(resp1, "1");
     
     // Should not use the cache
     options.setHeader("x-reqnum", "2");
     response = client.get(CHECK_NO_CACHE, options);
   
-    String resp2 = getResponse(response.getInputStream());
+    String resp2 = getResponse(response);
     assertEquals(resp2, "2");
     
     // Should use the cache
     options.setHeader("x-reqnum", "3");
     response = client.get(CHECK_NO_CACHE, options);
   
-    String resp3 = getResponse(response.getInputStream());
+    String resp3 = getResponse(response);
     assertEquals(resp3, "3");
   }
   
-  private static String getResponse(InputStream in) throws IOException {
+  private static String getResponse(Response response) throws IOException {
     ByteArrayOutputStream out = new ByteArrayOutputStream();
     int m = -1;
+    InputStream in = response.getInputStream();
     while ((m = in.read()) != -1) {
       out.write(m);
     }
+    in.close();
     String resp = new String(out.toByteArray());
     return resp.trim();
   }



Mime
View raw message