ZATS Cookies"

From Documentation
Line 14: Line 14:
 
</zk>
 
</zk>
 
</source>
 
</source>
 
  
 
'''CookieComposer.java'''
 
'''CookieComposer.java'''
Line 36: Line 35:
 
* '''Line 13''': This will add a cookie when beginning.
 
* '''Line 13''': This will add a cookie when beginning.
 
* '''Line 16, 17''': It changes the cookie from server-side when the user clicking the button.
 
* '''Line 16, 17''': It changes the cookie from server-side when the user clicking the button.
 
  
 
'''Test.java'''
 
'''Test.java'''
Line 57: Line 55:
  
 
'''cookie.zul'''
 
'''cookie.zul'''
<source lang="xml">
+
<source lang="xml" high="3">
 
<zk>
 
<zk>
 
<div apply="CookieComposer">
 
<div apply="CookieComposer">
<label id="msg" value="first time reading"/>
+
<label id="msg" value="first time reading" />
 
</div>
 
</div>
 
</zk>
 
</zk>
 
</source>
 
</source>
 
+
'''Line 3''': It will remind user of last read page number.
  
 
'''CookieComposer'''
 
'''CookieComposer'''
<source lang="java" start="10" high="">
+
<source lang="java" start="10" high="17, 18">
 
private Label msg;
 
private Label msg;
 
public void doAfterCompose(Component comp) throws Exception {
 
public void doAfterCompose(Component comp) throws Exception {
Line 81: Line 79:
 
}
 
}
 
</source>
 
</source>
 +
'''Line 17, 18: It will show the last read page according to the stored cookie.
  
 
'''Test.java'''
 
'''Test.java'''
<source lang="java" start="10" high="13, 14">
+
<source lang="java" start="10" high="12, 13, 14">
 
@Test
 
@Test
 
public void test() {
 
public void test() {
 
Client client = Zats.newClient();
 
Client client = Zats.newClient();
 
client.setCookie("page", "99");
 
client.setCookie("page", "99");
DesktopAgent desktop = client.connect("/~./basic/cookie3.zul");
+
DesktopAgent desktop = client.connect("/cookie.zul");
 
String msg = desktop.query("#msg").as(Label.class).getValue();
 
String msg = desktop.query("#msg").as(Label.class).getValue();
 
Assert.assertEquals("last read page: 99", msg);
 
Assert.assertEquals("last read page: 99", msg);
 
}
 
}
 
</source>
 
</source>
* '''Line 13, 14''': We can directly append a new cookie or replace old value with the same name in the test code. If we pass an cookie with <tt>null</tt> value, it indicates that removing the specific cookie. Notice that the name of cookie can not start with '''$''' character, it would be a reserved name.
+
* '''Line 12, 13, 14''': We can append a new cookie before connecting with a ZK application. It can validate the business logic of the ZK application.
  
 
'''Notes'''
 
'''Notes'''

Revision as of 07:41, 11 June 2012

WarningTriangle-32x32.png This page is under construction, so we cannot guarantee the accuracy of the content!

aowang



Since 1.1.0

Cookie validation

In order to provide handling the HTTP cookies for developers, ZATS Mimic introduces a group of methods on Client. ZATS Mimic seamlessly maintains cookies after connecting with a ZK application. We can read the current cookies and verify the behavior of the ZK application. The following is a typical example about cookie validation:

cookie.zul

<zk>
	<div apply="CookieComposer">
		<button id="change" label="change" />
	</div>
</zk>

CookieComposer.java

public class CookieComposer extends GenericForwardComposer {
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		setCookie("foo", "bar");
	}

	public void onClick$change() {
		setCookie("foo", "hello");
	}

	public void setCookie(String name, String value) {
		HttpServletResponse resp = (HttpServletResponse) Executions.getCurrent().getNativeResponse();
		resp.addCookie(new Cookie(name, value));
	}
}
  • Line 13: This will add a cookie when beginning.
  • Line 16, 17: It changes the cookie from server-side when the user clicking the button.

Test.java

@Test
public void Test() {
	Client client = Zats.newClient();
	DesktopAgent desktop = client.connect("/cookie.zul");
	Assert.assertEquals("bar", client.getCookie("foo"));
	Assert.assertEquals(null, client.getCookie("not existed"));
	desktop.query("#change").click();
	Assert.assertEquals("hello", client.getCookie("foo"));
}
  • Line 13, 14, 15: After connected to a ZUL page, we can get the cookies and verify them.
  • Line 16, 17: ZATS Mimic maintains all cookies during any operations.

Cookie handling from client-side

Besides applying cookies from the server-side, developers can also handle cookies from the client-side. In a ZK application, we can achieve the above behavior through the Client-side programming[1]. Because ZATS Mimic doesn't perform the JavaScript code, it lets developers add, modify or remove cookies through Client in test code to simulate the Client-side programming.

cookie.zul

<zk>
	<div apply="CookieComposer">
		<label id="msg" value="first time reading" />
	</div>
</zk>

Line 3: It will remind user of last read page number.

CookieComposer

private Label msg;
public void doAfterCompose(Component comp) throws Exception {
	super.doAfterCompose(comp);
	Execution exec = Executions.getCurrent();
	Cookie[] cookies = ((HttpServletRequest) exec.getNativeRequest()).getCookies();
	if (cookies != null) {
		for (Cookie cookie : cookies) {
			if ("page".equals(cookie.getName()))
				msg.setValue("last read page: " + cookie.getValue());
		}
	}
}

Line 17, 18: It will show the last read page according to the stored cookie.

Test.java

@Test
public void test() {
	Client client = Zats.newClient();
	client.setCookie("page", "99");
	DesktopAgent desktop = client.connect("/cookie.zul");
	String msg = desktop.query("#msg").as(Label.class).getValue();
	Assert.assertEquals("last read page: 99", msg);
}
  • Line 12, 13, 14: We can append a new cookie before connecting with a ZK application. It can validate the business logic of the ZK application.

Notes

  1. for more detail. Please refer to ZK_Client-side_Reference



Last Update : 2012/06/11

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.