changeset 501:352af87ef80e

HttpSessionCookieFilterTest refactoring
author Devel 1
date Wed, 16 Aug 2017 11:59:56 +0200
parents 32113de80f6d
children 483eae2a2565
files stress-tester/src/main/resources/log4j2.xml stress-tester/src/test/java/com/passus/st/client/http/filter/HttpSessionCookieFilterTest.java
diffstat 2 files changed, 57 insertions(+), 87 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/resources/log4j2.xml	Wed Aug 16 11:16:24 2017 +0200
+++ b/stress-tester/src/main/resources/log4j2.xml	Wed Aug 16 11:59:56 2017 +0200
@@ -6,11 +6,14 @@
 	</Console>
   </Appenders>
   <Loggers>
-	<Logger name="com.passus.st.client.http.HttpSynchClientWorker" level="debug" additivity="false">
+<!--	<Logger name="com.passus.st.client.http.HttpSynchClientWorker" level="debug" additivity="false">
 	  <AppenderRef ref="Console"/>
 	</Logger>
 	<Logger name="com.passus.st.client.http.HttpFlowBasedClientWorker" level="debug" additivity="false">
 	  <AppenderRef ref="Console"/>
+	</Logger>-->
+	<Logger name="com.passus.st.client.http.filter" level="debug" additivity="false">
+	  <AppenderRef ref="Console"/>
 	</Logger>
 	<Root level="error">
 	  <AppenderRef ref="Console"/>
--- a/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpSessionCookieFilterTest.java	Wed Aug 16 11:16:24 2017 +0200
+++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpSessionCookieFilterTest.java	Wed Aug 16 11:59:56 2017 +0200
@@ -3,9 +3,9 @@
 import com.passus.net.http.HttpCookie;
 import com.passus.net.http.HttpMessageHelper;
 import com.passus.net.http.HttpRequest;
-import com.passus.net.http.HttpRequestBuilder;
+import static com.passus.net.http.HttpRequestBuilder.get;
 import com.passus.net.http.HttpResponse;
-import com.passus.net.http.HttpResponseBuilder;
+import static com.passus.net.http.HttpResponseBuilder.ok;
 import static com.passus.st.client.http.HttpConsts.TAG_SESSION_ID;
 import com.passus.st.client.http.HttpFlowContext;
 import static com.passus.st.client.http.filter.HttpFilterTestUtils.createMockContext;
@@ -20,14 +20,9 @@
  */
 public class HttpSessionCookieFilterTest {
 
-    private final String cookieName = "PHPSESSID";
-    private final HttpFlowContext fc = null;
+    private static final String COOKIE_NAME = "PHPSESSID";
     private final HttpMessageHelper mh = HttpMessageHelper.NOT_STRICT;
 
-    private final String req1Obsolete = "GET /login HTTP/1.1\r\n"
-            + "Host: 172.16.60.23\r\n"
-            + "Cookie: PHPSESSID=obsolete\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"
@@ -52,25 +47,25 @@
 
     @Test
     public void testObsoleteCookieInFirstRequest() {
-        HttpRequest req1 = request(req1Obsolete);
+        HttpRequest req1 = get("http://172.16.60.23/login").cookie(COOKIE_NAME, "obsolete").build();
         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 = HttpResponseBuilder.ok().build();
+        HttpResponse resp3 = ok().build();
 
-        HttpSessionCookieFilter processor = new HttpSessionCookieFilter("PHPSESSID");
+        HttpSessionCookieFilter processor = new HttpSessionCookieFilter();
         HttpFlowContext mockContext = createMockContext();
 
         when(mockContext.origReponse()).thenReturn(resp1);
-        processor.filterOutbound(req1, resp1, mockContext);
+        processor.filterOutbound(req1, resp1, null);
         processor.filterInbound(req1, real1, mockContext);
 
-        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(mh.getCookie(req2, COOKIE_NAME).getValue().toString(), "pcap_pre");
+        processor.filterOutbound(req2, resp2, null); // -> x_pre <- pcap_post
+        assertEquals(mh.getCookie(req2, COOKIE_NAME).getValue().toString(), "real_pre");
         assertEquals(req2.getTag(TAG_SESSION_ID), "real_pre");
         assertEquals(resp2.getTag(TAG_SESSION_ID), "real_pre");
 
@@ -79,33 +74,33 @@
         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(mh.getCookie(req3, COOKIE_NAME).getValue().toString(), "pcap_post");
+        processor.filterOutbound(req3, resp3, null); // -> x_post
+        assertEquals(mh.getCookie(req3, COOKIE_NAME).getValue().toString(), "real_post");
         assertEquals(req3.getTag(TAG_SESSION_ID), "real_post");
     }
 
     @Test
     public void testNoCookieInFirstRequest() {
-        HttpRequest req1 = HttpRequestBuilder.get("http://172.16.60.23/login").build();
+        HttpRequest req1 = 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);
         HttpResponse resp2 = response(resp2s);
         HttpResponse real2 = response(resp2s.replace("pcap_post", "real_post"));
         HttpRequest req3 = request(req3s);
-        HttpResponse resp3 = HttpResponseBuilder.ok().build();
+        HttpResponse resp3 = ok().build();
 
-        HttpSessionCookieFilter processor = new HttpSessionCookieFilter("PHPSESSID");
+        HttpSessionCookieFilter processor = new HttpSessionCookieFilter();
         HttpFlowContext mockContext = createMockContext();
 
         when(mockContext.origReponse()).thenReturn(resp1);
-        processor.filterOutbound(req1, resp1, fc);
+        processor.filterOutbound(req1, resp1, null);
         processor.filterInbound(req1, real1, mockContext);
 
-        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(mh.getCookie(req2, COOKIE_NAME).getValue().toString(), "pcap_pre");
+        processor.filterOutbound(req2, resp2, null); // -> x_pre <- pcap_post
+        assertEquals(mh.getCookie(req2, COOKIE_NAME).getValue().toString(), "real_pre");
         assertEquals(req2.getTag(TAG_SESSION_ID), "real_pre");
         assertEquals(resp2.getTag(TAG_SESSION_ID), "real_pre");
 
@@ -114,9 +109,9 @@
         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(mh.getCookie(req3, COOKIE_NAME).getValue().toString(), "pcap_post");
+        processor.filterOutbound(req3, resp3, null); // -> x_post
+        assertEquals(mh.getCookie(req3, COOKIE_NAME).getValue().toString(), "real_post");
         assertEquals(req3.getTag(TAG_SESSION_ID), "real_post");
     }
 
