commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject cvs commit: jakarta-commons/httpclient/src/test/org/apache/commons/httpclient TestRedirects.java TestWebapp.java TestWebappRedirect.java
Date Sat, 02 Oct 2004 21:22:03 GMT
olegk       2004/10/02 14:22:03

  Modified:    httpclient/src/test/org/apache/commons/httpclient
                        TestRedirects.java TestWebapp.java
  Removed:     httpclient/src/test/org/apache/commons/httpclient
                        TestWebappRedirect.java
  Log:
  Remaining redirect test cases refactored
  
  Contributed by Oleg Kalnichevski
  
  Revision  Changes    Path
  1.4       +141 -24   jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestRedirects.java
  
  Index: TestRedirects.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestRedirects.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- TestRedirects.java	28 Sep 2004 21:08:48 -0000	1.3
  +++ TestRedirects.java	2 Oct 2004 21:22:03 -0000	1.4
  @@ -75,13 +75,21 @@
   
       private class BasicRedirectService implements HttpService {
           
  -            private String host = null;
  +    		private int statuscode = HttpStatus.SC_MOVED_TEMPORARILY;
  +    		private String host = null;
               private int port;
   
  -            public BasicRedirectService(final String host, int port) {
  +            public BasicRedirectService(final String host, int port, int statuscode) {
                   super();
                   this.host = host;
                   this.port = port;
  +                if (statuscode > 0) {
  +                	this.statuscode = statuscode;
  +                }
  +            }
  +
  +            public BasicRedirectService(final String host, int port) {
  +                this(host, port, -1);
               }
   
               public boolean process(final SimpleRequest request, final SimpleResponse response)
  @@ -90,7 +98,7 @@
                   RequestLine reqline = request.getRequestLine();
                   HttpVersion ver = reqline.getHttpVersion();
                   if (reqline.getUri().equals("/oldlocation/")) {
  -                    response.setStatusLine(ver, HttpStatus.SC_MOVED_TEMPORARILY);
  +                    response.setStatusLine(ver, this.statuscode);
                       response.addHeader(new Header("Location", 
                       		"http://" + this.host + ":" + this.port + "/newlocation/"));
                   } else if (reqline.getUri().equals("/newlocation/")) {
  @@ -175,41 +183,119 @@
               }
           }
       
  -    public void testMaxRedirectCheck() throws IOException {
  -        this.server.setHttpService(new CircularRedirectService());
  -        GetMethod httpget = new GetMethod("/circular-oldlocation/");
  +    public void testBasicRedirect300() throws IOException {
  +        String host = this.server.getLocalAddress();
  +        int port = this.server.getLocalPort();
  +        this.server.setHttpService(
  +                new BasicRedirectService(host, port, HttpStatus.SC_MULTIPLE_CHOICES));
  +        GetMethod httpget = new GetMethod("/oldlocation/");
  +        httpget.setFollowRedirects(false);
           try {
  -            this.client.getParams().setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS,
true);
  -            this.client.getParams().setIntParameter(HttpClientParams.MAX_REDIRECTS, 5);
               this.client.executeMethod(httpget);
  -            fail("RedirectException exception should have been thrown");
  +            assertEquals(HttpStatus.SC_MULTIPLE_CHOICES, httpget.getStatusCode());
  +            assertEquals("/oldlocation/", httpget.getPath());
  +            assertEquals(new URI("/oldlocation/", false), httpget.getURI());
  +        } finally {
  +            httpget.releaseConnection();
           }
  -        catch (RedirectException e) {
  -            // expected
  +    }
  +
  +    public void testBasicRedirect301() throws IOException {
  +        String host = this.server.getLocalAddress();
  +        int port = this.server.getLocalPort();
  +        this.server.setHttpService(
  +                new BasicRedirectService(host, port, HttpStatus.SC_MOVED_PERMANENTLY));
  +        GetMethod httpget = new GetMethod("/oldlocation/");
  +        httpget.setFollowRedirects(true);
  +        try {
  +            this.client.executeMethod(httpget);
  +            assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
  +            assertEquals("/newlocation/", httpget.getPath());
  +            assertEquals(host, httpget.getURI().getHost());
  +            assertEquals(port, httpget.getURI().getPort());
  +            assertEquals(new URI("http://" + host + ":" + port + "/newlocation/", false),
httpget.getURI());
           } finally {
               httpget.releaseConnection();
           }
       }
   
  -    public void testCircularRedirect() throws IOException {
  -        this.server.setHttpService(new CircularRedirectService());
  -        GetMethod httpget = new GetMethod("/circular-oldlocation/");
  +    public void testBasicRedirect302() throws IOException {
  +        String host = this.server.getLocalAddress();
  +        int port = this.server.getLocalPort();
  +        this.server.setHttpService(
  +                new BasicRedirectService(host, port, HttpStatus.SC_MOVED_TEMPORARILY));
  +        GetMethod httpget = new GetMethod("/oldlocation/");
  +        httpget.setFollowRedirects(true);
           try {
  -            this.client.getParams().setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS,
false);
               this.client.executeMethod(httpget);
  -            fail("RedirectException exception should have been thrown");
  +            assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
  +            assertEquals("/newlocation/", httpget.getPath());
  +            assertEquals(host, httpget.getURI().getHost());
  +            assertEquals(port, httpget.getURI().getPort());
  +            assertEquals(new URI("http://" + host + ":" + port + "/newlocation/", false),
httpget.getURI());
  +        } finally {
  +            httpget.releaseConnection();
           }
  -        catch (RedirectException e) {
  -            // expected
  +    }
  +
  +    public void testBasicRedirect303() throws IOException {
  +        String host = this.server.getLocalAddress();
  +        int port = this.server.getLocalPort();
  +        this.server.setHttpService(
  +                new BasicRedirectService(host, port, HttpStatus.SC_SEE_OTHER));
  +        GetMethod httpget = new GetMethod("/oldlocation/");
  +        httpget.setFollowRedirects(true);
  +        try {
  +            this.client.executeMethod(httpget);
  +            assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
  +            assertEquals("/newlocation/", httpget.getPath());
  +            assertEquals(host, httpget.getURI().getHost());
  +            assertEquals(port, httpget.getURI().getPort());
  +            assertEquals(new URI("http://" + host + ":" + port + "/newlocation/", false),
httpget.getURI());
           } finally {
               httpget.releaseConnection();
           }
       }
   
  -    public void testBasicRedirect() throws IOException {
  +    public void testBasicRedirect304() throws IOException {
           String host = this.server.getLocalAddress();
           int port = this.server.getLocalPort();
  -        this.server.setHttpService(new BasicRedirectService(host, port));
  +        this.server.setHttpService(
  +                new BasicRedirectService(host, port, HttpStatus.SC_NOT_MODIFIED));
  +        GetMethod httpget = new GetMethod("/oldlocation/");
  +        httpget.setFollowRedirects(true);
  +        try {
  +            this.client.executeMethod(httpget);
  +            assertEquals(HttpStatus.SC_NOT_MODIFIED, httpget.getStatusCode());
  +            assertEquals("/oldlocation/", httpget.getPath());
  +            assertEquals(new URI("/oldlocation/", false), httpget.getURI());
  +        } finally {
  +            httpget.releaseConnection();
  +        }
  +    }
  +
  +    public void testBasicRedirect305() throws IOException {
  +        String host = this.server.getLocalAddress();
  +        int port = this.server.getLocalPort();
  +        this.server.setHttpService(
  +                new BasicRedirectService(host, port, HttpStatus.SC_USE_PROXY));
  +        GetMethod httpget = new GetMethod("/oldlocation/");
  +        httpget.setFollowRedirects(true);
  +        try {
  +            this.client.executeMethod(httpget);
  +            assertEquals(HttpStatus.SC_USE_PROXY, httpget.getStatusCode());
  +            assertEquals("/oldlocation/", httpget.getPath());
  +            assertEquals(new URI("/oldlocation/", false), httpget.getURI());
  +        } finally {
  +            httpget.releaseConnection();
  +        }
  +    }
  +
  +    public void testBasicRedirect307() throws IOException {
  +        String host = this.server.getLocalAddress();
  +        int port = this.server.getLocalPort();
  +        this.server.setHttpService(
  +                new BasicRedirectService(host, port, HttpStatus.SC_TEMPORARY_REDIRECT));
           GetMethod httpget = new GetMethod("/oldlocation/");
           httpget.setFollowRedirects(true);
           try {
  @@ -235,6 +321,37 @@
               assertEquals(HttpStatus.SC_MOVED_TEMPORARILY, httpget.getStatusCode());
               assertEquals("/oldlocation/", httpget.getPath());
               assertEquals(new URI("/oldlocation/", false), httpget.getURI());
  +        } finally {
  +            httpget.releaseConnection();
  +        }
  +    }
  +
  +    public void testMaxRedirectCheck() throws IOException {
  +        this.server.setHttpService(new CircularRedirectService());
  +        GetMethod httpget = new GetMethod("/circular-oldlocation/");
  +        try {
  +            this.client.getParams().setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS,
true);
  +            this.client.getParams().setIntParameter(HttpClientParams.MAX_REDIRECTS, 5);
  +            this.client.executeMethod(httpget);
  +            fail("RedirectException exception should have been thrown");
  +        }
  +        catch (RedirectException e) {
  +            // expected
  +        } finally {
  +            httpget.releaseConnection();
  +        }
  +    }
  +
  +    public void testCircularRedirect() throws IOException {
  +        this.server.setHttpService(new CircularRedirectService());
  +        GetMethod httpget = new GetMethod("/circular-oldlocation/");
  +        try {
  +            this.client.getParams().setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS,
false);
  +            this.client.executeMethod(httpget);
  +            fail("RedirectException exception should have been thrown");
  +        }
  +        catch (RedirectException e) {
  +            // expected
           } finally {
               httpget.releaseConnection();
           }
  
  
  
  1.11      +4 -5      jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebapp.java
  
  Index: TestWebapp.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebapp.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- TestWebapp.java	25 Apr 2004 12:25:09 -0000	1.10
  +++ TestWebapp.java	2 Oct 2004 21:22:03 -0000	1.11
  @@ -64,7 +64,6 @@
           suite.addTest(TestWebappMethods.suite());
           suite.addTest(TestWebappParameters.suite());
           suite.addTest(TestWebappHeaders.suite());
  -        suite.addTest(TestWebappRedirect.suite());
           suite.addTest(TestWebappBasicAuth.suite());
           suite.addTest(TestWebappPostMethod.suite());
           suite.addTest(TestWebappMultiPostMethod.suite());
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message