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