changeset 500:32113de80f6d

HttpSessionCookieFilter keeps session when sessionId changes
author Devel 1
date Wed, 16 Aug 2017 11:16:24 +0200
parents 5588b03739b0
children 352af87ef80e
files stress-tester/src/main/java/com/passus/st/client/http/filter/HttpSessionCookieFilter.java stress-tester/src/test/java/com/passus/st/client/http/filter/HttpFilterTestUtils.java stress-tester/src/test/java/com/passus/st/client/http/filter/HttpHostRewriterFilterTest.java stress-tester/src/test/java/com/passus/st/client/http/filter/HttpSessionCookieFilterTest.java
diffstat 4 files changed, 39 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpSessionCookieFilter.java	Wed Aug 16 10:23:21 2017 +0200
+++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpSessionCookieFilter.java	Wed Aug 16 11:16:24 2017 +0200
@@ -139,13 +139,16 @@
         HttpCookie requestCookie = findSessionCookie(modifiedRequest);
         HttpCookie inCookie = findSessionCookie(context.origReponse());
 
+        String oldLiveSessionId = null;
         if (inCookie != null) {
             if (requestCookie != null) {
-                deleteMapping(requestCookie, context);
+                oldLiveSessionId = requestCookie.getValue().toString();
+                ParametersBag session = context.scopes().getSession(oldLiveSessionId);
+                deleteMapping(session);
             }
-            makeMappingTagsAndSession(modifiedRequest, realResponse, context, inCookie);
+            makeMappingTagsAndSession(modifiedRequest, realResponse, context, inCookie, oldLiveSessionId);
         } else if (requestCookie != null) {
-            makeMappingTagsAndSession(modifiedRequest, realResponse, context, requestCookie);
+            makeMappingTagsAndSession(modifiedRequest, realResponse, context, requestCookie, oldLiveSessionId);
         } else {
             Object sessionId = modifiedRequest.getTag(TAG_SESSION_ID);
             if (sessionId != null) {
@@ -157,17 +160,15 @@
         return DUNNO;
     }
 
-    private void deleteMapping(HttpCookie requestCookie, HttpFlowContext context) {
-        ParametersBag oldSession = context.scopes().getSession(requestCookie.getValue().toString());
-        ByteString oldInValue = (ByteString) oldSession.get(TAG_ORIG_SESSION_ID);
+    private void deleteMapping(ParametersBag session) {
+        ByteString oldInValue = (ByteString) session.get(TAG_ORIG_SESSION_ID);
         if (oldInValue != null) {
             ByteString oldLiveValue = cookiesMap.remove(oldInValue);
             LOGGER.debug("Mapping deleted: {} -> {}", oldInValue, oldLiveValue);
         }
-
     }
 
-    private void makeMappingTagsAndSession(HttpRequest modifiedRequest, HttpResponse realResponse, HttpFlowContext context, HttpCookie keyCookie) {
+    private void makeMappingTagsAndSession(HttpRequest modifiedRequest, HttpResponse realResponse, HttpFlowContext context, HttpCookie keyCookie, String oldLiveSessionId) {
         HttpCookie liveCookie = findSessionCookie(realResponse);
         ByteString keyValue = keyCookie.getValue();
         if (liveCookie != null && keyValue != null && !keyValue.isEmpty()) {
@@ -176,9 +177,10 @@
             cookiesMap.put(keyValue, liveValue);
             setTag(modifiedRequest, TAG_SESSION_ID, liveSessionId);
             setTag(realResponse, TAG_SESSION_ID, liveSessionId);
-            ParametersBag session = context.scopes().createSession(liveSessionId);
+            ParametersBag session = context.scopes().updateSession(oldLiveSessionId, liveSessionId);
             session.set(TAG_ORIG_SESSION_ID, keyValue);
             LOGGER.debug("Mapping created: {} -> {} [{}]", keyValue, liveValue, modifiedRequest.getUrl());
+            LOGGER.debug("Scopes updated: {} -> {}", oldLiveSessionId, liveSessionId);
         } else {
             // TODO: refactor
             Object sessionId = modifiedRequest.getTag(TAG_SESSION_ID);
--- a/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpFilterTestUtils.java	Wed Aug 16 10:23:21 2017 +0200
+++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpFilterTestUtils.java	Wed Aug 16 11:16:24 2017 +0200
@@ -1,12 +1,16 @@
 package com.passus.st.client.http.filter;
 
+import com.passus.commons.utils.ReflectionUtils;
 import com.passus.config.validation.Errors;
 import com.passus.config.validation.ObjectError;
 import com.passus.net.http.HttpMessage;
+import com.passus.st.ParametersBag;
 import static com.passus.st.client.http.HttpConsts.TAG_SESSION_ID;
+import static com.passus.st.client.http.HttpConsts.ZONE_DEFAULT;
 import com.passus.st.client.http.HttpFlowContext;
 import com.passus.st.client.http.HttpScopes;
 import java.util.List;
+import java.util.Map;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
@@ -38,4 +42,12 @@
             }
         }
     }
+
+    protected static Map<String, ParametersBag> getSessions(HttpScopes scopes) {
+        Map<String, Map<String, ParametersBag>> zonesSessions = ReflectionUtils.<Map>getField(scopes, "zonesSessions");
+        Map<String, ParametersBag> defaultZone = zonesSessions.get(ZONE_DEFAULT);
+        System.out.println(defaultZone);
+        return defaultZone;
+    }
+
 }
--- a/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpHostRewriterFilterTest.java	Wed Aug 16 10:23:21 2017 +0200
+++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpHostRewriterFilterTest.java	Wed Aug 16 11:16:24 2017 +0200
@@ -1,6 +1,7 @@
 package com.passus.st.client.http.filter;
 
 import static com.passus.commons.collection.FluentBuilder.*;
+import static com.passus.commons.utils.ReflectionUtils.getField;
 import com.passus.config.validation.Errors;
 import com.passus.data.ByteString;
 import com.passus.net.http.HttpHeaders;
@@ -8,7 +9,6 @@
 import com.passus.net.http.HttpRequest;
 import com.passus.net.http.HttpRequestBuilder;
 import com.passus.st.client.http.filter.HttpHostRewriterFilter.HostPort;
-import java.lang.reflect.Field;
 import java.util.List;
 import java.util.Map;
 import static org.testng.AssertJUnit.*;
@@ -80,20 +80,6 @@
         assertEquals(hp.port, 80);
     }
 
-    private static final Field hostMapField;
-    private static final Field urlMapField;
-
-    static {
-        try {
-            hostMapField = HttpHostRewriterFilter.class.getDeclaredField("hostMap");
-            hostMapField.setAccessible(true);
-            urlMapField = HttpHostRewriterFilter.class.getDeclaredField("urlMap");
-            urlMapField.setAccessible(true);
-        } catch (Exception ex) {
-            throw new ExceptionInInitializerError(ex);
-        }
-    }
-
     @Test
     public void testConfigure() throws Exception {
         String filterConfig = "filters:\n"
@@ -120,8 +106,8 @@
                 e(hp("example.com", 8080), hp("example.org", -1))
         );
 
-        assertEquals(expectedHostMap, hostMapField.get(filter));
-        assertEquals(expectedUrlMap, urlMapField.get(filter));
+        assertEquals(expectedHostMap, getField(filter, "hostMap"));
+        assertEquals(expectedUrlMap, getField(filter, "urlMap"));
     }
 
     private static ByteString bs(CharSequence cs) {
--- a/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpSessionCookieFilterTest.java	Wed Aug 16 10:23:21 2017 +0200
+++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpSessionCookieFilterTest.java	Wed Aug 16 11:16:24 2017 +0200
@@ -28,9 +28,6 @@
             + "Host: 172.16.60.23\r\n"
             + "Cookie: PHPSESSID=obsolete\r\n\r\n";
 
-    private final String req1sClean = "GET /login HTTP/1.1\r\n"
-            + "Host: 172.16.60.23\r\n\r\n";
-
     private final String resp1s = "HTTP/1.1 200 OK\r\n"
             + "Set-Cookie: PHPSESSID=pcap_pre; path=/; HttpOnly\r\n"
             + "Content-Length: 10\r\n"
@@ -71,23 +68,26 @@
         processor.filterOutbound(req1, resp1, mockContext);
         processor.filterInbound(req1, real1, mockContext);
 
-        when(mockContext.origReponse()).thenReturn(resp2);
         assertEquals(mh.getCookie(req2, cookieName).getValue().toString(), "pcap_pre");
         processor.filterOutbound(req2, resp2, mockContext); // -> x_pre <- pcap_post
         assertEquals(mh.getCookie(req2, cookieName).getValue().toString(), "real_pre");
         assertEquals(req2.getTag(TAG_SESSION_ID), "real_pre");
         assertEquals(resp2.getTag(TAG_SESSION_ID), "real_pre");
+
+        mockContext.scopes().getSession("real_pre").set("marker", "marker");
+        when(mockContext.origReponse()).thenReturn(resp2);
         processor.filterInbound(req2, real2, mockContext); // <- real_post
+        assertEquals(mockContext.scopes().getSession("real_post").get("marker"), "marker");
 
         assertEquals(mh.getCookie(req3, cookieName).getValue().toString(), "pcap_post");
         processor.filterOutbound(req3, resp3, mockContext); // -> x_post
+        assertEquals(mh.getCookie(req3, cookieName).getValue().toString(), "real_post");
         assertEquals(req3.getTag(TAG_SESSION_ID), "real_post");
-        assertEquals(mh.getCookie(req3, cookieName).getValue().toString(), "real_post");
     }
 
     @Test
     public void testNoCookieInFirstRequest() {
-        HttpRequest req1 = request(req1sClean);
+        HttpRequest req1 = HttpRequestBuilder.get("http://172.16.60.23/login").build();
         HttpResponse resp1 = response(resp1s);
         HttpResponse real1 = response(resp1s.replace("pcap_pre", "real_pre"));
         HttpRequest req2 = request(req2s);
@@ -105,13 +105,19 @@
 
         assertEquals(mh.getCookie(req2, cookieName).getValue().toString(), "pcap_pre");
         processor.filterOutbound(req2, resp2, fc); // -> x_pre <- pcap_post
+        assertEquals(mh.getCookie(req2, cookieName).getValue().toString(), "real_pre");
+        assertEquals(req2.getTag(TAG_SESSION_ID), "real_pre");
+        assertEquals(resp2.getTag(TAG_SESSION_ID), "real_pre");
+
+        mockContext.scopes().getSession("real_pre").set("marker", "marker");
         when(mockContext.origReponse()).thenReturn(resp2);
-        assertEquals(mh.getCookie(req2, cookieName).getValue().toString(), "real_pre");
         processor.filterInbound(req2, real2, mockContext);       //          <- real_post
+        assertEquals(mockContext.scopes().getSession("real_post").get("marker"), "marker");
 
         assertEquals(mh.getCookie(req3, cookieName).getValue().toString(), "pcap_post");
         processor.filterOutbound(req3, resp3, fc); // -> x_post
         assertEquals(mh.getCookie(req3, cookieName).getValue().toString(), "real_post");
+        assertEquals(req3.getTag(TAG_SESSION_ID), "real_post");
     }
 
     @Test