changeset 636:b1f5360ef5a8

HttpMessageHeaderValueExtractor parser improvements
author Devel 2
date Thu, 02 Nov 2017 09:06:07 +0100
parents 84d8ab723e8d
children 1d9bc9f80a6a
files stress-tester/src/main/java/com/passus/st/filter/HttpMessageHeaderValueExtractor.java stress-tester/src/test/java/com/passus/st/filter/HttpMessageHeaderValueExtractorTest.java
diffstat 2 files changed, 70 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/filter/HttpMessageHeaderValueExtractor.java	Tue Oct 31 13:55:35 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/filter/HttpMessageHeaderValueExtractor.java	Thu Nov 02 09:06:07 2017 +0100
@@ -1,5 +1,6 @@
 package com.passus.st.filter;
 
+import com.passus.commons.Assert;
 import com.passus.data.ByteString;
 import com.passus.filter.ValueExtractor;
 import com.passus.net.http.HttpMessage;
@@ -31,11 +32,6 @@
     static final Pattern RESP_HEADER_FUN = Pattern.compile("resp\\.getHeader\\(.*'(.*)'\\)");
     static final Pattern HTTP_SESS_FUN = Pattern.compile("httpSession\\.get\\(.*'(.*)'\\)");
 
-    private static final String CONTENT_RULE_JSON = "@json:";
-    private static final String CONTENT_RULE_XML = "@xml:";
-    private static final String CONTENT_RULE_REGEXP = "@regexp:";
-    private static final String CONTENT_RULE_POST = "@post:";
-
     static final String REQ_COOKIE_SF = "req.cookie@";
     static final String REQ_HEADER_SF = "req.header@";
     static final String REQ_FULL_CONTENT_SF = "req.content";
@@ -141,20 +137,45 @@
         }
 
         public ExtractContentOperation(String rule) {
+            Assert.notNull(rule, "rule");
+            if (rule.length() < 4) {
+                throw new IllegalArgumentException("Invalid extractor rule '" + rule + "'.");
+            }
+
+            int pos = rule.indexOf(':');
+            if (pos == -1) {
+                throw new IllegalArgumentException("Invalid extractor rule '" + rule + "'.");
+            }
+
             try {
-                if (rule.startsWith(CONTENT_RULE_JSON)) {
-                    contentExtractor = new JsonValueExtractor(rule.substring(CONTENT_RULE_JSON.length()));
-                } else if (rule.startsWith(CONTENT_RULE_XML)) {
-                    contentExtractor = new XmlValueExtractor(rule.substring(CONTENT_RULE_XML.length()));
-                } else if (rule.startsWith(CONTENT_RULE_REGEXP)) {
-                    contentExtractor = new RegexValueExtractor(rule.substring(CONTENT_RULE_REGEXP.length()));
-                } else if (rule.startsWith(CONTENT_RULE_POST)) {
-                    contentExtractor = new PostValueExtractor(rule.substring(CONTENT_RULE_REGEXP.length()));
+                String extractorType = rule.substring(0, pos);
+                String expr = rule.substring(pos + 1);
+                switch (extractorType.toLowerCase()) {
+                    case "json":
+                        contentExtractor = new JsonValueExtractor(expr);
+                        break;
+                    case "xml":
+                        contentExtractor = new XmlValueExtractor(expr);
+                        break;
+                    case "post":
+                        contentExtractor = new PostValueExtractor(expr);
+                        break;
+                    case "regexp":
+                        contentExtractor = new RegexValueExtractor(expr);
+                        break;
+                    default:
+                        throw new IllegalArgumentException("Invalid extractor rule '" + rule + "'. Invalid extractor type '" + extractorType + "'.");
                 }
             } catch (Exception e) {
                 if (LOGGER.isDebugEnabled()) {
                     LOGGER.debug(e.getMessage(), e);
                 }
+
+                throw new IllegalArgumentException("Invalid extractor rule '" + rule + "'." + e.getMessage(), e);
+            }
+
+            if (contentExtractor == null) {
+                throw new IllegalArgumentException("Invalid extractor rule '" + rule + "'.");
             }
         }
 
@@ -265,8 +286,13 @@
         } else if (expr.startsWith(REQ_FULL_CONTENT_SF)) {
             if (REQ_FULL_CONTENT_SF.length() == expr.length()) {
                 op = new ExtractReqContentOperation();
-            } else if (expr.length() > REQ_FULL_CONTENT_SF.length()) {
-                op = new ExtractReqContentOperation(expr.substring(REQ_FULL_CONTENT_SF.length()));
+            } else {
+                String subExpr = expr.substring(REQ_FULL_CONTENT_SF.length());
+                if (subExpr.charAt(0) == '@') {
+                    op = new ExtractReqContentOperation(subExpr.substring(1));
+                } else {
+                    throw new IllegalArgumentException("Invalid extractor '" + expr + "'.");
+                }
             }
         } else if (expr.startsWith(RESP_COOKIE_SF)) {
             op = new GetRespCookieOperation();
@@ -280,8 +306,13 @@
         } else if (expr.startsWith(RESP_FULL_CONTENT_SF)) {
             if (RESP_FULL_CONTENT_SF.length() == expr.length()) {
                 op = new ExtractRespContentOperation();
-            } else if (expr.length() > RESP_FULL_CONTENT_SF.length()) {
-                op = new ExtractRespContentOperation(expr.substring(RESP_FULL_CONTENT_SF.length()));
+            } else {
+                String subExpr = expr.substring(RESP_FULL_CONTENT_SF.length());
+                if (subExpr.charAt(0) == '@') {
+                    op = new ExtractRespContentOperation(subExpr.substring(1));
+                } else {
+                    throw new IllegalArgumentException("Invalid extractor '" + expr + "'.");
+                }
             }
         }
 
--- a/stress-tester/src/test/java/com/passus/st/filter/HttpMessageHeaderValueExtractorTest.java	Tue Oct 31 13:55:35 2017 +0100
+++ b/stress-tester/src/test/java/com/passus/st/filter/HttpMessageHeaderValueExtractorTest.java	Thu Nov 02 09:06:07 2017 +0100
@@ -10,6 +10,7 @@
 import com.passus.st.client.http.filter.HttpMessageWrapper;
 import static com.passus.st.filter.HttpMessageHeaderValueExtractor.*;
 import static org.testng.AssertJUnit.*;
+import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
 
 /**
@@ -57,6 +58,19 @@
     private final HttpMessageWrapper WRAPPER = new HttpMessageWrapper(REQ, RESP, CONTEXT);
     private final HttpMessageWrapper NULL_WRAPPER = new HttpMessageWrapper(null, null, CONTEXT);
 
+    @DataProvider(name = "invalidExtractorRules")
+    public Object[][] invalidExtractorRules() {
+        return new Object[][]{
+            {"req.content@unknownLang:$.reqNode"},
+            {"req.content@json:$....reqNode"},
+            {"req.content@json:"},
+            {"req.content@json"},
+            {"req.content@"},
+            {"req.content@   "},
+            {"req.content#   "}
+        };
+    }
+
     @Test
     public void testCreateForShortForm() {
         assertNotNull(createForShortForm(S_REQ_COOKIE));
@@ -130,4 +144,12 @@
         assertEquals("5", createForFunction(F_SESS_VAR).extract(WRAPPER).toString());
     }
 
+    @Test(dataProvider = "invalidExtractorRules")
+    public void testInvalidExtractRules(String rule) {
+        try {
+            createForShortForm(rule);
+            fail("Exception required for rule '" + rule + "'.");
+        } catch (Exception e) {
+        }
+    }
 }