1、測試類

package httpclient; import org.apache.http.HttpResponse; import org.apache.http.client.CookieStore; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.cookie.Cookie; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; 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 MyCookiesForGet { 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"); } //沒有cookie @Test public void testGetCookies() throws IOException { String result; //從配置文件中拼接測試url String uri = bundle.getString("getCookees.uri"); String testUrl = this.url+uri; HttpGet get = new HttpGet(testUrl); HttpClient client = new DefaultHttpClient(); HttpResponse httpResponse= client.execute(get); result = EntityUtils.toString(httpResponse.getEntity()); System.out.println(result); } //獲取cookie @Test public void testGetCookies1() throws IOException { String result; //從配置文件中拼接測試url String uri = bundle.getString("getCookees.uri"); String testUrl = this.url+uri; HttpGet get = new HttpGet(testUrl); DefaultHttpClient client = new DefaultHttpClient(); HttpResponse httpResponse= client.execute(get); result = EntityUtils.toString(httpResponse.getEntity()); System.out.println(result); //獲取cookie信息 this.store = client.getCookieStore(); List<Cookie> cookieList = store.getCookies(); for (Cookie cookie:cookieList ) { String name = cookie.getName(); String value = cookie.getValue(); System.out.println("cookie name"+name+"cookie value"+value); } } @Test(dependsOnMethods = "testGetCookies1") public void testGetWithCookies() throws IOException { String uri = bundle.getString("test.get.with.cookies"); String testUrl = this.url+uri; HttpGet get = new HttpGet(testUrl); DefaultHttpClient client = new DefaultHttpClient(); //設置cookies信息 client.setCookieStore(this.store); HttpResponse response = client.execute(get); //獲取響應的狀態碼 int statusCode = response.getStatusLine().getStatusCode(); System.out.println("statusCode="+statusCode); if (statusCode==200){ String result = EntityUtils.toString(response.getEntity()); System.out.println(result); } } }
2、配置文件:sources/application.properties

test.url=http://127.0.0.1:8888 dev.url=http://127.0.0.1:8888 getCookees.uri=/getCookies test.get.with.cookies=/get/with/cookies test.post.with.cookies=/post/with/cookies login=/login
3、Moco的mock文件

[ { "description":"這是一個會返回cookies信息的get請求", "request":{ "uri":"/getCookies", "method":"get" }, "response":{ "cookies":{ "login":"true" }, "text":"恭喜你獲得cookies信息成功" } }, { "description":"這是一個帶cookies信息的get請求", "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":"wangkc", "age":"18" } }, "response":{ "status":200, "json":{ "huhansan":"success", "status":"1" } } } ]
4、mock啟動命令:java -jar ./moco-runner-0.11.0-standalone.jar http -p 8888 -c startupWithCoolies.json