001: //========================================================================
002: //$Id: HttpGeneratorTest.java,v 1.1 2005/10/05 14:09:41 janb Exp $
003: //Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
004: //------------------------------------------------------------------------
005: //Licensed under the Apache License, Version 2.0 (the "License");
006: //you may not use this file except in compliance with the License.
007: //You may obtain a copy of the License at
008: //http://www.apache.org/licenses/LICENSE-2.0
009: //Unless required by applicable law or agreed to in writing, software
010: //distributed under the License is distributed on an "AS IS" BASIS,
011: //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: //See the License for the specific language governing permissions and
013: //limitations under the License.
014: //========================================================================
015:
016: package org.mortbay.jetty;
017:
018: import java.util.Enumeration;
019: import java.util.Map;
020:
021: import javax.servlet.ServletContext;
022: import javax.servlet.http.HttpSessionContext;
023:
024: import org.mortbay.jetty.handler.ContextHandler;
025: import org.mortbay.jetty.servlet.AbstractSessionManager;
026: import org.mortbay.jetty.servlet.HashSessionIdManager;
027: import org.mortbay.jetty.servlet.HashSessionManager;
028: import java.util.Locale;
029:
030: import junit.framework.TestCase;
031:
032: /**
033: * @author gregw
034: *
035: * To change the template for this generated type comment go to
036: * Window - Preferences - Java - Code Generation - Code and Comments
037: */
038: public class ResponseTest extends TestCase {
039: Server server = new Server();
040: LocalConnector connector = new LocalConnector();
041:
042: public ResponseTest(String arg0) {
043: super (arg0);
044: server.setConnectors(new Connector[] { connector });
045: server.setHandler(new DumpHandler());
046: }
047:
048: public static void main(String[] args) {
049: junit.textui.TestRunner.run(ResponseTest.class);
050: }
051:
052: /*
053: * @see TestCase#setUp()
054: */
055: protected void setUp() throws Exception {
056: super .setUp();
057:
058: server.start();
059: }
060:
061: /*
062: * @see TestCase#tearDown()
063: */
064: protected void tearDown() throws Exception {
065: super .tearDown();
066: server.stop();
067: }
068:
069: public void testContentType() throws Exception {
070:
071: HttpConnection connection = new HttpConnection(connector,
072: connector.endp, connector.server);
073: Response response = connection.getResponse();
074:
075: assertEquals(null, response.getContentType());
076:
077: response.setContentType("foo/bar");
078: assertEquals("foo/bar", response.getContentType());
079: response.getWriter();
080: assertEquals("foo/bar; charset=ISO-8859-1", response
081: .getContentType());
082: response.setContentType("foo2/bar2");
083: assertEquals("foo2/bar2; charset=ISO-8859-1", response
084: .getContentType());
085: response.setHeader("name", "foo");
086: Enumeration en = response.getHeaders("name");
087: assertEquals("foo", en.nextElement());
088: assertFalse(en.hasMoreElements());
089: response.addHeader("name", "bar");
090: en = response.getHeaders("name");
091: assertEquals("foo", en.nextElement());
092: assertEquals("bar", en.nextElement());
093: assertFalse(en.hasMoreElements());
094:
095: response.recycle();
096:
097: response.setContentType("text/html");
098: assertEquals("text/html", response.getContentType());
099: response.getWriter();
100: assertEquals("text/html; charset=iso-8859-1", response
101: .getContentType());
102: response.setContentType("foo2/bar2");
103: assertEquals("foo2/bar2; charset=ISO-8859-1", response
104: .getContentType());
105:
106: response.recycle();
107: }
108:
109: public void testLocale() throws Exception {
110:
111: HttpConnection connection = new HttpConnection(connector,
112: connector.endp, connector.server);
113: Request request = connection.getRequest();
114: Response response = connection.getResponse();
115: ContextHandler context = new ContextHandler();
116: context.addLocaleEncoding(Locale.ENGLISH.toString(),
117: "ISO-8859-1");
118: context.addLocaleEncoding(Locale.ITALIAN.toString(),
119: "ISO-8859-2");
120: request.setContext(context.getServletContext());
121:
122: response.setLocale(java.util.Locale.ITALIAN);
123: assertEquals(null, response.getContentType());
124: response.setContentType("text/plain");
125: assertEquals("text/plain; charset=ISO-8859-2", response
126: .getContentType());
127:
128: response.recycle();
129: response.setContentType("text/plain");
130: response.setCharacterEncoding("utf-8");
131: response.setLocale(java.util.Locale.ITALIAN);
132: assertEquals("text/plain; charset=utf-8", response
133: .getContentType());
134: assertTrue(response.toString().indexOf("charset=utf-8") > 0);
135: }
136:
137: public void testContentTypeCharacterEncoding() throws Exception {
138: HttpConnection connection = new HttpConnection(connector,
139: connector.endp, connector.server);
140:
141: Request request = connection.getRequest();
142: Response response = connection.getResponse();
143:
144: response.setContentType("foo/bar");
145: response.setCharacterEncoding("utf-8");
146: assertEquals("foo/bar; charset=utf-8", response
147: .getContentType());
148: response.getWriter();
149: assertEquals("foo/bar; charset=utf-8", response
150: .getContentType());
151: response.setContentType("foo2/bar2");
152: assertEquals("foo2/bar2; charset=utf-8", response
153: .getContentType());
154: response.setCharacterEncoding("ISO-8859-1");
155: assertEquals("foo2/bar2; charset=utf-8", response
156: .getContentType());
157:
158: response.recycle();
159:
160: response.setContentType("text/html");
161: response.setCharacterEncoding("utf-8");
162: assertEquals("text/html; charset=utf-8", response
163: .getContentType());
164: response.getWriter();
165: assertEquals("text/html; charset=utf-8", response
166: .getContentType());
167: response.setContentType("text/xml");
168: assertEquals("text/xml; charset=utf-8", response
169: .getContentType());
170: response.setCharacterEncoding("ISO-8859-1");
171: assertEquals("text/xml; charset=utf-8", response
172: .getContentType());
173:
174: }
175:
176: public void testCharacterEncodingContentType() throws Exception {
177: Response response = new Response(new HttpConnection(connector,
178: connector.endp, connector.server));
179:
180: response.setCharacterEncoding("utf-8");
181: response.setContentType("foo/bar");
182: assertEquals("foo/bar; charset=utf-8", response
183: .getContentType());
184: response.getWriter();
185: assertEquals("foo/bar; charset=utf-8", response
186: .getContentType());
187: response.setContentType("foo2/bar2");
188: assertEquals("foo2/bar2; charset=utf-8", response
189: .getContentType());
190: response.setCharacterEncoding("ISO-8859-1");
191: assertEquals("foo2/bar2; charset=utf-8", response
192: .getContentType());
193:
194: response.recycle();
195:
196: response.setCharacterEncoding("utf-8");
197: response.setContentType("text/html");
198: assertEquals("text/html; charset=utf-8", response
199: .getContentType());
200: response.getWriter();
201: assertEquals("text/html; charset=utf-8", response
202: .getContentType());
203: response.setContentType("text/xml");
204: assertEquals("text/xml; charset=utf-8", response
205: .getContentType());
206: response.setCharacterEncoding("iso-8859-1");
207: assertEquals("text/xml; charset=utf-8", response
208: .getContentType());
209:
210: }
211:
212: public void testContentTypeWithCharacterEncoding() throws Exception {
213: Response response = new Response(new HttpConnection(connector,
214: connector.endp, connector.server));
215:
216: response.setCharacterEncoding("utf16");
217: response.setContentType("foo/bar; charset=utf-8");
218: assertEquals("foo/bar; charset=utf-8", response
219: .getContentType());
220: response.getWriter();
221: assertEquals("foo/bar; charset=utf-8", response
222: .getContentType());
223: response.setContentType("foo2/bar2");
224: assertEquals("foo2/bar2; charset=utf-8", response
225: .getContentType());
226: response.setCharacterEncoding("ISO-8859-1");
227: assertEquals("foo2/bar2; charset=utf-8", response
228: .getContentType());
229:
230: response.recycle();
231:
232: response.setCharacterEncoding("utf16");
233: response.setContentType("text/html; charset=utf-8");
234: assertEquals("text/html; charset=utf-8", response
235: .getContentType());
236: response.getWriter();
237: assertEquals("text/html; charset=utf-8", response
238: .getContentType());
239: response.setContentType("text/xml");
240: assertEquals("text/xml; charset=utf-8", response
241: .getContentType());
242: response.setCharacterEncoding("iso-8859-1");
243: assertEquals("text/xml; charset=utf-8", response
244: .getContentType());
245:
246: }
247:
248: public void testContentTypeWithOther() throws Exception {
249: Response response = new Response(new HttpConnection(connector,
250: connector.endp, connector.server));
251:
252: response.setContentType("foo/bar; other=xyz");
253: assertEquals("foo/bar; other=xyz", response.getContentType());
254: response.getWriter();
255: assertEquals("foo/bar; other=xyz charset=ISO-8859-1", response
256: .getContentType());
257: response.setContentType("foo2/bar2");
258: assertEquals("foo2/bar2; charset=ISO-8859-1", response
259: .getContentType());
260:
261: response.recycle();
262:
263: response.setCharacterEncoding("utf-8");
264: response.setContentType("text/html; other=xyz");
265: assertEquals("text/html; other=xyz charset=utf-8", response
266: .getContentType());
267: response.getWriter();
268: assertEquals("text/html; other=xyz charset=utf-8", response
269: .getContentType());
270: response.setContentType("text/xml");
271: assertEquals("text/xml; charset=utf-8", response
272: .getContentType());
273: }
274:
275: public void testContentTypeWithCharacterEncodingAndOther()
276: throws Exception {
277: Response response = new Response(new HttpConnection(connector,
278: connector.endp, connector.server));
279:
280: response.setCharacterEncoding("utf16");
281: response.setContentType("foo/bar; charset=utf-8 other=xyz");
282: assertEquals("foo/bar; charset=utf-8 other=xyz", response
283: .getContentType());
284: response.getWriter();
285: assertEquals("foo/bar; charset=utf-8 other=xyz", response
286: .getContentType());
287:
288: response.recycle();
289:
290: response.setCharacterEncoding("utf16");
291: response.setContentType("text/html; other=xyz charset=utf-8");
292: assertEquals("text/html; other=xyz charset=utf-8", response
293: .getContentType());
294: response.getWriter();
295: assertEquals("text/html; other=xyz charset=utf-8", response
296: .getContentType());
297:
298: response.recycle();
299:
300: response.setCharacterEncoding("utf16");
301: response
302: .setContentType("foo/bar; other=pq charset=utf-8 other=xyz");
303: assertEquals("foo/bar; other=pq charset=utf-8 other=xyz",
304: response.getContentType());
305: response.getWriter();
306: assertEquals("foo/bar; other=pq charset=utf-8 other=xyz",
307: response.getContentType());
308:
309: }
310:
311: public void testStatusCodes() throws Exception {
312: Response response = newResponse();
313:
314: response.sendError(404);
315: assertEquals(404, response.getStatus());
316: assertEquals(null, response.getReason());
317:
318: response = newResponse();
319:
320: response.sendError(500, "Database Error");
321: assertEquals(500, response.getStatus());
322: assertEquals("Database Error", response.getReason());
323:
324: response = newResponse();
325:
326: response.setStatus(200);
327: assertEquals(200, response.getStatus());
328: assertEquals(null, response.getReason());
329:
330: response = newResponse();
331:
332: response.sendError(406, "Super Nanny");
333: assertEquals(406, response.getStatus());
334: assertEquals("Super Nanny", response.getReason());
335: }
336:
337: public void testEncodeRedirect() throws Exception {
338: HttpConnection connection = new HttpConnection(connector,
339: connector.endp, connector.server);
340: Response response = new Response(connection);
341: Request request = connection.getRequest();
342:
343: assertEquals(
344: "http://host:port/path/info;param?query=0&more=1#target",
345: response
346: .encodeRedirectUrl("http://host:port/path/info;param?query=0&more=1#target"));
347:
348: request.setRequestedSessionId("12345");
349: request.setRequestedSessionIdFromCookie(false);
350: AbstractSessionManager manager = new HashSessionManager();
351: manager.setIdManager(new HashSessionIdManager());
352: request.setSessionManager(manager);
353: request.setSession(new TestSession(manager, "12345"));
354:
355: assertEquals(
356: "http://host:port/path/info;param;jsessionid=12345?query=0&more=1#target",
357: response
358: .encodeRedirectUrl("http://host:port/path/info;param?query=0&more=1#target"));
359:
360: }
361:
362: public void testSetBufferSize() throws Exception {
363: Response response = new Response(new HttpConnection(connector,
364: connector.endp, connector.server));
365: response.setBufferSize(20 * 1024);
366: response.getWriter().print("hello");
367: try {
368: response.setBufferSize(21 * 1024);
369: fail("Expected IllegalStateException on Request.setBufferSize");
370: } catch (Exception e) {
371: assertTrue(e instanceof IllegalStateException);
372: }
373: }
374:
375: private Response newResponse() {
376: HttpConnection connection = new HttpConnection(connector,
377: connector.endp, connector.server);
378: connection.getGenerator().reset(false);
379: HttpConnection.setCurrentConnection(connection);
380: Response response = connection.getResponse();
381: connection.getRequest().setRequestURI("/test");
382: return response;
383: }
384:
385: class TestSession extends AbstractSessionManager.Session {
386: public TestSession(
387: AbstractSessionManager abstractSessionManager, String id) {
388: abstractSessionManager.super (System.currentTimeMillis(), id);
389: }
390:
391: public Object getAttribute(String name) {
392: return null;
393: }
394:
395: public Enumeration getAttributeNames() {
396:
397: return null;
398: }
399:
400: public long getCreationTime() {
401:
402: return 0;
403: }
404:
405: public String getId() {
406: return "12345";
407: }
408:
409: public long getLastAccessedTime() {
410: return 0;
411: }
412:
413: public int getMaxInactiveInterval() {
414: return 0;
415: }
416:
417: public ServletContext getServletContext() {
418: return null;
419: }
420:
421: public HttpSessionContext getSessionContext() {
422: return null;
423: }
424:
425: public Object getValue(String name) {
426: return null;
427: }
428:
429: public String[] getValueNames() {
430: return null;
431: }
432:
433: public void invalidate() {
434: }
435:
436: public boolean isNew() {
437: return false;
438: }
439:
440: public void putValue(String name, Object value) {
441: }
442:
443: public void removeAttribute(String name) {
444: }
445:
446: public void removeValue(String name) {
447: }
448:
449: public void setAttribute(String name, Object value) {
450: }
451:
452: public void setMaxInactiveInterval(int interval) {
453: }
454:
455: protected Map newAttributeMap() {
456: // TODO Auto-generated method stub
457: return null;
458: }
459: }
460: }
|