📄 responsetest.java
字号:
//========================================================================//$Id: HttpGeneratorTest.java,v 1.1 2005/10/05 14:09:41 janb Exp $//Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.//------------------------------------------------------------------------//Licensed under the Apache License, Version 2.0 (the "License");//you may not use this file except in compliance with the License.//You may obtain a copy of the License at //http://www.apache.org/licenses/LICENSE-2.0//Unless required by applicable law or agreed to in writing, software//distributed under the License is distributed on an "AS IS" BASIS,//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.//See the License for the specific language governing permissions and//limitations under the License.//========================================================================package org.mortbay.jetty;import java.io.IOException;import java.io.InputStreamReader;import java.io.LineNumberReader;import java.io.PrintWriter;import java.net.Socket;import java.util.Enumeration;import java.util.Locale;import java.util.Map;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSessionContext;import junit.framework.TestCase;import org.mortbay.jetty.bio.SocketConnector;import org.mortbay.jetty.handler.AbstractHandler;import org.mortbay.jetty.handler.ContextHandler;import org.mortbay.jetty.servlet.AbstractSessionManager;import org.mortbay.jetty.servlet.HashSessionIdManager;import org.mortbay.jetty.servlet.HashSessionManager;/** * @author gregw * * To change the template for this generated type comment go to * Window - Preferences - Java - Code Generation - Code and Comments */public class ResponseTest extends TestCase{ Server server = new Server(); LocalConnector connector = new LocalConnector(); public ResponseTest(String arg0) { super(arg0); server.setConnectors(new Connector[]{connector}); server.setHandler(new DumpHandler()); } public static void main(String[] args) { junit.textui.TestRunner.run(ResponseTest.class); } /* * @see TestCase#setUp() */ protected void setUp() throws Exception { super.setUp(); server.start(); } /* * @see TestCase#tearDown() */ protected void tearDown() throws Exception { super.tearDown(); server.stop(); } public void testContentType() throws Exception { HttpConnection connection = new HttpConnection(connector,connector._endp,connector._server); Response response = connection.getResponse(); assertEquals(null,response.getContentType()); response.setContentType("foo/bar"); assertEquals("foo/bar",response.getContentType()); response.getWriter(); assertEquals("foo/bar; charset=ISO-8859-1",response.getContentType()); response.setContentType("foo2/bar2"); assertEquals("foo2/bar2; charset=ISO-8859-1",response.getContentType()); response.setHeader("name","foo"); Enumeration en=response.getHeaders("name"); assertEquals("foo",en.nextElement()); assertFalse(en.hasMoreElements()); response.addHeader("name","bar"); en=response.getHeaders("name"); assertEquals("foo",en.nextElement()); assertEquals("bar",en.nextElement()); assertFalse(en.hasMoreElements()); response.recycle(); response.setContentType("text/html"); assertEquals("text/html",response.getContentType()); response.getWriter(); assertEquals("text/html; charset=iso-8859-1",response.getContentType()); response.setContentType("foo2/bar2"); assertEquals("foo2/bar2; charset=ISO-8859-1",response.getContentType()); response.recycle(); } public void testLocale() throws Exception { HttpConnection connection = new HttpConnection(connector,connector._endp,connector._server); Request request = connection.getRequest(); Response response = connection.getResponse(); ContextHandler context = new ContextHandler(); context.addLocaleEncoding(Locale.ENGLISH.toString(),"ISO-8859-1"); context.addLocaleEncoding(Locale.ITALIAN.toString(),"ISO-8859-2"); request.setContext(context.getServletContext()); response.setLocale(java.util.Locale.ITALIAN); assertEquals(null,response.getContentType()); response.setContentType("text/plain"); assertEquals("text/plain; charset=ISO-8859-2",response.getContentType()); response.recycle(); response.setContentType("text/plain"); response.setCharacterEncoding("utf-8"); response.setLocale(java.util.Locale.ITALIAN); assertEquals("text/plain; charset=utf-8",response.getContentType()); assertTrue(response.toString().indexOf("charset=utf-8")>0); } public void testContentTypeCharacterEncoding() throws Exception { HttpConnection connection = new HttpConnection(connector,connector._endp,connector._server); Request request = connection.getRequest(); Response response = connection.getResponse(); response.setContentType("foo/bar"); response.setCharacterEncoding("utf-8"); assertEquals("foo/bar; charset=utf-8",response.getContentType()); response.getWriter(); assertEquals("foo/bar; charset=utf-8",response.getContentType()); response.setContentType("foo2/bar2"); assertEquals("foo2/bar2; charset=utf-8",response.getContentType()); response.setCharacterEncoding("ISO-8859-1"); assertEquals("foo2/bar2; charset=utf-8",response.getContentType()); response.recycle(); response.setContentType("text/html"); response.setCharacterEncoding("utf-8"); assertEquals("text/html; charset=utf-8",response.getContentType()); response.getWriter(); assertEquals("text/html; charset=utf-8",response.getContentType()); response.setContentType("text/xml"); assertEquals("text/xml; charset=utf-8",response.getContentType()); response.setCharacterEncoding("ISO-8859-1"); assertEquals("text/xml; charset=utf-8",response.getContentType()); } public void testCharacterEncodingContentType() throws Exception { Response response = new Response(new HttpConnection(connector,connector._endp,connector._server)); response.setCharacterEncoding("utf-8"); response.setContentType("foo/bar"); assertEquals("foo/bar; charset=utf-8",response.getContentType()); response.getWriter(); assertEquals("foo/bar; charset=utf-8",response.getContentType()); response.setContentType("foo2/bar2"); assertEquals("foo2/bar2; charset=utf-8",response.getContentType()); response.setCharacterEncoding("ISO-8859-1"); assertEquals("foo2/bar2; charset=utf-8",response.getContentType()); response.recycle(); response.setCharacterEncoding("utf-8"); response.setContentType("text/html"); assertEquals("text/html; charset=utf-8",response.getContentType()); response.getWriter(); assertEquals("text/html; charset=utf-8",response.getContentType()); response.setContentType("text/xml"); assertEquals("text/xml; charset=utf-8",response.getContentType()); response.setCharacterEncoding("iso-8859-1"); assertEquals("text/xml; charset=utf-8",response.getContentType()); } public void testContentTypeWithCharacterEncoding() throws Exception { Response response = new Response(new HttpConnection(connector,connector._endp,connector._server)); response.setCharacterEncoding("utf16"); response.setContentType("foo/bar; charset=utf-8"); assertEquals("foo/bar; charset=utf-8",response.getContentType()); response.getWriter(); assertEquals("foo/bar; charset=utf-8",response.getContentType()); response.setContentType("foo2/bar2"); assertEquals("foo2/bar2; charset=utf-8",response.getContentType()); response.setCharacterEncoding("ISO-8859-1"); assertEquals("foo2/bar2; charset=utf-8",response.getContentType()); response.recycle(); response.setCharacterEncoding("utf16"); response.setContentType("text/html; charset=utf-8"); assertEquals("text/html; charset=utf-8",response.getContentType()); response.getWriter(); assertEquals("text/html; charset=utf-8",response.getContentType()); response.setContentType("text/xml"); assertEquals("text/xml; charset=utf-8",response.getContentType()); response.setCharacterEncoding("iso-8859-1"); assertEquals("text/xml; charset=utf-8",response.getContentType()); } public void testContentTypeWithOther() throws Exception { Response response = new Response(new HttpConnection(connector,connector._endp,connector._server)); response.setContentType("foo/bar; other=xyz"); assertEquals("foo/bar; other=xyz",response.getContentType()); response.getWriter(); assertEquals("foo/bar; other=xyz charset=ISO-8859-1",response.getContentType()); response.setContentType("foo2/bar2"); assertEquals("foo2/bar2; charset=ISO-8859-1",response.getContentType()); response.recycle(); response.setCharacterEncoding("utf-8"); response.setContentType("text/html; other=xyz");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -