ZATS Cookies"

From Documentation
(Created page with "{{Template:UnderConstruction}} {{ZATSEssentialsPageHeader}} Since 1.1.0 =Cookies= We usually perform the file downloading through the <javadoc>org.zkoss.zul.Filedownload</java...")
 
m (correct highlight (via JWB))
 
(43 intermediate revisions by one other user not shown)
Line 3: Line 3:
 
  Since 1.1.0
 
  Since 1.1.0
  
=Cookies=
+
== Cookie validation ==
 +
In order to provide handling the [http://www.ietf.org/rfc/rfc2965.txt HTTP cookies] for developers, ZATS Mimic introduces a group of methods on [http://www.zkoss.org/javadoc/latest/zats/org/zkoss/zats/mimic/Client.html 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:
  
We usually perform the file downloading through the <javadoc>org.zkoss.zul.Filedownload</javadoc> when some events occurred<ref>for more detail, please refer to [[ZK_Component_Reference/Essential_Components/Filedownload]]</ref>. The following is a typical example about downloading a file:
+
'''cookie.zul'''
 
+
<source lang="xml">
<source lang="java" high="4">
 
 
<zk>
 
<zk>
<button id="btn" label="download">
+
<div apply="CookieComposer">
<attribute name="onClick"><![CDATA[
+
<button id="change" label="change" />
org.zkoss.zul.Filedownload.save("foo.txt", "application/octet-stream");
+
</div>
]]>
 
</attribute>
 
</button>
 
 
</zk>
 
</zk>
 
</source>
 
</source>
  
Actually, the downloaded mechanism is a process with two steps. When you invoke <tt>save()</tt>, the <tt>Filedownload</tt> simply notifies ZK client engine of the downloaded URL. Then, the ZK client engine downloads such files according to the received URL.
+
'''CookieComposer.java'''
 +
<source lang="java" start="10" highlight="13, 16, 18">
 +
public class CookieComposer extends SelectorComposer<Component> {
 +
public void doAfterCompose(Component comp) throws Exception {
 +
super.doAfterCompose(comp);
 +
setCookie("foo", "bar");
 +
}
  
'''Notes'''
+
@Listen("onClick=#change")
<references/>
+
public void change() {
 +
setCookie("foo", "hello");
 +
}
  
== Download files in a ZATS Mimic test case ==
+
public void setCookie(String name, String value) {
 +
HttpServletResponse resp = (HttpServletResponse)Executions.getCurrent().getNativeResponse();
 +
resp.addCookie(new Cookie(name, value));
 +
}
 +
}
 +
</source>
 +
* '''Line 13''': This will add a cookie when beginning.
 +
* '''Line 16, 18''': It changes the cookie from server-side when the user clicking the button.
 +
 
 +
'''Test.java'''
 +
<source lang="java" start="10" highlight="13,14,15,16,17">
 +
@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"));
 +
}
 +
</source>
 +
* '''Line 13-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.
  
In order to simulate same behavior as ZK client engine doing, ZATS Mimic introduces the <tt>Resource</tt> interface. It represents a downloadable resource file at server. The typical steps for testing downloading are:
+
== Set cookie in a test case ==
# perform some operations through operation agents
+
Usually, developers use cookies to keep some information between individual connections. For example, it can trace activities of users or remember some useful information for users. ZATS Mimic lets us add, change or remove cookies through the <code>Client</code>. That is useful for testing above cases. The following is a simple example about using cookie to remind last read page number:
# check is there a downloadable resource through desktop agent
 
# fetch and verify the information or content of the resource
 
  
<source lang="java" high="8,9,10,11,12,13">
+
'''cookie.zul'''
@Test
+
<source lang="xml" highlight="3">
public void test() throws Exception {
+
<zk>
Zats.init(".");
+
<div apply="CookieComposer">
try {
+
<label id="msg" value="first time reading" />
DesktopAgent desktop = Zats.newClient().connect("/foo.zul");
+
</div>
Assert.assertNull(desktop.getDownloadable());
+
</zk>
 +
</source>
 +
* '''Line 3''': It reminds user of last read page number here.
  
desktop.query("#btn").click();
+
'''CookieComposer'''
Resource resource = desktop.getDownloadable();
+
<source lang="java" start="10" highlight="20,21">
Assert.assertNotNull(resource);
+
public class CookieComposer extends SelectorComposer<Component> {
Assert.assertEquals("hello.txt", resource.getName());
+
@Wire
String content = fetchContent(resource.getInputStream());
+
private Label msg;
Assert.assertEquals("Hello world!", content);
+
} finally {
+
public void doAfterCompose(Component comp) throws Exception {
Zats.end();
+
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());
 +
}
 +
}
 
}
 
}
 
}
 
}
 
</source>
 
</source>
 +
* '''Line 20-21''': This will show the last read page according to the cookie stored at client-side.
  
* '''Line 6, 9, 10''': Because ZATS Mimic handles the response from ZK application automatically, we can get the current downloadable resource file from the <tt>DesktopAgent.getDownloadable()</tt>. The method might return <tt>null</tt> when getting the downloadable resource, it indicates that there is no downloadable resource after the previous operation.
+
'''Test.java'''
 
+
<source lang="java" start="10" highlight="12, 13, 14">
* '''Line 11-13''': We can get more information from <tt>Resource</tt>, or fetch the content of resource file in binary through the input stream.
+
@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);
 +
}
 +
</source>
 +
* '''Line 12-14''': We can append a new cookie before connecting with a page for testing.
  
 
{{ZATSEssentialsPageFooter}}
 
{{ZATSEssentialsPageFooter}}

Latest revision as of 02:59, 20 January 2022

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 SelectorComposer<Component> {
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		setCookie("foo", "bar");
	}

	@Listen("onClick=#change")
	public void 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, 18: 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-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.

Set cookie in a test case

Usually, developers use cookies to keep some information between individual connections. For example, it can trace activities of users or remember some useful information for users. ZATS Mimic lets us add, change or remove cookies through the Client. That is useful for testing above cases. The following is a simple example about using cookie to remind last read page number:

cookie.zul

<zk>
	<div apply="CookieComposer">
		<label id="msg" value="first time reading" />
	</div>
</zk>
  • Line 3: It reminds user of last read page number here.

CookieComposer

public class CookieComposer extends SelectorComposer<Component> {
	@Wire
	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 20-21: This will show the last read page according to the cookie stored at client-side.

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-14: We can append a new cookie before connecting with a page for testing.



Last Update : 2022/01/20

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