package com.ifttt.httpclient;
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.JSONArray;
import org.json.JSONObject;
import org.skyscreamer.jsonassert.JSONAssert;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.io.IOException;
import java.util.Locale;
import java.util.ResourceBundle;
public class iftttlogs {
private String url;
private ResourceBundle bundle;
private CookieStore store;
@BeforeTest
public void beforeTest(){
bundle = ResourceBundle.getBundle("application", Locale.CHINA);
url = bundle.getString("test.url");
}
/*
//获取cookies,如果没有cookie,那就用不到这段代码
@Test
public void getCookies() throws IOException {
String result;
//从配置文件中拼接URL
String uri = bundle.getString("iftttlog.post.url");
String testUrl = this.url+uri;
//测试逻辑代码书写
HttpGet get = new HttpGet(testUrl);
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(get);
result = EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println(result);
//获取cookies信息
this.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
public void testPostMethod() throws IOException {
String uri = bundle.getString("iftttlog.post.url");
String testUrl = this.url + uri;
//声明一个Client对象,用来进行方法的执行
DefaultHttpClient client = new DefaultHttpClient();
//声明一个方法,这个方法就是post方法
HttpPost post = new HttpPost(testUrl);
//添加参数
JSONObject param = new JSONObject();
param.put("familyid","1234567890");
param.put("limit","20");
//设置请求头信息 设置header
post.setHeader("content-type","application/json");
//将参数信息添加到方法中
StringEntity entity = new StringEntity(param.toString(),"utf-8");
entity.setContentType("application/json");
post.setEntity(entity);
//声明一个对象来进行响应结果的存储
String result;
//设置cookies信息
// client.setCookieStore(this.store);
//执行post方法
HttpResponse response = client.execute(post);
//获取响应结果
result = EntityUtils.toString(response.getEntity());
System.out.println(result);
//处理结果,就是判断返回结果是否符合预期
//将返回的响应结果字符串转化成为json对象
JSONObject resultJson = new JSONObject(result);
//获取到结果值
String retCode = (String) resultJson.get("retCode");
String retInfo = (String) resultJson.get("retInfo");
//JSONObject data = resultJson.getJSONObject("data");
JSONArray data = resultJson.getJSONArray("data");
String datajob = data.toString();
String expectdata = "[{\"id\":\"1a68b24cf51f4377bf576bb55401fd7d\",\n" +
" \"sceneId\":\"1a68b24cf51f4377bf576bb55401fd7d\",\"sceneName\":\"testbobo\",\n" +
" \"sceneAlias\":\"测试bobo\",\n" +
" \"sceneDesc\":\"122\",\n" +
" \"userId\":\"renyaojing_userId\",\n" +
" \"familyId\":\"1234567890\",\n" +
" \"operationType\":\"1\",\n" +
" \"operationStatus\":\"true\",\n" +
" \"operationResult\":\"open success\",\n" +
" \"time\":\"1530467982731\",\n" +
" \"sn\":\"sn1111\",\n" +
" \"triggerType\":\"platform\",\n" +
" \"status\":\"success\"}]";
//具体的判断返回结果的值
Assert.assertEquals("00000",retCode);
Assert.assertEquals("success",retInfo);
JSONAssert.assertEquals(expectdata,datajob,false);
}
}