📄 httpservletrequesttest.java
字号:
package com.meterware.servletunit;/********************************************************************************************************************* $Id: HttpServletRequestTest.java,v 1.27 2005/03/06 20:18:32 russgold Exp $** Copyright (c) 2000-2005 by Russell Gold** Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated* documentation files (the "Software"), to deal in the Software without restriction, including without limitation* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and* to permit persons to whom the Software is furnished to do so, subject to the following conditions:** The above copyright notice and this permission notice shall be included in all copies or substantial portions* of the Software.** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER* DEALINGS IN THE SOFTWARE.********************************************************************************************************************/import java.util.*;import java.net.MalformedURLException;import java.io.InputStream;import java.io.ByteArrayInputStream;import java.io.BufferedInputStream;import java.io.ByteArrayOutputStream;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;import javax.servlet.Servlet;import javax.servlet.ServletException;import com.meterware.httpunit.*;import com.meterware.httpunit.cookies.CookieProperties;import junit.framework.Test;import junit.framework.TestSuite;/** * Tests the ServletUnitHttpRequest class. * * @author <a href="mailto:russgold@httpunit.org">Russell Gold</a> **/public class HttpServletRequestTest extends ServletUnitTest { private ServletUnitContext _context; public static void main(String args[]) { junit.textui.TestRunner.run( suite() ); } public static Test suite() { return new TestSuite( HttpServletRequestTest.class ); } public HttpServletRequestTest( String name ) { super( name ); } protected void setUp() throws Exception { super.setUp(); _context = new ServletUnitContext( null, null, new SessionListenerDispatcher() { public void sendSessionCreated( HttpSession session ) {} public void sendSessionDestroyed( HttpSession session ) {} public void sendAttributeAdded( HttpSession session, String name, Object value ) {} public void sendAttributeReplaced( HttpSession session, String name, Object oldValue ) {} public void sendAttributeRemoved( HttpSession session, String name, Object oldValue ) {} } ); } public void testHeaderAccess() throws Exception { WebRequest wr = new GetMethodWebRequest( "http://localhost/simple" ); wr.setHeaderField( "sample", "value" ); HttpServletRequest request = new ServletUnitHttpRequest( NULL_SERVLET_REQUEST, wr, _context, new Hashtable(), NO_MESSAGE_BODY ); assertEquals( "sample header value", "value", request.getHeader( "sample") ); assertContains( "Header names", "sample", request.getHeaderNames() ); Enumeration e = request.getHeaders( "Sample" ); assertNotNull( "No header enumeration returned", e ); assertTrue( "Enumeration is empty", e.hasMoreElements() ); assertEquals( "first header", "value", e.nextElement() ); assertFalse( "Enumeration has spurious header value", e.hasMoreElements() ); } private void assertContains( String comment, String string, Enumeration headerNames ) { while (headerNames != null && headerNames.hasMoreElements()) { String name = (String) headerNames.nextElement(); if (name.equalsIgnoreCase( string )) return; } fail( comment + " does not contain " + string ); } public void testGetDefaultProperties() throws Exception { WebRequest wr = new GetMethodWebRequest( "http://localhost/simple" ); HttpServletRequest request = new ServletUnitHttpRequest( NULL_SERVLET_REQUEST, wr, _context, new Hashtable(), NO_MESSAGE_BODY ); assertNull( "Authorization incorrectly specified", request.getAuthType() ); assertNull( "Character encoding incorrectly specified", request.getCharacterEncoding() ); assertEquals( "Parameters unexpectedly specified", "", request.getQueryString() ); assertNotNull( "No input stream available", request.getInputStream() ); } public void testSetSingleValuedParameter() throws Exception { WebRequest wr = new GetMethodWebRequest( "http://localhost/simple" ); wr.setParameter( "age", "12" ); wr.setParameter( "color", new String[] { "red", "blue" } ); HttpServletRequest request = new ServletUnitHttpRequest( NULL_SERVLET_REQUEST, wr, _context, new Hashtable(), NO_MESSAGE_BODY ); assertEquals( "age parameter", "12", request.getParameter( "age" ) ); assertNull( "unset parameter should be null", request.getParameter( "unset" ) ); } public void testSetMultiValuedParameter() throws Exception { WebRequest wr = new GetMethodWebRequest( "http://localhost/simple" ); wr.setParameter( "age", "12" ); wr.setParameter( "color", new String[] { "red", "blue" } ); HttpServletRequest request = new ServletUnitHttpRequest( NULL_SERVLET_REQUEST, wr, _context, new Hashtable(), NO_MESSAGE_BODY ); assertMatchingSet( "age parameter", new String[] { "12" }, request.getParameterValues( "age" ) ); assertMatchingSet( "color parameter", new String[] { "red", "blue" }, request.getParameterValues( "color" ) ); assertNull( "unset parameter should be null", request.getParameterValues( "unset" ) ); } public void testParameterMap() throws Exception { WebRequest wr = new GetMethodWebRequest( "http://localhost/simple" ); wr.setParameter( "age", "12" ); wr.setParameter( "color", new String[] { "red", "blue" } ); HttpServletRequest request = new ServletUnitHttpRequest( NULL_SERVLET_REQUEST, wr, _context, new Hashtable(), NO_MESSAGE_BODY ); Map map = request.getParameterMap(); assertMatchingSet( "age parameter", new String[] { "12" }, (Object[]) map.get( "age" ) ); assertMatchingSet( "color parameter", new String[] { "red", "blue" }, (Object[]) map.get( "color" ) ); assertNull( "unset parameter should be null", map.get( "unset" ) ); } public void testSetQueryString() throws Exception { WebRequest wr = new GetMethodWebRequest( "http://localhost/simple" ); wr.setParameter( "age", "12" ); wr.setParameter( "color", new String[] { "red", "blue" } ); HttpServletRequest request = new ServletUnitHttpRequest( NULL_SERVLET_REQUEST, wr, _context, new Hashtable(), NO_MESSAGE_BODY ); assertEquals( "query string", "color=red&color=blue&age=12", request.getQueryString() ); } public void testInlineSingleValuedParameter() throws Exception { WebRequest wr = new GetMethodWebRequest( "http://localhost/simple?color=red&color=blue&age=12" ); HttpServletRequest request = new ServletUnitHttpRequest( NULL_SERVLET_REQUEST, wr, _context, new Hashtable(), NO_MESSAGE_BODY ); assertEquals( "age parameter", "12", request.getParameter( "age" ) ); assertNull( "unset parameter should be null", request.getParameter( "unset" ) ); } public void testInlineParameterWithEmbeddedSpace() throws Exception { WebRequest wr = new GetMethodWebRequest( "http://localhost/simple?color=dark+red&age=12" ); HttpServletRequest request = new ServletUnitHttpRequest( NULL_SERVLET_REQUEST, wr, _context, new Hashtable(), NO_MESSAGE_BODY ); assertEquals( "age parameter", "12", request.getParameter( "age" ) ); assertEquals( "color parameter", "dark red", request.getParameter( "color" ) ); } public void testInlineMultiValuedParameter() throws Exception { WebRequest wr = new GetMethodWebRequest( "http://localhost/simple?color=red&color=blue&age=12" ); HttpServletRequest request = new ServletUnitHttpRequest( NULL_SERVLET_REQUEST, wr, _context, new Hashtable(), NO_MESSAGE_BODY ); assertMatchingSet( "age parameter", new String[] { "12" }, request.getParameterValues( "age" ) ); assertMatchingSet( "color parameter", new String[] { "red", "blue" }, request.getParameterValues( "color" ) ); assertNull( "unset parameter should be null", request.getParameterValues( "unset" ) ); } public void notestInlineQueryString() throws Exception { // TODO make this work WebRequest wr = new GetMethodWebRequest( "http://localhost/simple?color=red&color=blue&age=12" ); HttpServletRequest request = new ServletUnitHttpRequest( NULL_SERVLET_REQUEST, wr, _context, new Hashtable(), NO_MESSAGE_BODY ); assertEquals( "query string", "color=red&color=blue&age=12", request.getQueryString() ); } public void testRequestMessageBody() throws Exception { String body = "12345678901234567890"; InputStream stream = new ByteArrayInputStream( body.getBytes( "UTF-8" ) ); WebRequest wr = new PutMethodWebRequest( "http://localhost/simple", stream, "text/plain" ); HttpServletRequest request = new ServletUnitHttpRequest( NULL_SERVLET_REQUEST, wr, _context, new Hashtable(), body.getBytes() ); assertEquals( "Request content length", body.length(), request.getContentLength() ); BufferedInputStream bis = new BufferedInputStream( request.getInputStream() ); byte[] buffer = new byte[ request.getContentLength() ]; bis.read( buffer ); assertEquals( "Request content", body, new String( buffer ) ); } public void testDefaultAttributes() throws Exception { WebRequest wr = new GetMethodWebRequest( "http://localhost/simple" ); HttpServletRequest request = new ServletUnitHttpRequest( NULL_SERVLET_REQUEST, wr, _context, new Hashtable(), NO_MESSAGE_BODY ); assertNull( "attribute should not be defined yet", request.getAttribute( "unset" ) ); assertTrue( "attribute enumeration should be empty", !request.getAttributeNames().hasMoreElements() ); } public void testNonDefaultAttributes() throws Exception { WebRequest wr = new GetMethodWebRequest( "http://localhost/simple" ); HttpServletRequest request = new ServletUnitHttpRequest( NULL_SERVLET_REQUEST, wr, _context, new Hashtable(), NO_MESSAGE_BODY ); Object value = new Integer(1); request.setAttribute( "one", value ); assertEquals( "attribute one", value, request.getAttribute( "one" ) ); Enumeration names = request.getAttributeNames(); assertTrue( "attribute enumeration should not be empty", names.hasMoreElements() ); assertEquals( "contents in enumeration", "one", names.nextElement() ); assertTrue( "attribute enumeration should now be empty", !names.hasMoreElements() ); } public void testDuplicateAttributes() throws Exception { WebRequest wr = new GetMethodWebRequest( "http://localhost/simple" ); HttpServletRequest request = new ServletUnitHttpRequest( NULL_SERVLET_REQUEST, wr, _context, new Hashtable(), NO_MESSAGE_BODY ); request.setAttribute( "one", new Integer(1) ); request.setAttribute( "one", "One" ); assertEquals( "Revised attribute value", "One", request.getAttribute( "one" ) ); } public void testNullAttributeValue() throws Exception { WebRequest wr = new GetMethodWebRequest( "http://localhost/simple" ); HttpServletRequest request = new ServletUnitHttpRequest( NULL_SERVLET_REQUEST, wr, _context, new Hashtable(), NO_MESSAGE_BODY ); request.setAttribute( "one", "One" ); assertEquals( "Initial attribute value", "One", request.getAttribute( "one" ) ); request.setAttribute( "one", null ); assertNull( "Attribute 'one' should have been removed", request.getAttribute( "one" ) ); } public void testDefaultCookies() throws Exception { WebRequest wr = new GetMethodWebRequest( "http://localhost/simple" ); HttpServletRequest request = new ServletUnitHttpRequest( NULL_SERVLET_REQUEST, wr, _context, new Hashtable(), NO_MESSAGE_BODY ); Cookie[] cookies = request.getCookies(); assertNull( "Unexpected cookies found", cookies ); } public void testSetCookieViaRequestHeader() throws Exception { WebRequest wr = new GetMethodWebRequest( "http://localhost/simple" ); wr.setHeaderField( "Cookie", "flavor=vanilla,variety=sandwich"); ServletUnitHttpRequest request = new ServletUnitHttpRequest( NULL_SERVLET_REQUEST, wr, _context, new Hashtable(), NO_MESSAGE_BODY ); Cookie[] cookies = request.getCookies(); assertNotNull( "No cookies found", cookies ); assertEquals( "Num cookies found", 2, cookies.length ); assertEquals( "Cookie 1 name", "flavor", cookies[0].getName() ); assertEquals( "Cookie 1 value", "vanilla", cookies[0].getValue() ); assertEquals( "Cookie 2 name", "variety", cookies[1].getName() ); assertEquals( "Cookie 2 value", "sandwich", cookies[1].getValue() ); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -