changeset 901:a609224a38df

messageModify/extractors - bugfix
author Devel 1
date Thu, 12 Apr 2018 10:24:14 +0200
parents 8cb5192c4b62
children f8b01b4c713f
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/HttpMessageModificationFilterTransformerTest.java
diffstat 3 files changed, 77 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilter.java	Tue Apr 10 13:47:02 2018 +0200
+++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilter.java	Thu Apr 12 10:24:14 2018 +0200
@@ -332,7 +332,7 @@
 
         protected final ByteString name;
 
-        protected final ValueExtractor extractor;
+        protected final ValueExtractor valueExtractor;
 
         protected boolean escape = true;
 
@@ -341,7 +341,7 @@
             Assert.notNull(value, "value");
 
             if (value instanceof CharSequence) {
-                this.extractor = new UnmutableValueExtractor(value);
+                this.valueExtractor = new UnmutableValueExtractor(value);
             } else if (value instanceof List) {
                 for (Object val : (List) value) {
                     if (!(val instanceof CharSequence)) {
@@ -349,7 +349,7 @@
                     }
                 }
 
-                this.extractor = new UnmutableValueExtractor(value);
+                this.valueExtractor = new UnmutableValueExtractor(value);
             } else {
                 throw new IllegalArgumentException("Value should be CharSequence or List<CharSequence>.");
             }
@@ -361,7 +361,7 @@
             Assert.notNull(name, "name");
             Assert.notNull(extractor, "extractor");
             this.name = ByteString.create(name);
