changeset 932:60057c161d4d

ST-111 conf parsing
author Devel 1
date Mon, 07 May 2018 13:19:05 +0200
parents 5ce6fbe6a4e7
children 55590bb0e482
files stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformer.java stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationOperations.java stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformerTest.java stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationOperationsTest.java
diffstat 4 files changed, 134 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformer.java	Fri Apr 27 16:09:10 2018 +0200
+++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformer.java	Mon May 07 13:19:05 2018 +0200
@@ -3,9 +3,11 @@
 import com.passus.config.*;
 import com.passus.config.schema.NodeTransformer;
 import com.passus.config.validation.Errors;
+import com.passus.filter.UnmutableValueExtractor;
 import com.passus.filter.ValueExtractor;
 import com.passus.filter.config.ExpressionNodeTransformer;
 import com.passus.lookup.filter.LookupValueExtractorTransformer;
+import com.passus.net.http.HttpMethod;
 import com.passus.st.client.http.extractor.ContentExtractorUtils;
 import com.passus.st.client.http.extractor.ContentReplacer;
 import com.passus.st.client.http.filter.HttpMessageModificationOperations.*;
@@ -25,7 +27,7 @@
 
     private FieldValueExtractorTransformer fieldValueExtractorTransformer = new FieldValueExtractorTransformer();
 
