1 30 31 package org.apache.commons.httpclient; 32 33 import junit.framework.*; 34 35 41 public class TestStatusLine extends TestCase { 42 43 private StatusLine statusLine = null; 44 45 public TestStatusLine(String testName) { 47 super(testName); 48 } 49 50 public static void main(String args[]) { 52 String [] testCaseName = { TestStatusLine.class.getName() }; 53 junit.textui.TestRunner.main(testCaseName); 54 } 55 56 58 public static Test suite() { 59 return new TestSuite(TestStatusLine.class); 60 } 61 62 64 65 67 public void testIfStatusLine() throws Exception { 68 assertTrue(StatusLine.startsWithHTTP("HTTP")); 69 assertTrue(StatusLine.startsWithHTTP(" HTTP")); 70 assertTrue(StatusLine.startsWithHTTP("\rHTTP")); 71 assertTrue(StatusLine.startsWithHTTP("\tHTTP")); 72 assertFalse(StatusLine.startsWithHTTP("crap")); 73 assertFalse(StatusLine.startsWithHTTP("HTT")); 74 assertFalse(StatusLine.startsWithHTTP("http")); 75 } 76 77 public void testSuccess() throws Exception { 78 statusLine = new StatusLine("HTTP/1.1 200 OK"); 80 assertEquals("HTTP/1.1 200 OK", statusLine.toString()); 81 assertEquals("HTTP/1.1", statusLine.getHttpVersion()); 82 assertEquals(200, statusLine.getStatusCode()); 83 assertEquals("OK", statusLine.getReasonPhrase()); 84 85 statusLine = new StatusLine("HTTP/1.1 404 Not Found"); 87 assertEquals(404, statusLine.getStatusCode()); 88 assertEquals("Not Found", statusLine.getReasonPhrase()); 89 90 statusLine = new StatusLine("HTTP/1.1 404 Non Trouve"); 92 assertEquals("Non Trouve", statusLine.getReasonPhrase()); 93 94 statusLine = new StatusLine("HTTP/1.1 404 Not Found\r\n"); 96 assertEquals("Not Found", statusLine.getReasonPhrase()); 97 98 statusLine = new StatusLine("HTTP/1.1 200 "); 100 assertEquals(200, statusLine.getStatusCode()); 101 assertEquals("", statusLine.getReasonPhrase()); 102 103 statusLine = new StatusLine("HTTP/1.1 200"); 105 assertEquals(200, statusLine.getStatusCode()); 106 assertEquals("", statusLine.getReasonPhrase()); 107 108 statusLine = new StatusLine("HTTP/1.1 200 OK"); 110 assertEquals(200, statusLine.getStatusCode()); 111 assertEquals("OK", statusLine.getReasonPhrase()); 112 113 statusLine = new StatusLine("\rHTTP/1.1 200 OK"); 115 assertEquals(200, statusLine.getStatusCode()); 116 assertEquals("OK", statusLine.getReasonPhrase()); 117 assertEquals("HTTP/1.1", statusLine.getHttpVersion()); 118 119 statusLine = new StatusLine(" HTTP/1.1 200 OK"); 121 assertEquals(200, statusLine.getStatusCode()); 122 assertEquals("OK", statusLine.getReasonPhrase()); 123 assertEquals("HTTP/1.1", statusLine.getHttpVersion()); 124 } 125 126 public void testFailure() throws Exception { 127 try { 128 statusLine = new StatusLine(null); 129 fail(); 130 } catch (NullPointerException e) { } 131 132 try { 133 statusLine = new StatusLine("xxx 200 OK"); 134 fail(); 135 } catch (HttpException e) { } 136 137 try { 138 statusLine = new StatusLine("HTTP/1.1 xxx OK"); 139 fail(); 140 } catch (HttpException e) { } 141 142 try { 143 statusLine = new StatusLine("HTTP/1.1 "); 144 fail(); 145 } catch (HttpException e) { } 146 } 147 148 } 149 | Popular Tags |