Cookie Handling"

From Documentation
(Created page with "{{Template:UnderConstruction}} {{ZATSEssentialsPageHeader}} Since 1.1.0 == Cookie validation == In order to provide handling the [http://www.ietf.org/rfc/rfc2965.txt HTTP cooki...")
 
m (correct highlight (via JWB))
 
(10 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{Template:UnderConstruction}}
 
 
{{ZATSEssentialsPageHeader}}
 
{{ZATSEssentialsPageHeader}}
 +
__TOC__
 
  Since 1.1.0
 
  Since 1.1.0
  
== Cookie validation ==
+
= 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:
+
 
 +
In order to provide the handling of [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. It can read the current cookies and verify the behaviors of the ZK application. Following is a typical example of cookie validation:
  
 
'''cookie.zul'''
 
'''cookie.zul'''
Line 14: Line 15:
 
</zk>
 
</zk>
 
</source>
 
</source>
 +
  
 
'''CookieComposer.java'''
 
'''CookieComposer.java'''
<source lang="java" start="10" high="13, 16, 18">
+
<source lang="java" start="10" highlight="13, 16, 18">
 
public class CookieComposer extends SelectorComposer<Component> {
 
public class CookieComposer extends SelectorComposer<Component> {
 
public void doAfterCompose(Component comp) throws Exception {
 
public void doAfterCompose(Component comp) throws Exception {
Line 34: Line 36:
 
}
 
}
 
</source>
 
</source>
* '''Line 13''': This will add a cookie when beginning.
+
* '''Line 13''': This will add a cookie at the beginning.
* '''Line 16, 18''': It changes the cookie from server-side when the user clicking the button.
+
* '''Line 16, 18''': Changes the cookie from server-side when the user clicks the button.
 +
 
  
 
'''Test.java'''
 
'''Test.java'''
<source lang="java" start="10" high="13,14,15,16,17">
+
<source lang="java" start="10" highlight="13,14,15,16,17">
 
@Test
 
@Test
 
public void Test() {
 
public void Test() {
Line 49: Line 52:
 
}
 
}
 
</source>
 
</source>
* '''Line 13-15''': After connected to a ZUL page, we can get the cookies and verify them.
+
* '''Line 13-15''': After connecting to a ZUL page, we can get the cookies and verify them.
 
* '''Line 16-17''': ZATS Mimic maintains all cookies during any operations.
 
* '''Line 16-17''': ZATS Mimic maintains all cookies during any operations.
  
== Set cookie in a test case ==
+
= 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 <tt>Client</tt>. That is useful for testing above cases. The following is a simple example about using cookie to remind last read page number:
+
Usually, developers use cookies to retain some information between individual connections. For example, it can trace activities of users or remember some useful information for users. ZATS Mimic allows us to add, change or remove cookies through the <code>Client</code> which comes in very handy for testing the above cases. Following is a simple example on using a cookie to remind a user of his/her last read page number:
  
 
'''cookie.zul'''
 
'''cookie.zul'''
<source lang="xml" high="3">
+
<source lang="xml" highlight="3">
 
<zk>
 
<zk>
 
<div apply="CookieComposer">
 
<div apply="CookieComposer">
Line 63: Line 66:
 
</zk>
 
</zk>
 
</source>
 
</source>
* '''Line 3''': It reminds user of last read page number here.
+
* '''Line 3''': Reminds user of his/her last read page number here.
 +
 
  
 
'''CookieComposer'''
 
'''CookieComposer'''
<source lang="java" start="10" high="20,21">
+
<source lang="java" start="10" highlight="20,21">
 
public class CookieComposer extends SelectorComposer<Component> {
 
public class CookieComposer extends SelectorComposer<Component> {
 
@Wire
 
@Wire
Line 85: Line 89:
 
</source>
 
</source>
 
* '''Line 20-21''': This will show the last read page according to the cookie stored at client-side.
 
* '''Line 20-21''': This will show the last read page according to the cookie stored at client-side.
 +
  
 
'''Test.java'''
 
'''Test.java'''
<source lang="java" start="10" high="12, 13, 14">
+
<source lang="java" start="10" highlight="12, 13, 14">
 
@Test
 
@Test
 
public void test() {
 
public void test() {
Line 99: Line 104:
 
* '''Line 12-14''': We can append a new cookie before connecting with a page for testing.
 
* '''Line 12-14''': We can append a new cookie before connecting with a page for testing.
  
 +
 +
 +
{{ZATSEssentialsPageHeader}}
 
{{ZATSEssentialsPageFooter}}
 
{{ZATSEssentialsPageFooter}}

Latest revision as of 02:54, 18 January 2022


Since 1.1.0

Cookie Validation

In order to provide the handling of HTTP cookies for developers, ZATS Mimic introduces a group of methods on Client. ZATS Mimic seamlessly maintains cookies after connecting with a ZK application. It can read the current cookies and verify the behaviors of the ZK application. Following is a typical example of 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 at the beginning.
  • Line 16, 18: Changes the cookie from server-side when the user clicks 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 connecting 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 retain some information between individual connections. For example, it can trace activities of users or remember some useful information for users. ZATS Mimic allows us to add, change or remove cookies through the Client which comes in very handy for testing the above cases. Following is a simple example on using a cookie to remind a user of his/her last read page number:

cookie.zul

<zk>
	<div apply="CookieComposer">
		<label id="msg" value="first time reading" />
	</div>
</zk>
  • Line 3: Reminds user of his/her 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/18

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