测试场景
consumer | provider |
---|
发送请求 | 接收请求并设置cookie |
接收响应并再次发送请求 | 查看cookie是否有 |
接收响应并再次发送请求 | 环境出现变化,对cookie中的内容进行修改并返回 |
接收响应并再次发送请求 | 获取到新的cookie中的值 |
cookie测试
package com.example.cookiedemotest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class CookiedemotestApplication {
public static void main(String[] args) {
SpringApplication.run(CookiedemotestApplication.class, args);
}
}
package com.example.cookiedemotest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Map;
import static java.lang.Thread.sleep;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = CookiedemotestApplication.class)
public class cookieTest {
protected final Logger logger = LoggerFactory.getLogger(cookieTest.class);
@Test
public void testCookie(){
HttpUtils httpUtil = new HttpUtils();
try {
String url = "http://xx.xx.xx.xxx:xxx/testCookie";
for (int i= 0 ; i<= 50; i++){
String ret = httpUtil.post(url,"test");
logger.info("result:"+ret);
}
}
catch (Exception e){
logger.info("发生了异常:"+e.getMessage());
return;
}
return;
}
}
package com.example.cookiedemotest;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.http.client.CookieStore;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.Charset;
public class HttpUtils {
protected final Logger logger = LoggerFactory.getLogger(HttpUtils.class);
private static CookieStore cookieStore;
public String get(String url) {
String result = null;
CloseableHttpResponse response = null;
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
URIBuilder builder = new URIBuilder(url);
URI uri = builder.build();
HttpGet httpGet = new HttpGet(uri);
response = httpclient.execute(httpGet);
if (response.getStatusLine().getStatusCode() == 200) {
result = EntityUtils.toString(response.getEntity(), "UTF-8");
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public String post(String url, String jsonStr) {
DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.setCookieStore(this.cookieStore);
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-type", "application/json;charset=utf-8");
httpPost.setHeader("Connection", "Close");
StringEntity entity = new StringEntity(jsonStr, Charset.forName("UTF-8"));
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
CloseableHttpResponse httpResponse;
String result = null;
try {
httpResponse = httpClient.execute(httpPost);
Object object = httpResponse.getHeaders("set-cookie").toString();
logger.info("set-cookie:"+object);
this.cookieStore = httpClient.getCookieStore();
result = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}