Mercurial > stress-tester
changeset 623:758409ac6dad
extractors - support for session variables
author | Devel 1 |
---|---|
date | Fri, 13 Oct 2017 14:46:44 +0200 |
parents | bb4d01b23f40 |
children | d599f5455379 |
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, 39 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/filter/HttpMessageHeaderValueExtractor.java Fri Oct 13 14:44:36 2017 +0200 +++ b/stress-tester/src/main/java/com/passus/st/filter/HttpMessageHeaderValueExtractor.java Fri Oct 13 14:46:44 2017 +0200 @@ -2,6 +2,7 @@ import com.passus.data.ByteString; import com.passus.filter.ValueExtractor; +import com.passus.st.ParametersBag; import com.passus.st.client.http.filter.HttpMessageWrapper; import java.util.Objects; import java.util.regex.Matcher; @@ -17,11 +18,13 @@ 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\\(.*'(.*)'\\)"); 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 final String HTTP_SESSION_SF = "sess@"; static abstract class Operation { @@ -82,6 +85,15 @@ } } + private static class GetSessionVariable extends Operation { + + @Override + public Object extract(HttpMessageWrapper wrapper) { + ParametersBag httpSession = wrapper.getHttpSession(); + return httpSession == null ? null : httpSession.get(name); + } + } + static Operation createForFunction(String expr) { Operation op = null; String name; @@ -97,6 +109,9 @@ } else if ((name = matchAndExtract(RESP_HEADER_FUN, expr)) != null) { op = new GetRespHeaderOperation(); op.name = name; + } else if ((name = matchAndExtract(HTTP_SESS_FUN, expr)) != null) { + op = new GetSessionVariable(); + op.name = name; } return op; } @@ -115,6 +130,9 @@ } else if (expr.startsWith(RESP_HEADER_SF)) { op = new GetRespHeaderOperation(); op.name = expr.substring(RESP_HEADER_SF.length()); + } else if (expr.startsWith(HTTP_SESSION_SF)) { + op = new GetSessionVariable(); + op.name = expr.substring(HTTP_SESSION_SF.length()); } return op; }
--- a/stress-tester/src/test/java/com/passus/st/filter/HttpMessageHeaderValueExtractorTest.java Fri Oct 13 14:44:36 2017 +0200 +++ b/stress-tester/src/test/java/com/passus/st/filter/HttpMessageHeaderValueExtractorTest.java Fri Oct 13 14:46:44 2017 +0200 @@ -4,6 +4,9 @@ import com.passus.net.http.HttpRequestBuilder; import com.passus.net.http.HttpResponse; import com.passus.net.http.HttpResponseBuilder; +import com.passus.st.client.http.HttpConsts; +import com.passus.st.client.http.HttpFlowContext; +import com.passus.st.client.http.HttpScopes; import com.passus.st.client.http.filter.HttpMessageWrapper; import static com.passus.st.filter.HttpMessageHeaderValueExtractor.*; import static org.testng.AssertJUnit.*; @@ -19,19 +22,29 @@ 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 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 static final HttpRequest REQ = HttpRequestBuilder.get("http://www.example.com") - .cookie("abc", "1").header("def", "2").build(); + .cookie("abc", "1").header("def", "2").tag(HttpConsts.TAG_SESSION_ID, "sid").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); + private static final HttpFlowContext CONTEXT; + + static { + 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); @Test public void testCreateForShortForm() { @@ -39,6 +52,7 @@ assertNotNull(createForShortForm(S_REQ_HEADER)); assertNotNull(createForShortForm(S_RESP_COOKIE)); assertNotNull(createForShortForm(S_RESP_HEADER)); + assertNotNull(createForShortForm(S_SESS_VAR)); assertNull(createForShortForm("req.getVersion()")); } @@ -48,6 +62,7 @@ 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()); + assertEquals("5", createForShortForm(S_SESS_VAR).extract(WRAPPER).toString()); } @Test @@ -64,6 +79,9 @@ assertEquals("jkl", matchAndExtract(RESP_HEADER_FUN, F_RESP_HEADER)); assertNotNull(createForFunction(F_RESP_HEADER)); + assertEquals("mno", matchAndExtract(HTTP_SESS_FUN, F_SESS_VAR)); + assertNotNull(createForFunction(F_SESS_VAR)); + assertNull(createForFunction("req.getVersion()")); } @@ -73,6 +91,7 @@ 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()); + assertEquals("5", createForFunction(F_SESS_VAR).extract(WRAPPER).toString()); } }