blob: 115adda4bdeda7cacf43c4925200079ca4e8e2a0 [file] [log] [blame]
Vinay Vishal57171472018-09-18 20:22:00 +05301/*
2 * Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v. 2.0, which is available at
6 * http://www.eclipse.org/legal/epl-2.0.
7 *
8 * This Source Code may also be made available under the following Secondary
9 * Licenses when the conditions for such availability set forth in the
10 * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11 * version 2 with the GNU Classpath Exception, which is available at
12 * https://www.gnu.org/software/classpath/license.html.
13 *
14 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15 */
16
17import java.io.*;
18import java.net.*;
19import com.sun.ejte.ccl.reporter.*;
20
21/*
22 * Unit test for customizing the name of the session tracking cookie.
23 *
24 * This test leverages a ServletContextListener to change the name
25 * of the session tracking cookie from JSESSIONID to MYJSESSIONID.
26 */
27public class WebTest {
28
29 private static String TEST_NAME = "servlet-3.0-session-cookie-custom-name-programmtic";
30
31 private static final String MYJSESSIONID = "MYJSESSIONID";
32
33 private static final String EXPECTED_RESPONSE = "HTTP/1.1 200 OK";
34 private static SimpleReporterAdapter stat
35 = new SimpleReporterAdapter("appserv-tests");
36
37 private String host;
38 private String port;
39 private String contextRoot;
40
41 public WebTest(String[] args) {
42 host = args[0];
43 port = args[1];
44 contextRoot = args[2];
45 }
David Matějčekf4dc06a2021-05-17 12:10:57 +020046
Vinay Vishal57171472018-09-18 20:22:00 +053047 public static void main(String[] args) {
48
49 stat.addDescription("Unit test for customizing name of session " +
50 "tracking cookie");
51 WebTest webTest = new WebTest(args);
52
53 try {
54 webTest.secondRun(webTest.firstRun());
55 stat.addStatus(TEST_NAME, stat.PASS);
56 } catch( Exception ex) {
57 ex.printStackTrace();
58 stat.addStatus(TEST_NAME, stat.FAIL);
59 }
60
David Matějčekf4dc06a2021-05-17 12:10:57 +020061 stat.printSummary();
Vinay Vishal57171472018-09-18 20:22:00 +053062 }
63
64 public String firstRun() throws Exception {
65
66 Socket sock = null;
67 InputStream is = null;
68 BufferedReader br = null;
69 OutputStream os = null;
70 String line = null;
71
72 try {
73 sock = new Socket(host, new Integer(port).intValue());
74 os = sock.getOutputStream();
75 String get = "GET " + contextRoot + "/CreateSession" + " HTTP/1.0\n";
76 System.out.println(get);
77 os.write(get.getBytes());
78 os.write("\r\n".getBytes());
David Matějčekf4dc06a2021-05-17 12:10:57 +020079
Vinay Vishal57171472018-09-18 20:22:00 +053080 // Get the MYJSESSIONID from the response
81 is = sock.getInputStream();
82 br = new BufferedReader(new InputStreamReader(is));
83 while ((line = br.readLine()) != null) {
84 System.out.println(line);
85 if (line.startsWith("Set-Cookie:")
86 || line.startsWith("Set-cookie:")) {
87 break;
88 }
89 }
90 } finally {
91 try {
92 if (sock != null) {
93 sock.close();
94 }
95 } catch(IOException ioe) {
96 // ignore
97 }
98 try {
99 if (is != null) {
100 is.close();
101 }
102 } catch(IOException ioe) {
103 // ignore
104 }
105 try {
106 if (br != null) {
107 br.close();
108 }
109 } catch(IOException ioe) {
110 // ignore
111 }
112 try {
113 if (os != null) {
114 os.close();
115 }
116 } catch(IOException ioe) {
117 // ignore
118 }
119 }
120
121 if (line == null) {
122 throw new Exception("Missing Set-Cookie response header");
123 }
124
125 System.out.println();
126
127 return getSessionCookie(line, MYJSESSIONID);
128 }
129
130 public void secondRun(String sessionCookie) throws Exception {
131
132 Socket sock = null;
133 OutputStream os = null;
134 InputStream is = null;
135 BufferedReader br = null;
136 boolean found = false;
137
138 try {
139 sock = new Socket(host, new Integer(port).intValue());
140 os = sock.getOutputStream();
141 String get = "GET " + contextRoot + "/ResumeSession" + " HTTP/1.0\n";
142 System.out.print(get);
143 os.write(get.getBytes());
144 String cookie = "Cookie: " + sessionCookie + "\n";
145 System.out.println(cookie);
146 os.write(cookie.getBytes());
147 os.write("\r\n".getBytes());
David Matějčekf4dc06a2021-05-17 12:10:57 +0200148
Vinay Vishal57171472018-09-18 20:22:00 +0530149 is = sock.getInputStream();
150 br = new BufferedReader(new InputStreamReader(is));
151
152 String line = null;
153 while ((line = br.readLine()) != null) {
154 System.out.println(line);
155 if (line.contains(EXPECTED_RESPONSE)) {
156 found = true;
157 break;
158 }
159 }
160 } finally {
161 try {
162 if (sock != null) {
163 sock.close();
164 }
165 } catch(IOException ioe) {
166 // ignore
167 }
168 try {
169 if (os != null) {
170 os.close();
171 }
172 } catch(IOException ioe) {
173 // ignore
174 }
175 try {
176 if (is != null) {
177 is.close();
178 }
179 } catch(IOException ioe) {
180 // ignore
181 }
182 try {
183 if (br != null) {
184 br.close();
185 }
186 } catch(IOException ioe) {
187 // ignore
188 }
189 }
190
191 if (found) {
192 stat.addStatus(TEST_NAME, stat.PASS);
193 } else {
194 throw new Exception("Wrong response. Expected response: "
195 + EXPECTED_RESPONSE + " not found");
196 }
197 }
198
199 private String getSessionCookie(String header, String cookieName) {
200
201 String ret = null;
202
203 int index = header.indexOf(cookieName);
204 if (index != -1) {
205 int endIndex = header.indexOf(';', index);
206 if (endIndex != -1) {
207 ret = header.substring(index, endIndex);
208 } else {
209 ret = header.substring(index);
210 }
211 ret = ret.trim();
212 }
213
214 return ret;
215 }
216}