changeset 828:f59c62ed5c4b

ST-109
author Devel 1
date Fri, 19 Jan 2018 11:45:08 +0100
parents e8a25190d143
children 97154448d880
files stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilter.java stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformer.java stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTest.java stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformerTest.java
diffstat 4 files changed, 92 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilter.java	Fri Jan 19 09:57:28 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilter.java	Fri Jan 19 11:45:08 2018 +0100
@@ -15,7 +15,6 @@
 import com.passus.net.http.HttpRequest;
 import com.passus.net.http.HttpResponse;
 import com.passus.st.client.http.HttpFlowContext;
-import com.passus.st.client.http.extractor.ContentExtractorUtils;
 import com.passus.st.client.http.extractor.ContentReplacer;
 import com.passus.st.config.HeaderOperationNodeDefinition;
 import com.passus.st.plugin.PluginConstants;
@@ -89,13 +88,13 @@
 
     }
 
-    private static abstract class AbstractNameValueOperation extends Operation {
+    protected static abstract class AbstractNameValueOperation extends Operation {
 
         protected final ByteString name;
 
         protected final ValueExtractor valueExtractor;
 
-        protected boolean escape;
+        protected boolean escape = true;
 
         public AbstractNameValueOperation(CharSequence name, CharSequence value) {
             this(name, new UnmutableValueExtractor(value));
@@ -274,7 +273,7 @@
         @Override
         protected void doProcess(CharSequence value, HttpRequest req, HttpResponse resp, HttpFlowContext context) {
             try {
-                HttpMessageHelper.get().addQueryParameter(req, name, value);
+                HttpMessageHelper.get().addQueryParameter(req, name, value, escape);
             } catch (ParseException ex) {
                 if (LOGGER.isDebugEnabled()) {
                     LOGGER.debug(ex.getMessage(), ex);
@@ -297,7 +296,7 @@
         @Override
         protected void doProcess(CharSequence value, HttpRequest req, HttpResponse resp, HttpFlowContext context) {
             try {
-                HttpMessageHelper.get().setQueryParameter(req, name, value);
+                HttpMessageHelper.get().setQueryParameter(req, name, value, escape);
             } catch (ParseException ex) {
                 if (LOGGER.isDebugEnabled()) {
                     LOGGER.debug(ex.getMessage(), ex);
@@ -334,7 +333,7 @@
 
         protected final ValueExtractor extractor;
 
-        protected boolean escape;
+        protected boolean escape = true;
 
         public AddParamOperation(CharSequence name, Object value) {
             Assert.notNull(name, "name");
@@ -445,9 +444,9 @@
         @Override
         public void process(HttpRequest req, HttpResponse resp, HttpFlowContext context) {
             try {
-                HttpParameters params = HttpMessageHelper.get().decodeFormUrlencoded(req);
+                HttpParameters params = HttpMessageHelper.get().decodeFormUrlencoded(req, escape);
                 doAdd(params, req, resp, context);
-                HttpMessageHelper.get().setFormUrlencoded(req, params);
+                HttpMessageHelper.get().setFormUrlencoded(req, params, escape);
             } catch (Exception e) {
                 if (LOGGER.isDebugEnabled()) {
                     LOGGER.debug(e.getMessage(), e);
@@ -470,9 +469,9 @@
         @Override
         public void process(HttpRequest req, HttpResponse resp, HttpFlowContext context) {
             try {
-                HttpParameters params = HttpMessageHelper.get().decodeFormUrlencoded(req);
+                HttpParameters params = HttpMessageHelper.get().decodeFormUrlencoded(req, escape);
                 doSet(params, req, resp, context);
-                HttpMessageHelper.get().setFormUrlencoded(req, params);
+                HttpMessageHelper.get().setFormUrlencoded(req, params, escape);
             } catch (Exception e) {
                 if (LOGGER.isDebugEnabled()) {
                     LOGGER.debug(e.getMessage(), e);
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformer.java	Fri Jan 19 09:57:28 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformer.java	Fri Jan 19 11:45:08 2018 +0100
@@ -90,8 +90,8 @@
                 }
             }
 
-            if( op instanceof AddParamOperation ) {
-                ((AddParamOperation)op).setEscape(escape);
+            if (op instanceof AbstractNameValueOperation) {
+                ((AbstractNameValueOperation) op).setEscape(escape);
             }
         }
 
@@ -142,8 +142,8 @@
                 }
             }
 
-            if( op instanceof AddParamOperation ) {
-                ((AddParamOperation)op).setEscape(escape);
+            if (op instanceof AddParamOperation) {
+                ((AddParamOperation) op).setEscape(escape);
             }
         }
 
--- a/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTest.java	Fri Jan 19 09:57:28 2018 +0100
+++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTest.java	Fri Jan 19 11:45:08 2018 +0100
@@ -91,9 +91,22 @@
         filter.addOperation(new AddQueryParameterOperation("uParam2", "3@"));
         filter.addOperation(new AddQueryParameterOperation("uParam3", "3@"));
 
+        PostDataAddParamOperation addPostRaw = new PostDataAddParamOperation("param7", "{@var7}");
+        addPostRaw.setEscape(false);
+        filter.addOperation(addPostRaw);
+        PostDataSetParamOperation setPostRaw = new PostDataSetParamOperation("param8", "{@var8}");
+        setPostRaw.setEscape(false);
+        filter.addOperation(setPostRaw);
+        AddQueryParameterOperation addQueryRaw = new AddQueryParameterOperation("uParam4", "{@var4}");
+        addQueryRaw.setEscape(false);
+        filter.addOperation(addQueryRaw);
+        SetQueryParameterOperation setQueryRaw = new SetQueryParameterOperation("uParam5", "{@var5}");
+        setQueryRaw.setEscape(false);
+        filter.addOperation(setQueryRaw);
+
         filter.filterOutbound(req, null, null);
 
-        assertEquals("/path/test?uParam2=2%40&uParam3=3%40", req.getUri().toString());
+        assertEquals("/path/test?uParam2=2%40&uParam3=3%40&uParam4={@var4}&uParam5={@var5}", req.getUri().toString());
 
         HttpHeaders headers = req.getHeaders();
         assertFalse(headers.contains("Header1"));
@@ -102,8 +115,9 @@
         assertEquals("Header1Value3a", headers.get("Header3").toString());
         assertEquals("myCookie1=myValue1;myCookie2=myValue2ab;myCookie4=myValue4@;myCookie6=myValue6@", headers.get("Cookie").toString());
 
-        HttpParameters params = HttpMessageHelper.get().decodeFormUrlencoded(req);
-        assertEquals("param1=value1&param5=value5%40&param6=value6a&param6=value6b&param2=value2a%40", params.toString());
+        System.out.println(helper.contentToString(req));
+        assertEquals("param1=value1&param5=value5%40&param6=value6a&param6=value6b&param2=value2a%40"
+                + "&param7={@var7}&param8={@var8}", helper.contentToString(req));
     }
 
     @Test(enabled = true, dataProvider = "contents")
@@ -130,8 +144,8 @@
                 + "            '*escape': true\n"
                 + "            Header4: HeaderValue4\n"
                 + "        addHeader: \n"
+                + "            Header4: HeaderValue4a\n"
                 + "            '*escape': false\n"
-                + "            Header4: HeaderValue4a\n"
                 + "        setHeader: \n"
                 + "            Header5: HeaderValue5\n"
                 + "        setHeader: \n"
@@ -143,7 +157,6 @@
 
         Errors errors = new Errors();
         List<HttpFilter> filters = HttpFiltersConfigurator.getFilters(filterConfig, errors, null);
-        errors.getAllErrors().forEach(System.out::println);
         assertEquals(0, errors.getErrorCount());
         assertEquals(1, filters.size());
         assertTrue(filters.get(0) instanceof HttpMessageModificationFilter);
--- a/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformerTest.java	Fri Jan 19 09:57:28 2018 +0100
+++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformerTest.java	Fri Jan 19 11:45:08 2018 +0100
@@ -6,9 +6,7 @@
 import com.passus.config.CValueNode;
 import com.passus.config.YamlConfigurationReader;
 import com.passus.config.validation.Errors;
-import com.passus.st.client.http.extractor.PostValueExtractor;
 import com.passus.st.client.http.filter.HttpMessageModificationFilter.*;
-import java.lang.reflect.Field;
 import java.util.List;
 import static org.testng.AssertJUnit.*;
 import org.testng.annotations.Test;
@@ -87,18 +85,23 @@
     @Test
     public void testTransform_options() throws Exception {
         String config = "operations:\n"
-                //                + "  setCookie:\n"
-                //                + "    C2: value\n"
-                //                + "  addCookie:\n"
-                //                + "    C3: value\n"
-                //                + "  setQueryParam:\n"
-                //                + "    Q2: value\n"
-                //                + "  addQueryParam:\n"
-                //                + "    Q3: value\n"
-                //                + "  setPostParam:\n"
-                //                + "    P2: value\n"
-                //                + "  addPostParam:\n"
-                //                + "    P3: value\n"
+                + "  addPostParam:\n"
+                + "    P0: value0\n"
+                + "  addPostParam:\n"
+                + "    '*escape': true\n"
+                + "    P1: value1\n"
+                + "  addPostParam:\n"
+                + "    P2: value2\n"
+                + "    '*escape': false\n"
+                + "  setPostParam:\n"
+                + "    P3: value3\n"
+                + "    '*escape': false\n"
+                + "  setQueryParam:\n"
+                + "    Q4: value4\n"
+                + "    '*escape': false\n"
+                + "  addQueryParam:\n"
+                + "    Q5: value5\n"
+                + "    '*escape': false\n"
                 + "  setContent:\n"
                 + "    post:fld1: '{@var}'\n"
                 + "  setContent:\n"
@@ -113,15 +116,52 @@
         List<Operation> operations = (List) value.getValue();
 
         assertEquals(0, errors.getErrorCount());
-//        assertEquals(7, operations.size());
-        System.out.println("size: " + operations.size());
+        assertEquals(8, operations.size());
 
-        assertTrue(getEscapeValue(operations.get(0)));
-        assertFalse(getEscapeValue(operations.get(1)));
+        assertTrue(ReflectionUtils.getInheritedField(operations.get(0), "escape"));
+
+        Operation op1 = operations.get(1);
+        assertEquals("P1", getName(op1));
+        assertEquals("value1", getExtractorValue(op1));
+        assertTrue(ReflectionUtils.getInheritedField(op1, "escape"));
+
+        Operation op2 = operations.get(2);
+        assertEquals("P2", getName(op2));
+        assertEquals("value2", getExtractorValue(op2));
+        assertFalse(ReflectionUtils.getInheritedField(op2, "escape"));
+
+        Operation op3 = operations.get(3);
+        assertEquals("P3", getName(op3));
+        assertEquals("value3", getExtractorValue(op3));
+        assertFalse(ReflectionUtils.getInheritedField(op3, "escape"));
+
+        Operation op4 = operations.get(4);
+        assertEquals("q4", getName(op4));
+        assertEquals("value4", getExtractorValue2(op4));
+        assertFalse(ReflectionUtils.getInheritedField(op4, "escape"));
+
+        Operation op5 = operations.get(5);
+        assertEquals("q5", getName(op5));
+        assertEquals("value5", getExtractorValue2(op5));
+        assertFalse(ReflectionUtils.getInheritedField(op5, "escape"));
+
+        assertTrue(getReplacerEscape(operations.get(6)));
+        assertFalse(getReplacerEscape(operations.get(7)));
     }
 
-    private static boolean getEscapeValue(Operation op) throws Exception {
-        PostValueExtractor replacer = ReflectionUtils.getField(op, "replacer");
-        return ReflectionUtils.getField(replacer, "escape");
+    private static boolean getReplacerEscape(Operation o) {
+        return ReflectionUtils.getFieldByPath(o, "replacer.escape");
+    }
+
+    private static String getName(Operation o) {
+        return ReflectionUtils.getInheritedFieldByPath(o, "name").toString();
+    }
+
+    private static Object getExtractorValue(Operation o) {
+        return ReflectionUtils.getInheritedFieldByPath(o, "extractor.value");
+    }
+
+    private static Object getExtractorValue2(Operation o) {
+        return ReflectionUtils.getInheritedFieldByPath(o, "valueExtractor.value");
     }
 }