📄 rfc2616test.java
字号:
//========================================================================//Copyright 2004-2008 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.//========================================================================/* * Created on 9/01/2004 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */package org.mortbay.jetty;import java.util.Date;import java.util.Enumeration;import java.util.List;import junit.framework.TestCase;import org.mortbay.jetty.handler.ContextHandler;import org.mortbay.jetty.handler.HandlerCollection;/** * @author gregw * */public class RFC2616Test extends TestCase{ Server server=new Server(); LocalConnector connector=new LocalConnector(); /** * Constructor for RFC2616Test. * * @param arg0 */ public RFC2616Test(String arg0) { super(arg0); server.setConnectors(new Connector[] { connector }); ContextHandler vcontext=new ContextHandler(); vcontext.setContextPath("/"); vcontext.setVirtualHosts(new String[] { "VirtualHost" }); vcontext.setHandler(new DumpHandler("Virtual Dump")); ContextHandler context=new ContextHandler(); context.setContextPath("/"); context.setHandler(new DumpHandler()); HandlerCollection collection=new HandlerCollection(); collection.setHandlers(new Handler[] { vcontext, context }); server.setHandler(collection); } /* * @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 test3_3() { try { HttpFields fields=new HttpFields(); fields.put("D1","Sun, 6 Nov 1994 08:49:37 GMT"); fields.put("D2","Sunday, 6-Nov-94 08:49:37 GMT"); fields.put("D3","Sun Nov 6 08:49:37 1994"); Date d1=new Date(fields.getDateField("D1")); Date d2=new Date(fields.getDateField("D2")); Date d3=new Date(fields.getDateField("D3")); assertEquals("3.3.1 RFC 822 RFC 850",d2,d1); assertEquals("3.3.1 RFC 850 ANSI C",d3,d2); fields.putDateField("Date",d1.getTime()); assertEquals("3.3.1 RFC 822 preferred","Sun, 06 Nov 1994 08:49:37 GMT",fields.getStringField("Date")); } catch (Exception e) { e.printStackTrace(); assertTrue(false); } } /* --------------------------------------------------------------- */ public void test3_6() { String response=null; try { int offset=0; // Chunk last offset=0; connector.reopen(); response=connector.getResponses("GET /R1 HTTP/1.1\n"+"Host: localhost\n"+"Transfer-Encoding: chunked,identity\n"+"Content-Type: text/plain\n" +"\015\012"+"5;\015\012"+"123\015\012\015\012"+"0;\015\012\015\012"); checkContains(response,offset,"HTTP/1.1 400 Bad","Chunked last"); // Chunked offset=0; connector.reopen(); response=connector.getResponses("GET /R1 HTTP/1.1\n"+"Host: localhost\n"+"Transfer-Encoding: chunked\n"+"Content-Type: text/plain\n"+"\n"+"2;\n" +"12\n"+"3;\n"+"345\n"+"0;\n\n"+ "GET /R2 HTTP/1.1\n"+"Host: localhost\n"+"Transfer-Encoding: chunked\n"+"Content-Type: text/plain\n"+"\n"+"4;\n"+"6789\n"+"5;\n"+"abcde\n" +"0;\n\n"+ "GET /R3 HTTP/1.1\n"+"Host: localhost\n"+"Connection: close\n"+"\n"); offset=checkContains(response,offset,"HTTP/1.1 200","3.6.1 Chunking"); offset=checkContains(response,offset,"12345","3.6.1 Chunking"); offset=checkContains(response,offset,"HTTP/1.1 200","3.6.1 Chunking"); offset=checkContains(response,offset,"6789abcde","3.6.1 Chunking"); offset=checkContains(response,offset,"/R3","3.6.1 Chunking"); // Chunked offset=0; connector.reopen(); response=connector.getResponses("POST /R1 HTTP/1.1\n"+"Host: localhost\n"+"Transfer-Encoding: chunked\n"+"Content-Type: text/plain\n"+"\n"+"3;\n" +"fgh\n"+"3;\n"+"Ijk\n"+"0;\n\n"+ "POST /R2 HTTP/1.1\n"+"Host: localhost\n"+"Transfer-Encoding: chunked\n"+"Content-Type: text/plain\n"+"\n"+"4;\n"+"lmno\n"+"5;\n"+"Pqrst\n" +"0;\n\n"+ "GET /R3 HTTP/1.1\n"+"Host: localhost\n"+"Connection: close\n"+"\n"); checkNotContained(response,"HTTP/1.1 100","3.6.1 Chunking"); offset=checkContains(response,offset,"HTTP/1.1 200","3.6.1 Chunking"); offset=checkContains(response,offset,"fghIjk","3.6.1 Chunking"); offset=checkContains(response,offset,"HTTP/1.1 200","3.6.1 Chunking"); offset=checkContains(response,offset,"lmnoPqrst","3.6.1 Chunking"); offset=checkContains(response,offset,"/R3","3.6.1 Chunking"); // Chunked and keep alive offset=0; connector.reopen(); response=connector.getResponses("GET /R1 HTTP/1.1\n"+"Host: localhost\n"+"Transfer-Encoding: chunked\n"+"Content-Type: text/plain\n" +"Connection: keep-alive\n"+"\n"+"3;\n"+"123\n"+"3;\n"+"456\n"+"0;\n\n"+ "GET /R2 HTTP/1.1\n"+"Host: localhost\n"+"Connection: close\n"+"\n"); offset=checkContains(response,offset,"HTTP/1.1 200","3.6.1 Chunking")+10; offset=checkContains(response,offset,"123456","3.6.1 Chunking"); offset=checkContains(response,offset,"/R2","3.6.1 Chunking")+10; } catch (Exception e) { e.printStackTrace(); assertTrue(false); if (response!=null) System.err.println(response); } } /* --------------------------------------------------------------- */ public void test3_9() { try { HttpFields fields=new HttpFields(); fields.put("Q","bbb;q=0.5,aaa,ccc;q=0.002,d;q=0,e;q=0.0001,ddd;q=0.001,aa2,abb;q=0.7"); Enumeration qualities=fields.getValues("Q",", \t"); List list=HttpFields.qualityList(qualities); assertEquals("Quality parameters","aaa",HttpFields.valueParameters(list.get(0).toString(),null)); assertEquals("Quality parameters","aa2",HttpFields.valueParameters(list.get(1).toString(),null)); assertEquals("Quality parameters","abb",HttpFields.valueParameters(list.get(2).toString(),null)); assertEquals("Quality parameters","bbb",HttpFields.valueParameters(list.get(3).toString(),null)); assertEquals("Quality parameters","ccc",HttpFields.valueParameters(list.get(4).toString(),null)); assertEquals("Quality parameters","ddd",HttpFields.valueParameters(list.get(5).toString(),null)); } catch (Exception e) { e.printStackTrace(); assertTrue(false); } } /* --------------------------------------------------------------- */ public void test4_4() { try { String response; int offset=0; connector.reopen(); // 2 // If _content length not used, second request will not be read. offset=0; connector.reopen(); response=connector.getResponses("GET /R1 HTTP/1.1\n"+"Host: localhost\n"+"Transfer-Encoding: identity\n"+"Content-Type: text/plain\n" +"Content-Length: 5\n"+"\n"+"123\015\012"+ "GET /R2 HTTP/1.1\n"+"Host: localhost\n"+"Connection: close\n"+"\n"); offset=checkContains(response,offset,"HTTP/1.1 200 OK","2. identity")+10; offset=checkContains(response,offset,"/R1","2. identity")+3; offset=checkContains(response,offset,"HTTP/1.1 200 OK","2. identity")+10; offset=checkContains(response,offset,"/R2","2. identity")+3; // 3 // _content length is ignored, as chunking is used. If it is // not ignored, the second request wont be seen. offset=0; connector.reopen(); response=connector.getResponses("GET /R1 HTTP/1.1\n"+"Host: localhost\n"+"Transfer-Encoding: chunked\n"+"Content-Type: text/plain\n" +"Content-Length: 100\n"+"\n"+"3;\n"+"123\n"+"3;\n"+"456\n"+"0;\n"+"\n"+ "GET /R2 HTTP/1.1\n"+"Host: localhost\n"+"Connection: close\n"+"Content-Type: text/plain\n"+"Content-Length: 6\n"+"\n"+"123456"); offset=checkContains(response,offset,"HTTP/1.1 200 OK","3. ignore c-l")+1; offset=checkContains(response,offset,"/R1","3. ignore c-l")+1; offset=checkContains(response,offset,"123456","3. ignore c-l")+1; offset=checkContains(response,offset,"HTTP/1.1 200 OK","3. ignore c-l")+1; offset=checkContains(response,offset,"/R2","3. _content-length")+1; offset=checkContains(response,offset,"123456","3. _content-length")+1; // No _content length assertTrue("Skip 411 checks as IE breaks this rule",true); // offset=0; connector.reopen(); // response=connector.getResponses("GET /R2 HTTP/1.1\n"+ // "Host: localhost\n"+ // "Content-Type: text/plain\n"+ // "Connection: close\n"+ // "\n"+ // "123456"); // offset=checkContains(response,offset, // "HTTP/1.1 411 ","411 length required")+10; // offset=0; connector.reopen(); // response=connector.getResponses("GET /R2 HTTP/1.0\n"+ // "Content-Type: text/plain\n"+ // "\n"+ // "123456"); // offset=checkContains(response,offset, // "HTTP/1.0 411 ","411 length required")+10; } catch (Exception e) { e.printStackTrace(); assertTrue(false); } } /* --------------------------------------------------------------- */ public void test5_2() throws Exception { String response; int offset=0; connector.reopen(); // Default Host offset=0; connector.reopen(); response=connector.getResponses("GET /path/R1 HTTP/1.1\n"+"Host: localhost\n"+"\n"); offset=checkContains(response,offset,"HTTP/1.1 200","Default host")+1; offset=checkContains(response,offset,"Dump HttpHandler","Default host")+1; offset=checkContains(response,offset,"pathInfo=/path/R1","Default host")+1; // Virtual Host offset=0; connector.reopen(); response=connector.getResponses("GET /path/R1 HTTP/1.1\n"+"Host: VirtualHost\n"+"\n"); offset=checkContains(response,offset,"HTTP/1.1 200","2. virtual host field")+1; offset=checkContains(response,offset,"Virtual Dump","2. virtual host field")+1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -