changeset 441:24d9595a4fe2

HttpSessionCookieFilter
author Devel 1
date Fri, 28 Jul 2017 09:25:18 +0200
parents 76b6252b9e5c
children 20d6a963e889
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/HttpSessionCookieFilterTest.java
diffstat 2 files changed, 58 insertions(+), 47 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpSessionCookieFilter.java	Fri Jul 28 09:25:06 2017 +0200
+++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpSessionCookieFilter.java	Fri Jul 28 09:25:18 2017 +0200
@@ -141,24 +141,11 @@
 
         if (inCookie != null) {
             if (requestCookie != null) {
-                ParametersBag oldSession = context.scopes().getSession(requestCookie.getValue().toString());
-                ByteString oldInValue = (ByteString) oldSession.get(TAG_ORIG_SESSION_ID);
-                ByteString oldLiveValue = cookiesMap.remove(oldInValue);
-                LOGGER.debug("Mapping deleted: {} -> {} [{}]", oldInValue, oldLiveValue, modifiedRequest.getUrl());
+                deleteMapping(requestCookie, context);
             }
-
-            HttpCookie liveCookie = findSessionCookie(realResponse);
-            ByteString inValue = inCookie.getValue();
-            if (inValue != null && liveCookie != null && !inValue.isEmpty()) {
-                ByteString liveValue = liveCookie.getValue();
-                String liveSessionId = liveValue.toString();
-                cookiesMap.put(inValue, liveValue);
-                setTag(modifiedRequest, TAG_SESSION_ID, liveSessionId);
-                setTag(realResponse, TAG_SESSION_ID, liveSessionId);
-                ParametersBag session = context.scopes().createSession(liveSessionId);
-                session.set(TAG_ORIG_SESSION_ID, inValue);
-                LOGGER.debug("Mapping created: {} -> {} [{}]", inValue, liveValue, modifiedRequest.getUrl());
-            }
+            makeMappingTagsAndSession(modifiedRequest, realResponse, context, inCookie);
+        } else if (requestCookie != null) {
+            makeMappingTagsAndSession(modifiedRequest, realResponse, context, requestCookie);
         } else {
             String sessionId = (String) modifiedRequest.getTag(TAG_SESSION_ID);
             if (sessionId != null) {
@@ -169,6 +156,29 @@
         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);
+        ByteString oldLiveValue = cookiesMap.remove(oldInValue);
+        LOGGER.debug("Mapping deleted: {} -> {}", oldInValue, oldLiveValue);
+
+    }
+
+    private void makeMappingTagsAndSession(HttpRequest modifiedRequest, HttpResponse realResponse, HttpFlowContext context, HttpCookie keyCookie) {
+        HttpCookie liveCookie = findSessionCookie(realResponse);
+        ByteString keyValue = keyCookie.getValue();
+        if (liveCookie != null && keyValue != null && !keyValue.isEmpty()) {
+            ByteString liveValue = liveCookie.getValue();
+            String liveSessionId = liveValue.toString();
+            cookiesMap.put(keyValue, liveValue);
+            setTag(modifiedRequest, TAG_SESSION_ID, liveSessionId);
+            setTag(realResponse, TAG_SESSION_ID, liveSessionId);
+            ParametersBag session = context.scopes().createSession(liveSessionId);
+            session.set(TAG_ORIG_SESSION_ID, keyValue);
+            LOGGER.debug("Mapping created: {} -> {} [{}]", keyValue, liveValue, modifiedRequest.getUrl());
+        }
+    }
+
     @Override
     public HttpFilter instanceForWorker(int index) {
         return new HttpSessionCookieFilter(cookieNames, removeInvalidSessionId);
--- a/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpSessionCookieFilterTest.java	Fri Jul 28 09:25:06 2017 +0200
+++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpSessionCookieFilterTest.java	Fri Jul 28 09:25:18 2017 +0200
@@ -1,5 +1,6 @@
 package com.passus.st.client.http.filter;
 
+import com.passus.net.http.HttpCookie;
 import com.passus.net.http.HttpMessageHelper;
 import com.passus.net.http.HttpRequest;
 import com.passus.net.http.HttpRequestBuilder;
@@ -32,16 +33,10 @@
 
     private final String req1Obsolete = "GET /login HTTP/1.1\r\n"
             + "Host: 172.16.60.23\r\n"
-            + "User-Agent: Mozilla/5.0\r\n"
-            + "Accept: text/html;q=0.9,*/*;q=0.8\r\n"
-            + "Cookie: PHPSESSID=obsolete\r\n"
-            + "Connection: keep-alive\r\n\r\n";
+            + "Cookie: PHPSESSID=obsolete\r\n\r\n";
 
-    private final String req1s = "GET /login HTTP/1.1\r\n"
-            + "Host: 172.16.60.23\r\n"
-            + "User-Agent: Mozilla/5.0\r\n"
-            + "Accept: text/html;q=0.9,*/*;q=0.8\r\n"
-            + "Connection: keep-alive\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"
@@ -51,34 +46,19 @@
 
     private final String req2s = "POST /login_check HTTP/1.1\r\n"
             + "Host: 172.16.60.23\r\n"
-            + "User-Agent: Mozilla/5.0\r\n"
-            + "Accept: text/html;q=0.9,*/*;q=0.8\r\n"
-            + "Referer: http://172.16.60.23/login\r\n"
             + "Content-Type: application/x-www-form-urlencoded\r\n"
             + "Content-Length: 32\r\n"
-            + "Cookie: PHPSESSID=pcap_pre\r\n"
-            + "Connection: keep-alive\r\n\r\n"
+            + "Cookie: PHPSESSID=pcap_pre\r\n\r\n"
             + "_username=admin&_password=qwerty";
 
     private final String resp2s = "HTTP/1.1 302 Found\r\n"
             + "Set-Cookie: PHPSESSID=pcap_post; path=/; HttpOnly\r\n"
             + "Location: http://172.16.60.23/\r\n"
-            + "Content-Length: 106\r\n"
-            + "Content-Type: text/html; charset=UTF-8\r\n\r\n"
-            + "<!DOCTYPE html><html><body><a href=\"http://172.16.60.23/login\">http://172.16.60.23/login</a></body></html>";
+            + "Content-Length: 0\r\n\r\n";
 
     private final String req3s = "GET / HTTP/1.1\r\n"
             + "Host: 172.16.60.23\r\n"
-            + "User-Agent: Mozilla/5.0\r\n"
-            + "Accept: text/html;q=0.9,*/*;q=0.8\r\n"
-            + "Referer: http://172.16.60.23/login\r\n"
-            + "Cookie: PHPSESSID=pcap_post\r\n"
-            + "Connection: keep-alive\r\n\r\n";
-
-    private final String resp3s = "HTTP/1.1 200 OK\r\n"
-            + "Content-Length: 4\r\n"
-            + "Content-Type: text/html; charset=UTF-8\r\n\r\n"
-            + "BODY";
+            + "Cookie: PHPSESSID=pcap_post\r\n\r\n";
 
     @Test
     public void testObsoleteCookieInFirstRequest() {
@@ -89,7 +69,7 @@
         HttpResponse resp2 = response(resp2s);
         HttpResponse real2 = response(resp2s.replace("pcap_post", "real_post"));
         HttpRequest req3 = request(req3s);
-        HttpResponse resp3 = response(resp3s);
+        HttpResponse resp3 = HttpResponseBuilder.ok().build();
 
         HttpSessionCookieFilter processor = new HttpSessionCookieFilter("PHPSESSID");
 
@@ -113,14 +93,14 @@
 
     @Test
     public void testNoCookieInFirstRequest() {
-        HttpRequest req1 = request(req1s);
+        HttpRequest req1 = request(req1sClean);
         HttpResponse resp1 = response(resp1s);
         HttpResponse real1 = response(resp1s.replace("pcap_pre", "real_pre"));
         HttpRequest req2 = request(req2s);
         HttpResponse resp2 = response(resp2s);
         HttpResponse real2 = response(resp2s.replace("pcap_post", "real_post"));
         HttpRequest req3 = request(req3s);
-        HttpResponse resp3 = response(resp3s);
+        HttpResponse resp3 = HttpResponseBuilder.ok().build();
 
         HttpSessionCookieFilter processor = new HttpSessionCookieFilter("PHPSESSID");
 
@@ -164,4 +144,25 @@
 
         //assertEquals(resp1.getTag(TAG_SESSION_ID), "1");
     }
+    
+    @Test
+    public void test() {
+        HttpRequest req = HttpRequestBuilder.get("http://test/test")
+                .cookie("JSESSIONID", "oldValue")
+                .build();
+        HttpResponse respIn = HttpResponseBuilder.ok().build();
+        HttpResponse respLive = HttpResponseBuilder.ok()
+                .cookie("JSESSIONID", "newValue")
+                .build();
+        
+        HttpSessionCookieFilter filter = new HttpSessionCookieFilter();
+        filter.setRemoveInvalidSessionId(false);
+        
+        when(mockContext.origReponse()).thenReturn(respIn);
+        filter.filterInbound(req, respLive, mockContext);
+        filter.filterOutbound(req, respLive, mockContext);
+        
+        HttpCookie cookie = mh.getCookie(req, "JSESSIONID");
+        assertEquals(cookie.getValue().toString(), "newValue");
+    }
 }