Mercurial > stress-tester
changeset 935:53ff4d2e0abc
ST-111 node def creator
author | Devel 1 |
---|---|
date | Mon, 07 May 2018 14:51:41 +0200 |
parents | 2d014d618480 |
children | d49c6a6c308a |
files | stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilter.java stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformer.java stress-tester/src/main/java/com/passus/st/validation/HttpValidators.java stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTest.java stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformerTest.java |
diffstat | 5 files changed, 108 insertions(+), 11 deletions(-) [+] |
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilter.java Mon May 07 14:36:16 2018 +0200 +++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilter.java Mon May 07 14:51:41 2018 +0200 @@ -16,6 +16,7 @@ import com.passus.st.config.HeaderOperationNodeDefinition; import com.passus.st.plugin.PluginConstants; import com.passus.st.validation.HeaderNameValidator; +import com.passus.st.validation.HttpValidators; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -157,7 +158,16 @@ .add("removeQueryParam", paramNodeDef) .add("addQueryParam", paramNodeDef) .add("setQueryParam", paramNodeDef) - .add("setContent", mappingDef().setChildrenNum(1)); + .add("setContent", mappingDef().setChildrenNum(1)) + .add("setMethod", valueDef().addValidator(HttpValidators.METHOD_VALIDATOR)) + .add("setVersion", valueDef().addValidator(HttpValidators.VERSION_VALIDATOR)) + .add("setUri", valueDef()) + .add("setUrlQuery", valueDef()) + .add("setUrlRef", valueDef()) + .add("setUrlPath", mixedDef( + valueDef(), + mappingDef().setChildrenNum(1) + )); return mapDef( tupleDef("operations", operationsDef),
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformer.java Mon May 07 14:36:16 2018 +0200 +++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformer.java Mon May 07 14:51:41 2018 +0200 @@ -3,16 +3,11 @@ import com.passus.config.*; import com.passus.config.schema.NodeTransformer; import com.passus.config.validation.Errors; -import com.passus.filter.UnmutableValueExtractor; import com.passus.filter.ValueExtractor; -import com.passus.filter.config.ExpressionNodeTransformer; -import com.passus.lookup.filter.LookupValueExtractorTransformer; -import com.passus.net.http.HttpMethod; import com.passus.st.client.http.extractor.ContentExtractorUtils; import com.passus.st.client.http.extractor.ContentReplacer; import com.passus.st.client.http.filter.HttpMessageModificationOperations.*; import com.passus.st.config.FieldValueExtractorTransformer; -import com.passus.st.filter.Transformers; import java.util.ArrayList; import java.util.Collections; @@ -25,7 +20,7 @@ */ public class HttpMessageModificationFilterTransformer implements NodeTransformer { - private FieldValueExtractorTransformer fieldValueExtractorTransformer = new FieldValueExtractorTransformer(); + private final FieldValueExtractorTransformer fieldValueExtractorTransformer = new FieldValueExtractorTransformer(); private Operation createSingleParamOperation(CTupleNode tuple, Class<? extends Operation> clazz, @@ -241,9 +236,6 @@ operations = new ArrayList<>(tuples.size()); } - final ExpressionNodeTransformer expressionNodeTransformer = Transformers.expressionNodeTransformer(context); - final LookupValueExtractorTransformer lookupValueTransformer = Transformers.lookupValueTransformer(context); - for (CTupleNode tuple : tuples) { String opName = tuple.getName(); try {
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/main/java/com/passus/st/validation/HttpValidators.java Mon May 07 14:51:41 2018 +0200 @@ -0,0 +1,53 @@ +package com.passus.st.validation; + +import com.passus.config.validation.Errors; +import com.passus.config.validation.Validator; +import com.passus.net.http.HttpMethod; +import com.passus.net.http.HttpUtils; + +/** + * + * @author mikolaj.podbielski + */ +public class HttpValidators { + + public static final MethodValidator METHOD_VALIDATOR = new MethodValidator(); + + public static final VersionValidator VERSION_VALIDATOR = new VersionValidator(); + + public static class MethodValidator implements Validator { + + @Override + public boolean supports(Class<?> clazz) { + return CharSequence.class.isAssignableFrom(clazz); + } + + @Override + public void validate(Object target, Errors errors) { + CharSequence value = ((CharSequence) target); + if (!HttpUtils.isValidRequestMethod(value)) { + errors.reject("Invalid HTTP method \"%s\"", value); + } + if (HttpMethod.valueOfIgnoreCase(value) == null) { + errors.reject("Unknown HTTP method \"%s\"", value); + } + } + } + + public static class VersionValidator implements Validator { + + @Override + public boolean supports(Class<?> clazz) { + return CharSequence.class.isAssignableFrom(clazz); + } + + @Override + public void validate(Object target, Errors errors) { + CharSequence value = ((CharSequence) target); + if (!HttpUtils.isValidVersion(value)) { + errors.reject("Invalid HTTP method \"%s\"", value); + } + } + } + +}
--- a/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTest.java Mon May 07 14:36:16 2018 +0200 +++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTest.java Mon May 07 14:51:41 2018 +0200 @@ -219,6 +219,48 @@ } @Test + public void testConfigureAndFilterOutbound_firstLineOps() throws Exception { + String filterConfig + = "httpFilters:\n" + + " - type: modifyMessage\n" + + " operations:\n" + + " setMethod: HEAD\n" + + " setVersion: 'HTTP/1.2'\n" + + " setUri: '/aaa/bbb/ccc?q1x=v1x'\n" + + " - type: modifyMessage\n" + + " operations:\n" + + " setUrlQuery: 'qqq=vvv'\n" + + " setUrlRef: anchor\n" + + " setUrlPath: '/xxx/yyy/zzz'\n" + + " setUrlPath:\n" + + " regexp:xxx\\/(.+)\\/zzz: new\n" + + " setUrlPath:\n" + + " pos:2: '@req.method'\n"; + + Errors errors = new Errors(); + ConfigurationContextImpl emptyContext = new ConfigurationContextImpl(); + List<HttpFilter> filters = HttpFiltersConfigurator.getFilters(filterConfig, errors, emptyContext); + HttpFilterTestUtils.printErrors(errors); + assertEquals(0, errors.getErrorCount()); + assertEquals(2, filters.size()); + assertTrue(filters.get(0) instanceof HttpMessageModificationFilter); + assertTrue(filters.get(1) instanceof HttpMessageModificationFilter); + + HttpFilter f0 = filters.get(0); + HttpRequest req0 = HttpRequestBuilder.get("http://example.com?q=1#a1").build(); + f0.filterOutbound(req0, null, null); + assertEquals(HttpMethod.HEAD, req0.getMethod()); + assertEquals("HTTP/1.2", req0.getVersion().toString()); + assertEquals("/aaa/bbb/ccc?q1x=v1x", req0.getUri().toString()); + assertEquals("example.com/aaa/bbb/ccc?q1x=v1x", req0.getUrl().toString()); + + HttpFilter f1 = filters.get(1); + HttpRequest req1 = HttpRequestBuilder.get("http://example.com?q=1#a1").build(); + f1.filterOutbound(req1, null, null); + assertEquals("example.com/xxx/new/GET?qqq=vvv#anchor", req1.getUrl().toString()); + } + + @Test public void testConfigureAndFilterOutbound_appVars() throws Exception { Map<String, ValueExtractor> appVars = map( e("var0", expr("req.url.host")),
--- a/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformerTest.java Mon May 07 14:36:16 2018 +0200 +++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformerTest.java Mon May 07 14:51:41 2018 +0200 @@ -107,7 +107,7 @@ + " setVersion: 'HTTP/1.2'\n" + " setUri: '/part0x/part1x/part2x?q1x=v1x'\n" + " setUrlQuery: 'qqq=vvv'\n" - + " setUrlRef: anchor'\n" + + " setUrlRef: anchor\n" + " setUrlPath: '/x/y/z'\n" + " setUrlPath:\n" + " pos:2: '@req.method'\n"