📄 pseudoservertest.java
字号:
SocketConnection conn = new SocketConnection( "localhost", getHostPort() ); SocketConnection.SocketResponse response = conn.getResponse( "POST", '/' + resourceName, "name=" + name ); assertEquals( "Content type", "text/plain", response.getHeader( "Content-Type" ) ); assertEquals( "Response", expectedResponse, new String( response.getBody() ) ); } public void testPseudoServletWithGET() throws Exception { String resourceName = "tellMe"; String name = "Charlie"; final String prefix = "Hello there, "; String expectedResponse = prefix + name; defineResource( resourceName, new PseudoServlet() { public WebResource getGetResponse() { return new WebResource( prefix + getParameter( "name" )[0], "text/plain" ); } } ); SocketConnection conn = new SocketConnection( "localhost", getHostPort() ); SocketConnection.SocketResponse response = conn.getResponse( "GET", '/' + resourceName + "?name=" + name ); assertEquals( "Response code", 200, response.getResponseCode() ); assertEquals( "Content type", "text/plain", response.getHeader( "Content-Type" ) ); assertEquals( "Response", expectedResponse, new String( response.getBody() ) ); } public void testChunkedRequest() throws Exception { super.defineResource( "/chunkedServlet", new PseudoServlet() { public WebResource getPostResponse() { return new WebResource( super.getBody(), "text/plain" ); } } ); SocketConnection conn = new SocketConnection( "localhost", getHostPort() ); conn.startChunkedResponse( "POST", "/chunkedServlet" ); conn.sendChunk( "This " ); conn.sendChunk( "is " ); conn.sendChunk( "chunked."); SocketConnection.SocketResponse response = conn.getResponse(); assertEquals( "retrieved body", "This is chunked.", new String( response.getBody() ) ); } // need test: respond with HTTP_BAD_REQUEST if header line is bad public void testChunkedRequestFollowedByAnother() throws Exception { super.defineResource( "/chunkedServlet", new PseudoServlet() { public WebResource getPostResponse() { return new WebResource( super.getBody(), "text/plain" ); } } ); SocketConnection conn = new SocketConnection( "localhost", getHostPort() ); conn.startChunkedResponse( "POST", "/chunkedServlet" ); conn.sendChunk( "This " ); conn.sendChunk( "is " ); conn.sendChunk( "chunked."); SocketConnection.SocketResponse response = conn.getResponse(); assertEquals( "retrieved body", "This is chunked.", new String( response.getBody() ) ); // Make a second request to duplicate the problem... conn.startChunkedResponse( "POST", "/chunkedServlet" ); conn.sendChunk( "This " ); conn.sendChunk( "is " ); conn.sendChunk( "also " ); conn.sendChunk( "chunked."); SocketConnection.SocketResponse response2 = conn.getResponse(); assertEquals( "retrieved body", "This is also chunked.", new String( response2.getBody() ) ); } public void testChunkedResponse() throws Exception { defineResource( "/chunkedServlet", new PseudoServlet() { public WebResource getGetResponse() { WebResource webResource = new WebResource( "5\r\nSent \r\n3\r\nin \r\n07\r\nchunks.\r\n0\r\n\r\n", "text/plain" ); webResource.addHeader( "Transfer-Encoding: chunked" ); return webResource; } } ); SocketConnection conn = new SocketConnection( "localhost", getHostPort() ); SocketConnection.SocketResponse response = conn.getResponse( "GET", "/chunkedServlet" ); assertEquals( "retrieved body", "Sent in chunks.", new String( response.getBody() ) ); assertNull( "No Content-Length header should have been sent", response.getHeader( "Content-Length" ) ); } public void testPersistentConnection() throws Exception { super.defineResource( "/testServlet", new TestMethodServlet() ); SocketConnection conn = new SocketConnection( "localhost", getHostPort() ); SocketConnection.SocketResponse resp1 = conn.getResponse( "HEAD", "/testServlet" ); assertEquals( "test-header", "test-value1", resp1.getHeader( "test-header1") ); SocketConnection.SocketResponse resp2 = conn.getResponse( "GET", "/testServlet" ); assertEquals( "retrieved body", TestMethodServlet.GET_DATA, new String( resp2.getBody() ) ); SocketConnection.SocketResponse resp3 = conn.getResponse( "OPTIONS", "/testServlet" ); assertEquals( "allow header", "GET", resp3.getHeader( "Allow") ); } private class TestMethodServlet extends PseudoServlet { private static final String GET_DATA = "This is from the TestMethodServlet - GET"; public WebResource getResponse( String method ) throws IOException { if (method.equals( "GET" )) { return new WebResource( GET_DATA ); } else if (method.equals( "HEAD" )) { WebResource headResource = new WebResource( "" ); headResource.addHeader( "test-header1:test-value1" ); return headResource; } else if (method.equals( "OPTIONS" )) { WebResource optionsResource = new WebResource( GET_DATA ); optionsResource.addHeader( "Allow:GET" ); optionsResource.addHeader( "test-header1:test-value1" ); return optionsResource; } else { return super.getResponse( method ); } } } public void testBadMethodUsingPseudoServlet() throws Exception { String resourceName = "tellMe"; defineResource( resourceName, new PseudoServlet() {} ); SocketConnection conn = new SocketConnection( "localhost", getHostPort() ); SocketConnection.SocketResponse response = conn.getResponse( "HEAD", '/' + resourceName ); assertEquals( "Status code returned", HttpURLConnection.HTTP_BAD_METHOD, response.getResponseCode() ); } public void testClasspathDirectory() throws Exception { mapToClasspath( "/some/classes" ); SocketConnection conn = new SocketConnection( "localhost", getHostPort() ); conn.getResponse( "GET", "/some/classes/" + SocketConnection.SocketResponse.class.getName().replace('.','/') + ".class" ); } public void testPseudoServletRequestAccess() throws Exception { defineResource( "/properties", new PseudoServlet() { public WebResource getGetResponse() { return new WebResource( super.getRequest().getURI(), "text/plain" ); } } ); SocketConnection conn = new SocketConnection( "localhost", getHostPort() ); SocketConnection.SocketResponse response = conn.getResponse( "GET", "/properties" ); assertEquals( "retrieved body", "/properties", new String( response.getBody() ) ); } public void testLargeDelayedPseudoServletRequest() throws Exception { defineResource( "/largeRequest", new PseudoServlet() { public WebResource getPostResponse() { return new WebResource( super.getBody(), super.getHeader( "CONTENT-TYPE" ) ); } } ); Socket sock = new Socket( "localhost", getHostPort() ); sock.setKeepAlive( true ); sock.setTcpNoDelay( true ); sock.setSoTimeout( 5000 ); byte[] requestData = null; requestData = generateLongMIMEPostData().getBytes();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -