hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r160102 - jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/TestAllImpl.java jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/TestDefaultEntityGenerator.java
Date Mon, 04 Apr 2005 21:51:45 GMT
Author: olegk
Date: Mon Apr  4 14:51:44 2005
New Revision: 160102

URL: http://svn.apache.org/viewcvs?view=rev&rev=160102
Log:
Extra test coverage ('Transfer-Encoding' not covered yet)

Added:
    jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/TestDefaultEntityGenerator.java
  (with props)
Modified:
    jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/TestAllImpl.java

Modified: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/TestAllImpl.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/TestAllImpl.java?view=diff&r1=160101&r2=160102
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/TestAllImpl.java (original)
+++ jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/TestAllImpl.java Mon
Apr  4 14:51:44 2005
@@ -42,6 +42,7 @@
         suite.addTest(TestNIOHttpTransmitterAndReceiver.suite());
         suite.addTest(TestAutoCloseInputStream.suite());
         suite.addTest(TestDefaultConnectionReuseStrategy.suite());
+        suite.addTest(TestDefaultEntityGenerator.suite());
         return suite;
     }
 

Added: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/TestDefaultEntityGenerator.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/TestDefaultEntityGenerator.java?view=auto&rev=160102
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/TestDefaultEntityGenerator.java
(added)
+++ jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/TestDefaultEntityGenerator.java
Mon Apr  4 14:51:44 2005
@@ -0,0 +1,255 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ * ====================================================================
+ *
+ *  Copyright 2002-2004 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.impl;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+
+import org.apache.http.Header;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpMutableMessage;
+import org.apache.http.ProtocolException;
+import org.apache.http.io.ChunkedInputStream;
+import org.apache.http.io.ContentLengthInputStream;
+import org.apache.http.io.HttpDataInputStream;
+import org.apache.http.io.HttpDataReceiver;
+import org.apache.http.io.InputStreamHttpDataReceiver;
+import org.apache.http.mockup.HttpDataReceiverMockup;
+import org.apache.http.params.HttpParams;
+import org.apache.http.params.HttpProtocolParams;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+public class TestDefaultEntityGenerator extends TestCase {
+
+    public TestDefaultEntityGenerator(String testName) {
+        super(testName);
+    }
+
+    // ------------------------------------------------------- TestCase Methods
+
+    public static Test suite() {
+        return new TestSuite(TestDefaultEntityGenerator.class);
+    }
+
+    // ------------------------------------------------------------------- Main
+    public static void main(String args[]) {
+        String[] testCaseName = { TestDefaultEntityGenerator.class.getName() };
+        junit.textui.TestRunner.main(testCaseName);
+    }
+
+    public void testIllegalResponseArg() throws Exception {
+        EntityGenerator entitygen = new DefaultEntityGenerator();
+        try {
+            entitygen.generate(null, null);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+        try {
+            entitygen.generate(new HttpDataReceiverMockup(new byte[] {}) , null);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+    }
+
+    public void testEntityWithContentLength() throws Exception {
+        HttpDataReceiver datareceiver = new HttpDataReceiverMockup(new byte[] {});
+        HttpMutableMessage message = new BasicHttpMessage();
+        HttpParams params = new DefaultHttpParams(null);
+        message.setParams(params);
+        
+        // lenient mode 
+        params.setBooleanParameter(HttpProtocolParams.STRICT_TRANSFER_ENCODING, false);
+        message.addHeader(new Header("Content-Type", "unknown"));
+        message.addHeader(new Header("Content-Length", "0"));
+        EntityGenerator entitygen = new DefaultEntityGenerator();
+        HttpEntity entity = entitygen.generate(datareceiver, message);
+        assertNotNull(entity);
+        assertEquals(0, entity.getContentLength());
+        assertFalse(entity.isChunked());
+        assertTrue(entity.getInputStream() instanceof ContentLengthInputStream);
+    }
+
+    public void testEntityWithMultipleContentLength() throws Exception {
+        HttpDataReceiver datareceiver = new HttpDataReceiverMockup(new byte[] {'0'});
+        HttpMutableMessage message = new BasicHttpMessage();
+        HttpParams params = new DefaultHttpParams(null);
+        message.setParams(params);
+
+        // lenient mode 
+        params.setBooleanParameter(HttpProtocolParams.STRICT_TRANSFER_ENCODING, false);
+        message.addHeader(new Header("Content-Type", "unknown"));
+        message.addHeader(new Header("Content-Length", "0"));
+        message.addHeader(new Header("Content-Length", "0"));
+        message.addHeader(new Header("Content-Length", "1"));
+        EntityGenerator entitygen = new DefaultEntityGenerator();
+        HttpEntity entity = entitygen.generate(datareceiver, message);
+        assertNotNull(entity);
+        assertEquals(1, entity.getContentLength());
+        assertFalse(entity.isChunked());
+        assertNotNull(entity.getInputStream());
+        assertTrue(entity.getInputStream() instanceof ContentLengthInputStream);
+        
+        // strict mode 
+        params.setBooleanParameter(HttpProtocolParams.STRICT_TRANSFER_ENCODING, true);
+        try {
+            entitygen.generate(datareceiver, message);
+            fail("ProtocolException should have been thrown");
+        } catch (ProtocolException ex) {
+            // expected
+        }
+    }
+    
+    public void testEntityWithMultipleContentLengthSomeWrong() throws Exception {
+        HttpDataReceiver datareceiver = new HttpDataReceiverMockup(new byte[] {'0'});
+        HttpMutableMessage message = new BasicHttpMessage();
+        HttpParams params = new DefaultHttpParams(null);
+        message.setParams(params);
+
+        // lenient mode 
+        params.setBooleanParameter(HttpProtocolParams.STRICT_TRANSFER_ENCODING, false);
+        message.addHeader(new Header("Content-Type", "unknown"));
+        message.addHeader(new Header("Content-Length", "1"));
+        message.addHeader(new Header("Content-Length", "yyy"));
+        message.addHeader(new Header("Content-Length", "xxx"));
+        EntityGenerator entitygen = new DefaultEntityGenerator();
+        HttpEntity entity = entitygen.generate(datareceiver, message);
+        assertNotNull(entity);
+        assertEquals(1, entity.getContentLength());
+        assertFalse(entity.isChunked());
+        assertNotNull(entity.getInputStream());
+        assertTrue(entity.getInputStream() instanceof ContentLengthInputStream);
+        
+        // strict mode 
+        params.setBooleanParameter(HttpProtocolParams.STRICT_TRANSFER_ENCODING, true);
+        try {
+            entitygen.generate(datareceiver, message);
+            fail("ProtocolException should have been thrown");
+        } catch (ProtocolException ex) {
+            // expected
+        }
+    }
+    
+    public void testEntityWithMultipleContentLengthAllWrong() throws Exception {
+        HttpDataReceiver datareceiver = new HttpDataReceiverMockup(new byte[] {'0'});
+        HttpMutableMessage message = new BasicHttpMessage();
+        HttpParams params = new DefaultHttpParams(null);
+        message.setParams(params);
+
+        // lenient mode 
+        params.setBooleanParameter(HttpProtocolParams.STRICT_TRANSFER_ENCODING, false);
+        message.addHeader(new Header("Content-Type", "unknown"));
+        message.addHeader(new Header("Content-Length", "yyy"));
+        message.addHeader(new Header("Content-Length", "xxx"));
+        EntityGenerator entitygen = new DefaultEntityGenerator();
+        HttpEntity entity = entitygen.generate(datareceiver, message);
+        assertNotNull(entity);
+        assertEquals(-1, entity.getContentLength());
+        assertFalse(entity.isChunked());
+        assertNotNull(entity.getInputStream());
+        assertFalse(entity.getInputStream() instanceof ContentLengthInputStream);
+        assertTrue(entity.getInputStream() instanceof HttpDataInputStream);
+        
+        // strict mode 
+        params.setBooleanParameter(HttpProtocolParams.STRICT_TRANSFER_ENCODING, true);
+        try {
+            entitygen.generate(datareceiver, message);
+            fail("ProtocolException should have been thrown");
+        } catch (ProtocolException ex) {
+            // expected
+        }
+    }
+
+    public void testEntityWithInvalidContentLength() throws Exception {
+        HttpDataReceiver datareceiver = new HttpDataReceiverMockup(new byte[] {'0'});
+        HttpMutableMessage message = new BasicHttpMessage();
+        HttpParams params = new DefaultHttpParams(null);
+        message.setParams(params);
+
+        // lenient mode 
+        params.setBooleanParameter(HttpProtocolParams.STRICT_TRANSFER_ENCODING, false);
+        message.addHeader(new Header("Content-Type", "unknown"));
+        message.addHeader(new Header("Content-Length", "xxx"));
+        EntityGenerator entitygen = new DefaultEntityGenerator();
+        HttpEntity entity = entitygen.generate(datareceiver, message);
+        assertNotNull(entity);
+        assertEquals(-1, entity.getContentLength());
+        assertFalse(entity.isChunked());
+        assertNotNull(entity.getInputStream());
+        assertFalse(entity.getInputStream() instanceof ContentLengthInputStream);
+        assertTrue(entity.getInputStream() instanceof HttpDataInputStream);
+        
+        // strict mode 
+        params.setBooleanParameter(HttpProtocolParams.STRICT_TRANSFER_ENCODING, true);
+        try {
+            entitygen.generate(datareceiver, message);
+            fail("ProtocolException should have been thrown");
+        } catch (ProtocolException ex) {
+            // expected
+        }
+    }
+
+    public void testEntityNeitherContentLengthNorTransferEncoding() throws Exception {
+        HttpDataReceiver datareceiver = new HttpDataReceiverMockup(new byte[] {'0'});
+        HttpMutableMessage message = new BasicHttpMessage();
+        HttpParams params = new DefaultHttpParams(null);
+        message.setParams(params);
+
+        // lenient mode 
+        params.setBooleanParameter(HttpProtocolParams.STRICT_TRANSFER_ENCODING, false);
+        EntityGenerator entitygen = new DefaultEntityGenerator();
+        HttpEntity entity = entitygen.generate(datareceiver, message);
+        assertNotNull(entity);
+        assertEquals(-1, entity.getContentLength());
+        assertFalse(entity.isChunked());
+        assertNotNull(entity.getInputStream());
+        assertFalse(entity.getInputStream() instanceof ContentLengthInputStream);
+        assertFalse(entity.getInputStream() instanceof ChunkedInputStream);
+        assertTrue(entity.getInputStream() instanceof HttpDataInputStream);
+    }
+
+    public void testOldIOWrapper() throws Exception {
+        InputStream instream = new ByteArrayInputStream(new byte[] {});
+        HttpDataReceiver datareceiver = new InputStreamHttpDataReceiver(instream);
+        HttpMutableMessage message = new BasicHttpMessage();
+        message.addHeader(new Header("Content-Type", "unknown"));
+        EntityGenerator entitygen = new DefaultEntityGenerator();
+        HttpEntity entity = entitygen.generate(datareceiver, message);
+        assertNotNull(entity);
+        assertEquals(-1, entity.getContentLength());
+        assertFalse(entity.isChunked());
+        assertTrue(entity.getInputStream() instanceof ByteArrayInputStream);
+    }
+
+}
+

Propchange: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/TestDefaultEntityGenerator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/TestDefaultEntityGenerator.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/impl/TestDefaultEntityGenerator.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message