changeset 940:d59a9a3f74d7

HttpMessageWrapperStaticExtractor improvements
author Devel 2
date Fri, 15 Jun 2018 13:23:04 +0200
parents bcda60787f9e
children b6f72cdcac16
files stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageWrapper.java stress-tester/src/main/java/com/passus/st/filter/HttpMessageWrapperStaticExtractor.java stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageWrapperTest.java stress-tester/src/test/java/com/passus/st/filter/HttpMessageValueExtractorTest.java stress-tester/src/test/java/com/passus/st/filter/HttpMessageWrapperStaticExtractorTest.java
diffstat 5 files changed, 196 insertions(+), 95 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageWrapper.java	Wed Jun 06 10:02:06 2018 +0200
+++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageWrapper.java	Fri Jun 15 13:23:04 2018 +0200
@@ -62,7 +62,11 @@
         return null;
     }
 
-    public ParametersBag getHttpSession() {
+    public ParametersBag getGlobalParams() {
+        return getScopes().getGlobal();
+    }
+
+    public ParametersBag getSessionParams() {
         if (req == null) {
             return null;
         }
@@ -75,6 +79,15 @@
         return null;
     }
 
+    public ParametersBag getConvParams() {
+        if (req == null) {
+            return null;
+        }
+
+        HttpScopes scopes = getScopes();
+        return scopes.getConversation(req.message);
+    }
+
     private HttpFilterMessageWrapper createWrapper(HttpMessage message) {
         if (message == null) {
             return null;
--- a/stress-tester/src/main/java/com/passus/st/filter/HttpMessageWrapperStaticExtractor.java	Wed Jun 06 10:02:06 2018 +0200
+++ b/stress-tester/src/main/java/com/passus/st/filter/HttpMessageWrapperStaticExtractor.java	Fri Jun 15 13:23:04 2018 +0200
@@ -8,14 +8,14 @@
 import com.passus.st.client.http.extractor.ContentExtractor;
 import com.passus.st.client.http.extractor.ContentExtractorUtils;
 import com.passus.st.client.http.filter.HttpMessageWrapper;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
 import java.util.Objects;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
-import org.apache.logging.log4j.LogManager;
-import org.apache.logging.log4j.Logger;
 
 /**
- *
  * @author mikolaj.podbielski
  */
 public class HttpMessageWrapperStaticExtractor implements ValueExtractor {
@@ -36,7 +36,9 @@
     static final String RESP_HEADER_SF = "resp.header@";
     static final String RESP_FULL_CONTENT_SF = "resp.content";
 
-    static final String HTTP_SESSION_SF = "sess@";
+    static final String HTTP_SESSION_PARAM_SF = "sessionParams@";
+    static final String HTTP_GLOBAL_PARAM_SF = "globalParams@";
+    static final String HTTP_CONV_PARAM_SF = "convParams@";
 
     static abstract class Operation {
 
@@ -113,12 +115,30 @@
         }
     }
 
-    private static class GetSessionVariable extends Operation {
+    private static class GetSessionParams extends Operation {
 
         @Override
         public Object extract(HttpMessageWrapper wrapper) {
-            ParametersBag httpSession = wrapper.getHttpSession();
-            return httpSession == null ? null : httpSession.get(name);
+            ParametersBag params = wrapper.getSessionParams();
+            return params == null ? null : params.get(name);
+        }
+    }
+
+    private static class GetGlobalParams extends Operation {
+
+        @Override
+        public Object extract(HttpMessageWrapper wrapper) {
+            ParametersBag params = wrapper.getGlobalParams();
+            return params == null ? null : params.get(name);
+        }
+    }
+
+    private static class GetConvParams extends Operation {
+
+        @Override
+        public Object extract(HttpMessageWrapper wrapper) {
+            ParametersBag params = wrapper.getConvParams();
+            return params == null ? null : params.get(name);
         }
     }
 
@@ -226,7 +246,7 @@
             op = new GetRespHeaderOperation();
             op.name = name;
         } else if ((name = matchAndExtract(HTTP_SESS_FUN, expr)) != null) {
-            op = new GetSessionVariable();
+            op = new GetSessionParams();
             op.name = name;
         }
         return op;
@@ -257,9 +277,15 @@
         } else if (expr.startsWith(RESP_HEADER_SF)) {
             op = new GetRespHeaderOperation();
             op.name = expr.substring(RESP_HEADER_SF.length());
-        } else if (expr.startsWith(HTTP_SESSION_SF)) {
-            op = new GetSessionVariable();
-            op.name = expr.substring(HTTP_SESSION_SF.length());
+        } else if (expr.startsWith(HTTP_SESSION_PARAM_SF)) {
+            op = new GetSessionParams();
+            op.name = expr.substring(HTTP_SESSION_PARAM_SF.length());
+        } else if (expr.startsWith(HTTP_GLOBAL_PARAM_SF)) {
+            op = new GetGlobalParams();
+            op.name = expr.substring(HTTP_GLOBAL_PARAM_SF.length());
+        } else if (expr.startsWith(HTTP_CONV_PARAM_SF)) {
+            op = new GetConvParams();
+            op.name = expr.substring(HTTP_CONV_PARAM_SF.length());
         } else if (expr.startsWith(RESP_FULL_CONTENT_SF)) {
             if (RESP_FULL_CONTENT_SF.length() == expr.length()) {
                 op = new ExtractRespContentOperation();
--- a/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageWrapperTest.java	Wed Jun 06 10:02:06 2018 +0200
+++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageWrapperTest.java	Fri Jun 15 13:23:04 2018 +0200
@@ -9,36 +9,43 @@
 import com.passus.net.http.HttpRequestBuilder;
 import com.passus.net.http.HttpResponse;
 import com.passus.net.http.HttpResponseBuilder;
-import com.passus.net.http.HttpStatus;
-import com.passus.net.http.URL;
 import com.passus.st.AppUtils;
 import com.passus.st.ParametersBag;
-import static com.passus.st.client.http.HttpConsts.TAG_SESSION_ID;
 import com.passus.st.client.http.HttpFlowContext;
 import com.passus.st.client.http.HttpScopes;
-import java.text.ParseException;
-import java.util.HashMap;
-import static org.testng.AssertJUnit.*;
 import org.testng.annotations.AfterClass;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
+import java.text.ParseException;
+import java.util.HashMap;
+
+import static com.passus.st.client.http.HttpConsts.TAG_SESSION_ID;
+import static com.passus.st.utils.Assert.assertEquals;
+import static com.passus.st.utils.Assert.assertTrue;
+
 /**
- *
  * @author mikolaj.podbielski
  */
 public class HttpMessageWrapperTest {
 
     private static final String SESSION_ID = "sessId";
+
     HttpRequest req = HttpRequestBuilder.get("http://example.com/index?action=delete")
-            .header("User-Agent", "stress-tester").cookie("id", "qowdhncdlasndf")
+            .header("User-Agent", "stress-tester")
+            .cookie("cookie1", "cookie1Val")
             .tag(TAG_SESSION_ID, SESSION_ID).build();
-    HttpResponse resp = HttpResponseBuilder.ok().header("Content-Type", "text/plain")
-            .cookie("id", "newid").tag(TAG_SESSION_ID, SESSION_ID).build();
+
+    HttpResponse resp = HttpResponseBuilder.ok()
+            .header("Content-Type", "text/plain")
+            .cookie("cookie1", "cookie1Val")
+            .tag(TAG_SESSION_ID, SESSION_ID).build();
+
     HttpScopes scopes = new HttpScopes();
     HttpFlowContext context = new HttpFlowContext(null, scopes);
 
     {
+        scopes.getGlobal().set("GlobalVar1", "GlobalVal1");
         ParametersBag session = scopes.createSession(SESSION_ID);
         session.set("Var1", "Val1");
     }
@@ -79,69 +86,6 @@
     }
 
     @Test
-    public void testBeanValueExtractor() throws ParseException {
-        BeanValueExtractor e;
-        Object value;
-
-        e = bean("@req.getVersion()");
-        value = e.extract(wrapper);
-        assertTrue(value instanceof ByteString);
-        assertEquals("HTTP/1.1", value.toString());
-
-        e = bean("@req.method");
-        value = e.extract(wrapper);
-        assertEquals("GET", value.toString());
-
-        e = bean("@req.version");
-        value = e.extract(wrapper);
-        assertEquals("HTTP/1.1", value.toString());
-
-        e = bean("@req.uri");
-        value = e.extract(wrapper);
-        assertEquals("/index?action=delete", value.toString());
-
-        e = bean("@req.url");
-        value = e.extract(wrapper);
-        assertTrue(value instanceof URL);
-
-        e = bean("@req.url.query");
-        value = e.extract(wrapper);
-        assertEquals("action=delete", value.toString());
-
-        e = bean("@req.getHeader('User-Agent')");
-        value = e.extract(wrapper);
-        assertEquals("stress-tester", value.toString());
-
-        e = bean("@resp.status");
-        value = e.extract(wrapper);
-        assertTrue(value instanceof HttpStatus);
-
-        e = bean("@resp.status.code");
-        value = e.extract(wrapper);
-        assertEquals(200, value);
-
-        e = bean("@resp.getHeader('Content-Type')");
-        value = e.extract(wrapper);
-        assertEquals("text/plain", value.toString());
-
-        e = bean("@context");
-        value = e.extract(wrapper);
-        assertNull(value);
-
-        e = bean("@scopes");
-        value = e.extract(wrapper);
-        assertTrue(value instanceof HttpScopes);
-
-        e = bean("@httpSession");
-        value = e.extract(wrapper);
-        assertTrue(value instanceof ParametersBag);
-
-        e = bean("@httpSession.get('Var1')");
-        value = e.extract(wrapper);
-        assertEquals("Val1", value);
-    }
-
-    @Test
     public void testBeanValueExtractorWithMap() throws ParseException {
         BeanValueExtractor e;
         Object value;
@@ -166,11 +110,7 @@
         e = mvel("req.version");
         value = e.extract(wrapper);
         assertEquals("HTTP/1.1", value.toString());
-
-//        e = mvel("req.url");
-//        value = e.extract(wrapper);
-//        // String
-
+        
         e = mvel("req.url.query");
         value = e.extract(wrapper);
         assertEquals("action=delete", value.toString());
@@ -183,19 +123,19 @@
         value = e.extract(wrapper);
         assertEquals(200, value);
 
-        e = mvel("httpSession.get('Var1')");
+        e = mvel("sessionParams.get('Var1')");
         value = e.extract(wrapper);
         assertEquals("Val1", value);
 
-        e = mvel("httpSession.get('NoSuchVar') or 'defaultValue'");
+        e = mvel("sessionParams.get('NoSuchVar') or 'defaultValue'");
         value = e.extract(wrapper);
         assertEquals("defaultValue", value);
 
-        e = mvel("httpSession.get('NoSuchVar') or httpSession.get('Var1')");
+        e = mvel("sessionParams.get('NoSuchVar') or sessionParams.get('Var1')");
         value = e.extract(wrapper);
         assertEquals("Val1", value);
 
-        e = mvel("httpSession.get('Var1') + httpSession.get('Var1')");
+        e = mvel("sessionParams.get('Var1') + sessionParams.get('Var1')");
         value = e.extract(wrapper);
         assertEquals("Val1Val1", value);
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/test/java/com/passus/st/filter/HttpMessageValueExtractorTest.java	Fri Jun 15 13:23:04 2018 +0200
@@ -0,0 +1,122 @@
+package com.passus.st.filter;
+
+import com.passus.filter.ValueExtractor;
+import com.passus.net.http.*;
+import com.passus.st.ParametersBag;
+import com.passus.st.client.http.HttpFlowContext;
+import com.passus.st.client.http.HttpScopes;
+import com.passus.st.client.http.filter.HttpMessageWrapper;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+import java.util.Objects;
+
+import static com.passus.st.client.http.HttpConsts.TAG_SESSION_ID;
+import static org.testng.AssertJUnit.assertEquals;
+
+public class HttpMessageValueExtractorTest {
+
+    private final HttpMessageWrapperExtractorResolver resolver = new HttpMessageWrapperExtractorResolver();
+
+    private final String SESSION_ID = "sessId";
+
+    private final HttpRequest req = HttpRequestBuilder.get("http://example.com/index?action=delete")
+            .header("User-Agent", "stress-tester")
+            .cookie("reqCookie1", "reqCookie1Val")
+            .tag(TAG_SESSION_ID, SESSION_ID).build();
+
+    private final HttpResponse resp = HttpResponseBuilder.ok()
+            .header("Content-Type", "text/plain")
+            .cookie("respCookie1", "respCookie1Val")
+            .tag(TAG_SESSION_ID, SESSION_ID).build();
+
+    private final HttpScopes scopes = new HttpScopes();
+    private final HttpFlowContext context = new HttpFlowContext(null, scopes);
+
+    {
+        scopes.getConversation(req).set("ConvVar1", "ConvVal1");
+        scopes.getGlobal().set("GlobalVar1", "GlobalVal1");
+        ParametersBag session = scopes.createSession(SESSION_ID);
+        session.set("Var1", "Val1");
+    }
+
+    private final HttpMessageWrapper wrapper = new HttpMessageWrapper(req, resp, context);
+
+    private void assertExtractor(String expr, Object expected) {
+        ValueExtractor e = resolver.resolveValueExtractor(expr);
+        Object value = e.extract(wrapper);
+
+        if (expected instanceof Class) {
+            assertEquals(expected, value.getClass());
+        } else if (expected instanceof String) {
+            assertEquals(expected, Objects.toString(value));
+        } else {
+            assertEquals(expected, value);
+        }
+    }
+
+    @DataProvider(name = "expressions")
+    public Object[][] expressions() {
+        return new Object[][]{
+                //Request
+                {"req.getVersion()", "HTTP/1.1"},
+                {"req.version", "HTTP/1.1"},
+                {"req.method", "GET"},
+                {"req.uri", "/index?action=delete"},
+                {"req.url", URL.class},
+                {"req.url.scheme", "http"},
+                {"req.url.query", "action=delete"},
+                {"req.url.host", "example.com"},
+                {"req.getHeader('User-Agent')", "stress-tester"},
+                {"req.getHeader('Unknown')", null},
+                {"req.header@User-Agent", "stress-tester"},
+                {"req.header@Unknown", null},
+                {"req.getCookie('reqCookie1')", "reqCookie1Val"},
+                {"req.getCookie('Unknown')", null},
+                {"req.cookie@reqCookie1", "reqCookie1Val"},
+                {"req.cookie@Unknown", null},
+
+                //Response
+                {"resp.status", HttpStatus.class},
+                {"resp.status.code", 200},
+                {"resp.status.reasonPhrase", "OK"},
+                {"resp.status.informational", false},
+                {"resp.status.successful", true},
+                {"resp.status.redirection", false},
+                {"resp.status.clientError", false},
+                {"resp.status.serverError", false},
+                {"resp.getHeader('Content-Type')", "text/plain"},
+                {"resp.getHeader('Unknown')", null},
+                {"resp.header@Content-Type", "text/plain"},
+                {"resp.header@Unknown)", null},
+                {"resp.getCookie('respCookie1')", "respCookie1Val"},
+                {"resp.getCookie('Unknown')", null},
+                {"resp.cookie@respCookie1", "respCookie1Val"},
+                {"resp.cookie@Unknown", null},
+
+                //Scopes
+                {"scopes", HttpScopes.class},
+                {"scopes.global", ParametersBag.class},
+                {"scopes.global.get('GlobalVar1')", "GlobalVal1"},
+                {"globalParams", ParametersBag.class},
+                {"globalParams.get('GlobalVar1')", "GlobalVal1"},
+                {"globalParams@GlobalVar1", "GlobalVal1"},
+                {"sessionParams", ParametersBag.class},
+                {"sessionParams.get('Var1')", "Val1"},
+                {"sessionParams@Var1", "Val1"},
+                {"convParams", ParametersBag.class},
+                {"convParams.get('ConvVar1')", "ConvVal1"},
+                {"convParams@ConvVar1", "ConvVal1"},
+
+                //Other
+                {"context", null},
+        };
+    }
+
+    @Test(dataProvider = "expressions")
+    public void testResolveAndExtract(String expr, Object expected) {
+        assertExtractor(expr, expected);
+    }
+
+
+}
\ No newline at end of file
--- a/stress-tester/src/test/java/com/passus/st/filter/HttpMessageWrapperStaticExtractorTest.java	Wed Jun 06 10:02:06 2018 +0200
+++ b/stress-tester/src/test/java/com/passus/st/filter/HttpMessageWrapperStaticExtractorTest.java	Fri Jun 15 13:23:04 2018 +0200
@@ -37,7 +37,7 @@
     private final String S_RESP_JSON_CONTENT = "resp.content|json:$.respNode";
     private final String S_RESP_REGEXP_CONTENT = "resp.content|regexp:(\\:.+)";
 
-    private final String S_SESS_VAR = "sess@mno";
+    private final String S_SESS_VAR = "sessionParams@mno";
 
     private final HttpRequest REQ = HttpRequestBuilder.get("http://www.example.com")
             .cookie("abc", "1").header("def", "2").tag(HttpConsts.TAG_SESSION_ID, "sid")