public inbox for mauve-patches@sourceware.org
 help / color / mirror / Atom feed
* FYI: New tests for HttpURLConnection
@ 2006-02-04 14:07 Wolfgang Baer
  2006-02-04 14:16 ` Wolfgang Baer
  2006-02-09  1:58 ` Tom Tromey
  0 siblings, 2 replies; 7+ messages in thread
From: Wolfgang Baer @ 2006-02-04 14:07 UTC (permalink / raw)
  To: mauve-patches

[-- Attachment #1: Type: text/plain, Size: 932 bytes --]

Hi,

working on cups exposed some more api incompabilities in our HttpURLConnection
implementation. So most of these new tests fail currently and I will try to
follow up with patches on classpath-patches for approval.

One existing test is removed as the used HTTP server doesn't send the tested
header key anymore and therefore this test wrongly fails.

2006-02-04  Wolfgang Baer  <WBaer@gmx.de>

	* gnu/testlet/java/net/HttpURLConnection: New directory.
	* gnu/testlet/java/net/HttpURLConnection/fileNotFound.java,	
	* gnu/testlet/java/net/HttpURLConnection/nullPointerException.java,
	* gnu/testlet/java/net/HttpURLConnection/illegalStateException.java,
	* gnu/testlet/java/net/HttpURLConnection/getRequestProperty.java,
	* gnu/testlet/java/net/HttpURLConnection/getOutputStream.java: New tests.
	* gnu/testlet/java/net/URLConnection/URLConnectionTest.java:
	(test_getHeaderField): Removed failing wrong test.

Regards,
Wolfgang

[-- Attachment #2: URLConnectionPatch.patch --]
[-- Type: text/x-patch, Size: 1193 bytes --]

Index: URLConnectionTest.java
===================================================================
RCS file: /cvs/mauve/mauve/gnu/testlet/java/net/URLConnection/URLConnectionTest.java,v
retrieving revision 1.3
diff -u -r1.3 URLConnectionTest.java
--- URLConnectionTest.java	3 May 2004 03:21:15 -0000	1.3
+++ URLConnectionTest.java	4 Feb 2006 14:03:29 -0000
@@ -295,9 +295,7 @@
       String str2 = conn.getHeaderField("Server");
       harness.debug("header-2: " + str2);
       String str3 = conn.getHeaderField("Last-Modified");
-      harness.debug("header-3: " + str3);
-      String str4 = conn.getHeaderField("Transfer-Encoding");
-      harness.debug("header-4: " + str4);
+      harness.debug("header-3: " + str3);    
       String str5 = conn.getHeaderField("Content-Type");
       harness.debug("header-5: " + str5);
 
@@ -309,10 +307,6 @@
 			   "Error in test_getHeaderField  - 1 " + 
 			   " 2 header field wrong" );
 
-      harness.check ( str4, "chunked",
-			   "Error in test_getHeaderField  - 2 " + 
-			   " 4 header field wrong" );
-
       harness.check ( str5.indexOf("text/html") != -1,
 			   "Error in test_getHeaderField  - 3 " + 
 			   " 5 header field wrong" );

[-- Attachment #3: fileNotFound.java --]
[-- Type: text/x-java, Size: 1590 bytes --]

//Tags: JDK1.1

//Copyright (C) 2006 Free Software Foundation, Inc.
//Written by Wolfgang Baer (WBaer@gmx.de)

//This file is part of Mauve.

//Mauve is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; either version 2, or (at your option)
//any later version.

//Mauve is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with Mauve; see the file COPYING.  If not, write to
//the Free Software Foundation, 51 Franklin Street, Fifth Floor,
//Boston, MA, 02110-1301 USA.


package gnu.testlet.java.net.HttpURLConnection;

import gnu.testlet.TestHarness;
import gnu.testlet.Testlet;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Tests that a 404 passes to the caller without exception.
 */
public class fileNotFound implements Testlet
{
   
  public void test(TestHarness h)
  { 
    try
      {
        URL url = new URL("http://www.redhat.com/mauve/testarea/edeltraut.html");        
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        
        conn.setRequestMethod("GET");
        h.check(conn.getResponseCode() == 404);
        conn.disconnect();        
      }   
    catch (IOException e)
      {       
        e.printStackTrace();
      }
  }

}

[-- Attachment #4: getOutputStream.java --]
[-- Type: text/x-java, Size: 2629 bytes --]

//Tags: JDK1.1

//Copyright (C) 2006 Free Software Foundation, Inc.
//Written by Wolfgang Baer (WBaer@gmx.de)

//This file is part of Mauve.

//Mauve is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; either version 2, or (at your option)
//any later version.

//Mauve is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with Mauve; see the file COPYING.  If not, write to
//the Free Software Foundation, 51 Franklin Street, Fifth Floor,
//Boston, MA, 02110-1301 USA.


package gnu.testlet.java.net.HttpURLConnection;

import gnu.testlet.TestHarness;
import gnu.testlet.Testlet;

import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;

/**
 * Test calling behaviour of getOutputStream. Implicit
 * opening of connection, failing if doOutput is false ...
 */
public class getOutputStream implements Testlet
{   
  public void test(TestHarness h)
  { 
    try
      {
        URL url = new URL("http://sources.redhat.com/mauve/testarea/index.html");        
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        
        conn.setRequestMethod("POST");
        conn.setDoOutput(false);
        
        try
          {
            // doOutput must be true to succeed
            conn.getOutputStream();
            h.check(false);
          }
        catch (ProtocolException e1)
          {
            h.check(true);
          }
        
        conn.setDoOutput(true);
        // getOutputStream must implicit open the connection
        OutputStream stream = conn.getOutputStream();
        
        try
          {
            // and therefore throw this expection
            // no other way to test if we are connected
            conn.getRequestProperties();
            h.check(false);
          }
        catch (IllegalStateException e)
          {
            h.check(true);            
          }
        
        // subsequent calls to getOutputStream must be ignored
        // and the identical stream returned
        OutputStream stream2 = conn.getOutputStream();              
        h.check(stream == stream2);              
        
      }   
    catch (IOException e)
      {       
        e.printStackTrace();
      }
  }
}

[-- Attachment #5: getRequestProperty.java --]
[-- Type: text/x-java, Size: 1533 bytes --]

//Tags: JDK1.1

//Copyright (C) 2006 Free Software Foundation, Inc.
//Written by Wolfgang Baer (WBaer@gmx.de)

//This file is part of Mauve.

//Mauve is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; either version 2, or (at your option)
//any later version.

//Mauve is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with Mauve; see the file COPYING.  If not, write to
//the Free Software Foundation, 51 Franklin Street, Fifth Floor,
//Boston, MA, 02110-1301 USA.


package gnu.testlet.java.net.HttpURLConnection;

import gnu.testlet.TestHarness;
import gnu.testlet.Testlet;

import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Tests the getRequestProperty method
 */
public class getRequestProperty implements Testlet
{
  public void test(TestHarness h)
  {
    try
      {
        URL url = new URL("http://sources.redhat.com/mauve/testarea/index.html");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        conn.setRequestMethod("GET");
               
        String str = conn.getRequestProperty(null);     
        h.check(str == null);
      }
    catch (Exception e)
      {
        e.printStackTrace();
      }
  }
}

[-- Attachment #6: illegalStateException.java --]
[-- Type: text/x-java, Size: 4361 bytes --]

//Tags: JDK1.1

//Copyright (C) 2006 Free Software Foundation, Inc.
//Written by Wolfgang Baer (WBaer@gmx.de)

//This file is part of Mauve.

//Mauve is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; either version 2, or (at your option)
//any later version.

//Mauve is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with Mauve; see the file COPYING.  If not, write to
//the Free Software Foundation, 51 Franklin Street, Fifth Floor,
//Boston, MA, 02110-1301 USA.


package gnu.testlet.java.net.HttpURLConnection;

import gnu.testlet.TestHarness;
import gnu.testlet.Testlet;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Test correct throwing of IllegalStateExceptions
 * for the connected connection.
 */
public class illegalStateException implements Testlet
{
  public void test(TestHarness h)
  {
    try
      {
        URL url = new URL("http://sources.redhat.com/mauve/testarea/index.html");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        conn.setRequestMethod("GET");
        conn.connect();

        try
          {
            conn.getRequestProperties();
            h.check(false);
          }
        catch (IllegalStateException e)
          {
            h.check(true);
          }

        
        // For some reasons SUN does not whats defined
        // in the API and does NOT throw a IllegalStateException
        // for this method - Tested with JDK1.4/1.5
        // to be compatible with SUN we also don't do it
        try
          {
            conn.getRequestProperty("accept");
            h.check(true);
          }
        catch (IllegalStateException e)
          {
            h.check(false);
          }

        try
          {
            conn.setDoInput(true);
            h.check(false);
          }
        catch (IllegalStateException e)
          {
            h.check(true);
          }

        try
          {
            conn.setDoOutput(true);
            h.check(false);
          }
        catch (IllegalStateException e)
          {
            h.check(true);
          }

        try
          {
            conn.setAllowUserInteraction(true);
            h.check(false);
          }
        catch (IllegalStateException e)
          {
            h.check(true);
          }

        try
          {
            conn.setUseCaches(true);
            h.check(false);
          }
        catch (IllegalStateException e)
          {
            h.check(true);
          }

        try
          {
            conn.setIfModifiedSince(100000L);
            h.check(false);
          }
        catch (IllegalStateException e)
          {
            h.check(true);
          }

        try
          {
            conn.setRequestProperty("ssss", "dddd");
            h.check(false);
          }
        catch (IllegalStateException e)
          {
            h.check(true);
          }

        try
        {
          // if already connected the IllegalStateException
          // must take precedence over the NPE
          conn.setRequestProperty(null, "dddd");
          h.check(false);
        }
      catch (IllegalStateException e)
        {
          h.check(true);
        }
      catch (NullPointerException e)
        {
          h.check(false);
        }

        try
          {
            conn.addRequestProperty("ssss", "dddd");
            h.check(false);
          }
        catch (IllegalStateException e)
          {
            h.check(true);
          }

        try
          {
            // if already connected the IllegalStateException
            // must take precedence over the NPE
            conn.addRequestProperty(null, "dddd");
            h.check(false);
          }
        catch (IllegalStateException e)
          {
            h.check(true);
          }
        catch (NullPointerException e)
          {
            h.check(false);
          }

      }
    catch (IOException e)
      {
        e.printStackTrace();
      }
  }
}

[-- Attachment #7: nullPointerException.java --]
[-- Type: text/x-java, Size: 2020 bytes --]

//Tags: JDK1.1

//Copyright (C) 2006 Free Software Foundation, Inc.
//Written by Wolfgang Baer (WBaer@gmx.de)

//This file is part of Mauve.

//Mauve is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; either version 2, or (at your option)
//any later version.

//Mauve is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with Mauve; see the file COPYING.  If not, write to
//the Free Software Foundation, 51 Franklin Street, Fifth Floor,
//Boston, MA, 02110-1301 USA.


package gnu.testlet.java.net.HttpURLConnection;

import gnu.testlet.TestHarness;
import gnu.testlet.Testlet;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Test correct throwing of NPEs in unconnected connections.
 */
public class nullPointerException implements Testlet
{
  public void test(TestHarness h)
  {
    try
      {
        URL url = new URL("http://sources.redhat.com/mauve/testarea/index.html");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        conn.setRequestMethod("GET");
               
        String str = conn.getRequestProperty(null);          
        h.check(str == null);     

        try
          {
            conn.setRequestProperty(null, "dddd");
            h.check(false);
          }
        catch (NullPointerException e)
          {
            h.check(true);
          }     

        try
          {
            conn.addRequestProperty(null, "dddd");
            h.check(false);
          }
        catch (NullPointerException e)
          {
            h.check(true);
          }     

      }
    catch (IOException e)
      {
        e.printStackTrace();
      }
  }
}

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: FYI: New tests for HttpURLConnection
  2006-02-04 14:07 FYI: New tests for HttpURLConnection Wolfgang Baer
@ 2006-02-04 14:16 ` Wolfgang Baer
  2006-02-09  1:58 ` Tom Tromey
  1 sibling, 0 replies; 7+ messages in thread
From: Wolfgang Baer @ 2006-02-04 14:16 UTC (permalink / raw)
  To: mauve-patches

[-- Attachment #1: Type: text/plain, Size: 121 bytes --]

Hi,

the attached files had some debug stuff in. Attached now the new files
which will be committed. Sorry.

Wolfgang




[-- Attachment #2: fileNotFound.java --]
[-- Type: text/x-java, Size: 1745 bytes --]

//Tags: JDK1.1

//Copyright (C) 2006 Free Software Foundation, Inc.
//Written by Wolfgang Baer (WBaer@gmx.de)

//This file is part of Mauve.

//Mauve is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; either version 2, or (at your option)
//any later version.

//Mauve is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with Mauve; see the file COPYING.  If not, write to
//the Free Software Foundation, 51 Franklin Street, Fifth Floor,
//Boston, MA, 02110-1301 USA.


package gnu.testlet.java.net.HttpURLConnection;

import gnu.testlet.TestHarness;
import gnu.testlet.Testlet;

import java.io.FileNotFoundException;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Tests that a 404 passes to the caller without exception.
 */
public class fileNotFound implements Testlet
{
   
  public void test(TestHarness h)
  { 
    try
      {
        URL url = new URL("http://www.redhat.com/mauve/testarea/edeltraut.html");        
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        
        conn.setRequestMethod("GET");
        int code = conn.getResponseCode();
        h.check(code == 404);
        conn.disconnect();        
      }   
    catch (FileNotFoundException e)
      {       
        h.check(false);
      }
    catch (Exception e)
    {       
      h.debug(e);
      h.fail("Unexpected error: " + e.getMessage ());
    }
  }

}

[-- Attachment #3: getOutputStream.java --]
[-- Type: text/x-java, Size: 2646 bytes --]

//Tags: JDK1.1

//Copyright (C) 2006 Free Software Foundation, Inc.
//Written by Wolfgang Baer (WBaer@gmx.de)

//This file is part of Mauve.

//Mauve is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; either version 2, or (at your option)
//any later version.

//Mauve is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with Mauve; see the file COPYING.  If not, write to
//the Free Software Foundation, 51 Franklin Street, Fifth Floor,
//Boston, MA, 02110-1301 USA.


package gnu.testlet.java.net.HttpURLConnection;

import gnu.testlet.TestHarness;
import gnu.testlet.Testlet;

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;

/**
 * Test calling behaviour of getOutputStream. Implicit
 * opening of connection, failing if doOutput is false ...
 */
public class getOutputStream implements Testlet
{   
  public void test(TestHarness h)
  { 
    try
      {
        URL url = new URL("http://sources.redhat.com/mauve/testarea/index.html");        
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        
        conn.setRequestMethod("POST");
        conn.setDoOutput(false);
        
        try
          {
            // doOutput must be true to succeed
            conn.getOutputStream();
            h.check(false);
          }
        catch (ProtocolException e1)
          {
            h.check(true);
          }
        
        conn.setDoOutput(true);
        // getOutputStream must implicit open the connection
        OutputStream stream = conn.getOutputStream();
        
        try
          {
            // and therefore throw this expection
            // no other way to test if we are connected
            conn.getRequestProperties();
            h.check(false);
          }
        catch (IllegalStateException e)
          {
            h.check(true);            
          }
        
        // subsequent calls to getOutputStream must be ignored
        // and the identical stream returned
        OutputStream stream2 = conn.getOutputStream();              
        h.check(stream == stream2);              
        
      }   
    catch (Exception e)
      {       
        h.debug(e);
        h.fail("Unexpected error: " + e.getMessage ());
      }
  }
}

[-- Attachment #4: getRequestProperty.java --]
[-- Type: text/x-java, Size: 1580 bytes --]

//Tags: JDK1.1

//Copyright (C) 2006 Free Software Foundation, Inc.
//Written by Wolfgang Baer (WBaer@gmx.de)

//This file is part of Mauve.

//Mauve is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; either version 2, or (at your option)
//any later version.

//Mauve is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with Mauve; see the file COPYING.  If not, write to
//the Free Software Foundation, 51 Franklin Street, Fifth Floor,
//Boston, MA, 02110-1301 USA.


package gnu.testlet.java.net.HttpURLConnection;

import gnu.testlet.TestHarness;
import gnu.testlet.Testlet;

import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Tests the getRequestProperty method
 */
public class getRequestProperty implements Testlet
{
  public void test(TestHarness h)
  {
    try
      {
        URL url = new URL("http://sources.redhat.com/mauve/testarea/index.html");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        conn.setRequestMethod("GET");
               
        String str = conn.getRequestProperty(null);     
        h.check(str == null);
      }
    catch (Exception e)
      {
        h.debug(e);
        h.fail("Unexpected error: " + e.getMessage ());
      }
  }
}

[-- Attachment #5: illegalStateException.java --]
[-- Type: text/x-java, Size: 4397 bytes --]

//Tags: JDK1.1

//Copyright (C) 2006 Free Software Foundation, Inc.
//Written by Wolfgang Baer (WBaer@gmx.de)

//This file is part of Mauve.

//Mauve is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; either version 2, or (at your option)
//any later version.

//Mauve is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with Mauve; see the file COPYING.  If not, write to
//the Free Software Foundation, 51 Franklin Street, Fifth Floor,
//Boston, MA, 02110-1301 USA.


package gnu.testlet.java.net.HttpURLConnection;

import gnu.testlet.TestHarness;
import gnu.testlet.Testlet;

import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Test correct throwing of IllegalStateExceptions
 * for the connected connection.
 */
public class illegalStateException implements Testlet
{
  public void test(TestHarness h)
  {
    try
      {
        URL url = new URL("http://sources.redhat.com/mauve/testarea/index.html");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        conn.setRequestMethod("GET");
        conn.connect();

        try
          {
            conn.getRequestProperties();
            h.check(false);
          }
        catch (IllegalStateException e)
          {
            h.check(true);
          }

        // For some reasons SUN does not whats defined
        // in the API and does NOT throw a IllegalStateException
        // for this method - Tested with JDK1.4/1.5
        // to be compatible with SUN we also don't do it
        try
          {
            conn.getRequestProperty("accept");
            h.check(true);
          }
        catch (IllegalStateException e)
          {
            h.check(false);
          }

        try
          {
            conn.setDoInput(true);
            h.check(false);
          }
        catch (IllegalStateException e)
          {
            h.check(true);
          }

        try
          {
            conn.setDoOutput(true);
            h.check(false);
          }
        catch (IllegalStateException e)
          {
            h.check(true);
          }

        try
          {
            conn.setAllowUserInteraction(true);
            h.check(false);
          }
        catch (IllegalStateException e)
          {
            h.check(true);
          }

        try
          {
            conn.setUseCaches(true);
            h.check(false);
          }
        catch (IllegalStateException e)
          {
            h.check(true);
          }

        try
          {
            conn.setIfModifiedSince(100000L);
            h.check(false);
          }
        catch (IllegalStateException e)
          {
            h.check(true);
          }

        try
          {
            conn.setRequestProperty("ssss", "dddd");
            h.check(false);
          }
        catch (IllegalStateException e)
          {
            h.check(true);
          }

        try
          {
            // if already connected the IllegalStateException
            // must take precedence over the NPE
            conn.setRequestProperty(null, "dddd");
            h.check(false);
          }
        catch (IllegalStateException e)
          {
            h.check(true);
          }
        catch (NullPointerException e)
          {
            h.check(false);
          }

        try
          {
            conn.addRequestProperty("ssss", "dddd");
            h.check(false);
          }
        catch (IllegalStateException e)
          {
            h.check(true);
          }

        try
          {
            // if already connected the IllegalStateException
            // must take precedence over the NPE
            conn.addRequestProperty(null, "dddd");
            h.check(false);
          }
        catch (IllegalStateException e)
          {
            h.check(true);
          }
        catch (NullPointerException e)
          {
            h.check(false);
          }

      }
    catch (Exception e)
      {
        h.debug(e);
        h.fail("Unexpected error: " + e.getMessage ());
      }
  }
}

[-- Attachment #6: nullPointerException.java --]
[-- Type: text/x-java, Size: 2037 bytes --]

//Tags: JDK1.1

//Copyright (C) 2006 Free Software Foundation, Inc.
//Written by Wolfgang Baer (WBaer@gmx.de)

//This file is part of Mauve.

//Mauve is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; either version 2, or (at your option)
//any later version.

//Mauve is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with Mauve; see the file COPYING.  If not, write to
//the Free Software Foundation, 51 Franklin Street, Fifth Floor,
//Boston, MA, 02110-1301 USA.


package gnu.testlet.java.net.HttpURLConnection;

import gnu.testlet.TestHarness;
import gnu.testlet.Testlet;

import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Test correct throwing of NPEs in unconnected connections.
 */
public class nullPointerException implements Testlet
{
  public void test(TestHarness h)
  {
    try
      {
        URL url = new URL("http://sources.redhat.com/mauve/testarea/index.html");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        conn.setRequestMethod("GET");
               
        String str = conn.getRequestProperty(null);          
        h.check(str == null);     

        try
          {
            conn.setRequestProperty(null, "dddd");
            h.check(false);
          }
        catch (NullPointerException e)
          {
            h.check(true);
          }     

        try
          {
            conn.addRequestProperty(null, "dddd");
            h.check(false);
          }
        catch (NullPointerException e)
          {
            h.check(true);
          }     

      }
    catch (Exception e)
      {
        h.debug(e);
        h.fail("Unexpected error: " + e.getMessage ());
      }
  }
}

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: FYI: New tests for HttpURLConnection
  2006-02-04 14:07 FYI: New tests for HttpURLConnection Wolfgang Baer
  2006-02-04 14:16 ` Wolfgang Baer
@ 2006-02-09  1:58 ` Tom Tromey
  2006-02-09  7:48   ` Wolfgang Baer
  1 sibling, 1 reply; 7+ messages in thread
From: Tom Tromey @ 2006-02-09  1:58 UTC (permalink / raw)
  To: Wolfgang Baer; +Cc: mauve-patches

>>>>> "Wolfgang" == Wolfgang Baer <WBaer@gmx.de> writes:

Wolfgang> One existing test is removed as the used HTTP server doesn't
Wolfgang> send the tested header key anymore and therefore this test
Wolfgang> wrongly fails.

David Daney had the interesting idea that we could have a mini http
server inside of Mauve, which we could then use to test all the http
modes we care about.  Specifically what he had suggested was encoding
the desired response into the request, so we could check 404s,
redirects, chunking, etc.

Just in case you're really motivated to hack in this area ...  :-)

Tom

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: FYI: New tests for HttpURLConnection
  2006-02-09  1:58 ` Tom Tromey
@ 2006-02-09  7:48   ` Wolfgang Baer
  2006-03-02 21:26     ` RFC: HTTPTestServer (was: Re: FYI: New tests for HttpURLConnection) Wolfgang Baer
  0 siblings, 1 reply; 7+ messages in thread
From: Wolfgang Baer @ 2006-02-09  7:48 UTC (permalink / raw)
  To: tromey; +Cc: mauve-patches

Tom Tromey wrote:
>>>>>>"Wolfgang" == Wolfgang Baer <WBaer@gmx.de> writes:
> 
> 
> Wolfgang> One existing test is removed as the used HTTP server doesn't
> Wolfgang> send the tested header key anymore and therefore this test
> Wolfgang> wrongly fails.
> 
> David Daney had the interesting idea that we could have a mini http
> server inside of Mauve, which we could then use to test all the http
> modes we care about.  Specifically what he had suggested was encoding
> the desired response into the request, so we could check 404s,
> redirects, chunking, etc.

Thats definitly the way to go.
> 
> Just in case you're really motivated to hack in this area ...  :-)

Not at this time. I just tried to get the bugs fixed I found during
working on the IPP backend for printing and which prevented me from
hacking.

Maybe after printing is finished :-)

Wolfgang

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RFC: HTTPTestServer (was: Re: FYI: New tests for HttpURLConnection)
  2006-02-09  7:48   ` Wolfgang Baer
@ 2006-03-02 21:26     ` Wolfgang Baer
  2006-03-02 23:49       ` RFC: HTTPTestServer David Daney
  0 siblings, 1 reply; 7+ messages in thread
From: Wolfgang Baer @ 2006-03-02 21:26 UTC (permalink / raw)
  To: mauve-patches; +Cc: tromey, David Daney

[-- Attachment #1: Type: text/plain, Size: 1686 bytes --]

Hi,

Wolfgang Baer wrote:
> Tom Tromey wrote:
>>David Daney had the interesting idea that we could have a mini http
>>server inside of Mauve, which we could then use to test all the http
>>modes we care about.  Specifically what he had suggested was encoding
>>the desired response into the request, so we could check 404s,
>>redirects, chunking, etc.
> 
> 
> Thats definitly the way to go.
> 
>>Just in case you're really motivated to hack in this area ...  :-)
> 
> 
> Not at this time. I just tried to get the bugs fixed I found during
> working on the IPP backend for printing and which prevented me from
> hacking.

Well, as I wanted to test Davids Headers rewrite patch I needed to do
a first hack of a TestHttpServer.

Here we go:

The TestHttpServer has methods to set the Headers and the Body it
should return upon request. Furthermore it provides an interface
which can be implemented to test the serverside received headers and body.

As I am not experienced in this networking area this might be a hack.
However a hack which is working so far :-)

I also wrote two tests to show the usage.

responseCodeTest - test for all error codes the correct handling for
                   getInputStream and getErrorStream.
responseHeadersTest - tests everything with response headers.

2006-03-02  Wolfgang Baer  <WBaer@gmx.de>

	* gnu/testlet/java/net/HttpURLConnection/TestHttpServer.java: New file.
	* gnu/testlet/java/net/HttpURLConnection/responseCodeTest.java:
	New test using TestHttpServer.java.
	* gnu/testlet/java/net/HttpURLConnection/responseHeadersTest.java:
	Likewise.


Comments or OK to commit ?
BTW, whats the best port to use for the TestHttpServer ?

Wolfgang





[-- Attachment #2: TestHttpServer.java --]
[-- Type: text/x-java, Size: 6776 bytes --]

//Tags: not-a-test

//Copyright (C) 2006 Free Software Foundation, Inc.
//Written by Wolfgang Baer (WBaer@gmx.de)

//This file is part of Mauve.

//Mauve is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; either version 2, or (at your option)
//any later version.

//Mauve is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with Mauve; see the file COPYING.  If not, write to
//the Free Software Foundation, 51 Franklin Street, Fifth Floor,
//Boston, MA, 02110-1301 USA.

package gnu.testlet.java.net.HttpURLConnection;


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;


/**
 * A HTTP server for testing purpose only. The server can
 * be started on a given port and the response headers and
 * response body to be returned set. This way one can test
 * arbritrary testcases for the http client side library.
 *  
 * @see gnu.testlet.java.net.HttpURLConnection.responseCodeTest
 * @see gnu.testlet.java.net.HttpURLConnection.responseHeadersTest
 * 
 * @author Wolfgang Baer (WBaer@gmx.de)
 */
public final class TestHttpServer implements Runnable
{ 
  /**
   * The interface to be implemented if checking
   * of the serverside received headers and body
   * is desired.
   */
  interface CheckReceivedRequest 
  {
    /**
     * Will be called with a List of headers in 
     * the sequence received by the test server.
     * 
     * @param headers List of headers
     */
    public void checkHeaders(List headers);
    
    /**
     * Will be called with the body if one was
     * received.
     * 
     * @param body the body
     */
    public void checkBody(byte[] body);    
  }
  
  /**
   * The actual request handler.
   * It always returns what is set in the TestHttpServer
   * for the response headers and response body.
   */
  class TestHttpRequestHandler implements Runnable
  {  
    Socket socket;
    OutputStream output;
    InputStream input;
  
    public TestHttpRequestHandler(Socket socket) throws Exception
    {
      this.socket = socket;
      output = socket.getOutputStream();
      input = socket.getInputStream();
    }
  
    public void run()
    {
      try
        {
          // Read the whole request into a byte array
          ByteArrayOutputStream buffer = new ByteArrayOutputStream();
          
          byte[] b = new byte[1024];
          int bytes = 0 ;
          while ((bytes = input.read(b)) != -1)
            { 
              buffer.write(b, 0, bytes);
              if (bytes < 1024)
                break;
            }
          
          byte[] request = buffer.toByteArray();

          // Parse the request headers from the byte array          
          List headerList = new ArrayList();
          
          ByteArrayOutputStream line;
          int i = 0;
          line = new ByteArrayOutputStream();
          for (; i < request.length; i++)
            {
              if (request[i] != (byte) 0x0a) // LF
                line.write(request[i]);
              else
                {
                  byte[] array = line.toByteArray();
                  if (array.length == 1) // the last is only a LF
                    break;

                  String headerLine = new String(array);
                  headerList.add(headerLine);
                  line = new ByteArrayOutputStream();
                }
            }

          // Put the remaining bytes into the request body 
          byte[] body = new byte[(request.length - (i + 1))];
          System.arraycopy(request, i + 1, body, 0, body.length);
          
          // Check everything          
          if (check != null)
            {
              check.checkHeaders(headerList);
              if (body.length > 0)
                check.checkBody(body);
            }         
             
          // Response writing          
          // write the response headers
          output.write(responseHeader);
          
          // write the body 
          if (responseBody != null)
            output.write(responseBody);
          
          // Clean up
          output.close();
          input.close();
          socket.close();            
        }
      catch (Exception e)
        {
          // ignore
        }
    }
  }

  int port;
  byte[] responseHeader;
  byte[] responseBody;
  CheckReceivedRequest check;
  boolean kill = false;
  ServerSocket serverSocket;  

  /**
   * Create a TestHttpServer on given port
   * @param port the port to use.
   */
  public TestHttpServer(int port)
  {
    this.port = port;
  }
  
  /**
   * An object implementing the CheckReceivedRequest
   * interface. The methods will be called to enable
   * checks of the serverside received request.
   * 
   * @param responseBody the byte[] of the body
   */
  public void setCheckReceivedRequest(CheckReceivedRequest object)
  {
    this.check = object;
  }
  
  /**
   * The bytes which should be sent as the response body.
   * @param responseBody the byte[] of the body
   */
  public void setResponseBody(byte[] responseBody)
  {
    this.responseBody = responseBody;
  }
  
  /**
   * The bytes which should be sent as the response headers. 
   * @param responseHeaders the byte[] of the headers
   */
  public void setResponseHeaders(byte[] responseHeaders)
  {
    this.responseHeader = responseHeaders;
  }
  
  /**
   * This cleans up recources so more than one
   * TestHttpServer can be used in one mauve run.
   */
  public void killTestServer()
  {
    kill = true;
    try
      {
        serverSocket.close();
      }
    catch (IOException e)
      {
        // ignore
      }
  }
  
  /**
   * Listens on the port and creates a Handler for
   * incoming connections.
   */
  public void run() 
  {   
    try
      {
        serverSocket = new ServerSocket(port);
        while (! kill)
          {
            Socket socket = serverSocket.accept();
            try
              {
                TestHttpRequestHandler request = 
                  new TestHttpRequestHandler(socket);
                Thread thread = new Thread(request);
                
                thread.start();
              }
            catch (Exception e)
              {
                // ignore
              }
          }
      }
    catch (IOException e)
      {
        // ignore
      }
  }
}

[-- Attachment #3: responseCodeTest.java --]
[-- Type: text/x-java, Size: 3809 bytes --]

//Tags: JDK1.1
//Uses: TestHttpServer

//Copyright (C) 2006 Free Software Foundation, Inc.
//Written by Wolfgang Baer (WBaer@gmx.de)

//This file is part of Mauve.

//Mauve is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; either version 2, or (at your option)
//any later version.

//Mauve is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with Mauve; see the file COPYING.  If not, write to
//the Free Software Foundation, 51 Franklin Street, Fifth Floor,
//Boston, MA, 02110-1301 USA.

package gnu.testlet.java.net.HttpURLConnection;

import gnu.testlet.TestHarness;
import gnu.testlet.Testlet;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Tests correct behaviour of getInputStream(), getErrorStream()
 * for all error response codes.
 */
public class responseCodeTest implements Testlet
{
  /**
   * Starts an HTTP server on port 8080 and calls
   * the test_ResponseCode for the error codes.
   */
  public void test(TestHarness h) 
  {  
    TestHttpServer server = null;
    try
      {
        server = new TestHttpServer(8080);
        Thread thread = new Thread(server);
        thread.start();
        try
          {
            Thread.sleep(500);
          }
        catch (InterruptedException e1)
          {
          }
        
        for (int i=400; i < 418; i++)
          test_ResponseCode(i, h, server);
        
        for (int i=500; i < 506; i++)
          test_ResponseCode(i, h, server);
      }
    finally
      {
        server.killTestServer();
      }   
  }
  
  public void test_ResponseCode(int responseCode, 
      TestHarness h, TestHttpServer server)
  {    
    try
      {        
        URL url = new URL("http://localhost:8080/");        
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();        
        conn.setRequestMethod("GET");
        
        h.checkPoint("Test " + responseCode + " response");

        // construct what should be returned by the test server as headers        
        ByteArrayOutputStream headers = new ByteArrayOutputStream();

        // status line (the responsecode encoded for the test)
        headers.write(new String("HTTP/1.0 " + responseCode + " OK\r\n").getBytes());
        headers.write("Server: TestServer\r\n\r\n".getBytes());

        server.setResponseHeaders(headers.toByteArray());
        
        // test the responsecode
        int code = conn.getResponseCode();
        h.check(code == responseCode);

        // getInputStream should always throw an IOException
        try 
          {
            conn.getInputStream();
            h.check(false);
          }
        catch (IOException e)
          {
            // for a 404/410 it must be a FNFE
            if (responseCode == 404 
                || responseCode == 410)
              {
                if (e instanceof FileNotFoundException)
                  h.check(true);
                else
                  h.check(false);
              }
            else
              h.check(true);
          }
        
        // the errorstream must be set always
        InputStream error = conn.getErrorStream();
        h.check(error != null);
        
        conn.disconnect();       
      }   
    catch (IOException e)
      {       
        h.debug("Unexpected IOException");
        h.debug(e);
      }
  }
}

[-- Attachment #4: responseHeadersTest.java --]
[-- Type: text/x-java, Size: 7648 bytes --]

//Tags: JDK1.4
//Uses: TestHttpServer

//Copyright (C) 2006 Free Software Foundation, Inc.
//Written by Wolfgang Baer (WBaer@gmx.de)

//This file is part of Mauve.

//Mauve is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; either version 2, or (at your option)
//any later version.

//Mauve is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with Mauve; see the file COPYING.  If not, write to
//the Free Software Foundation, 51 Franklin Street, Fifth Floor,
//Boston, MA, 02110-1301 USA.

package gnu.testlet.java.net.HttpURLConnection;

import gnu.testlet.TestHarness;
import gnu.testlet.Testlet;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;

public class responseHeadersTest implements Testlet
{

  public void test(TestHarness harness)
  {
    TestHttpServer server = null;
    try
      {
        server = new TestHttpServer(8080);
        Thread thread = new Thread(server);
        thread.start();
        try
          {
            Thread.sleep(500);
          }
        catch (InterruptedException e1)
          {
          }
        
        test_MultiHeaders(harness, server);
      }
    finally
      {
        server.killTestServer();
      }   
  }
  
  public void test_MultiHeaders(TestHarness h, TestHttpServer server)
  {    
    try
      {        
        URL url = new URL("http://localhost:8080/");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");

        // construct what should be returned by the test server as headers
        ByteArrayOutputStream headers = new ByteArrayOutputStream();
        headers.write("HTTP/1.0 200 OK\r\n".getBytes());
        headers.write("Server: TestServer\r\n".getBytes());
        headers.write("Key1: value, value2\r\n".getBytes());
        // set the header a second time with different values
        // these values must be prepended to key1
        headers.write("Key1: value3\r\n".getBytes());
        headers.write("IntHeader: 1234\r\n".getBytes());
        headers.write("IntHeaderMalformed: 1234XY\r\n".getBytes());
        headers.write("DateHeader: Thu, 02 Mar 2006 14:34:55 +0000\r\n".getBytes());
        headers.write("DateHeaderMalformed: Thu, 02 Mar 2006V 14:13:07 +0000\r\n\r\n".getBytes());

        server.setResponseHeaders(headers.toByteArray());
        
        h.checkPoint("getHeaderFields()");
        Map fields = conn.getHeaderFields();
        
        // check that map is unmodifiable
        try 
          {
            fields.clear();
            h.check(false);
          }
        catch (UnsupportedOperationException e) 
          {
            h.check(true);
          }
      
        // exactly 7 headers with status and server header        
        h.check(fields.size() == 7);
        
        // check for list and that case matters for key
        Object obj = fields.get("Key1");
        if (! (obj instanceof List))
          h.check(false);
        else
          {
            h.check(true);
            List value = (List) obj;
            h.check(value.size() == 2);
            h.check(value.get(0).equals("value3"));
            h.check(value.get(1).equals("value, value2"));
            
            // check that it is an unmodifiable list
            try 
              {
                value.remove(0);
                h.check(false);
              }
            catch (UnsupportedOperationException e) 
              {
                h.check(true);
              }
          }
        
        // wrong case for key
        obj = fields.get("key1");
        h.check(obj == null);
          
        
        // checks for getHeaderField/Key(int)        
        h.checkPoint("getHeaderField(int)");
        // check that index 0 is the statusline
        String statusline = conn.getHeaderField(0);
        h.check(statusline.equals("HTTP/1.0 200 OK"));
        // indexes out of bound must return null
        String aboutIndex = conn.getHeaderField(44);
        h.check(aboutIndex == null);
        String belowIndex = conn.getHeaderField(-1);
        h.check(belowIndex == null);
        // check that correct key/value name is returned
        String key1_Value = conn.getHeaderField(2);
        h.check(key1_Value.equals("value, value2"));
        
        h.checkPoint("getHeaderFieldKey(int)");       
        // check that index 0 is the statusline
        String statuslineKey = conn.getHeaderFieldKey(0);        
        h.check(statuslineKey == null);
        // indexes out of bound must return null
        String aboutIndexKey = conn.getHeaderFieldKey(44);        
        h.check(aboutIndexKey == null);
        String belowIndexKey = conn.getHeaderFieldKey(-1);
        h.check(belowIndexKey == null);
        // check that correct key/value name is returned
        String key1_Key = conn.getHeaderFieldKey(2);
        h.check(key1_Key.equals("Key1"));
        
        
        // checks getHeaderFieldDate
        h.checkPoint("getHeaderFieldDate()");
        // correct date header field
        long dateHeader = conn.getHeaderFieldDate("DateHeader", 5555);
        h.check(dateHeader == 1141310095000L);
        // missing date header field
        dateHeader = conn.getHeaderFieldDate("DateHeaderXX", 5555);
        h.check(dateHeader == 5555);
        // malformed date header value 
        dateHeader = conn.getHeaderFieldDate("DateHeaderMalformed", 5555);
        h.check(dateHeader == 5555);
        
        // checks getHeaderFieldInt
        h.checkPoint("getHeaderFieldInt()");
        // correct int header field
        int intHeader = conn.getHeaderFieldInt("IntHeader", 5555);
        h.check(intHeader == 1234);
        // missing int header field
        intHeader = conn.getHeaderFieldInt("IntHeaderXX", 5555);
        h.check(intHeader == 5555);
        // malformed int header value 
        intHeader = conn.getHeaderFieldInt("IntHeaderMalformed", 5555);
        h.check(intHeader == 5555);
        
        
        // checks that the convenience methods of the headers
        // not set in this test return the correct default values
        h.checkPoint("convenience methods");
        h.check(conn.getLastModified() == 0);
        h.check(conn.getDate() == 0);
        h.check(conn.getExpiration() == 0);
        h.check(conn.getContentEncoding() == null);
        h.check(conn.getContentType() == null);
        h.check(conn.getContentLength() == -1);
        
        
        // checks getHeaderField(String)
        h.checkPoint("getHeaderField(String)");
        String field = conn.getHeaderField("Server");
        String field1 = conn.getHeaderField("server");
        h.check(field.equals("TestServer"));
        h.check(field == field1);
        
        String none = conn.getHeaderField("NotExistent");
        h.check(none == null);
        
        // check for multiple times same key
        String field_key1 = conn.getHeaderField("Key1");
        h.check(field_key1.equals("value3"));

      }
    catch (IOException e)
    {
      h.debug("Unexpected IOException");
      h.debug(e);
    }
    catch (RuntimeException e)
      {
        h.debug("Unexpected IOException");
        h.debug(e);
      }
    
  }

}

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: RFC: HTTPTestServer
  2006-03-02 21:26     ` RFC: HTTPTestServer (was: Re: FYI: New tests for HttpURLConnection) Wolfgang Baer
@ 2006-03-02 23:49       ` David Daney
  2006-03-06 18:46         ` Wolfgang Baer
  0 siblings, 1 reply; 7+ messages in thread
From: David Daney @ 2006-03-02 23:49 UTC (permalink / raw)
  To: Wolfgang Baer; +Cc: mauve-patches, tromey

Wolfgang Baer wrote:
> Hi,
> 
> Wolfgang Baer wrote:
> 
>>Tom Tromey wrote:
>>
>>>David Daney had the interesting idea that we could have a mini http
>>>server inside of Mauve, which we could then use to test all the http
>>>modes we care about.  Specifically what he had suggested was encoding
>>>the desired response into the request, so we could check 404s,
>>>redirects, chunking, etc.
>>
>>
>>Thats definitly the way to go.
>>
>>
>>>Just in case you're really motivated to hack in this area ...  :-)
>>
>>
>>Not at this time. I just tried to get the bugs fixed I found during
>>working on the IPP backend for printing and which prevented me from
>>hacking.
> 
> 
> Well, as I wanted to test Davids Headers rewrite patch I needed to do
> a first hack of a TestHttpServer.
> 
> Here we go:
> 
> The TestHttpServer has methods to set the Headers and the Body it
> should return upon request. Furthermore it provides an interface
> which can be implemented to test the serverside received headers and body.
> 
> As I am not experienced in this networking area this might be a hack.
> However a hack which is working so far :-)
> 
> I also wrote two tests to show the usage.
> 
> responseCodeTest - test for all error codes the correct handling for
>                    getInputStream and getErrorStream.
> responseHeadersTest - tests everything with response headers.
> 
> 2006-03-02  Wolfgang Baer  <WBaer@gmx.de>
> 
> 	* gnu/testlet/java/net/HttpURLConnection/TestHttpServer.java: New file.
> 	* gnu/testlet/java/net/HttpURLConnection/responseCodeTest.java:
> 	New test using TestHttpServer.java.
> 	* gnu/testlet/java/net/HttpURLConnection/responseHeadersTest.java:
> 	Likewise.
> 
> 
> Comments or OK to commit ?

I am of a mixed mind on that.

Here in mauve, no formal permission is needed to commit.

At the highest level this is what I had in mind, but I had pictured it 
in my mind as being implemented slightly differently.  I was thinking 
about something similar to a servlet engine, where the capabilities of 
the server could be changed by writing different plug-in classes for it. 
  You have a limited set of capabilities hard coded in the server, so 
the server has to change if we want to do something that it does not 
support.  Also it has no support for multiple connections or multiple 
requests on a keep-alive connection.

Not withstanding all of that, I think it is an excellent start to a very 
much needed capability.  Just because it is not exactly what I would 
have done does not mean that it is not good and useful.

My call:  You decide.  Commit it in as is, or keep hacking and commit later.

Thanks,
David Daney

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: RFC: HTTPTestServer
  2006-03-02 23:49       ` RFC: HTTPTestServer David Daney
@ 2006-03-06 18:46         ` Wolfgang Baer
  0 siblings, 0 replies; 7+ messages in thread
From: Wolfgang Baer @ 2006-03-06 18:46 UTC (permalink / raw)
  To: David Daney; +Cc: mauve-patches

[-- Attachment #1: Type: text/plain, Size: 952 bytes --]

Hi David,

David Daney wrote:
[...]
> 
> Not withstanding all of that, I think it is an excellent start to a very
> much needed capability.  Just because it is not exactly what I would
> have done does not mean that it is not good and useful.
> 
> My call:  You decide.  Commit it in as is, or keep hacking and commit
> later.

I will commit it now to have the tests available. However you are certainly
right that its nothing extensible and could be improved a lot. But I currently
have no time to do this further. If anyone in the future comes up with
something modular I will happily convert the tests to use that.

Committed as follows:

2006-03-06  Wolfgang Baer  <WBaer@gmx.de>

	* gnu/testlet/java/net/HttpURLConnection/TestHttpServer.java: New file.
	* gnu/testlet/java/net/HttpURLConnection/responseCodeTest.java:
	New test using TestHttpServer.java.
	* gnu/testlet/java/net/HttpURLConnection/responseHeadersTest.java:
	Likewise.


Wolfgang



[-- Attachment #2: TestHttpServer.java --]
[-- Type: text/x-java, Size: 6776 bytes --]

//Tags: not-a-test

//Copyright (C) 2006 Free Software Foundation, Inc.
//Written by Wolfgang Baer (WBaer@gmx.de)

//This file is part of Mauve.

//Mauve is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; either version 2, or (at your option)
//any later version.

//Mauve is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with Mauve; see the file COPYING.  If not, write to
//the Free Software Foundation, 51 Franklin Street, Fifth Floor,
//Boston, MA, 02110-1301 USA.

package gnu.testlet.java.net.HttpURLConnection;


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;


/**
 * A HTTP server for testing purpose only. The server can
 * be started on a given port and the response headers and
 * response body to be returned set. This way one can test
 * arbritrary testcases for the http client side library.
 *  
 * @see gnu.testlet.java.net.HttpURLConnection.responseCodeTest
 * @see gnu.testlet.java.net.HttpURLConnection.responseHeadersTest
 * 
 * @author Wolfgang Baer (WBaer@gmx.de)
 */
public final class TestHttpServer implements Runnable
{ 
  /**
   * The interface to be implemented if checking
   * of the serverside received headers and body
   * is desired.
   */
  interface CheckReceivedRequest 
  {
    /**
     * Will be called with a List of headers in 
     * the sequence received by the test server.
     * 
     * @param headers List of headers
     */
    public void checkHeaders(List headers);
    
    /**
     * Will be called with the body if one was
     * received.
     * 
     * @param body the body
     */
    public void checkBody(byte[] body);    
  }
  
  /**
   * The actual request handler.
   * It always returns what is set in the TestHttpServer
   * for the response headers and response body.
   */
  class TestHttpRequestHandler implements Runnable
  {  
    Socket socket;
    OutputStream output;
    InputStream input;
  
    public TestHttpRequestHandler(Socket socket) throws Exception
    {
      this.socket = socket;
      output = socket.getOutputStream();
      input = socket.getInputStream();
    }
  
    public void run()
    {
      try
        {
          // Read the whole request into a byte array
          ByteArrayOutputStream buffer = new ByteArrayOutputStream();
          
          byte[] b = new byte[1024];
          int bytes = 0 ;
          while ((bytes = input.read(b)) != -1)
            { 
              buffer.write(b, 0, bytes);
              if (bytes < 1024)
                break;
            }
          
          byte[] request = buffer.toByteArray();

          // Parse the request headers from the byte array          
          List headerList = new ArrayList();
          
          ByteArrayOutputStream line;
          int i = 0;
          line = new ByteArrayOutputStream();
          for (; i < request.length; i++)
            {
              if (request[i] != (byte) 0x0a) // LF
                line.write(request[i]);
              else
                {
                  byte[] array = line.toByteArray();
                  if (array.length == 1) // the last is only a LF
                    break;

                  String headerLine = new String(array);
                  headerList.add(headerLine);
                  line = new ByteArrayOutputStream();
                }
            }

          // Put the remaining bytes into the request body 
          byte[] body = new byte[(request.length - (i + 1))];
          System.arraycopy(request, i + 1, body, 0, body.length);
          
          // Check everything          
          if (check != null)
            {
              check.checkHeaders(headerList);
              if (body.length > 0)
                check.checkBody(body);
            }         
             
          // Response writing          
          // write the response headers
          output.write(responseHeader);
          
          // write the body 
          if (responseBody != null)
            output.write(responseBody);
          
          // Clean up
          output.close();
          input.close();
          socket.close();            
        }
      catch (Exception e)
        {
          // ignore
        }
    }
  }

  int port;
  byte[] responseHeader;
  byte[] responseBody;
  CheckReceivedRequest check;
  boolean kill = false;
  ServerSocket serverSocket;  

  /**
   * Create a TestHttpServer on given port
   * @param port the port to use.
   */
  public TestHttpServer(int port)
  {
    this.port = port;
  }
  
  /**
   * An object implementing the CheckReceivedRequest
   * interface. The methods will be called to enable
   * checks of the serverside received request.
   * 
   * @param responseBody the byte[] of the body
   */
  public void setCheckReceivedRequest(CheckReceivedRequest object)
  {
    this.check = object;
  }
  
  /**
   * The bytes which should be sent as the response body.
   * @param responseBody the byte[] of the body
   */
  public void setResponseBody(byte[] responseBody)
  {
    this.responseBody = responseBody;
  }
  
  /**
   * The bytes which should be sent as the response headers. 
   * @param responseHeaders the byte[] of the headers
   */
  public void setResponseHeaders(byte[] responseHeaders)
  {
    this.responseHeader = responseHeaders;
  }
  
  /**
   * This cleans up recources so more than one
   * TestHttpServer can be used in one mauve run.
   */
  public void killTestServer()
  {
    kill = true;
    try
      {
        serverSocket.close();
      }
    catch (IOException e)
      {
        // ignore
      }
  }
  
  /**
   * Listens on the port and creates a Handler for
   * incoming connections.
   */
  public void run() 
  {   
    try
      {
        serverSocket = new ServerSocket(port);
        while (! kill)
          {
            Socket socket = serverSocket.accept();
            try
              {
                TestHttpRequestHandler request = 
                  new TestHttpRequestHandler(socket);
                Thread thread = new Thread(request);
                
                thread.start();
              }
            catch (Exception e)
              {
                // ignore
              }
          }
      }
    catch (IOException e)
      {
        // ignore
      }
  }
}

[-- Attachment #3: responseCodeTest.java --]
[-- Type: text/x-java, Size: 3940 bytes --]

//Tags: JDK1.1
//Uses: TestHttpServer

//Copyright (C) 2006 Free Software Foundation, Inc.
//Written by Wolfgang Baer (WBaer@gmx.de)

//This file is part of Mauve.

//Mauve is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; either version 2, or (at your option)
//any later version.

//Mauve is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with Mauve; see the file COPYING.  If not, write to
//the Free Software Foundation, 51 Franklin Street, Fifth Floor,
//Boston, MA, 02110-1301 USA.

package gnu.testlet.java.net.HttpURLConnection;

import gnu.testlet.TestHarness;
import gnu.testlet.Testlet;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Tests correct behaviour of getInputStream(), getErrorStream()
 * for all error response codes.
 */
public class responseCodeTest implements Testlet
{
  /**
   * Starts an HTTP server on port 8080 and calls
   * the test_ResponseCode for the error codes.
   */
  public void test(TestHarness h) 
  {  
    TestHttpServer server = null;
    try
      {
        server = new TestHttpServer(8080);
        Thread thread = new Thread(server);
        thread.start();
        try
          {
//          SUN JDK needs some time to open sockets
            Thread.sleep(100);
          }
        catch (InterruptedException e1)
          {
          }
        
        for (int i=400; i < 418; i++)
          test_ResponseCode(i, h, server);
        
        for (int i=500; i < 506; i++)
          test_ResponseCode(i, h, server);
      }
    finally
      {
        server.killTestServer();
      }   
  }
  
  public void test_ResponseCode(int responseCode, 
      TestHarness h, TestHttpServer server)
  {    
    try
      {        
        URL url = new URL("http://localhost:8080/");        
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();        
        conn.setRequestMethod("GET");
        
        h.checkPoint("Test " + responseCode + " response");

        // construct what should be returned by the test server as headers        
        ByteArrayOutputStream headers = new ByteArrayOutputStream();

        // status line (the responsecode encoded for the test)
        headers.write(new String("HTTP/1.0 " + responseCode + " OK\r\n").getBytes());
        headers.write("Server: TestServer\r\n\r\n".getBytes());

        server.setResponseHeaders(headers.toByteArray());
        
        // test the responsecode
        int code = conn.getResponseCode();
        h.check(code == responseCode);

        // getInputStream should always throw an IOException
        try 
          {
            conn.getInputStream();
            h.check(false);
          }
        catch (IOException e)
          {
            // for a 404/410 it must be a FNFE
            if (responseCode == 404 
                || responseCode == 410)
              {
                // Since JDK 1.5 as FNFE is thrown - so this will fail for 1.4
                if (e instanceof FileNotFoundException)
                  h.check(true);
                else
                  h.check(false);
              }
            else
              h.check(true);
          }
        
        // the errorstream must be set always
        InputStream error = conn.getErrorStream();
        h.check(error != null);
        
        conn.disconnect();       
      }   
    catch (IOException e)
      {       
        h.debug("Unexpected IOException");
        h.debug(e);
      }
  }
}

[-- Attachment #4: responseHeadersTest.java --]
[-- Type: text/x-java, Size: 9894 bytes --]

//Tags: JDK1.4
//Uses: TestHttpServer

//Copyright (C) 2006 Free Software Foundation, Inc.
//Written by Wolfgang Baer (WBaer@gmx.de)

//This file is part of Mauve.

//Mauve is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; either version 2, or (at your option)
//any later version.

//Mauve is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with Mauve; see the file COPYING.  If not, write to
//the Free Software Foundation, 51 Franklin Street, Fifth Floor,
//Boston, MA, 02110-1301 USA.

package gnu.testlet.java.net.HttpURLConnection;

import gnu.testlet.TestHarness;
import gnu.testlet.Testlet;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;

public class responseHeadersTest implements Testlet
{

  public void test(TestHarness harness)
  {
    TestHttpServer server = null;
    try
      {
        server = new TestHttpServer(8080);
        Thread thread = new Thread(server);
        thread.start();
        try
          {
            // SUN JDK needs some time to open sockets
            Thread.sleep(100);
          }
        catch (InterruptedException e1)
          {
          }
        
        test_MultiHeaders(harness, server);
        test_LowerUpperCaseHeaders(harness, server);
      }
    finally
      {
        server.killTestServer();
      }   
  }
  
  public void test_MultiHeaders(TestHarness h, TestHttpServer server)
  {    
    try
      {        
        URL url = new URL("http://localhost:8080/");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");

        // construct what should be returned by the test server as headers
        ByteArrayOutputStream headers = new ByteArrayOutputStream();
        headers.write("HTTP/1.0 200 OK\r\n".getBytes());
        headers.write("Server: TestServer\r\n".getBytes());
        headers.write("Key1: value, value2\r\n".getBytes());
        // set the header a second time with different values
        // these values must be prepended to key1
        headers.write("Key1: value3\r\n".getBytes());
        headers.write("IntHeader: 1234\r\n".getBytes());
        headers.write("IntHeaderMalformed: 1234XY\r\n".getBytes());
        headers.write("DateHeader: Thu, 02 Mar 2006 14:34:55 +0000\r\n".getBytes());
        headers.write("DateHeaderMalformed: Thu, 02 Mar 2006V 14:13:07 +0000\r\n\r\n".getBytes());

        server.setResponseHeaders(headers.toByteArray());
        
        h.checkPoint("getHeaderFields()");
        Map fields = conn.getHeaderFields();
        
        // check that map is unmodifiable
        try 
          {
            fields.clear();
            h.check(false);
          }
        catch (UnsupportedOperationException e) 
          {
            h.check(true);
          }
      
        // exactly 7 headers with status and server header        
        h.check(fields.size() == 7);
        
        // check for list and that case matters for key
        Object obj = fields.get("Key1");
        if (! (obj instanceof List))
          h.check(false);
        else
          {
            h.check(true);
            List value = (List) obj;
            h.check(value.size() == 2);
            h.check(value.get(0).equals("value3"));
            h.check(value.get(1).equals("value, value2"));
            
            // check that it is an unmodifiable list
            try 
              {
                value.remove(0);
                h.check(false);
              }
            catch (UnsupportedOperationException e) 
              {
                h.check(true);
              }
          }
        
        // wrong case for key
        obj = fields.get("key1");
        h.check(obj == null);
          
        
        // checks for getHeaderField/Key(int)        
        h.checkPoint("getHeaderField(int)");
        // check that index 0 is the statusline
        String statusline = conn.getHeaderField(0);
        h.check(statusline.equals("HTTP/1.0 200 OK"));
        // indexes out of bound must return null
        String aboutIndex = conn.getHeaderField(44);
        h.check(aboutIndex == null);
        String belowIndex = conn.getHeaderField(-1);
        h.check(belowIndex == null);
        // check that correct key/value name is returned
        String key1_Value = conn.getHeaderField(2);
        h.check(key1_Value.equals("value, value2"));
        
        h.checkPoint("getHeaderFieldKey(int)");       
        // check that index 0 is the statusline
        String statuslineKey = conn.getHeaderFieldKey(0);        
        h.check(statuslineKey == null);
        // indexes out of bound must return null
        String aboutIndexKey = conn.getHeaderFieldKey(44);        
        h.check(aboutIndexKey == null);
        String belowIndexKey = conn.getHeaderFieldKey(-1);
        h.check(belowIndexKey == null);
        // check that correct key/value name is returned
        String key1_Key = conn.getHeaderFieldKey(2);
        h.check(key1_Key.equals("Key1"));
        
        
        // checks getHeaderFieldDate
        h.checkPoint("getHeaderFieldDate()");
        // correct date header field
        long dateHeader = conn.getHeaderFieldDate("DateHeader", 5555);
        h.check(dateHeader == 1141310095000L);
        // missing date header field
        dateHeader = conn.getHeaderFieldDate("DateHeaderXX", 5555);
        h.check(dateHeader == 5555);
        // malformed date header value 
        dateHeader = conn.getHeaderFieldDate("DateHeaderMalformed", 5555);
        h.check(dateHeader == 5555);
        
        // checks getHeaderFieldInt
        h.checkPoint("getHeaderFieldInt()");
        // correct int header field
        int intHeader = conn.getHeaderFieldInt("IntHeader", 5555);
        h.check(intHeader == 1234);
        // missing int header field
        intHeader = conn.getHeaderFieldInt("IntHeaderXX", 5555);
        h.check(intHeader == 5555);
        // malformed int header value 
        intHeader = conn.getHeaderFieldInt("IntHeaderMalformed", 5555);
        h.check(intHeader == 5555);
        
        
        // checks that the convenience methods of the headers
        // not set in this test return the correct default values
        h.checkPoint("convenience methods");
        h.check(conn.getLastModified() == 0);
        h.check(conn.getDate() == 0);
        h.check(conn.getExpiration() == 0);
        h.check(conn.getContentEncoding() == null);
        h.check(conn.getContentType() == null);
        h.check(conn.getContentLength() == -1);
        
        
        // checks getHeaderField(String)
        h.checkPoint("getHeaderField(String)");
        String field = conn.getHeaderField("Server");
        String field1 = conn.getHeaderField("server");
        h.check(field.equals("TestServer"));
        h.check(field == field1);
        
        String none = conn.getHeaderField("NotExistent");
        h.check(none == null);
        
        // check for multiple times same key
        String field_key1 = conn.getHeaderField("Key1");
        h.check(field_key1.equals("value3"));

      }
    catch (IOException e)
    {
      h.debug("Unexpected IOException");
      h.debug(e);
    }
    catch (RuntimeException e)
      {
        h.debug("Unexpected IOException");
        h.debug(e);
      }
    
  }
  
  public void test_LowerUpperCaseHeaders(TestHarness h, TestHttpServer server)
  {    
    try
      {        
        URL url = new URL("http://localhost:8080/");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");

        // construct what should be returned by the test server as headers
        ByteArrayOutputStream headers = new ByteArrayOutputStream();
        headers.write("HTTP/1.0 200 OK\r\n".getBytes());
        headers.write("Server: TestServer\r\n".getBytes());  
        headers.write("AnotherKey: value\r\n".getBytes());
        headers.write("Key: value\r\n".getBytes());
        headers.write("Key: value1\r\n".getBytes());
        headers.write("key: value2\r\n".getBytes());
        headers.write("key: value3\r\n\r\n".getBytes());
        
        server.setResponseHeaders(headers.toByteArray());
        
        h.checkPoint("LowerUpperCase header fields tests");
        
        Map fields = conn.getHeaderFields();  
                     
        // exactly 5 headers with status and server header        
        h.check(fields.size() == 5);
        
        // check for list and that case matters for key
        List value = (List) fields.get("Key");
        h.check(value.size() == 2);
        h.check(value.get(0).equals("value1"));
        
        List value2 = (List) fields.get("key");
        h.check(value2.size() == 2);
        h.check(value2.get(0).equals("value3"));
        
        List value3 = (List) fields.get("AnotherKey");
        h.check(value3.get(0).equals("value"));
        
        value3 = (List) fields.get("anotherkey");
        h.check(value3 == null);
                     
        // checks getHeaderField(String)
        String field = conn.getHeaderField("Key");
        String field1 = conn.getHeaderField("key");
        h.check(field.equals("value3"));
        h.check(field == field1);
      }
    catch (IOException e)
    {
      h.debug("Unexpected IOException");
      h.debug(e);
    }
    catch (RuntimeException e)
      {
        h.debug("Unexpected IOException");
        h.debug(e);
      }
  }
}

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2006-03-06 18:46 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-02-04 14:07 FYI: New tests for HttpURLConnection Wolfgang Baer
2006-02-04 14:16 ` Wolfgang Baer
2006-02-09  1:58 ` Tom Tromey
2006-02-09  7:48   ` Wolfgang Baer
2006-03-02 21:26     ` RFC: HTTPTestServer (was: Re: FYI: New tests for HttpURLConnection) Wolfgang Baer
2006-03-02 23:49       ` RFC: HTTPTestServer David Daney
2006-03-06 18:46         ` Wolfgang Baer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).