-            this.extractor = extractor;
+            this.valueExtractor = extractor;
         }
 
         public boolean isEscape() {
@@ -373,7 +373,7 @@
         }
 
         protected void doAdd(HttpParameters params, HttpRequest req, HttpResponse resp, HttpFlowContext context) {
-            Object value = HttpFiltersUtils.extractValue(extractor, req, resp, context);
+            Object value = HttpFiltersUtils.extractValue(valueExtractor, req, resp, context);
             try {
                 if (value instanceof CharSequence) {
                     params.add(name, (CharSequence) value);
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformer.java	Tue Apr 10 13:47:02 2018 +0200
+++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformer.java	Thu Apr 12 10:24:14 2018 +0200
@@ -55,7 +55,7 @@
             List<CTupleNode> tuples = ((CMapNode) nodeTuple.getNode()).getChildren();
             boolean escape = true;
             String field = null;
-            ValueExtractor valueTransformer = null;
+            ValueExtractor valueExtractor = null;
             for (CTupleNode tuple : tuples) {
                 String name = tuple.getName().toLowerCase();
                 switch (name) {
@@ -64,10 +64,10 @@
                         break;
                     default:
                         CNode valNode = tuple.getNode();
-                        if (valNode.getType() == NodeType.VALUE
-                                || valNode.getType() == NodeType.MAP) {
+                        NodeType type = valNode.getType();
+                        if (type == NodeType.VALUE || type == NodeType.MAP) {
                             try {
-                                valueTransformer = expressionNodeTransformer.transform(valNode);
+                                valueExtractor = expressionNodeTransformer.transform(valNode);
                                 field = name;
                             } catch (Exception e) {
                                 errors.reject("Invalid expression.", valNode);
@@ -84,11 +84,11 @@
                 }
             }
 
-            if (valueTransformer != null) {
+            if (valueExtractor != null) {
                 try {
                     op = clazz
                             .getConstructor(CharSequence.class, ValueExtractor.class)
-                            .newInstance(field, valueTransformer);
+                            .newInstance(field, valueExtractor);
                     op.setEscape(escape);
                 } catch (Exception e) {
                     throw new RuntimeException(e.getMessage(), e);
@@ -100,13 +100,14 @@
     }
 
     private static AddParamOperation createAddParamOperation(CTupleNode nodeTuple,
-            Errors errors, Class<? extends AddParamOperation> clazz) {
+            Errors errors, Class<? extends AddParamOperation> clazz,
+            ExpressionNodeTransformer expressionNodeTransformer) {
 
         AddParamOperation op = null;
         if (validateType(nodeTuple.getNode(), NodeType.MAP, errors)) {
             List<CTupleNode> tuples = ((CMapNode) nodeTuple.getNode()).getChildren();
             String paramName = null;
-            Object value = null;
+            ValueExtractor valueExtractor = null;
             boolean escape = true;
             for (CTupleNode tuple : tuples) {
                 String name = tuple.getName().toLowerCase();
@@ -117,18 +118,16 @@
                     default:
                         paramName = tuple.getName();
                         CNode valNode = tuple.getNode();
-                        if (valNode.getType() == NodeType.VALUE) {
-                            value = ((CValueNode) valNode).getValue().toString();
-                        } else if (valNode.getType() == NodeType.LIST) {
-                            CListNode listNode = (CListNode) valNode;
-                            List<String> values = new ArrayList<>(listNode.size());
-                            for (CNode lValNode : listNode.getChildren()) {
-                                values.add(((CValueNode) lValNode).getValue().toString());
+                        NodeType type = valNode.getType();
+                        if (type == NodeType.VALUE || type == NodeType.MAP) {
+                            try {
+                                valueExtractor = expressionNodeTransformer.transform(valNode);
+                            } catch (Exception e) {
+                                errors.reject("Invalid expression.", valNode);
+                                return null;
                             }
-
-                            value = values;
                         } else {
-                            throw new RuntimeException("Value or List node required.");
+                            throw new RuntimeException("Value or Map node required.");
                         }
                         break;
                 }
@@ -139,8 +138,8 @@
             }
             try {
                 op = clazz
-                        .getConstructor(CharSequence.class, Object.class)
-                        .newInstance(paramName, value);
+                        .getConstructor(CharSequence.class, ValueExtractor.class)
+                        .newInstance(paramName, valueExtractor);
                 op.setEscape(escape);
             } catch (Exception e) {
                 throw new RuntimeException(e.getMessage(), e);
@@ -195,10 +194,10 @@
                         op = createNameOperation(tuple, errors, PostDataRemoveParamOperation.class);
                         break;
                     case "addpostparam":
-                        op = createAddParamOperation(tuple, errors, PostDataAddParamOperation.class);
+                        op = createAddParamOperation(tuple, errors, PostDataAddParamOperation.class, transformer);
                         break;
                     case "setpostparam":
-                        op = createAddParamOperation(tuple, errors, PostDataSetParamOperation.class);
+                        op = createAddParamOperation(tuple, errors, PostDataSetParamOperation.class, transformer);
                         break;
                     case "removequeryparam":
                         op = createNameOperation(tuple, errors, RemoveQueryParameterOperation.class);
--- a/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformerTest.java	Tue Apr 10 13:47:02 2018 +0200
+++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformerTest.java	Thu Apr 12 10:24:14 2018 +0200
@@ -9,6 +9,7 @@
 import com.passus.config.ConfigurationContextImpl;
 import com.passus.config.YamlConfigurationReader;
 import com.passus.config.validation.Errors;
+import com.passus.filter.UnmutableValueExtractor;
 import com.passus.filter.ValueExtractor;
 import com.passus.st.client.http.filter.HttpMessageModificationFilter.*;
 import static com.passus.st.client.http.filter.HttpVarsFilterTest.expr;
@@ -93,6 +94,53 @@
     }
 
     @Test
+    public void testTransform_extractors() throws Exception {
+        String config = "operations:\n"
+                + "  setHeader:\n"
+                + "    H2: '@req.url'\n"
+                + "  setCookie:\n"
+                + "    C2: '@req.url'\n"
+                + "  setQueryParam:\n"
+                + "    Q2: '@req.url'\n"
+                + "  setPostParam:\n"
+                + "    P2: '@req.url'\n"
+                + "  setQueryParam:\n"
+                + "    Q2: 'req.url'\n"
+                + "  setPostParam:\n"
+                + "    P2: 'req.url'\n";
+        CTupleNode node = read(config);
+
+        Errors errors = new Errors();
+        ConfigurationContextImpl emptyContext = new ConfigurationContextImpl();
+        CValueNode value = transformer.transform(node.getNode(), errors, emptyContext);
+        HttpFilterTestUtils.printErrors(errors);
+        List<Operation> operations = (List) value.getValue();
+
+        assertEquals(0, errors.getErrorCount());
+        assertEquals(6, operations.size());
+        assertTrue(operations.get(0) instanceof SetHeaderOperation);
+        assertTrue(operations.get(1) instanceof SetCookieOperation);
+        assertTrue(operations.get(2) instanceof SetQueryParameterOperation);
+        assertTrue(operations.get(3) instanceof PostDataSetParamOperation);
+        assertTrue(operations.get(4) instanceof SetQueryParameterOperation);
+        assertTrue(operations.get(5) instanceof PostDataSetParamOperation);
+
+        SetHeaderOperation op0 = (SetHeaderOperation) operations.get(0);
+        SetCookieOperation op1 = (SetCookieOperation) operations.get(1);
+        SetQueryParameterOperation op2 = (SetQueryParameterOperation) operations.get(2);
+        PostDataSetParamOperation op3 = (PostDataSetParamOperation) operations.get(3);
+        SetQueryParameterOperation op4 = (SetQueryParameterOperation) operations.get(4);
+        PostDataSetParamOperation op5 = (PostDataSetParamOperation) operations.get(5);
+
+        assertFalse(op0.valueExtractor instanceof UnmutableValueExtractor);
+        assertFalse(op1.valueExtractor instanceof UnmutableValueExtractor);
+        assertFalse(op2.valueExtractor instanceof UnmutableValueExtractor);
+        assertFalse(op3.valueExtractor instanceof UnmutableValueExtractor);
+        assertTrue(op4.valueExtractor instanceof UnmutableValueExtractor);
+        assertTrue(op5.valueExtractor instanceof UnmutableValueExtractor);
+    }
+
+    @Test
     public void testTransform_options() throws Exception {
         String config = "operations:\n"
                 + "  addPostParam:\n"
@@ -148,12 +196,12 @@
 
         Operation op4 = operations.get(4);
         assertEquals("q4", getName(op4));
-        assertEquals("value4", getExtractorValue2(op4));
+        assertEquals("value4", getExtractorValue(op4));
         assertFalse(ReflectionUtils.getInheritedField(op4, "escape"));
 
         Operation op5 = operations.get(5);
         assertEquals("q5", getName(op5));
-        assertEquals("value5", getExtractorValue2(op5));
+        assertEquals("value5", getExtractorValue(op5));
         assertFalse(ReflectionUtils.getInheritedField(op5, "escape"));
 
         assertTrue(getReplacerEscape(operations.get(6)));
@@ -186,7 +234,7 @@
         List<Operation> operations = (List) value.getValue();
         assertEquals("req.url.path", getFieldName(operations.get(0)));
         assertEquals("req.url.host", getFieldName(operations.get(1)));
-        assertEquals("value1", getExtractorValue2(operations.get(2)));
+        assertEquals("value1", getExtractorValue(operations.get(2)));
     }
 
     private static boolean getReplacerEscape(Operation o) {
@@ -198,10 +246,6 @@
     }
 
     private static Object getExtractorValue(Operation o) {
-        return ReflectionUtils.getInheritedFieldByPath(o, "extractor.value");
-    }
-
-    private static Object getExtractorValue2(Operation o) {
         return ReflectionUtils.getInheritedFieldByPath(o, "valueExtractor.value");
     }