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