你可以使用HtmlUnit接收Cookie或發送Cookie,以 Cookie的範例來說,若想要測試勾選「自動登入」與否,User類別的Servlet是否發送Cookie,則可以如下:
package test.cc.openhome;
import static org.junit.Assert.*;
import java.net.URL;
import java.util.Arrays;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.HttpMethod;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.util.NameValuePair;
public class LoginDoTest {
private WebClient webClient;
private WebRequest request;
@Before
public void setUp() throws Exception {
webClient = new WebClient(BrowserVersion.FIREFOX_3);
request = new WebRequest(
new URL("http://localhost:8080/CookieDemo/login.do"),
HttpMethod.POST);
request.setCharset("UTF-8");
}
@After
public void tearDown() throws Exception {
webClient.closeAllWindows();
}
@Test
public void testLogin() throws Exception {
request.setRequestParameters(Arrays.asList(
new NameValuePair("user", "caterpillar"),
new NameValuePair("passwd", "123456")));
HtmlPage page = webClient.getPage(request);
assertEquals("caterpillar已登入", page.getElementsByTagName("h1")
.get(0).getFirstChild().getNodeValue());
assertNull(webClient.getCookieManager().getCookie("user"));
}
@Test
public void testLoginWithAuto() throws Exception {
request.setRequestParameters(Arrays.asList(
new NameValuePair("user", "caterpillar"),
new NameValuePair("passwd", "123456"),
new NameValuePair("login", "auto")));
webClient.getPage(request);
assertEquals("caterpillar", webClient.getCookieManager()
.getCookie("user").getValue());
}
}
你可以從WebClient的getCookieManager()取得CookieManager實例,CookieManager管理Cookie的接收與發送,在同一個WebClient還沒關閉期間所接收到的Cookie,再度發送請求時也會一併送出。
如果在新建立WebClient後,想要發送Cookie給伺服端,則可以建立Cookie實例,透過WebClient取得的CookieManager之addCookie()新增。例如,測試Index的Servlet在接收到Cookie時,能否自動登入,可以如下:
package test.cc.openhome;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.Calendar;
import org.apache.http.client.params.CookiePolicy;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.CookieManager;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.util.Cookie;
public class IndexViewTest {
private WebClient webClient;
@Before
public void setUp() throws Exception {
webClient = new WebClient(BrowserVersion.FIREFOX_3);
}
@After
public void tearDown() throws Exception {
webClient.closeAllWindows();
}
@Test
public void testNoCookie() throws Exception {
HtmlPage page = webClient.getPage(
"http://localhost:8080/CookieDemo/index.view");
assertTrue(page.getUrl().toString().endsWith("login.html"));
}
@Test
public void testWithCookie() throws Exception {
Calendar c = Calendar.getInstance();
c.add(Calendar.DAY_OF_WEEK, 7);
Cookie cookie = new Cookie("localhost", "user", "caterpillar",
"/CookieDemo", c.getTime(), false);
CookieManager cookieManager = webClient.getCookieManager();
cookieManager.addCookie(cookie);
HtmlPage page = webClient.getPage(
"http://localhost:8080/CookieDemo/index.view");
assertEquals("caterpillar已登入", page.getElementsByTagName("h1")
.get(0).getFirstChild().getNodeValue());
}
}
如果有兩個以上的WebClient需要共用同一個CookieManager,則可以新建CookieManager,並透過WebClient的setCookieManager()設定。