From fa874897b2fede7df922d1bb247a837de29f446b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Chlup?= Date: Wed, 27 Nov 2024 14:15:21 +0100 Subject: [PATCH 1/2] Add result print, fail in case of missing cookies --- test-demo-webapp.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/test-demo-webapp.py b/test-demo-webapp.py index 293cc90..294a48e 100755 --- a/test-demo-webapp.py +++ b/test-demo-webapp.py @@ -8,8 +8,7 @@ class testdemowebapp: def __init__(self): - self.url = "http://localhost:8000/demo-1.0/demo" - self.counter = 0 + self.url = "http://localhost:8000/demo-1.0/demo" self.JSESSIONID = None # Return False if sticky True otherwise @@ -26,7 +25,7 @@ def request_response(self): self.JSESSIONID = c.value elif self.JSESSIONID != c.value: return False - return True + return len(r.cookies) > 0 # Return True if sticky False if error or not sticky def request_response_loop(self, n): @@ -45,6 +44,7 @@ def start(result_queue, n, tid): result_queue.put((tid, 'done' if mydemo.request_response_loop(n) else 'failed')) if __name__ == "__main__": + res = "OK" args = sys.argv[1:] count = 1 loop = 1000 @@ -64,5 +64,8 @@ def start(result_queue, n, tid): for i in range(count): r = q.get() if r[1] != 'done': - print("Failed") + res = "NOK" break + + print(res) + From 4bd325d0a18374a1436dcecea36b44389c091dda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Chlup?= Date: Thu, 28 Nov 2024 12:34:00 +0100 Subject: [PATCH 2/2] Polish TestServlet formatting, add cookie to response header --- .../org/example/tomcat/cloud/TestServlet.java | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/example/tomcat/cloud/TestServlet.java b/src/main/java/org/example/tomcat/cloud/TestServlet.java index 627fc09..8c56be0 100644 --- a/src/main/java/org/example/tomcat/cloud/TestServlet.java +++ b/src/main/java/org/example/tomcat/cloud/TestServlet.java @@ -23,6 +23,8 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; +import javax.servlet.http.Cookie; + import java.io.IOException; import java.io.PrintWriter; import java.net.InetAddress; @@ -44,19 +46,18 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro response.setContentType("application/json;charset=UTF-8"); HttpSession session = request.getSession(); - Integer counter; - if (session.isNew()) { - /* we have a new session */ - counter = 0; - } else { - counter = (Integer) session.getAttribute("counter"); - if (counter == null) { - counter = 1; - } else { + Integer counter; + if (session.isNew()) { + /* we have a new session */ + counter = 0; + } else { + counter = (Integer) session.getAttribute("counter"); + if (counter == null) { + counter = 0; + } counter++; - } - session.setAttribute("counter", counter); - } + session.setAttribute("counter", counter); + } String id = session.getId(); String isNew = session.isNew() ? "true" : "false"; @@ -64,6 +65,8 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro String hostname = InetAddress.getLocalHost().getHostName(); String last = session.getLastAccessedTime() + ""; + response.addCookie(new Cookie("JSESSIONID", id)); + try (PrintWriter out = response.getWriter()) { out.println(String.format(TEMPLATE, counter, id, isNew, server, hostname, last)); }