junit--有登录账号和密码的junit测试代码

来源:互联网 发布:js保留三位小数 编辑:程序博客网 时间:2024/05/01 18:17
package com.juneyaoair.platform.controller.api;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.net.SocketException;import java.nio.charset.Charset;import mars.fw.core.ftp.FtpConfig;import mars.fw.core.ftp.FtpService;import net.sf.json.JSONObject;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.ContentType;import org.apache.http.entity.StringEntity;import org.apache.http.entity.mime.HttpMultipartMode;import org.apache.http.entity.mime.MultipartEntityBuilder;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.message.BasicHeader;import org.apache.http.protocol.HTTP;import org.apache.http.util.EntityUtils;import org.junit.Test;import com.juneyaoair.platform.model.api.EmailSendApiInput;import com.mars.fw.util.json.JsonUtil;public class EmailUploadAttApiControllerTest {    /**     * 模拟发送请求信息。     *     * @throws ClientProtocolException     * @throws IOException     */    @Test    public void uploadAttTest1() throws ClientProtocolException, IOException {        CloseableHttpClient httpClient = HttpClients.createDefault();        HttpPost post = new HttpPost(                "http://192.168.0.115:8080/emailplatform/email/uploadAtt");        ContentType contentType = ContentType.create("text/plain", "UTF-8");        MultipartEntityBuilder builder = MultipartEntityBuilder.create();        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);        builder.setCharset(Charset.forName("UTF-8"));        //本地上传的附件        File file1 = new File("E:\\123.txt");        builder.addBinaryBody("file", file1);        //账号密码        builder.addTextBody("userId", "xxx", contentType);        builder.addTextBody("password", "123456", contentType);        HttpEntity entity = builder.build();        post.setEntity(entity);        HttpResponse response = httpClient.execute(post);        System.out.println("xxxx code :"                + response.getStatusLine().getStatusCode());        String str = EntityUtils.toString(response.getEntity());        //打印返回 信息        System.out.println(str );        //字符串转JSONObject         JSONObject jsonResult = JSONObject.fromObject(str);        //根据key获取值        String ss = jsonResult.getString("attId");    }}
0 0
原创粉丝点击