📄 responsetest.java
字号:
assertEquals("text/html; other=xyz charset=utf-8",response.getContentType()); response.getWriter(); assertEquals("text/html; other=xyz charset=utf-8",response.getContentType()); response.setContentType("text/xml"); assertEquals("text/xml; charset=utf-8",response.getContentType()); } public void testContentTypeWithCharacterEncodingAndOther() throws Exception { Response response = new Response(new HttpConnection(connector,connector._endp,connector._server)); response.setCharacterEncoding("utf16"); response.setContentType("foo/bar; charset=utf-8 other=xyz"); assertEquals("foo/bar; charset=utf-8 other=xyz",response.getContentType()); response.getWriter(); assertEquals("foo/bar; charset=utf-8 other=xyz",response.getContentType()); response.recycle(); response.setCharacterEncoding("utf16"); response.setContentType("text/html; other=xyz charset=utf-8"); assertEquals("text/html; other=xyz charset=utf-8",response.getContentType()); response.getWriter(); assertEquals("text/html; other=xyz charset=utf-8",response.getContentType()); response.recycle(); response.setCharacterEncoding("utf16"); response.setContentType("foo/bar; other=pq charset=utf-8 other=xyz"); assertEquals("foo/bar; other=pq charset=utf-8 other=xyz",response.getContentType()); response.getWriter(); assertEquals("foo/bar; other=pq charset=utf-8 other=xyz",response.getContentType()); } public void testStatusCodes() throws Exception { Response response=newResponse(); response.sendError(404); assertEquals(404, response.getStatus()); assertEquals(null, response.getReason()); response=newResponse(); response.sendError(500, "Database Error"); assertEquals(500, response.getStatus()); assertEquals("Database Error", response.getReason()); assertEquals("must-revalidate,no-cache,no-store", response.getHeader(HttpHeaders.CACHE_CONTROL)); response=newResponse(); response.setStatus(200); assertEquals(200, response.getStatus()); assertEquals(null, response.getReason()); response=newResponse(); response.sendError(406, "Super Nanny"); assertEquals(406, response.getStatus()); assertEquals("Super Nanny", response.getReason()); assertEquals("must-revalidate,no-cache,no-store", response.getHeader(HttpHeaders.CACHE_CONTROL)); } public void testEncodeRedirect() throws Exception { HttpConnection connection=new HttpConnection(connector,connector._endp,connector._server); Response response = new Response(connection); Request request = connection.getRequest(); assertEquals("http://host:port/path/info;param?query=0&more=1#target",response.encodeRedirectUrl("http://host:port/path/info;param?query=0&more=1#target")); request.setRequestedSessionId("12345"); request.setRequestedSessionIdFromCookie(false); AbstractSessionManager manager=new HashSessionManager(); manager.setIdManager(new HashSessionIdManager()); request.setSessionManager(manager); request.setSession(new TestSession(manager,"12345")); assertEquals("http://host:port/path/info;param;jsessionid=12345?query=0&more=1#target",response.encodeRedirectUrl("http://host:port/path/info;param?query=0&more=1#target")); } public void testSetBufferSize () throws Exception { Response response = new Response(new HttpConnection(connector,connector._endp,connector._server)); response.setBufferSize(20*1024); response.getWriter().print("hello"); try { response.setBufferSize(21*1024); fail("Expected IllegalStateException on Request.setBufferSize"); } catch (Exception e) { assertTrue(e instanceof IllegalStateException); } } public void testHead() throws Exception { Server server = new Server(); try { SocketConnector socketConnector = new SocketConnector(); socketConnector.setPort(0); server.addConnector(socketConnector); server.addHandler(new AbstractHandler() { public void handle(String string, HttpServletRequest request, HttpServletResponse response, int i) throws IOException, ServletException { response.setStatus(200); response.setContentType("text/plain"); PrintWriter w = response.getWriter(); w.flush(); w.println("Geht"); w.flush(); w.println("Doch"); ((Request) request).setHandled(true); } }); server.start(); Socket socket = new Socket("localhost",socketConnector.getLocalPort()); socket.getOutputStream().write("HEAD / HTTP/1.1\r\nHost: localhost\r\n\r\n".getBytes()); socket.getOutputStream().write("GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n".getBytes()); socket.getOutputStream().flush(); LineNumberReader reader = new LineNumberReader(new InputStreamReader(socket.getInputStream())); String line = reader.readLine(); while (line!=null && line.length()>0) line = reader.readLine(); while (line!=null && line.length()==0) line = reader.readLine(); assertTrue(line.startsWith("HTTP/1.1 200 OK")); } finally { server.stop(); } } private Response newResponse() { HttpConnection connection=new HttpConnection(connector,connector._endp,connector._server); connection.getGenerator().reset(false); HttpConnection.setCurrentConnection(connection); Response response = connection.getResponse(); connection.getRequest().setRequestURI("/test"); return response; } class TestSession extends AbstractSessionManager.Session { public TestSession(AbstractSessionManager abstractSessionManager, String id) { abstractSessionManager.super(System.currentTimeMillis(), id); } public Object getAttribute(String name) { return null; } public Enumeration getAttributeNames() { return null; } public long getCreationTime() { return 0; } public String getId() { return "12345"; } public long getLastAccessedTime() { return 0; } public int getMaxInactiveInterval() { return 0; } public ServletContext getServletContext() { return null; } public HttpSessionContext getSessionContext() { return null; } public Object getValue(String name) { return null; } public String[] getValueNames() { return null; } public void invalidate() { } public boolean isNew() { return false; } public void putValue(String name, Object value) { } public void removeAttribute(String name) { } public void removeValue(String name) { } public void setAttribute(String name, Object value) { } public void setMaxInactiveInterval(int interval) { } protected Map newAttributeMap() { // TODO Auto-generated method stub return null; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -