测试框架httpclent 4.HttpClient Post方法实现
startupWithCookies.json
[
{
"description":"这是一个会返回cookies信息的get请求",
"request":{
"uri":"/getCookies",
"method":"get" },
"response":{
"cookies":{
"login":"true"
},
"text":"恭喜获得cookies信息成功"
} }, {
"description":"这是一个带cookies的请求",
"request":{
"uri":"/get/with/cookies",
"method":"get",
"cookies":{
"login":"true"
}
},
"response":{
"text":"这是一个需要携带cookies信息才能访问的get请求"
}
}, {
"description":"这是一个带cookies的post请求",
"request":{
"uri":"/post/with/cookies",
"method":"post",
"cookies":{
"login":"true"
},
"json":{
"name":"huhanshan",
"age":"18"
}
},
"response":{
"status":200,
"json":{
"huhanshan":"success",
"status":"1"
}
}
} ]
新建一个 MyCookiesForPost.java 类
package com.course.httpclient.cookies; import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test; import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle; public class MyCookiesForPost { private String url;
private ResourceBundle bundle; //用来存储cookies信息的变量
private CookieStore store; @BeforeTest
public void beforeTest(){
bundle = ResourceBundle.getBundle("application",Locale.CHINA);
url = bundle.getString("test.url"); } @Test
public void testGetGookies() throws IOException {
String result;
String uri = bundle.getString("getCookies.uri");
HttpGet get = new HttpGet(this.url + uri);
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(get); result = EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println(result); //获取cookies的信息,因为cookie里面不只是一个,他是一个cookie类型的list
store = client.getCookieStore();
List<Cookie> cookieList = store.getCookies(); for(Cookie cookie : cookieList){
String name = cookie.getName();
String value = cookie.getValue();
System.out.println("name = "+name+",value = "+value);
}
} @Test(dependsOnMethods = "testGetGookies")
public void testPostMethod() throws IOException {
String uri = bundle.getString("test.post.with.cookies");
//拼接最终的地址
String testUrl = url+uri; //声明一个方法post
HttpPost post = new HttpPost(testUrl); //声明一个Client对象
DefaultHttpClient client = new DefaultHttpClient(); //添加参数
JSONObject param = new JSONObject();
param.put("name","huhanshan");
param.put("age","18"); //设置请求头信息 header
post.setHeader("content-type","application/json"); //将参数信息添加到方法中
StringEntity entity = new StringEntity(param.toString(),"utf-8");
post.setEntity(entity); //声明一个对象来进行响应结果的存储
String result; //设置cookies信息
client.setCookieStore(store); //执行post方法
HttpResponse response = client.execute(post); //获取响应状态码
int statusCode = response.getStatusLine().getStatusCode(); if(statusCode==200){
//获取响应结果
result = EntityUtils.toString(response.getEntity(),"uft-8"); //将返回的响应结果字符串转化为json对象
JSONObject resultJson = new JSONObject(result); //判断返回结果的值
String status = resultJson.getString("status");
String success = resultJson.getString("huhanshan");
Assert.assertEquals("1",status,"status的类型是"+status.getClass());
Assert.assertEquals("success",success);
System.out.println(result);
}
} }
首先在terminal运行命令: java -jar ./moco-runner-0.12.0-standalone.jar http -p 8888 -c startupWithCookies.json
测试框架httpclent 4.HttpClient Post方法实现的更多相关文章
- 测试框架httpclent 2.配置优化方法
优化就是为了使代码看起来更简便,如果代码里面的每一个请求都写一次url,那么整体代码看起来很乱,而且一旦某个服务器的端口号或者域名有变动,那么所有的url都需要改变,成本太大.为了让代码看起来更简便, ...
- 测试框架httpclent 1.HttpClient简介及第一个demo
httpclient就是一个模拟 发送http请求的一个工具. 首先在pom.xml文件里面添加工具类 <dependencies> <dependency> <grou ...
- Robot Framework测试框架用例脚本设计方法
Robot Framework介绍 Robot Framework是一个通用的关键字驱动自动化测试框架.测试用例以HTML,纯文本或TSV(制表符分隔的一系列值)文件存储.通过测试库中实现的关键字驱动 ...
- 测试框架httpclent 3.获取cookie的信息,然后带cookies去发送请求
在properties文件里面: startupWithCookies.json [ { "description":"这是一个会返回cookies信息的get请求&qu ...
- HttpClient测试框架
HttpClient是模拟Http协议客户端请求的一种技术,可以发送Get/Post等请求. 所以在学习HttpClient测试框架之前,先来看一下Http协议请求,主要看请求头信息. 如何查看HTT ...
- python利用unittest测试框架组织测试用例的5种方法
利用unittest测试框架可以编写测试用例,执行方式分两大类:利用main方法和利用testsuite,其中利用测试套件来组织测试用例可以有4种写法. 在此之前,先了解几个概念 TestCase:所 ...
- Pytest测试框架(二):pytest 的setup/teardown方法
PyTest支持xUnit style 结构, setup() 和 teardown() 方法用于初始化和清理测试环境,可以保证测试用例的独立性.pytest的setup/teardown方法包括:模 ...
- 【Android自动化】unittest测试框架关于用例执行的几种方法
# -*- coding:utf-8 -*- import unittest class test(unittest.TestCase): def setUp(self): print 'This i ...
- unittest测试框架,HTMLTestReportCN模块生成的测试报告中展示用例说明的配置方法
1.前言 想要生成的html测试报告中展示每个测试用例的说明信息,方便了解测试案例的测试点或者其他信息,目前知道的有2种 2.方法介绍 * 方法1: 要添加说明的测试用例,将说明信息用3个引号包裹起来 ...
随机推荐
- SQLServer之创建视图
视图定义 视图是一个虚拟的表,是一个表中的数据经过某种筛选后的显示方式,视图由一个预定义的查询select语句组成. 使用SSMS数据库管理工具创建视图 1.连接数据库,选择数据库,展开数据库-> ...
- 取消导航栏navigationBar的半透明/毛玻璃效果
iOS 7.0以上的系统,导航栏默认有毛玻璃效果,遮住了颜色 原因是7.0以上的系统,导航栏默认有毛玻璃效果,遮住了颜色,取消掉这个效果就行了. if( ([[[UIDevice currentDev ...
- 本地系统服务例程:Nt和Zw系列函数
Windows本地操作系统服务API由一系列以Nt或Zw为前缀的函数实现的,这些函数以内核模式运行,内核驱动可以直接调用这些函数,而用户层程序只能通过系统进行调用.通常情况下用户层应用程序不会直接调用 ...
- Java文件下载:如何编码文件名称以及如何设置HttpServletResponse
在下载文件时,经常遇到文件名乱码等问题. 本文说明如何编码文件名,以及如何设置HttpServletResponse对象. 1,如何编码文件名 String userAgent = request.g ...
- C++调用Opencv实践中遇到的问题备忘录
1.编写一个显示图片的项目,但显示的图片全灰色. 原因:需要在imshow()函数前加一个namedWindow()函数.https://blog.csdn.net/mao_hui_fei/artic ...
- php开发微信APP支付接口
之前在开发APP中用到了微信支付,因为是第一次用,所以中途也遇到了好多问题,通过查看文档和搜集资料,终于完成了该功能的实现.在这里简单分享一下后台php接口的开发实例. 原文地址:代码汇个人博客 ht ...
- LeetCode算法题-Heaters(Java实现)
这是悦乐书的第239次更新,第252篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第106题(顺位题号是475).冬天来了!您在比赛期间的第一份工作是设计一个固定温暖半径 ...
- IDF-cookie欺骗
原题链接:http://ctf.idf.cn/game/web/40/index.php 进入题目,发现一个长字符串,放到md5.base64均无意义. 观察地址栏,发现有两个参数,line和file ...
- HBase Client JAVA API
旧 的 HBase 接口逻辑与传统 JDBC 方式很不相同,新的接口与传统 JDBC 的逻辑更加相像,具有更加清晰的 Connection 管理方式. 同时,在旧的接口中,客户端何时将 Put 写到服 ...
- realm swift调研--草稿
realm swift调研: After you have added the object to the Realm you can continue using it, and all chang ...