1+ /*
2+ * Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+ package org .ocpsoft .rewrite .faces .jsession ;
17+
18+ import static org .junit .Assert .assertThat ;
19+
20+ import java .net .URL ;
21+
22+ import org .hamcrest .Matchers ;
23+ import org .jboss .arquillian .container .test .api .Deployment ;
24+ import org .jboss .arquillian .junit .Arquillian ;
25+ import org .jboss .arquillian .test .api .ArquillianResource ;
26+ import org .jboss .shrinkwrap .api .spec .WebArchive ;
27+ import org .junit .Ignore ;
28+ import org .junit .Test ;
29+ import org .junit .runner .RunWith ;
30+ import org .ocpsoft .rewrite .faces .annotation .RewriteFacesAnnotationsTest ;
31+ import org .ocpsoft .rewrite .test .RewriteTest ;
32+ import org .ocpsoft .rewrite .test .RewriteTestBase ;
33+ import org .openqa .selenium .By ;
34+ import org .openqa .selenium .WebDriver ;
35+ import org .openqa .selenium .htmlunit .HtmlUnitDriver ;
36+
37+ import com .gargoylesoftware .htmlunit .WebClient ;
38+
39+ @ RunWith (Arquillian .class )
40+ public class JSessionIdTest extends RewriteTestBase
41+ {
42+
43+ @ Deployment (testable = false )
44+ public static WebArchive getDeployment ()
45+ {
46+ return RewriteTest .getDeployment ()
47+ .addAsLibrary (RewriteFacesAnnotationsTest .getRewriteAnnotationArchive ())
48+ .addAsLibrary (RewriteFacesAnnotationsTest .getRewriteFacesArchive ())
49+ .addClass (JSessionIdBean .class )
50+ .addAsWebResource ("jsessionid.xhtml" , "test.xhtml" );
51+ }
52+
53+ @ ArquillianResource
54+ private URL baseUrl ;
55+
56+ @ Test
57+ public void withCookiesAndStandardOutcome () throws Exception
58+ {
59+
60+ // GIVEN a browser with cookies enabled
61+ WebDriver driver = createBrowserAndLoadPage (true );
62+ assertThat (driver .getPageSource (), Matchers .not (Matchers .containsString ("jsessionid" )));
63+
64+ // WHEN a JSF action redirects with a standard outcome
65+ driver .findElement (By .id ("form:standardOutcome" )).click ();
66+
67+ // THEN the browser should be redirected
68+ assertThat (driver .getCurrentUrl (), Matchers .containsString ("redirected=true" ));
69+
70+ // AND there should be no jsessionid in the URL
71+ assertThat (driver .getCurrentUrl (), Matchers .not (Matchers .containsString ("jsessionid" )));
72+
73+ }
74+
75+ @ Test
76+ public void withCookiesAndNavigateOutcome () throws Exception
77+ {
78+
79+ // GIVEN a browser with cookies enabled
80+ WebDriver driver = createBrowserAndLoadPage (true );
81+ assertThat (driver .getPageSource (), Matchers .not (Matchers .containsString ("jsessionid" )));
82+
83+ // WHEN a JSF action redirects with a standard outcome
84+ driver .findElement (By .id ("form:navigateOutcome" )).click ();
85+
86+ // THEN the browser should be redirected
87+ assertThat (driver .getCurrentUrl (), Matchers .containsString ("redirected=true" ));
88+
89+ // AND there should be no jsessionid in the URL
90+ assertThat (driver .getCurrentUrl (), Matchers .not (Matchers .containsString ("jsessionid" )));
91+
92+ }
93+
94+ @ Test
95+ // see: https://github.com/ocpsoft/rewrite/issues/147
96+ @ Ignore
97+ public void withoutCookiesAndStandardOutcome () throws Exception
98+ {
99+
100+ // GIVEN a browser with cookies disabled
101+ WebDriver driver = createBrowserAndLoadPage (false );
102+ assertThat (driver .getPageSource (), Matchers .containsString ("jsessionid" ));
103+
104+ // WHEN a JSF action redirects with a standard outcome
105+ driver .findElement (By .id ("form:standardOutcome" )).click ();
106+
107+ // THEN the browser should be redirected
108+ assertThat (driver .getCurrentUrl (), Matchers .containsString ("redirected=true" ));
109+
110+ // AND there should be a jsessionid in the URL
111+ assertThat (driver .getCurrentUrl (), Matchers .containsString ("jsessionid" ));
112+
113+ }
114+
115+ @ Test
116+ // see: https://github.com/ocpsoft/rewrite/issues/147
117+ @ Ignore
118+ public void withoutCookiesAndNavigateOutcome () throws Exception
119+ {
120+
121+ // GIVEN a browser with cookies disabled
122+ WebDriver driver = createBrowserAndLoadPage (false );
123+ assertThat (driver .getPageSource (), Matchers .containsString ("jsessionid" ));
124+
125+ // WHEN a JSF action redirects with a standard outcome
126+ driver .findElement (By .id ("form:navigateOutcome" )).click ();
127+
128+ // THEN the browser should be redirected
129+ assertThat (driver .getCurrentUrl (), Matchers .containsString ("redirected=true" ));
130+
131+ // AND there should be a jsessionid in the URL
132+ assertThat (driver .getCurrentUrl (), Matchers .containsString ("jsessionid" ));
133+
134+ }
135+
136+ private WebDriver createBrowserAndLoadPage (final boolean cookies )
137+ {
138+
139+ // custom HtmlUnitDriver with cookies enabled or disabled
140+ WebDriver driver = new HtmlUnitDriver () {
141+ @ Override
142+ protected WebClient modifyWebClient (WebClient client )
143+ {
144+ client .getCookieManager ().setCookiesEnabled (cookies );
145+ return client ;
146+ }
147+ };
148+
149+ // load the page twice to get rid of jsessionid if cookies are enabled
150+ driver .get (baseUrl + "test" );
151+ driver .get (baseUrl + "test" );
152+
153+ return driver ;
154+
155+ }
156+
157+ }
0 commit comments