-    private Operation createNameOperation(CTupleNode tuple,
+    private Operation createSingleParamOperation(CTupleNode tuple,
                                           Class<? extends Operation> clazz,
                                           Errors errors, ConfigurationContext context) {
         if (validateType(tuple.getNode(), NodeType.VALUE, errors)) {
@@ -42,6 +44,50 @@
         return null;
     }
 
+    private Operation createSetUrlPathParamOperation(CTupleNode tuple,
+                                          Errors errors, ConfigurationContext context) {
+        CNode node = tuple.getNode();
+        NodeType tupleNodeType = node.getType();
+
+        if (tupleNodeType == NodeType.VALUE) {
+            return createSingleParamOperation(tuple, SetUrlPath.class, errors, context);
+
+        } else if (tupleNodeType == NodeType.MAP) {
+            CMapNode mapNode = (CMapNode) node;
+            if (mapNode.size() != 1) {
+                errors.reject(node, "Invalid node. Single child expected.");
+                return null;
+            }
+
+            CTupleNode tupleNode = mapNode.getFirstChild();
+            String selector = tupleNode.getName();
+            CValueNode valNode = (CValueNode) tupleNode.getNode();
+            ValueExtractor ve = fieldValueExtractorTransformer.transform(valNode, errors, context);
+
+            int pos = selector.indexOf(':');
+            if (pos == -1) {
+                throw new IllegalArgumentException("Invalid path selector '" + selector + "'.");
+            }
+            String selectorType = selector.substring(0, pos);
+            String selectorValue = selector.substring(pos + 1);
+
+            switch (selectorType.toLowerCase()) {
+                case "pos":
+                    int index = Integer.parseInt(selectorValue);
+                    return new SetUrlPathPos(ve, index);
+                case "regex":
+                case "regexp":
+                    return new SetUrlPathRegex(ve, selectorValue);
+                default:
+                    throw new IllegalArgumentException("Invalid path selector '" + selector + "'. Invalid selector type '" + selectorType + "'.");
+            }
+        } else {
+            errors.reject(node, "Invalid node type. Expected VALUE or MAP but " + tupleNodeType + " given.");
+            return null;
+        }
+
+    }
+    
     private AbstractNameValueOperation createNameValueOperation(CTupleNode nodeTuple,
                                                                 Class<? extends AbstractNameValueOperation> clazz,
                                                                 Errors errors, ConfigurationContext context) {
@@ -205,7 +251,7 @@
                 Operation op = null;
                 switch (opName.toLowerCase()) {
                     case "removeheader":
-                        op = createNameOperation(tuple, RemoveHeaderOperation.class, errors, context);
+                        op = createSingleParamOperation(tuple, RemoveHeaderOperation.class, errors, context);
                         break;
                     case "addheader":
                         op = createNameValueOperation(tuple, AddHeaderOperation.class, errors, context);
@@ -214,7 +260,7 @@
                         op = createNameValueOperation(tuple, SetHeaderOperation.class, errors, context);
                         break;
                     case "removecookie":
-                        op = createNameOperation(tuple, RemoveCookieOperation.class, errors, context);
+                        op = createSingleParamOperation(tuple, RemoveCookieOperation.class, errors, context);
                         break;
                     case "addcookie":
                         op = createNameValueOperation(tuple, AddCookieOperation.class, errors, context);
@@ -223,7 +269,7 @@
                         op = createNameValueOperation(tuple, SetCookieOperation.class, errors, context);
                         break;
                     case "removepostparam":
-                        op = createNameOperation(tuple, PostDataRemoveParamOperation.class, errors, context);
+                        op = createSingleParamOperation(tuple, PostDataRemoveParamOperation.class, errors, context);
                         break;
                     case "addpostparam":
                         op = createAddParamOperation(tuple, PostDataAddParamOperation.class, errors, context);
@@ -232,7 +278,7 @@
                         op = createAddParamOperation(tuple, PostDataSetParamOperation.class, errors, context);
                         break;
                     case "removequeryparam":
-                        op = createNameOperation(tuple, RemoveQueryParameterOperation.class, errors, context);
+                        op = createSingleParamOperation(tuple, RemoveQueryParameterOperation.class, errors, context);
                         break;
                     case "addqueryparam":
                         op = createNameValueOperation(tuple, AddQueryParameterOperation.class, errors, context);
@@ -243,6 +289,26 @@
                     case "setcontent":
                         op = createContentOperation(tuple, errors, context);
                         break;
+                    case "setmethod":
+                        op = createSingleParamOperation(tuple, SetMethodOperation.class, errors, context);
+                        break;
+                    case "setversion":
+                        op = createSingleParamOperation(tuple, SetVersionOperation.class, errors, context);
+                        break;
+                    case "seturi":
+                        op = createSingleParamOperation(tuple, SetUriOperation.class, errors, context);
+                        break;
+                    case "seturlquery":
+                        op = createSingleParamOperation(tuple, SetUrlQuery.class, errors, context);
+                        break;
+                    case "seturlref":
+                        op = createSingleParamOperation(tuple, SetUrlRef.class, errors, context);
+                        break;
+                    case "seturlpath":
+                    case "seturlpathpos":
+                    case "seturlpathregex":
+                        op = createSetUrlPathParamOperation(tuple, errors, context);
+                        break;
                     default:
                         throw new IllegalStateException("Not supported operation '" + opName + "'.");
                 }
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationOperations.java	Fri Apr 27 16:09:10 2018 +0200
+++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationOperations.java	Mon May 07 13:19:05 2018 +0200
@@ -545,11 +545,19 @@
 
         private final HttpMethod method;
 
-        static SetMethodOperation create(CharSequence method) {
-            return HttpUtils.isValidRequestMethod(method) ? new SetMethodOperation(HttpMethod.valueOfIgnoreCase(method)) : null;
+        public SetMethodOperation(CharSequence method) {
+            Assert.notNull(method, "method");
+            if (!HttpUtils.isValidRequestMethod(method)) {
+                throw new IllegalArgumentException("Invalid HTTP method token: '" + method + "'");
+            }
+            this.method = HttpMethod.valueOfIgnoreCase(method);
+            if (this.method == null) {
+                throw new IllegalArgumentException("Invalid HTTP method: '" + method + "'");
+            }
         }
 
-        SetMethodOperation(HttpMethod method) {
+        public SetMethodOperation(HttpMethod method) {
+            Assert.notNull(method, "method");
             this.method = method;
         }
 
@@ -563,11 +571,10 @@
 
         private final ByteString version;
 
-        static SetVersionOperation create(CharSequence version) {
-            return HttpUtils.isValidVersion(version) ? new SetVersionOperation(version) : null;
-        }
-
         public SetVersionOperation(CharSequence version) {
+            if (!HttpUtils.isValidVersion(version)) {
+                throw new IllegalArgumentException("Invalid HTTP version '" + version + "'");
+            }
             this.version = ByteString.create(version);
         }
 
--- a/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformerTest.java	Fri Apr 27 16:09:10 2018 +0200
+++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformerTest.java	Mon May 07 13:19:05 2018 +0200
@@ -98,6 +98,51 @@
     }
 
     @Test
+    public void testTransform_firstLineOperations() throws Exception {
+        String config
+                = "operations:\n"
+                + "  setMethod: HEAD\n"
+                + "  setMethod: head\n"
+                //                + "  setMethod: blah\n"
+                + "  setVersion: 'HTTP/1.2'\n"
+                + "  setUri: '/part0x/part1x/part2x?q1x=v1x'\n"
+                + "  setUrlQuery: 'qqq=vvv'\n"
+                + "  setUrlRef: anchor'\n"
+                + "  setUrlPath: '/x/y/z'\n"
+                + "  setUrlPath:\n"
+                + "    pos:2: '@req.method'\n"
+                + "  setUrlPath:\n"
+                + "    regexp:aaa(.+)bbb: new\n"
+                + "  setUrlPathRegex:\n"
+                + "    regexp:aaa(.+)bbb: new\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(10, operations.size());
+
+        assertTrue(operations.get(0) instanceof SetMethodOperation);
+        assertTrue(operations.get(1) instanceof SetMethodOperation);
+        assertTrue(operations.get(2) instanceof SetVersionOperation);
+        assertTrue(operations.get(3) instanceof SetUriOperation);
+        assertTrue(operations.get(4) instanceof SetUrlQuery);
+        assertTrue(operations.get(5) instanceof SetUrlRef);
+        assertTrue(operations.get(6) instanceof SetUrlPath);
+        assertTrue(operations.get(7) instanceof SetUrlPathPos);
+        assertTrue(operations.get(8) instanceof SetUrlPathRegex);
+        assertTrue(operations.get(9) instanceof SetUrlPathRegex);
+
+        SetUrlPathPos pathPosOp = (SetUrlPathPos) operations.get(7);
+        ValueExtractor ve = ReflectionUtils.getField(pathPosOp, "valueExtractor");
+        assertFalse(ve instanceof UnmutableValueExtractor);
+    }
+
+    @Test
     public void testTransform_extractors() throws Exception {
         String config = "operations:\n"
                 + "  setHeader:\n"
@@ -247,7 +292,7 @@
         when(lookup.getName()).thenReturn("test");
         when(lookup.lookup(any(Set.class), any(Lookup.KeyInfo.class))).thenReturn("testValue");
 
-        LookupList lookups  = new LookupList(lookup);
+        LookupList lookups = new LookupList(lookup);
         context.add(ConfigurationContextConsts.LOOKUPS, lookups);
         String config = "operations:\n"
                 + "  setHeader:\n"
@@ -259,7 +304,7 @@
         assertEquals(0, errors.getErrorCount());
 
         List<Operation> operations = (List) value.getValue();
-        SetHeaderOperation operation = (SetHeaderOperation)operations.get(0);
+        SetHeaderOperation operation = (SetHeaderOperation) operations.get(0);
         assertTrue(operation.getValueExtractor() instanceof LookupValueExtractor);
 
         LookupValueExtractor lookupValueExtractor = (LookupValueExtractor) operation.getValueExtractor();
--- a/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationOperationsTest.java	Fri Apr 27 16:09:10 2018 +0200
+++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationOperationsTest.java	Mon May 07 13:19:05 2018 +0200
@@ -31,14 +31,14 @@
 
     @Test
     public void testSetMethodOperation() {
-        SetMethodOperation op = SetMethodOperation.create("head");
+        SetMethodOperation op = new SetMethodOperation("head");
         HttpRequest req = process(op);
         assertEquals("HEAD", req.getMethod().toString());
     }
 
     @Test
     public void testSetVersionOperation() {
-        SetVersionOperation op = SetVersionOperation.create("HTTP/1.2");
+        SetVersionOperation op = new SetVersionOperation("HTTP/1.2");
         HttpRequest req = process(op);
         assertEquals("HTTP/1.2", req.getVersion().toString());
     }