1 29 30 package org.apache.commons.httpclient.params; 31 32 import java.io.IOException ; 33 import java.util.ArrayList ; 34 35 import junit.framework.Test; 36 import junit.framework.TestSuite; 37 38 import org.apache.commons.httpclient.Header; 39 import org.apache.commons.httpclient.HostConfiguration; 40 import org.apache.commons.httpclient.HttpClientTestBase; 41 import org.apache.commons.httpclient.HttpStatus; 42 import org.apache.commons.httpclient.HttpVersion; 43 import org.apache.commons.httpclient.methods.GetMethod; 44 import org.apache.commons.httpclient.params.HostParams; 45 import org.apache.commons.httpclient.protocol.Protocol; 46 import org.apache.commons.httpclient.server.HttpService; 47 import org.apache.commons.httpclient.server.SimpleRequest; 48 import org.apache.commons.httpclient.server.SimpleResponse; 49 50 57 public class TestHttpParams extends HttpClientTestBase { 58 59 public TestHttpParams(final String testName) throws IOException { 61 super(testName); 62 } 63 64 public static void main(String args[]) { 66 String [] testCaseName = { TestHttpParams.class.getName() }; 67 junit.textui.TestRunner.main(testCaseName); 68 } 69 70 72 public static Test suite() { 73 return new TestSuite(TestHttpParams.class); 74 } 75 76 private class SimpleService implements HttpService { 77 78 public SimpleService() { 79 super(); 80 } 81 82 public boolean process(final SimpleRequest request, final SimpleResponse response) 83 throws IOException 84 { 85 String uri = request.getRequestLine().getUri(); 86 HttpVersion httpversion = request.getRequestLine().getHttpVersion(); 87 88 if ("/miss/".equals(uri)) { 89 response.setStatusLine(httpversion, HttpStatus.SC_MOVED_TEMPORARILY); 90 response.addHeader(new Header("Location", "/hit/")); 91 response.setBodyString("Missed!"); 92 } else if ("/hit/".equals(uri)) { 93 response.setStatusLine(httpversion, HttpStatus.SC_OK); 94 response.setBodyString("Hit!"); 95 } else { 96 response.setStatusLine(httpversion, HttpStatus.SC_NOT_FOUND); 97 response.setBodyString(uri + " not found"); 98 } 99 return true; 100 } 101 } 102 103 public void testDefaultHeaders() throws IOException { 104 this.server.setHttpService(new SimpleService()); 105 106 ArrayList defaults = new ArrayList (); 107 defaults.add(new Header("this-header", "value1")); 108 defaults.add(new Header("that-header", "value1")); 109 defaults.add(new Header("that-header", "value2")); 110 defaults.add(new Header("User-Agent", "test")); 111 112 HostConfiguration hostconfig = new HostConfiguration(); 113 hostconfig.setHost( 114 this.server.getLocalAddress(), 115 this.server.getLocalPort(), 116 Protocol.getProtocol("http")); 117 hostconfig.getParams().setParameter(HostParams.DEFAULT_HEADERS, defaults); 118 119 GetMethod httpget = new GetMethod("/miss/"); 120 try { 121 this.client.executeMethod(hostconfig, httpget); 122 } finally { 123 httpget.releaseConnection(); 124 } 125 assertEquals(HttpStatus.SC_OK, httpget.getStatusCode()); 126 Header[] thisheader = httpget.getRequestHeaders("this-header"); 127 assertEquals(1, thisheader.length); 128 Header[] thatheader = httpget.getRequestHeaders("that-header"); 129 assertEquals(2, thatheader.length); 130 assertEquals("test", httpget.getRequestHeader("User-Agent").getValue()); 131 } 132 133 public void testDefaults() throws IOException { 134 this.server.setHttpService(new SimpleService()); 135 136 this.client.getParams().setParameter(HttpMethodParams.USER_AGENT, "test"); 137 HostConfiguration hostconfig = new HostConfiguration(); 138 hostconfig.setHost( 139 this.server.getLocalAddress(), 140 this.server.getLocalPort(), 141 Protocol.getProtocol("http")); 142 143 GetMethod httpget = new GetMethod("/miss/"); 144 try { 145 this.client.executeMethod(hostconfig, httpget); 146 } finally { 147 httpget.releaseConnection(); 148 } 149 assertEquals(HttpStatus.SC_OK, httpget.getStatusCode()); 150 assertEquals("test", httpget.getRequestHeader("User-Agent").getValue()); 151 assertEquals("test", httpget.getParams(). 152 getParameter(HttpMethodParams.USER_AGENT)); 153 assertEquals("test", hostconfig.getParams(). 154 getParameter(HttpMethodParams.USER_AGENT)); 155 assertEquals("test", client.getParams(). 156 getParameter(HttpMethodParams.USER_AGENT)); 157 } 158 } 159 | Popular Tags |