changeset 450:3d993ed9ae3a

HttpMessageModificationFilter - request query modifications
author Devel 2
date Mon, 31 Jul 2017 15:07:28 +0200
parents dbc7a42e5db3
children e4ec98696716
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
diffstat 3 files changed, 105 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilter.java	Mon Jul 31 14:32:04 2017 +0200
+++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilter.java	Mon Jul 31 15:07:28 2017 +0200
@@ -7,8 +7,6 @@
 import static com.passus.config.schema.ConfigurationSchemaBuilder.listDef;
 import static com.passus.config.schema.ConfigurationSchemaBuilder.mixedDef;
 import static com.passus.config.schema.ConfigurationSchemaBuilder.tupleDef;
-import static com.passus.config.schema.ConfigurationSchemaBuilder.mapDef;
-import static com.passus.config.schema.ConfigurationSchemaBuilder.valueDef;
 import com.passus.config.schema.KeyNameVaryListNodeDefinition;
 import com.passus.config.schema.NodeDefinition;
 import com.passus.config.schema.NodeDefinitionCreator;
@@ -25,9 +23,13 @@
 import com.passus.st.validation.HeaderNameValidator;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.function.Predicate;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
+import static com.passus.config.schema.ConfigurationSchemaBuilder.mapDef;
+import static com.passus.config.schema.ConfigurationSchemaBuilder.valueDef;
+import java.text.ParseException;
+import static com.passus.config.schema.ConfigurationSchemaBuilder.mapDef;
+import static com.passus.config.schema.ConfigurationSchemaBuilder.valueDef;
 
 /**
  *
@@ -179,6 +181,73 @@
 
     }
 
+    public static class RemoveQueryParameterOperation extends AbstractRemoveOperation {
+
+        public RemoveQueryParameterOperation(CharSequence paramName) {
+            super(paramName);
+        }
+
+        @Override
+        public void process(HttpMessage message) {
+            if (message.isRequest()) {
+                try {
+                    HELPER.removeQueryParameter((HttpRequest) message, name);
+                } catch (ParseException ex) {
+                    if (LOGGER.isDebugEnabled()) {
+                        LOGGER.debug(ex.getMessage(), ex);
+                    }
+                }
+            }
+        }
+
+    }
+
+    public static class AddQueryParameterOperation extends AbstractNameValueOperation {
+
+        public AddQueryParameterOperation(CharSequence name, CharSequence value) {
+            super(name, value);
+        }
+
+        @Override
+        public void process(HttpMessage message) {
+
+            if (message.isRequest()) {
+                try {
+                    HELPER.addQueryParameter((HttpRequest) message, name, value);
+                } catch (ParseException ex) {
+                    if (LOGGER.isDebugEnabled()) {
+                        LOGGER.debug(ex.getMessage(), ex);
+                    }
+                }
+
+            }
+        }
+
+    }
+
+    public static class SetQueryParameterOperation extends AbstractNameValueOperation {
+
+        public SetQueryParameterOperation(CharSequence name, CharSequence value) {
+            super(name, value);
+        }
+
+        @Override
+        public void process(HttpMessage message) {
+
+            if (message.isRequest()) {
+                try {
+                    HELPER.addQueryParameter((HttpRequest) message, name, value);
+                } catch (ParseException ex) {
+                    if (LOGGER.isDebugEnabled()) {
+                        LOGGER.debug(ex.getMessage(), ex);
+                    }
+                }
+
+            }
+        }
+
+    }
+
     private static abstract class RemoveParamOperation extends AbstractRemoveOperation {
 
         public RemoveParamOperation(CharSequence headerName) {
@@ -389,21 +458,18 @@
     @Override
     public int filterOutbound(HttpRequest request, HttpResponse resp, HttpFlowContext context) {
         if (!outOps.isEmpty()) {
-            HttpMessageWrapper wrapper = null;
+            boolean exec = true;
             if (predicate != null) {
-                wrapper = new HttpMessageWrapper(request, resp, context);
+                HttpMessageWrapper wrapper = new HttpMessageWrapper(request, resp, context);
+                exec = predicate.test(wrapper);
             }
 
-            if (messageType == MessageType.Request || messageType == MessageType.Both) {
-                if (predicate == null || (predicate != null && predicate.test(wrapper))) {
-                    execOperations(outOps, request);
-                }
+            if (exec && (messageType == MessageType.Request || messageType == MessageType.Both)) {
+                execOperations(outOps, request);
             }
 
-            if (messageType == MessageType.Reponse || messageType == MessageType.Both) {
-                if (predicate == null || (predicate != null && predicate.test(wrapper))) {
-                    execOperations(outOps, resp);
-                }
+            if (exec && (messageType == MessageType.Reponse || messageType == MessageType.Both)) {
+                execOperations(outOps, resp);
             }
         }
 
@@ -413,21 +479,19 @@
     @Override
     public int filterInbound(HttpRequest request, HttpResponse resp, HttpFlowContext context) {
         if (!inOps.isEmpty()) {
-            HttpMessageWrapper wrapper = null;
+            boolean exec = true;
             if (predicate != null) {
-                wrapper = new HttpMessageWrapper(request, resp, context);
+                HttpMessageWrapper wrapper = new HttpMessageWrapper(request, resp, context);
+                exec = predicate.test(wrapper);
             }
 
-            if (messageType == MessageType.Request || messageType == MessageType.Both) {
-                if (predicate == null || (predicate != null && predicate.test(wrapper))) {
-                    execOperations(inOps, request);
-                }
+            if (exec && (messageType == MessageType.Request || messageType == MessageType.Both)) {
+                execOperations(inOps, request);
+
             }
 
-            if (messageType == MessageType.Reponse || messageType == MessageType.Both) {
-                if (predicate == null || (predicate != null && predicate.test(wrapper))) {
-                    execOperations(inOps, resp);
-                }
+            if (exec && (messageType == MessageType.Reponse || messageType == MessageType.Both)) {
+                execOperations(inOps, resp);
             }
         }
 
@@ -468,7 +532,10 @@
                     .add("$setCookie", headerNodeDef)
                     .add("$removePostParam", valueDef())
                     .add("$addPostParam", paramNodeDef)
-                    .add("$setPostParam", paramNodeDef);
+                    .add("$setPostParam", paramNodeDef)
+                    .add("$removeQueryParam", paramNodeDef)
+                    .add("$addQueryParam", paramNodeDef)
+                    .add("$setQueryParam", paramNodeDef);
 
             return mapDef(
                     tupleDef("operations", operationsDef),
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformer.java	Mon Jul 31 14:32:04 2017 +0200
+++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformer.java	Mon Jul 31 15:07:28 2017 +0200
@@ -134,6 +134,14 @@
                     case "$setpostparam":
                         op = createAddParamOperation(tuple, errors, PostDataSetParamOperation.class);
                         break;
+                    case "$removequeryparam":
+                        op = createNameOperation(tuple, errors, RemoveQueryParameterOperation.class);
+                        break;
+                    case "$addqueryparam":
+                        op = createAddParamOperation(tuple, errors, AddQueryParameterOperation.class);
+                        break;
+                    case "$setqueryparam":
+                        op = createAddParamOperation(tuple, errors, SetQueryParameterOperation.class);
                     default:
                         throw new IllegalStateException("Not supported operation '" + opName + "'.");
                 }
--- a/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTest.java	Mon Jul 31 14:32:04 2017 +0200
+++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTest.java	Mon Jul 31 15:07:28 2017 +0200
@@ -11,7 +11,6 @@
 import java.util.Arrays;
 import java.util.List;
 import static org.testng.AssertJUnit.*;
-import org.testng.TestNG;
 import org.testng.annotations.AfterClass;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
@@ -36,7 +35,7 @@
 
     @Test
     public void testFilterOutbound() throws Exception {
-        HttpRequest req = HttpRequestBuilder.post("http://test.com/path/test", "param1=value1&param2=value2&param3=value3")
+        HttpRequest req = HttpRequestBuilder.post("http://test.com/path/test?uParam1=1", "param1=value1&param2=value2&param3=value3")
                 .header("Header1", "Header1Value1")
                 .header("Header2", "Header1Value2")
                 .header("Header3", "Header1Value3")
@@ -58,9 +57,14 @@
         filter.addOperation(new PostDataAddParamOperation("param5", "value5"));
         filter.addOperation(new PostDataAddParamOperation("param6", Arrays.asList("value6a", "value6b")));
         filter.addOperation(new PostDataSetParamOperation("param2", "value2a"));
+        filter.addOperation(new RemoveQueryParameterOperation("uParam1"));
+        filter.addOperation(new SetQueryParameterOperation("uParam2", "2"));
+        filter.addOperation(new AddQueryParameterOperation("uParam2", "2"));
 
         filter.filterOutbound(req, null, null);
 
+        assertEquals("/path/test?uParam2=2", req.getUri().toString());
+        
         HttpHeaders headers = req.getHeaders();
         assertFalse(headers.contains("Header1"));
         assertEquals("HeaderValue4", headers.get("Header4").toString());