1 30 31 package org.apache.commons.httpclient; 32 33 import java.net.InetAddress ; 34 35 import junit.framework.Test; 36 import junit.framework.TestSuite; 37 38 import org.apache.commons.httpclient.methods.GetMethod; 39 40 59 public class TestWebappHeaders extends TestWebappBase { 60 61 public TestWebappHeaders(String testName) { 62 super(testName); 63 } 64 65 public static Test suite() { 66 TestSuite suite = new TestSuite(TestWebappHeaders.class); 67 return suite; 68 } 69 70 public static void main(String args[]) { 71 String [] testCaseName = { TestWebappHeaders.class.getName() }; 72 junit.textui.TestRunner.main(testCaseName); 73 } 74 75 77 80 public void testAddRequestHeader() throws Exception { 81 HttpClient client = createHttpClient(); 82 GetMethod method = new GetMethod("/" + getWebappContext() + "/headers"); 83 method.setRequestHeader(new Header("addRequestHeader(Header)","True")); 84 method.setRequestHeader("addRequestHeader(String,String)","Also True"); 85 86 try { 87 client.executeMethod(method); 88 } catch (Throwable t) { 89 t.printStackTrace(); 90 fail("Unable to execute method : " + t.toString()); 91 } 92 assertTrue(method.getResponseBodyAsString().indexOf("name=\"addrequestheader(header)\";value=\"True\"<br>") >= 0); 94 assertTrue(method.getResponseBodyAsString().indexOf("name=\"addrequestheader(string,string)\";value=\"Also True\"<br>") >= 0); 95 } 96 97 100 public void testRemoveRequestHeader() throws Exception { 101 HttpClient client = createHttpClient(); 102 GetMethod method = new GetMethod("/" + getWebappContext() + "/headers"); 103 method.setRequestHeader(new Header("XXX-A-HEADER","true")); 104 method.removeRequestHeader("XXX-A-HEADER"); 105 106 try { 107 client.executeMethod(method); 108 } catch (Throwable t) { 109 t.printStackTrace(); 110 fail("Unable to execute method : " + t.toString()); 111 } 112 assertTrue(!(method.getResponseBodyAsString().indexOf("xxx-a-header") >= 0)); 114 } 115 116 119 public void testOverwriteRequestHeader() throws Exception { 120 HttpClient client = createHttpClient(); 121 GetMethod method = new GetMethod("/" + getWebappContext() + "/headers"); 122 method.setRequestHeader(new Header("xxx-a-header","one")); 123 method.setRequestHeader("XXX-A-HEADER","two"); 124 125 try { 126 client.executeMethod(method); 127 } catch (Throwable t) { 128 t.printStackTrace(); 129 fail("Unable to execute method : " + t.toString()); 130 } 131 assertTrue(method.getResponseBodyAsString().indexOf("name=\"xxx-a-header\";value=\"two\"<br>") >= 0); 133 } 134 135 138 public void testGetResponseHeader() throws Exception { 139 HttpClient client = createHttpClient(); 140 GetMethod method = new GetMethod("/" + getWebappContext() + "/headers"); 141 142 try { 143 client.executeMethod(method); 144 } catch (Throwable t) { 145 t.printStackTrace(); 146 fail("Unable to execute method : " + t.toString()); 147 } 148 Header h = new Header("HeaderSetByServlet","Yes"); 149 assertEquals(h,method.getResponseHeader("headersetbyservlet")); 150 } 151 152 155 public void testHostRequestHeader() throws Exception { 156 InetAddress addr = InetAddress.getByName(getHost()); 157 String ip = addr.getHostAddress(); 158 String hostname = addr.getHostName(); 159 160 HttpClient client = new HttpClient(); 161 GetMethod get = new GetMethod("/" + getWebappContext()); 162 163 client.getHostConfiguration().setHost(ip, getPort(), getProtocol()); 168 try { 169 client.executeMethod(get); 170 } catch (Throwable t) { 171 t.printStackTrace(); 172 fail("Unable to execute method : " + t.toString()); 173 } 174 Header hostHeader = get.getRequestHeader("Host"); 175 assertTrue(hostHeader != null); 176 177 get.recycle(); 179 get.setPath("/" + getWebappContext()); 180 181 client.getHostConfiguration().setHost(hostname, getPort(), getProtocol()); 187 try { 188 client.executeMethod(get); 189 } catch (Throwable t) { 190 t.printStackTrace(); 191 fail("Unable to execute method : " + t.toString()); 192 } 193 hostHeader = get.getRequestHeader("Host"); 194 assertTrue(hostHeader != null); 195 if (getPort() == 80) { 196 assertTrue(hostHeader.getValue().equals(hostname)); 198 } else { 199 assertTrue(hostHeader.getValue().equals(hostname + ":" + getPort())); 200 } 201 } 202 203 } 204 205 | Popular Tags |