changeset 618:28da18935579

extractors
author Devel 1
date Thu, 12 Oct 2017 16:25:02 +0200
parents f8118e01a91a
children 26f51466052b
files stress-tester/src/main/java/com/passus/st/filter/HttpMessageFieldExtractorFactory.java stress-tester/src/main/java/com/passus/st/filter/HttpMessageHeaderValueExtractor.java stress-tester/src/main/java/com/passus/st/filter/HttpMessageValueExtractor.java stress-tester/src/test/java/com/passus/st/filter/HttpMessageHeaderValueExtractorTest.java stress-tester/src/test/java/com/passus/st/filter/HttpMessageValueExtractorTest.java
diffstat 5 files changed, 411 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/filter/HttpMessageFieldExtractorFactory.java	Thu Oct 12 16:25:02 2017 +0200
@@ -0,0 +1,23 @@
+package com.passus.st.filter;
+
+import com.passus.filter.ValueExtractor;
+import com.passus.filter.config.FieldExtractorFactory;
+
+/**
+ *
+ * @author mikolaj.podbielski
+ */
+public class HttpMessageFieldExtractorFactory extends FieldExtractorFactory {
+
+    @Override
+    public ValueExtractor createFieldExtractor(String fieldName) {
+        ValueExtractor fieldExtractor = super.createFieldExtractor(fieldName);
+        HttpMessageHeaderValueExtractor.Operation operation = HttpMessageHeaderValueExtractor.createForFunction(fieldName);
+        if (operation != null) {
+            return new HttpMessageHeaderValueExtractor(operation, fieldExtractor);
+        } else {
+            return new HttpMessageValueExtractor(fieldName, fieldExtractor);
+        }
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/filter/HttpMessageHeaderValueExtractor.java	Thu Oct 12 16:25:02 2017 +0200
@@ -0,0 +1,177 @@
+package com.passus.st.filter;
+
+import com.passus.data.ByteString;
+import com.passus.filter.ValueExtractor;
+import com.passus.st.client.http.filter.HttpMessageWrapper;
+import java.util.Objects;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ *
+ * @author mikolaj.podbielski
+ */
+public class HttpMessageHeaderValueExtractor implements ValueExtractor {
+
+    static final Pattern REQ_COOKIE_FUN = Pattern.compile("req\\.getCookie\\(.*'(.*)'\\)");
+    static final Pattern REQ_HEADER_FUN = Pattern.compile("req\\.getHeader\\(.*'(.*)'\\)");
+    static final Pattern RESP_COOKIE_FUN = Pattern.compile("resp\\.getCookie\\(.*'(.*)'\\)");
+    static final Pattern RESP_HEADER_FUN = Pattern.compile("resp\\.getHeader\\(.*'(.*)'\\)");
+
+    static final String REQ_COOKIE_SF = "req.cookie@";
+    static final String REQ_HEADER_SF = "req.header@";
+    static final String RESP_COOKIE_SF = "resp.cookie@";
+    static final String RESP_HEADER_SF = "resp.header@";
+
+    static abstract class Operation {
+
+        protected String name;
+
+        public abstract Object extract(HttpMessageWrapper wrapper);
+
+        @Override
+        public int hashCode() {
+            return getClass().hashCode() ^ name.hashCode();
+        }
+
+        @Override
+        public boolean equals(Object obj) {
+            if (this == obj) {
+                return true;
+            }
+            if (obj == null) {
+                return false;
+            }
+            if (getClass() != obj.getClass()) {
+                return false;
+            }
+            final Operation other = (Operation) obj;
+            return name.equals(other.name);
+        }
+    }
+
+    private static class GetReqCookieOperation extends Operation {
+
+        @Override
+        public ByteString extract(HttpMessageWrapper wrapper) {
+            return wrapper.getReq().getCookie(name);
+        }
+    }
+
+    private static class GetReqHeaderOperation extends Operation {
+
+        @Override
+        public ByteString extract(HttpMessageWrapper wrapper) {
+            return wrapper.getReq().getHeader(name);
+        }
+    }
+
+    private static class GetRespCookieOperation extends Operation {
+
+        @Override
+        public ByteString extract(HttpMessageWrapper wrapper) {
+            return wrapper.getResp().getCookie(name);
+        }
+    }
+
+    private static class GetRespHeaderOperation extends Operation {
+
+        @Override
+        public ByteString extract(HttpMessageWrapper wrapper) {
+            return wrapper.getResp().getHeader(name);
+        }
+    }
+
+    static Operation createForFunction(String expr) {
+        Operation op = null;
+        String name;
+        if ((name = matchAndExtract(REQ_COOKIE_FUN, expr)) != null) {
+            op = new GetReqCookieOperation();
+            op.name = name;
+        } else if ((name = matchAndExtract(REQ_HEADER_FUN, expr)) != null) {
+            op = new GetReqHeaderOperation();
+            op.name = name;
+        } else if ((name = matchAndExtract(RESP_COOKIE_FUN, expr)) != null) {
+            op = new GetRespCookieOperation();
+            op.name = name;
+        } else if ((name = matchAndExtract(RESP_HEADER_FUN, expr)) != null) {
+            op = new GetRespHeaderOperation();
+            op.name = name;
+        }
+        return op;
+    }
+
+    static Operation createForShortForm(String expr) {
+        Operation op = null;
+        if (expr.startsWith(REQ_COOKIE_SF)) {
+            op = new GetReqCookieOperation();
+            op.name = expr.substring(REQ_COOKIE_SF.length());
+        } else if (expr.startsWith(REQ_HEADER_SF)) {
+            op = new GetReqHeaderOperation();
+            op.name = expr.substring(REQ_HEADER_SF.length());
+        } else if (expr.startsWith(RESP_COOKIE_SF)) {
+            op = new GetRespCookieOperation();
+            op.name = expr.substring(RESP_COOKIE_SF.length());
+        } else if (expr.startsWith(RESP_HEADER_SF)) {
+            op = new GetRespHeaderOperation();
+            op.name = expr.substring(RESP_HEADER_SF.length());
+        }
+        return op;
+    }
+
+    static String matchAndExtract(Pattern pattern, String fieldName) {
+        Matcher matcher = pattern.matcher(fieldName);
+        if (matcher.find()) {
+            matcher.groupCount();
+            if (matcher.groupCount() >= 1) {
+                int start = matcher.start(1);
+                int end = matcher.end(1);
+                if (start != -1 && end != -1) {
+                    return fieldName.substring(start, end);
+                }
+            }
+        }
+        return null;
+    }
+
+    private final ValueExtractor fallbackExtractor;
+
+    private final Operation operation;
+
+    HttpMessageHeaderValueExtractor(Operation operation, ValueExtractor fallbackExtractor) {
+        this.operation = operation;
+        this.fallbackExtractor = fallbackExtractor;
+    }
+
+    @Override
+    public Object extract(Object object) {
+        if (object instanceof HttpMessageWrapper) {
+            return extractFromWrapper((HttpMessageWrapper) object);
+        }
+
+        return fallbackExtractor.extract(object);
+    }
+
+    private Object extractFromWrapper(HttpMessageWrapper wrapper) {
+        return operation.extract(wrapper);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hashCode(fallbackExtractor) ^ Objects.hashCode(operation);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (!(obj instanceof HttpMessageHeaderValueExtractor)) {
+            return false;
+        }
+
+        final HttpMessageHeaderValueExtractor other = (HttpMessageHeaderValueExtractor) obj;
+        return Objects.equals(fallbackExtractor, other.fallbackExtractor) && Objects.equals(operation, other.operation);
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/filter/HttpMessageValueExtractor.java	Thu Oct 12 16:25:02 2017 +0200
@@ -0,0 +1,81 @@
+package com.passus.st.filter;
+
+import com.passus.filter.ValueExtractor;
+import com.passus.st.client.http.filter.HttpMessageWrapper;
+import java.util.Objects;
+
+/**
+ *
+ * @author mikolaj.podbielski
+ */
+public class HttpMessageValueExtractor implements ValueExtractor {
+
+    private final ValueExtractor fallbackExtractor;
+
+    private final String fieldName;
+
+    public HttpMessageValueExtractor(String fieldName, ValueExtractor fallbackExtractor) {
+        this.fieldName = fieldName;
+        this.fallbackExtractor = fallbackExtractor;
+    }
+
+    @Override
+    public Object extract(Object object) {
+        if (object instanceof HttpMessageWrapper) {
+            return extractFromWrapper((HttpMessageWrapper) object);
+        }
+
+        return fallbackExtractor.extract(object);
+    }
+
+    private Object extractFromWrapper(HttpMessageWrapper wrapper) {
+
+        switch (fieldName) {
+            case "req.version":
+            case "req.getVersion()":
+                return wrapper.getReq().getVersion();
+            case "req.method":
+            case "req.getMethod()":
+                return wrapper.getReq().getMethod();
+            case "req.sessionId":
+            case "req.getSessionId()":
+                return wrapper.getReq().getSessionId();
+            case "req.url":
+            case "req.getUrl()":
+                return wrapper.getReq().getUrl();
+            case "req.uri":
+            case "req.getUri()":
+                return wrapper.getReq().getUri();
+            case "resp.version":
+            case "resp.getVersion()":
+                return wrapper.getResp().getVersion();
+            case "resp.status":
+            case "resp.getStatus()":
+                return wrapper.getResp().getStatus();
+            case "resp.status.code":
+            case "resp.getStatus().getCode()":
+                return wrapper.getResp().getStatus().getCode();
+            default:
+                return fallbackExtractor.extract(wrapper);
+        }
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hashCode(fallbackExtractor) ^ Objects.hashCode(fieldName);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (!(obj instanceof HttpMessageValueExtractor)) {
+            return false;
+        }
+
+        final HttpMessageValueExtractor other = (HttpMessageValueExtractor) obj;
+        return Objects.equals(fieldName, other.fieldName) && Objects.equals(fallbackExtractor, other.fallbackExtractor);
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/test/java/com/passus/st/filter/HttpMessageHeaderValueExtractorTest.java	Thu Oct 12 16:25:02 2017 +0200
@@ -0,0 +1,78 @@
+package com.passus.st.filter;
+
+import com.passus.net.http.HttpRequest;
+import com.passus.net.http.HttpRequestBuilder;
+import com.passus.net.http.HttpResponse;
+import com.passus.net.http.HttpResponseBuilder;
+import com.passus.st.client.http.filter.HttpMessageWrapper;
+import static com.passus.st.filter.HttpMessageHeaderValueExtractor.*;
+import static org.testng.AssertJUnit.*;
+import org.testng.annotations.Test;
+
+/**
+ *
+ * @author mikolaj.podbielski
+ */
+public class HttpMessageHeaderValueExtractorTest {
+
+    private static final String F_REQ_COOKIE = "req.getCookie('abc')";
+    private static final String F_REQ_HEADER = "req.getHeader('def')";
+    private static final String F_RESP_COOKIE = "resp.getCookie('ghi')";
+    private static final String F_RESP_HEADER = "resp.getHeader('jkl')";
+
+    private static final String S_REQ_COOKIE = "req.cookie@abc";
+    private static final String S_REQ_HEADER = "req.header@def";
+    private static final String S_RESP_COOKIE = "resp.cookie@ghi";
+    private static final String S_RESP_HEADER = "resp.header@jkl";
+
+    private static final HttpRequest REQ = HttpRequestBuilder.get("http://www.example.com")
+            .cookie("abc", "1").header("def", "2").build();
+
+    private static final HttpResponse RESP = HttpResponseBuilder.ok()
+            .cookie("ghi", "3").header("jkl", "4").build();
+
+    private static final HttpMessageWrapper WRAPPER = new HttpMessageWrapper(REQ, RESP, null);
+
+    @Test
+    public void testCreateForShortForm() {
+        assertNotNull(createForShortForm(S_REQ_COOKIE));
+        assertNotNull(createForShortForm(S_REQ_HEADER));
+        assertNotNull(createForShortForm(S_RESP_COOKIE));
+        assertNotNull(createForShortForm(S_RESP_HEADER));
+        assertNull(createForShortForm("req.getVersion()"));
+    }
+
+    @Test
+    public void testExtractByShortForm() {
+        assertEquals("1", createForShortForm(S_REQ_COOKIE).extract(WRAPPER).toString());
+        assertEquals("2", createForShortForm(S_REQ_HEADER).extract(WRAPPER).toString());
+        assertEquals("3", createForShortForm(S_RESP_COOKIE).extract(WRAPPER).toString());
+        assertEquals("4", createForShortForm(S_RESP_HEADER).extract(WRAPPER).toString());
+    }
+
+    @Test
+    public void testCreateForFunction() {
+        assertEquals("abc", matchAndExtract(REQ_COOKIE_FUN, F_REQ_COOKIE));
+        assertNotNull(createForFunction(F_REQ_COOKIE));
+
+        assertEquals("def", matchAndExtract(REQ_HEADER_FUN, F_REQ_HEADER));
+        assertNotNull(createForFunction(F_REQ_COOKIE));
+
+        assertEquals("ghi", matchAndExtract(RESP_COOKIE_FUN, F_RESP_COOKIE));
+        assertNotNull(createForFunction(F_REQ_COOKIE));
+
+        assertEquals("jkl", matchAndExtract(RESP_HEADER_FUN, F_RESP_HEADER));
+        assertNotNull(createForFunction(F_REQ_COOKIE));
+
+        assertNull(createForFunction("req.getVersion()"));
+    }
+
+    @Test
+    public void testExtractByFunction() {
+        assertEquals("1", createForFunction(F_REQ_COOKIE).extract(WRAPPER).toString());
+        assertEquals("2", createForFunction(F_REQ_HEADER).extract(WRAPPER).toString());
+        assertEquals("3", createForFunction(F_RESP_COOKIE).extract(WRAPPER).toString());
+        assertEquals("4", createForFunction(F_RESP_HEADER).extract(WRAPPER).toString());
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/test/java/com/passus/st/filter/HttpMessageValueExtractorTest.java	Thu Oct 12 16:25:02 2017 +0200
@@ -0,0 +1,52 @@
+package com.passus.st.filter;
+
+import com.passus.net.http.HttpRequest;
+import com.passus.net.http.HttpRequestBuilder;
+import com.passus.net.http.HttpResponse;
+import com.passus.net.http.HttpResponseBuilder;
+import com.passus.net.http.HttpStatus;
+import static com.passus.st.client.http.HttpConsts.TAG_SESSION_ID;
+import com.passus.st.client.http.filter.HttpMessageWrapper;
+import static org.testng.Assert.*;
+import org.testng.annotations.Test;
+
+/**
+ *
+ * @author mikolaj.podbielski
+ */
+public class HttpMessageValueExtractorTest {
+
+    private static final HttpRequest REQ = HttpRequestBuilder.get("http://www.example.com/1").build();
+
+    private static final HttpResponse RESP = HttpResponseBuilder.ok().build();
+
+    private static final HttpMessageWrapper WRAPPER = new HttpMessageWrapper(REQ, RESP, null);
+
+    static {
+        REQ.setTag(TAG_SESSION_ID, "sid1");
+    }
+
+    private static Object extract(String expr) {
+        return new HttpMessageValueExtractor(expr, null).extract(WRAPPER);
+    }
+
+    @Test
+    public void testExtract() {
+        assertEquals("HTTP/1.1", extract("req.version").toString());
+        assertEquals("HTTP/1.1", extract("req.getVersion()").toString());
+        assertEquals("GET", extract("req.method").toString());
+        assertEquals("GET", extract("req.getMethod()").toString());
+        assertEquals("sid1", extract("req.sessionId"));
+        assertEquals("sid1", extract("req.getSessionId()"));
+        assertEquals("http://www.example.com/1", extract("req.url").toString());
+        assertEquals("http://www.example.com/1", extract("req.getUrl()").toString());
+        assertEquals("/1", extract("req.uri").toString());
+        assertEquals("/1", extract("req.getUri()").toString());
+        assertEquals("HTTP/1.1", extract("resp.version").toString());
+        assertEquals("HTTP/1.1", extract("resp.getVersion()").toString());
+        assertEquals(HttpStatus.OK, extract("resp.status"));
+        assertEquals(HttpStatus.OK, extract("resp.getStatus()"));
+        assertEquals(200, extract("resp.status.code"));
+        assertEquals(200, extract("resp.getStatus().getCode()"));
+    }
+}