⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileuploadtest.java

📁 这是远程web服务调用的一个包,可以将JSP直接作为服务
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.meterware.httpunit;/********************************************************************************************************************* $Id: FileUploadTest.java,v 1.14 2002/11/21 22:32:04 russgold Exp $** Copyright (c) 2000-2001, 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 com.meterware.pseudoserver.PseudoServlet;import com.meterware.pseudoserver.WebResource;import java.io.*;import java.util.StringTokenizer;import java.net.URLEncoder;import javax.activation.DataSource;import javax.mail.MessagingException;import javax.mail.internet.MimeBodyPart;import javax.mail.internet.MimeMultipart;import junit.framework.Test;import junit.framework.TestSuite;/** * A unit test of the file upload simulation capability. **/public class FileUploadTest extends HttpUnitTest {    public static void main(String args[]) {        junit.textui.TestRunner.run( suite() );    }		    public static Test suite() {        return new TestSuite( FileUploadTest.class );    }    public FileUploadTest( String name ) {        super( name );    }    public void setUp() throws Exception {        super.setUp();    }	            public void testParametersMultipartEncoding() throws Exception {        defineResource( "ListParams", new MimeEcho() );        defineWebPage( "Default", "<form method=POST action = \"ListParams\" enctype=\"multipart/form-data\"> " +                                  "<Input type=text name=age value=12>" +                                  "<Textarea name=comment>first\nsecond</textarea>" +                                  "<Input type=submit name=update value=age>" +                                  "</form>" );        WebConversation wc = new WebConversation();        WebRequest request = new GetMethodWebRequest( getHostPath() + "/Default.html" );        WebResponse simplePage = wc.getResponse( request );        WebRequest formSubmit = simplePage.getForms()[0].getRequest();        WebResponse encoding = wc.getResponse( formSubmit );        assertEquals( "http://dummy?age=12&comment=first%0D%0Asecond&update=age", "http://dummy?" + encoding.getText().trim() );    }    public void testFileParameterValidation() throws Exception {        defineWebPage( "Default", "<form method=POST action = \"ListParams\" enctype=\"multipart/form-data\"> " +                                  "<Input type=file name=message>" +                                  "<Input type=submit name=update value=age>" +                                  "</form>" );        WebConversation wc = new WebConversation();        WebRequest request = new GetMethodWebRequest( getHostPath() + "/Default.html" );        WebResponse simplePage = wc.getResponse( request );        WebRequest formSubmit = simplePage.getForms()[0].getRequest();        try {            formSubmit.setParameter( "message", "text/plain" );            fail( "Should not allow setting of a file parameter to a text value" );        } catch (IllegalRequestParameterException e) {        }    }    public void testNonFileParameterValidation() throws Exception {        File file = new File( "temp.html" );        defineWebPage( "Default", "<form method=POST action = \"ListParams\" enctype=\"multipart/form-data\"> " +                                  "<Input type=text name=message>" +                                  "<Input type=submit name=update value=age>" +                                  "</form>" );        WebConversation wc = new WebConversation();        WebRequest request = new GetMethodWebRequest( getHostPath() + "/Default.html" );        WebResponse simplePage = wc.getResponse( request );        WebRequest formSubmit = simplePage.getForms()[0].getRequest();        try {            formSubmit.selectFile( "message", file );            fail( "Should not allow setting of a text parameter to a file value" );        } catch (IllegalRequestParameterException e) {        }    }    public void testURLEncodingFileParameterValidation() throws Exception {        File file = new File( "temp.html" );        defineWebPage( "Default", "<form method=POST action = \"ListParams\"> " +                                  "<Input type=file name=message>" +                                  "<Input type=submit name=update value=age>" +                                  "</form>" );        WebConversation wc = new WebConversation();        WebRequest request = new GetMethodWebRequest( getHostPath() + "/Default.html" );        WebResponse simplePage = wc.getResponse( request );        WebRequest formSubmit = simplePage.getForms()[0].getRequest();        try {            formSubmit.selectFile( "message", file );            fail( "Should not allow setting of a file parameter in a form which specifies url-encoding" );        } catch (IllegalRequestParameterException e) {        }    }    public void testFileMultipartEncoding() throws Exception {        File file = new File( "temp.txt" );        FileWriter fw = new FileWriter( file );        PrintWriter pw = new PrintWriter( fw );        pw.println( "Not much text" );        pw.println( "But two lines" );        pw.close();        defineResource( "ListParams", new MimeEcho() );        defineWebPage( "Default", "<form method=POST action = \"ListParams\" enctype=\"multipart/form-data\"> " +                                  "<Input type=file name=message>" +                                  "<Input type=submit name=update value=age>" +                                  "</form>" );        WebConversation wc = new WebConversation();        WebRequest request = new GetMethodWebRequest( getHostPath() + "/Default.html" );        WebResponse simplePage = wc.getResponse( request );        WebRequest formSubmit = simplePage.getForms()[0].getRequest();        formSubmit.selectFile( "message", file );        WebResponse encoding = wc.getResponse( formSubmit );        assertEquals( "text/plain:message.name=temp.txt&message.lines=2&update=age", encoding.getText().trim() );        file.delete();    }    public void testMultiFileSubmit() throws Exception {        File file = new File( "temp.txt" );        FileWriter fw = new FileWriter( file );        PrintWriter pw = new PrintWriter( fw );        pw.println( "Not much text" );        pw.println( "But two lines" );        pw.close();        File file2 = new File( "temp2.txt" );        fw = new FileWriter( file2 );        pw = new PrintWriter( fw );        pw.println( "Even less text on one line" );        pw.close();        defineResource( "ListParams", new MimeEcho() );        defineWebPage( "Default", "<form method=POST action = \"ListParams\" enctype=\"multipart/form-data\"> " +                                  "<Input type=file name=message>" +                                  "<Input type=file name=message>" +                                  "<Input type=submit name=update value=age>" +                                  "</form>" );        WebConversation wc = new WebConversation();        WebRequest request = new GetMethodWebRequest( getHostPath() + "/Default.html" );        WebResponse simplePage = wc.getResponse( request );        WebRequest formSubmit = simplePage.getForms()[0].getRequest();        formSubmit.setParameter( "message", new UploadFileSpec[] { new UploadFileSpec( file ), new UploadFileSpec( file2, "text/more" ) } );        WebResponse encoding = wc.getResponse( formSubmit );        assertEquals( "text/plain:message.name=temp.txt&message.lines=2&text/more:message.name=temp2.txt&message.lines=1&update=age", encoding.getText().trim() );        file.delete();        file2.delete();    }    public void testIllegalMultiFileSubmit() throws Exception {        File file = new File( "temp.txt" );        FileWriter fw = new FileWriter( file );        PrintWriter pw = new PrintWriter( fw );        pw.println( "Not much text" );        pw.println( "But two lines" );        pw.close();        File file2 = new File( "temp2.txt" );        fw = new FileWriter( file2 );        pw = new PrintWriter( fw );        pw.println( "Even less text on one line" );        pw.close();        defineResource( "ListParams", new MimeEcho() );        defineWebPage( "Default", "<form method=POST action = \"ListParams\" enctype=\"multipart/form-data\"> " +                                  "<Input type=file name=message>" +                                  "<Input type=submit name=update value=age>" +                                  "</form>" );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -