1 31 32 package org.apache.commons.httpclient; 33 34 import java.io.ByteArrayInputStream ; 35 import java.io.InputStream ; 36 37 import junit.framework.*; 38 39 45 public class TestHttpParser extends TestCase { 46 47 private static final String HTTP_ELEMENT_CHARSET = "US-ASCII"; 48 49 public TestHttpParser(String testName) { 51 super(testName); 52 } 53 54 public static void main(String args[]) { 56 String [] testCaseName = { TestHeader.class.getName() }; 57 junit.textui.TestRunner.main(testCaseName); 58 } 59 60 62 public static Test suite() { 63 return new TestSuite(TestHttpParser.class); 64 } 65 66 public void testReadHttpLine() throws Exception { 67 InputStream instream = new ByteArrayInputStream ( 68 "\r\r\nstuff\r\n".getBytes(HTTP_ELEMENT_CHARSET)); 69 assertEquals("\r", HttpParser.readLine(instream, HTTP_ELEMENT_CHARSET)); 70 assertEquals("stuff", HttpParser.readLine(instream, HTTP_ELEMENT_CHARSET)); 71 assertEquals(null, HttpParser.readLine(instream, HTTP_ELEMENT_CHARSET)); 72 73 instream = new ByteArrayInputStream ( 74 "\n\r\nstuff\r\n".getBytes("US-ASCII")); 75 assertEquals("", HttpParser.readLine(instream, HTTP_ELEMENT_CHARSET)); 76 assertEquals("", HttpParser.readLine(instream, HTTP_ELEMENT_CHARSET)); 77 assertEquals("stuff", HttpParser.readLine(instream, HTTP_ELEMENT_CHARSET)); 78 assertEquals(null, HttpParser.readLine(instream, HTTP_ELEMENT_CHARSET)); 79 } 80 81 public void testReadWellFormedHttpHeaders() throws Exception { 82 InputStream instream = new ByteArrayInputStream ( 83 "a: a\r\nb: b\r\n\r\nwhatever".getBytes(HTTP_ELEMENT_CHARSET)); 84 Header[] headers = HttpParser.parseHeaders(instream, HTTP_ELEMENT_CHARSET); 85 assertNotNull(headers); 86 assertEquals(2, headers.length); 87 assertEquals("a", headers[0].getName()); 88 assertEquals("a", headers[0].getValue()); 89 assertEquals("b", headers[1].getName()); 90 assertEquals("b", headers[1].getValue()); 91 } 92 93 public void testReadMalformedHttpHeaders() throws Exception { 94 InputStream instream = new ByteArrayInputStream ( 95 "a: a\r\nb b\r\n\r\nwhatever".getBytes(HTTP_ELEMENT_CHARSET)); 96 try { 97 Header[] headers = HttpParser.parseHeaders(instream, HTTP_ELEMENT_CHARSET); 98 fail("HttpException should have been thrown"); 99 } catch (HttpException expected) { 100 } 101 } 102 103 public void testHeadersTerminatorLeniency1() throws Exception { 104 InputStream instream = new ByteArrayInputStream ( 105 "a: a\r\nb: b\r\n\r\r\nwhatever".getBytes(HTTP_ELEMENT_CHARSET)); 106 Header[] headers = HttpParser.parseHeaders(instream, HTTP_ELEMENT_CHARSET); 107 assertNotNull(headers); 108 assertEquals(2, headers.length); 109 assertEquals("a", headers[0].getName()); 110 assertEquals("a", headers[0].getValue()); 111 assertEquals("b", headers[1].getName()); 112 assertEquals("b", headers[1].getValue()); 113 } 114 115 public void testHeadersTerminatorLeniency2() throws Exception { 116 InputStream instream = new ByteArrayInputStream ( 117 "a: a\r\nb: b\r\n \r\nwhatever".getBytes(HTTP_ELEMENT_CHARSET)); 118 Header[] headers = HttpParser.parseHeaders(instream, HTTP_ELEMENT_CHARSET); 119 assertNotNull(headers); 120 assertEquals(2, headers.length); 121 assertEquals("a", headers[0].getName()); 122 assertEquals("a", headers[0].getValue()); 123 assertEquals("b", headers[1].getName()); 124 assertEquals("b", headers[1].getValue()); 125 } 126 } 127 | Popular Tags |