个人学习使用:谨慎参考
1 Client类
import com.thoughtworks.gauge.Step;
import com.thoughtworks.gauge.Table;
import com.thoughtworks.gauge.TableRow;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by KSC on 2/7/2017.
*/
public class testClient {
//send get request
/**
*
* @param url destination address
* @param parametersTable request parameters
* @return remote response result
*/
@Step("the get method url is and parameters are ")
public static String sendGet(String url,Table parametersTable){
Map parameters &#61; new HashMap<>();
for(TableRow row : parametersTable.getTableRows()){
String key &#61; row.getCell("name");
String value &#61; row.getCell("age");
parameters.put(key,value);
}
for (String s: parameters.keySet()){
System.out.println(s&#43;"\t"&#43;parameters.get(s));
}
String result &#61; "";//返回的结果
BufferedReader in &#61; null;//读取响应的输入流
StringBuffer sf &#61; new StringBuffer();//存储参数
String params &#61; "";//编码后的参数
try{
if (parameters.size() &#61;&#61; 1){
for(String name : parameters.keySet()){
sf.append(name).append("&#61;").append(
java.net.URLEncoder.encode(parameters.get(name),"UTF-8")
);
}
params &#61; sf.toString();
}else{
for(String name : parameters.keySet()){
sf.append(name).append("&#61;").append(
java.net.URLEncoder.encode(parameters.get(name),"UTF-8")
).append("&");
}
String tempParams &#61; sf.toString();
params &#61; tempParams.substring(0,tempParams.length()-1);
System.out.println(params);
}
String fullUrl &#61; url &#43; "?" &#43; params;
System.out.println(fullUrl);
//创建URL对象
URL connUrl &#61; new URL(fullUrl);
//打开URL链接
HttpURLConnection httpConn &#61; (HttpURLConnection)connUrl.openConnection();
// 设置通用属性
httpConn.setRequestProperty("Accept","*/*");
httpConn.setRequestProperty("Connection","Keep-Alive");
httpConn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
//建立实际链接
httpConn.connect();
//响应头部的获取
Map> headers &#61; httpConn.getHeaderFields();
System.out.println("&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;");
//遍历头部所有字段
for(String key : headers.keySet()){
System.out.println(key&#43;"\t: \t"&#43;headers.get(key));
}
//定义bufferedReader输入流来读取URL的响应&#xff0c;并设置编码方式
in &#61; new BufferedReader(new InputStreamReader(httpConn.getInputStream(),"UTF-8"));
String line &#61; "";//读取返回的内容
while((line &#61; in.readLine()) !&#61; null){
result &#61; result&#43;line;
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(in !&#61; null){
in.close();
}
}catch(IOException ex){
ex.printStackTrace();
}
}
System.out.println("*****************************");
System.out.println(result);
System.out.println("&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;");
return result;
}
//send post request
&#64;Step("the post method url is and parameters are ")
public static String sendPost(String url,Table parametersTable){
Map parameters &#61; new HashMap<>();
for(TableRow row : parametersTable.getTableRows()){
String key &#61; row.getCell("name");
String value &#61; row.getCell("age");
parameters.put(key,value);
}
for (String s: parameters.keySet()){
System.out.println(s&#43;"\t\t"&#43;parameters.get(s));
}
System.out.println("---------------------");
String result &#61; "";
BufferedReader in &#61; null;
PrintWriter out &#61; null;//
StringBuffer sf &#61; new StringBuffer();
String params &#61; "";
try{
if (parameters.size() &#61;&#61; 1){
for(String name : parameters.keySet()){
sf.append(name).append("&#61;").append(
java.net.URLEncoder.encode(parameters.get(name),"UTF-8")
);
}
params &#61; sf.toString();
}else{
for(String name : parameters.keySet()){
sf.append(name).append("&#61;").append(
java.net.URLEncoder.encode(parameters.get(name),"UTF-8")
).append("&");
}
String tempParams &#61; sf.toString();
params &#61; tempParams.substring(0,tempParams.length()-1);
}
URL connUrl &#61; new URL(url);
HttpURLConnection httpConn &#61; (HttpURLConnection)connUrl.openConnection();
httpConn.setRequestProperty("Accept","*/*");
httpConn.setRequestProperty("Connection","Keep-Alive");
httpConn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
//设置post方式
httpConn.setDoInput(true);
httpConn.setDoOutput(true);
//获取HttpUrlConnection对象对应的输出流
out &#61; new PrintWriter(httpConn.getOutputStream());
//发送请求参数
out.write(params);
//flush(冲洗)输出流中的缓冲
out.flush();
in &#61; new BufferedReader(new InputStreamReader(httpConn.getInputStream(),"UTF-8"));
String line &#61; "";
while((line &#61; in.readLine()) !&#61; null){
result &#61; result&#43;line;
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(out !&#61; null){
out.close();
}
if(in !&#61; null){
in.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
System.out.println("*****************************");
System.out.println(result);
System.out.println("&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;");
return result;
}
}
2.测试类
用自动化测试工具gauge进行测试
Use http request for creating user
&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;
Created by KSC on 2/7/2017
This is an executable specification file which follows markdown syntax.
Every heading in this file denotes a scenario. Every bulleted point denotes a step.
call on url with method get
----------------
tags: get
*the get method url is "http://localhost:8080" and parameters are
|name|age|
|----|---|
|tom|20|
|maike|22|
call on url with method post
---------------------------
tags: post
*the post method url is "https://www.baidu.com/" and parameters are
|name|age|
|----|---|
|marry|20|
|rose|22|