@@ -128,19 +123,12 @@
          * req: newSessId
          */
 
-        HttpRequest req = HttpRequestBuilder.get("http://test/test")
-                .cookie("JSESSIONID", "orig")
-                .build();
-        HttpResponse origResp = HttpResponseBuilder.ok().build();
+        HttpRequest req = get("http://test/test").cookie(COOKIE_NAME, "orig").build();
+        HttpResponse origResp = ok().build();
+        HttpResponse respLive = ok().cookie(COOKIE_NAME, "newSessId").build();
 
-        HttpResponse respLive = HttpResponseBuilder.ok()
-                .cookie("JSESSIONID", "newSessId")
-                .build();
-
-        HttpRequest req2 = HttpRequestBuilder.get("http://test/test")
-                .cookie("JSESSIONID", "orig")
-                .build();
-        HttpResponse origResp2 = HttpResponseBuilder.ok().build();
+        HttpRequest req2 = get("http://test/test").cookie(COOKIE_NAME, "orig").build();
+        HttpResponse origResp2 = ok().build();
 
         HttpSessionCookieFilter filter = new HttpSessionCookieFilter();
         HttpFlowContext mockContext = createMockContext();
@@ -149,7 +137,7 @@
         filter.filterInbound(req, respLive, mockContext);
         filter.filterOutbound(req2, origResp2, mockContext);
 
-        HttpCookie cookie = mh.getCookie(req2, "JSESSIONID");
+        HttpCookie cookie = mh.getCookie(req2, COOKIE_NAME);
         assertEquals(cookie.getValue().toString(), "newSessId");
     }
 
@@ -161,16 +149,12 @@
          * req: orig
          */
 
-        HttpRequest req = HttpRequestBuilder.get("http://test/test")
-                .cookie("JSESSIONID", "orig")
-                .build();
-        HttpResponse origResp = HttpResponseBuilder.ok().build();
-        HttpResponse respLive = HttpResponseBuilder.ok().build();
+        HttpRequest req = get("http://test/test").cookie(COOKIE_NAME, "orig").build();
+        HttpResponse origResp = ok().build();
+        HttpResponse respLive = ok().build();
 
-        HttpRequest req2 = HttpRequestBuilder.get("http://test/test")
-                .cookie("JSESSIONID", "orig")
-                .build();
-        HttpResponse origResp2 = HttpResponseBuilder.ok().build();
+        HttpRequest req2 = get("http://test/test").cookie(COOKIE_NAME, "orig").build();
+        HttpResponse origResp2 = ok().build();
 
         HttpSessionCookieFilter filter = new HttpSessionCookieFilter();
         HttpFlowContext mockContext = createMockContext();
@@ -179,7 +163,7 @@
         filter.filterInbound(req, respLive, mockContext);
         filter.filterOutbound(req2, origResp2, mockContext);
 
-        HttpCookie cookie = mh.getCookie(req2, "JSESSIONID");
+        HttpCookie cookie = mh.getCookie(req2, COOKIE_NAME);
         assertEquals(cookie.getValue().toString(), "orig");
     }
 
