Mercurial > stress-tester
changeset 635:84d8ab723e8d
HttpMessageHeaderValueExtractor improvements
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/http/extractor/ContentExtractor.java Mon Oct 30 15:34:38 2017 +0100 +++ b/stress-tester/src/main/java/com/passus/st/client/http/extractor/ContentExtractor.java Tue Oct 31 13:55:35 2017 +0100 @@ -10,10 +10,10 @@ */ public interface ContentExtractor { - public default String extract(HttpMessage response) throws IOException { - String content = HttpMessageHelper.get().contentToString(response, true); + public default String extract(HttpMessage message) throws IOException { + String content = HttpMessageHelper.get().contentToString(message, true); return extract(content); } - public String extract(String content); + public String extract(String content) throws IOException; }
--- a/stress-tester/src/main/java/com/passus/st/client/http/extractor/JsonValueExtractor.java Mon Oct 30 15:34:38 2017 +0100 +++ b/stress-tester/src/main/java/com/passus/st/client/http/extractor/JsonValueExtractor.java Tue Oct 31 13:55:35 2017 +0100 @@ -10,7 +10,9 @@ */ public class JsonValueExtractor implements ContentExtractor { - private final Configuration configuration = Configuration.builder().options(DEFAULT_PATH_LEAF_TO_NULL).build(); + private final Configuration configuration = Configuration.builder() + .options(DEFAULT_PATH_LEAF_TO_NULL) + .build(); private final JsonPath path; @@ -25,10 +27,8 @@ .using(configuration) .parse(json) .read(path); -// System.out.println("path='" + path + "' result='" + result + "' class=" + (result == null ? null : result.getClass())); return result == null ? null : result.toString(); } catch (Exception ex) { -// System.out.println("path='" + path + "' exception=" + ex); return null; } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/main/java/com/passus/st/client/http/extractor/PostValueExtractor.java Tue Oct 31 13:55:35 2017 +0100 @@ -0,0 +1,51 @@ +package com.passus.st.client.http.extractor; + +import com.passus.commons.Assert; +import com.passus.data.ByteString; +import com.passus.net.http.HttpMessage; +import com.passus.net.http.HttpMessageHelper; +import com.passus.net.http.HttpParameters; +import java.io.IOException; + +/** + * + * @author Mirosław Hawrot + */ +public class PostValueExtractor implements ContentExtractor { + + protected final HttpMessageHelper helper = HttpMessageHelper.get(); + + private final ByteString fieldName; + + public PostValueExtractor(CharSequence fieldName) { + Assert.notNull(fieldName, "fieldName"); + this.fieldName = ByteString.create(fieldName); + } + + @Override + public String extract(HttpMessage message) throws IOException { + HttpParameters params = helper.decodeFormUrlencoded(message); + if (params != null) { + ByteString value = params.get(fieldName); + if (value != null) { + return value.toString(); + } + } + + return null; + } + + @Override + public String extract(String content) throws IOException { + HttpParameters params = helper.decodeFormUrlencoded(content); + if (params != null) { + ByteString value = params.get(fieldName); + if (value != null) { + return value.toString(); + } + } + + return null; + } + +}
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpFilterMessageWrapper.java Mon Oct 30 15:34:38 2017 +0100 +++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpFilterMessageWrapper.java Tue Oct 31 13:55:35 2017 +0100 @@ -24,6 +24,10 @@ request = message.isRequest(); } + public T getWrappedMessage() { + return message; + } + public boolean isRequest() { return request; } @@ -42,6 +46,10 @@ } public ByteString getCookie(CharSequence name) { + if (message == null) { + return null; + } + HttpCookie cookie = HttpMessageHelper.get().getCookie(message, name); return cookie == null ? null : cookie.getValue(); }
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageWrapper.java Mon Oct 30 15:34:38 2017 +0100 +++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageWrapper.java Tue Oct 31 13:55:35 2017 +0100 @@ -63,6 +63,10 @@ } public ParametersBag getHttpSession() { + if (req == null) { + return null; + } + String sessionId = req.getSessionId(); HttpScopes scopes = getScopes(); if (scopes != null && sessionId != null) {
--- a/stress-tester/src/main/java/com/passus/st/filter/HttpMessageHeaderValueExtractor.java Mon Oct 30 15:34:38 2017 +0100 +++ b/stress-tester/src/main/java/com/passus/st/filter/HttpMessageHeaderValueExtractor.java Tue Oct 31 13:55:35 2017 +0100 @@ -2,11 +2,20 @@ import com.passus.data.ByteString; import com.passus.filter.ValueExtractor; +import com.passus.net.http.HttpMessage; +import com.passus.net.http.HttpMessageHelper; import com.passus.st.ParametersBag; +import com.passus.st.client.http.extractor.ContentExtractor; +import com.passus.st.client.http.extractor.JsonValueExtractor; +import com.passus.st.client.http.extractor.PostValueExtractor; +import com.passus.st.client.http.extractor.RegexValueExtractor; +import com.passus.st.client.http.extractor.XmlValueExtractor; import com.passus.st.client.http.filter.HttpMessageWrapper; import java.util.Objects; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; /** * @@ -14,16 +23,27 @@ */ public class HttpMessageHeaderValueExtractor implements ValueExtractor { + private static final Logger LOGGER = LogManager.getLogger(HttpMessageHeaderValueExtractor.class); + 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 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"; + static final String RESP_COOKIE_SF = "resp.cookie@"; static final String RESP_HEADER_SF = "resp.header@"; + static final String RESP_FULL_CONTENT_SF = "resp.content"; + static final String HTTP_SESSION_SF = "sess@"; static abstract class Operation { @@ -57,6 +77,10 @@ @Override public ByteString extract(HttpMessageWrapper wrapper) { + if (wrapper.getReq() == null) { + return null; + } + return wrapper.getReq().getCookie(name); } } @@ -65,6 +89,10 @@ @Override public ByteString extract(HttpMessageWrapper wrapper) { + if (wrapper.getReq() == null) { + return null; + } + return wrapper.getReq().getHeader(name); } } @@ -73,6 +101,10 @@ @Override public ByteString extract(HttpMessageWrapper wrapper) { + if (wrapper.getResp() == null) { + return null; + } + return wrapper.getResp().getCookie(name); } } @@ -81,6 +113,10 @@ @Override public ByteString extract(HttpMessageWrapper wrapper) { + if (wrapper.getResp() == null) { + return null; + } + return wrapper.getResp().getHeader(name); } } @@ -94,6 +130,108 @@ } } + private static abstract class ExtractContentOperation extends Operation { + + protected final HttpMessageHelper httpMessageHelper = HttpMessageHelper.NOT_STRICT; + + private ContentExtractor contentExtractor; + + public ExtractContentOperation() { + + } + + public ExtractContentOperation(String 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())); + } + } catch (Exception e) { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug(e.getMessage(), e); + } + } + } + + public ExtractContentOperation(ContentExtractor contentExtractor) { + this.contentExtractor = contentExtractor; + } + + protected final Object extract(HttpMessage message) { + try { + if (message == null) { + return null; + } + + if (contentExtractor != null) { + return contentExtractor.extract(message); + } else { + return httpMessageHelper.contentToString(message, true); + } + } catch (Exception e) { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug(e.getMessage(), e); + } + + return ""; + } + } + + } + + private static class ExtractReqContentOperation extends ExtractContentOperation { + + public ExtractReqContentOperation() { + } + + public ExtractReqContentOperation(String rule) { + super(rule); + } + + public ExtractReqContentOperation(ContentExtractor contentExtractor) { + super(contentExtractor); + } + + @Override + public Object extract(HttpMessageWrapper wrapper) { + if (wrapper.getReq() == null) { + return null; + } + + return extract(wrapper.getReq().getWrappedMessage()); + } + + } + + private static class ExtractRespContentOperation extends ExtractContentOperation { + + public ExtractRespContentOperation() { + } + + public ExtractRespContentOperation(String rule) { + super(rule); + } + + public ExtractRespContentOperation(ContentExtractor contentExtractor) { + super(contentExtractor); + } + + @Override + public Object extract(HttpMessageWrapper wrapper) { + if (wrapper.getResp() == null) { + return null; + } + + return extract(wrapper.getResp().getWrappedMessage()); + } + + } + static Operation createForFunction(String expr) { Operation op = null; String name; @@ -124,6 +262,12 @@ } else if (expr.startsWith(REQ_HEADER_SF)) { op = new GetReqHeaderOperation(); op.name = expr.substring(REQ_HEADER_SF.length()); + } 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 if (expr.startsWith(RESP_COOKIE_SF)) { op = new GetRespCookieOperation(); op.name = expr.substring(RESP_COOKIE_SF.length()); @@ -133,7 +277,14 @@ } else if (expr.startsWith(HTTP_SESSION_SF)) { op = new GetSessionVariable(); op.name = expr.substring(HTTP_SESSION_SF.length()); + } 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())); + } } + return op; }
--- a/stress-tester/src/test/java/com/passus/st/filter/HttpMessageHeaderValueExtractorTest.java Mon Oct 30 15:34:38 2017 +0100 +++ b/stress-tester/src/test/java/com/passus/st/filter/HttpMessageHeaderValueExtractorTest.java Tue Oct 31 13:55:35 2017 +0100 @@ -18,40 +18,57 @@ */ 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 F_SESS_VAR = "httpSession.get('mno')"; + private final String F_REQ_COOKIE = "req.getCookie('abc')"; + private final String F_REQ_HEADER = "req.getHeader('def')"; + private final String F_RESP_COOKIE = "resp.getCookie('ghi')"; + private final String F_RESP_HEADER = "resp.getHeader('jkl')"; + private final String F_SESS_VAR = "httpSession.get('mno')"; - 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 String S_SESS_VAR = "sess@mno"; + private final String S_REQ_COOKIE = "req.cookie@abc"; + private final String S_REQ_HEADER = "req.header@def"; + private final String S_REQ_FULL_CONTENT = "req.content"; + private final String S_REQ_JSON_CONTENT = "req.content@json:$.reqNode"; + private final String S_REQ_REGEXP_CONTENT = "req.content@regexp:(\\:.+)"; - private static final HttpRequest REQ = HttpRequestBuilder.get("http://www.example.com") - .cookie("abc", "1").header("def", "2").tag(HttpConsts.TAG_SESSION_ID, "sid").build(); + private final String S_RESP_COOKIE = "resp.cookie@ghi"; + private final String S_RESP_HEADER = "resp.header@jkl"; + private final String S_RESP_FULL_CONTENT = "resp.content"; + private final String S_RESP_JSON_CONTENT = "resp.content@json:$.respNode"; + private final String S_RESP_REGEXP_CONTENT = "resp.content@regexp:(\\:.+)"; - private static final HttpResponse RESP = HttpResponseBuilder.ok() - .cookie("ghi", "3").header("jkl", "4").build(); + private final String S_SESS_VAR = "sess@mno"; - private static final HttpFlowContext CONTEXT; + private final HttpRequest REQ = HttpRequestBuilder.get("http://www.example.com") + .cookie("abc", "1").header("def", "2").tag(HttpConsts.TAG_SESSION_ID, "sid") + .content("{reqNode: reqNodeValue}").build(); - static { + private final HttpResponse RESP = HttpResponseBuilder.ok() + .cookie("ghi", "3").header("jkl", "4") + .content("{respNode: respNodeValue}").build(); + + private final HttpFlowContext CONTEXT; + + { HttpScopes scopes = new HttpScopes(); scopes.getSession("sid").set("mno", "5"); CONTEXT = new HttpFlowContext(null, scopes); } - private static final HttpMessageWrapper WRAPPER = new HttpMessageWrapper(REQ, RESP, CONTEXT); + private final HttpMessageWrapper WRAPPER = new HttpMessageWrapper(REQ, RESP, CONTEXT); + private final HttpMessageWrapper NULL_WRAPPER = new HttpMessageWrapper(null, null, CONTEXT); @Test public void testCreateForShortForm() { assertNotNull(createForShortForm(S_REQ_COOKIE)); assertNotNull(createForShortForm(S_REQ_HEADER)); + assertNotNull(createForShortForm(S_REQ_FULL_CONTENT)); + assertNotNull(createForShortForm(S_REQ_JSON_CONTENT)); + assertNotNull(createForShortForm(S_REQ_REGEXP_CONTENT)); assertNotNull(createForShortForm(S_RESP_COOKIE)); assertNotNull(createForShortForm(S_RESP_HEADER)); + assertNotNull(createForShortForm(S_RESP_FULL_CONTENT)); + assertNotNull(createForShortForm(S_RESP_JSON_CONTENT)); + assertNotNull(createForShortForm(S_RESP_REGEXP_CONTENT)); assertNotNull(createForShortForm(S_SESS_VAR)); assertNull(createForShortForm("req.getVersion()")); } @@ -60,9 +77,28 @@ public void testExtractByShortForm() { assertEquals("1", createForShortForm(S_REQ_COOKIE).extract(WRAPPER).toString()); assertEquals("2", createForShortForm(S_REQ_HEADER).extract(WRAPPER).toString()); + assertEquals("{reqNode: reqNodeValue}", createForShortForm(S_REQ_FULL_CONTENT).extract(WRAPPER).toString()); + assertEquals("reqNodeValue", createForShortForm(S_REQ_JSON_CONTENT).extract(WRAPPER).toString()); + assertEquals(": reqNodeValue}", createForShortForm(S_REQ_REGEXP_CONTENT).extract(WRAPPER).toString()); assertEquals("3", createForShortForm(S_RESP_COOKIE).extract(WRAPPER).toString()); assertEquals("4", createForShortForm(S_RESP_HEADER).extract(WRAPPER).toString()); assertEquals("5", createForShortForm(S_SESS_VAR).extract(WRAPPER).toString()); + assertEquals("{respNode: respNodeValue}", createForShortForm(S_RESP_FULL_CONTENT).extract(WRAPPER).toString()); + assertEquals("respNodeValue", createForShortForm(S_RESP_JSON_CONTENT).extract(WRAPPER).toString()); + } + + @Test + public void testExtractByShortForm_NullWrapper() { + assertEquals(null, createForShortForm(S_REQ_COOKIE).extract(NULL_WRAPPER)); + assertEquals(null, createForShortForm(S_REQ_HEADER).extract(NULL_WRAPPER)); + assertEquals(null, createForShortForm(S_REQ_FULL_CONTENT).extract(NULL_WRAPPER)); + assertEquals(null, createForShortForm(S_REQ_JSON_CONTENT).extract(NULL_WRAPPER)); + assertEquals(null, createForShortForm(S_REQ_REGEXP_CONTENT).extract(NULL_WRAPPER)); + assertEquals(null, createForShortForm(S_RESP_COOKIE).extract(NULL_WRAPPER)); + assertEquals(null, createForShortForm(S_RESP_HEADER).extract(NULL_WRAPPER)); + assertEquals(null, createForShortForm(S_SESS_VAR).extract(NULL_WRAPPER)); + assertEquals(null, createForShortForm(S_RESP_FULL_CONTENT).extract(NULL_WRAPPER)); + assertEquals(null, createForShortForm(S_RESP_JSON_CONTENT).extract(NULL_WRAPPER)); } @Test