Mercurial > stress-tester
changeset 928:c5435062adb3
ST-111 in progress
author | Devel 1 |
---|---|
date | Fri, 27 Apr 2018 15:33:13 +0200 |
parents | 4d8642593e5c |
children | bc5dbd2db8c9 |
files | stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationOperations.java stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationOperationsTest.java |
diffstat | 2 files changed, 290 insertions(+), 8 deletions(-) [+] |
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationOperations.java Fri Apr 27 15:02:35 2018 +0200 +++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationOperations.java Fri Apr 27 15:33:13 2018 +0200 @@ -4,15 +4,12 @@ import com.passus.data.ByteString; import com.passus.filter.UnmutableValueExtractor; import com.passus.filter.ValueExtractor; -import com.passus.net.http.HttpDataPart; -import com.passus.net.http.HttpMessageHelper; -import com.passus.net.http.HttpMultipartData; -import com.passus.net.http.HttpParameters; -import com.passus.net.http.HttpRequest; -import com.passus.net.http.HttpResponse; +import com.passus.net.http.*; import com.passus.st.client.http.HttpFlowContext; import com.passus.st.client.http.extractor.ContentReplacer; +import com.passus.st.client.http.extractor.RegexValueExtractor; import java.text.ParseException; +import java.util.Arrays; import java.util.List; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -47,7 +44,7 @@ } - private static abstract class AbstractRemoveOperation extends Operation { + protected static abstract class AbstractRemoveOperation extends Operation { protected final ValueExtractor nameExtractor; @@ -377,7 +374,7 @@ } - private static abstract class SetParamOperation extends AddParamOperation { + protected static abstract class SetParamOperation extends AddParamOperation { public SetParamOperation(CharSequence name, Object value) { this(name, new UnmutableValueExtractor(value)); @@ -548,4 +545,192 @@ } + public static class SetMethodOperation extends Operation { + + private final HttpMethod method; + + static SetMethodOperation create(CharSequence method) { + return HttpUtils.isValidRequestMethod(method) ? new SetMethodOperation(HttpMethod.valueOfIgnoreCase(method)) : null; + } + + SetMethodOperation(HttpMethod method) { + this.method = method; + } + + @Override + public void process(HttpRequest req, HttpResponse resp, HttpFlowContext context) { + req.setMethod(method); + } + } + + public static class SetVersionOperation extends Operation { + + private final ByteString version; + + static SetVersionOperation create(CharSequence version) { + return HttpUtils.isValidVersion(version) ? new SetVersionOperation(version) : null; + } + + public SetVersionOperation(CharSequence version) { + this.version = ByteString.create(version); + } + + @Override + public void process(HttpRequest req, HttpResponse resp, HttpFlowContext context) { + req.setVersion(version); + } + } + + public static class SetUriOperation extends Operation { + + private final CharSequence uri; + + public SetUriOperation(CharSequence uri) { + this.uri = uri; + } + + @Override + public void process(HttpRequest req, HttpResponse resp, HttpFlowContext context) { + req.setUri(uri); + } + } + + public static abstract class SetUrlPartOperation extends Operation { + + @Override + public void process(HttpRequest req, HttpResponse resp, HttpFlowContext context) { + try { + ByteString urlStr = req.getUrl(); + URLBuilder builder = new URLBuilder(urlStr); + process(builder); + URL url = builder.build(); + ByteString newUri = URL.generateUri(url.getPath(), url.getQuery(), url.getRef()); + req.setUri(newUri); + } catch (Exception ex) { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug(ex.getMessage(), ex); + } + } + } + + protected abstract void process(URLBuilder builder); + } + + public static class SetUrlQuery extends SetUrlPartOperation { + + private final CharSequence query; + + public SetUrlQuery(CharSequence query) { + this.query = query; + } + + @Override + protected void process(URLBuilder builder) { + builder.setQuery(query); + } + } + + public static class SetUrlRef extends SetUrlPartOperation { + + private final CharSequence ref; + + public SetUrlRef(CharSequence ref) { + this.ref = ref; + } + + @Override + protected void process(URLBuilder builder) { + builder.setRef(ref); + } + } + + public static class SetUrlPath extends SetUrlPartOperation { + + final CharSequence path; + + public SetUrlPath(CharSequence path) { + this.path = path; + } + + @Override + protected void process(URLBuilder builder) { + builder.setPath(path); + } + } + + public static class SetUrlPathPos extends Operation { + + private final ValueExtractor valueExtractor; + private final int index; + + public SetUrlPathPos(ValueExtractor valueExtractor, int index) { + this.valueExtractor = valueExtractor; + this.index = index; + } + + @Override + public void process(HttpRequest req, HttpResponse resp, HttpFlowContext context) { + CharSequence value = extractValue(valueExtractor, req, resp, context); + if (value != null) { + try { + ByteString urlStr = req.getUrl(); + URL url = URL.parse(urlStr); + ByteString[] parts = url.getPath().split(URLBuilder.PATH_ELEMENT_DELIMITER); + parts = normParts(parts); + int i = index < 0 ? index + parts.length : index; + if (i < 0 || i >= parts.length) { + return; + } + parts[i] = ByteString.create(value); + ByteString newPath = URLBuilder.joinPathElements(Arrays.asList(parts)); + ByteString newUri = URL.generateUri(newPath, url.getQuery(), url.getRef()); + req.setUri(newUri); + } catch (Exception ex) { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug(ex.getMessage(), ex); + } + } + } + } + + private static ByteString[] normParts(ByteString[] parts) { + boolean skipFirst = parts.length > 0 && parts[0].length() == 0; + if (!skipFirst) { + return parts; + } + ByteString[] norm = new ByteString[parts.length - 1]; + System.arraycopy(parts, 1, norm, 0, norm.length); + return norm; + } + } + + public static class SetUrlPathRegex extends Operation { + + private static final String PATH_ELEMENT_DELIMITER = "/"; + + private final ValueExtractor valueExtractor; + private final RegexValueExtractor regexReplacer; + + public SetUrlPathRegex(ValueExtractor valueExtractor, String pattern) { + this.valueExtractor = valueExtractor; + regexReplacer = new RegexValueExtractor(pattern); + } + + @Override + public void process(HttpRequest req, HttpResponse resp, HttpFlowContext context) { + CharSequence value = extractValue(valueExtractor, req, resp, context); + if (value != null) { + try { + ByteString urlStr = req.getUri(); + CharSequence newUri = regexReplacer.replace(urlStr, value); + req.setUri(newUri); + } catch (Exception ex) { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug(ex.getMessage(), ex); + } + } + } + } + } + }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationOperationsTest.java Fri Apr 27 15:33:13 2018 +0200 @@ -0,0 +1,97 @@ +package com.passus.st.client.http.filter; + +import com.passus.filter.UnmutableValueExtractor; +import com.passus.net.http.HttpRequest; +import com.passus.net.http.HttpRequestBuilder; +import com.passus.st.client.http.filter.HttpMessageModificationOperations.*; +import static org.testng.AssertJUnit.*; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +/** + * + * @author mikolaj.podbielski + */ +public class HttpMessageModificationOperationsTest { + + String url = "http://example.com/part0/part1/part2?q1=v1#r1"; + UnmutableValueExtractor uve = new UnmutableValueExtractor("uve"); + + private HttpRequest process(Operation op) { + HttpRequest req = HttpRequestBuilder.get(url).build(); + op.process(req, null, null); + return req; + } + + @BeforeClass + public void sanityCheck() { + HttpRequest req = HttpRequestBuilder.get(url).build(); + assertEquals("/part0/part1/part2?q1=v1#r1", req.getUri().toString()); + } + + @Test + public void testSetMethodOperation() { + SetMethodOperation op = SetMethodOperation.create("head"); + HttpRequest req = process(op); + assertEquals("HEAD", req.getMethod().toString()); + } + + @Test + public void testSetVersionOperation() { + SetVersionOperation op = SetVersionOperation.create("HTTP/1.2"); + HttpRequest req = process(op); + assertEquals("HTTP/1.2", req.getVersion().toString()); + } + + @Test + public void testSetUriOperation() { + SetUriOperation op = new SetUriOperation("/part0x/part1x/part2x?q1x=v1x"); + HttpRequest req = process(op); + assertEquals("/part0x/part1x/part2x?q1x=v1x", req.getUri().toString()); + } + + ///////////// + @Test + public void testSetUrlQuery() { + SetUrlQuery op = new SetUrlQuery("qqq=vvv"); + HttpRequest req = process(op); + assertEquals("/part0/part1/part2?qqq=vvv#r1", req.getUri().toString()); + } + + @Test + public void testSetUrlRef() { + SetUrlRef op = new SetUrlRef("anchor"); + HttpRequest req = process(op); + assertEquals("/part0/part1/part2?q1=v1#anchor", req.getUri().toString()); + } + + @Test + public void testSetUrlPath() { + SetUrlPath op = new SetUrlPath("/x/y/z"); + HttpRequest req = process(op); + assertEquals("/x/y/z?q1=v1#r1", req.getUri().toString()); + } + + @Test + public void testSetUrlPathPos() { + assertEquals("/uve/part1/part2?q1=v1#r1", process(new SetUrlPathPos(uve, 0)).getUri().toString()); + assertEquals("/uve/part1/part2?q1=v1#r1", process(new SetUrlPathPos(uve, -3)).getUri().toString()); + + assertEquals("/part0/uve/part2?q1=v1#r1", process(new SetUrlPathPos(uve, 1)).getUri().toString()); + assertEquals("/part0/uve/part2?q1=v1#r1", process(new SetUrlPathPos(uve, -2)).getUri().toString()); + + assertEquals("/part0/part1/uve?q1=v1#r1", process(new SetUrlPathPos(uve, 2)).getUri().toString()); + assertEquals("/part0/part1/uve?q1=v1#r1", process(new SetUrlPathPos(uve, -1)).getUri().toString()); + + assertEquals("/part0/part1/part2?q1=v1#r1", process(new SetUrlPathPos(uve, 111)).getUri().toString()); + assertEquals("/part0/part1/part2?q1=v1#r1", process(new SetUrlPathPos(uve, -111)).getUri().toString()); + } + + @Test + public void testSetUrlPathRegex() { + SetUrlPathRegex op = new SetUrlPathRegex(uve, "part0\\/(.+)\\/part2"); + HttpRequest req = process(op); + assertEquals("/part0/uve/part2?q1=v1#r1", req.getUri().toString()); + } + +}