@@ -191,14 +175,12 @@
          * req: -
          */
 
-        HttpRequest req = HttpRequestBuilder.get("http://test/test").build();
-        HttpResponse origResp = HttpResponseBuilder.ok().build();
-        HttpResponse respLive = HttpResponseBuilder.ok()
-                .cookie("JSESSIONID", "newSessId")
-                .build();
+        HttpRequest req = get("http://test/test").build();
+        HttpResponse origResp = ok().build();
+        HttpResponse respLive = ok().cookie(COOKIE_NAME, "newSessId").build();
 
-        HttpRequest req2 = HttpRequestBuilder.get("http://test/test").build();
-        HttpResponse origResp2 = HttpResponseBuilder.ok().build();
+        HttpRequest req2 = get("http://test/test").build();
+        HttpResponse origResp2 = ok().build();
 
         HttpSessionCookieFilter filter = new HttpSessionCookieFilter();
         HttpFlowContext mockContext = createMockContext();
@@ -207,7 +189,7 @@
         filter.filterInbound(req, respLive, mockContext);
         filter.filterOutbound(req2, origResp2, mockContext);
 
-        HttpCookie cookie = mh.getCookie(req2, "JSESSIONID");
+        HttpCookie cookie = mh.getCookie(req2, COOKIE_NAME);
         assertNull(cookie);
     }
 
@@ -219,20 +201,12 @@
          * req: newSessId (origNew)
          */
 
-        HttpRequest req = HttpRequestBuilder.get("http://test/test")
-                .cookie("JSESSIONID", "orig")
-                .build();
-        HttpResponse origResp = HttpResponseBuilder.ok()
-                .cookie("JSESSIONID", "origNew")
-                .build();
-        HttpResponse respLive = HttpResponseBuilder.ok()
-                .cookie("JSESSIONID", "newSessId")
-                .build();
+        HttpRequest req = get("http://test/test").cookie(COOKIE_NAME, "orig").build();
+        HttpResponse origResp = ok().cookie(COOKIE_NAME, "origNew").build();
+        HttpResponse respLive = ok().cookie(COOKIE_NAME, "newSessId").build();
 
-        HttpRequest req2 = HttpRequestBuilder.get("http://test/test")
-                .cookie("JSESSIONID", "origNew")
-                .build();
-        HttpResponse origResp2 = HttpResponseBuilder.ok().build();
+        HttpRequest req2 = get("http://test/test").cookie(COOKIE_NAME, "origNew").build();
+        HttpResponse origResp2 = ok().build();
 
         HttpSessionCookieFilter filter = new HttpSessionCookieFilter();
         HttpFlowContext mockContext = createMockContext();
@@ -241,7 +215,7 @@
         filter.filterInbound(req, respLive, mockContext);
         filter.filterOutbound(req2, origResp2, mockContext);
 
-        HttpCookie cookie = mh.getCookie(req2, "JSESSIONID");
+        HttpCookie cookie = mh.getCookie(req2, COOKIE_NAME);
         assertEquals(cookie.getValue().toString(), "newSessId");
     }
 
@@ -253,19 +227,12 @@
          * req: newSessId (origNew)
          */
 
-        HttpRequest req = HttpRequestBuilder.get("http://test/test")
-                .build();
-        HttpResponse origResp = HttpResponseBuilder.ok()
-                .cookie("JSESSIONID", "origNew")
-                .build();
-        HttpResponse respLive = HttpResponseBuilder.ok()
-                .cookie("JSESSIONID", "newSessId")
-                .build();
+        HttpRequest req = get("http://test/test").build();
+        HttpResponse origResp = ok().cookie(COOKIE_NAME, "origNew").build();
+        HttpResponse respLive = ok().cookie(COOKIE_NAME, "newSessId").build();
 
-        HttpRequest req2 = HttpRequestBuilder.get("http://test/test")
-                .cookie("JSESSIONID", "origNew")
-                .build();
-        HttpResponse origResp2 = HttpResponseBuilder.ok().build();
+        HttpRequest req2 = get("http://test/test").cookie(COOKIE_NAME, "origNew").build();
+        HttpResponse origResp2 = ok().build();
 
         HttpSessionCookieFilter filter = new HttpSessionCookieFilter();
         HttpFlowContext mockContext = createMockContext();
@@ -274,7 +241,7 @@
         filter.filterInbound(req, respLive, mockContext);
         filter.filterOutbound(req2, origResp2, mockContext);
 
-        HttpCookie cookie = mh.getCookie(req2, "JSESSIONID");
+        HttpCookie cookie = mh.getCookie(req2, COOKIE_NAME);
         assertEquals(cookie.getValue().toString(), "newSessId");
     }