Mercurial > stress-tester
changeset 918:1d9f14d387f6
HttpMessageModificationFilter - mulitpart operations
author | Devel 2 |
---|---|
date | Thu, 26 Apr 2018 09:10:02 +0200 |
parents | 4cd8f1649ef7 |
children | 25b3d4568893 |
files | stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilter.java stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTest.java |
diffstat | 2 files changed, 93 insertions(+), 27 deletions(-) [+] |
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilter.java Wed Apr 25 14:23:52 2018 +0200 +++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilter.java Thu Apr 26 09:10:02 2018 +0200 @@ -12,10 +12,7 @@ import com.passus.filter.UnmutableValueExtractor; import com.passus.filter.ValueExtractor; import com.passus.lookup.filter.LookupValueExtractorDefinitionCreator; -import com.passus.net.http.HttpMessageHelper; -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.config.HeaderOperationNodeDefinition; @@ -52,7 +49,10 @@ protected final CharSequence extractValue(ValueExtractor extractor, HttpRequest req, HttpResponse resp, HttpFlowContext context) { Object value = HttpFiltersUtils.extractValue(extractor, req, resp, context); if (value instanceof CharSequence) { - LOGGER.debug("extractor={} extracted={}", extractor, value); + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("extractor={} extracted={}", extractor, value); + } + return (CharSequence) value; } @@ -65,8 +65,8 @@ protected final ValueExtractor nameExtractor; - public AbstractRemoveOperation(CharSequence headerName) { - this(new UnmutableValueExtractor(ByteString.create(headerName))); + public AbstractRemoveOperation(CharSequence name) { + this(new UnmutableValueExtractor(ByteString.create(name))); } public AbstractRemoveOperation(ValueExtractor extractor) { @@ -310,8 +310,8 @@ protected static abstract class RemoveParamOperation extends AbstractRemoveOperation { - public RemoveParamOperation(CharSequence headerName) { - super(headerName); + public RemoveParamOperation(CharSequence name) { + super(name); } public RemoveParamOperation(ValueExtractor extractor) { @@ -409,8 +409,8 @@ public static class PostDataRemoveParamOperation extends RemoveParamOperation { - public PostDataRemoveParamOperation(CharSequence headerName) { - super(headerName); + public PostDataRemoveParamOperation(CharSequence name) { + super(name); } public PostDataRemoveParamOperation(ValueExtractor extractor) { @@ -420,9 +420,27 @@ @Override protected void doProcess(CharSequence value, HttpRequest req, HttpResponse resp, HttpFlowContext context) { try { - HttpParameters params = HttpMessageHelper.get().decodeFormUrlencoded(req); - if (doRemove(params, req, resp, context)) { - HttpMessageHelper.get().setFormUrlencoded(req, params); + HttpMessageHelper helper = HttpMessageHelper.get(); + + if (helper.isFormUrlencoded(req)) { + HttpParameters params = helper.decodeFormUrlencoded(req); + if (params != null) { + if (doRemove(params, req, resp, context)) { + helper.setFormUrlencoded(req, params); + } + } + } else if (helper.isMultipart(req)) { + HttpMultipartData parts = helper.decodeMultipart(req); + if (parts == null) { + return; + } + + CharSequence name = extractValue(nameExtractor, req, resp, context); + HttpDataPart part = parts.findByContentDispositionName(name); + if (part != null) { + parts.remove(part); + helper.setMultipartContent(req, parts); + } } } catch (Exception e) { if (LOGGER.isDebugEnabled()) { @@ -430,7 +448,6 @@ } } } - } public static class PostDataAddParamOperation extends AddParamOperation { @@ -446,16 +463,30 @@ @Override public void process(HttpRequest req, HttpResponse resp, HttpFlowContext context) { try { - HttpParameters params = HttpMessageHelper.get().decodeFormUrlencoded(req, escape); - doAdd(params, req, resp, context); - HttpMessageHelper.get().setFormUrlencoded(req, params, escape); + HttpMessageHelper helper = HttpMessageHelper.get(); + + if (helper.isFormUrlencoded(req)) { + HttpParameters params = helper.decodeFormUrlencoded(req, escape); + if (params != null) { + doAdd(params, req, resp, context); + helper.setFormUrlencoded(req, params, escape); + } + } else if (helper.isMultipart(req)) { + HttpMultipartData parts = helper.decodeMultipart(req); + if (parts == null) { + return; + } + + Object value = HttpFiltersUtils.extractValue(valueExtractor, req, resp, context); + parts.addFormData(this.name, value); + helper.setMultipartContent(req, parts); + } } catch (Exception e) { if (LOGGER.isDebugEnabled()) { LOGGER.debug(e.getMessage(), e); } } } - } public static class PostDataSetParamOperation extends SetParamOperation { @@ -471,9 +502,29 @@ @Override public void process(HttpRequest req, HttpResponse resp, HttpFlowContext context) { try { - HttpParameters params = HttpMessageHelper.get().decodeFormUrlencoded(req, escape); - doSet(params, req, resp, context); - HttpMessageHelper.get().setFormUrlencoded(req, params, escape); + HttpMessageHelper helper = HttpMessageHelper.get(); + if (helper.isFormUrlencoded(req)) { + HttpParameters params = helper.decodeFormUrlencoded(req, escape); + if (params != null) { + doSet(params, req, resp, context); + helper.setFormUrlencoded(req, params, escape); + } + } else if (helper.isMultipart(req)) { + HttpMultipartData parts = helper.decodeMultipart(req); + if (parts == null) { + return; + } + + Object value = HttpFiltersUtils.extractValue(valueExtractor, req, resp, context); + HttpDataPart part = parts.findByContentDispositionName(this.name); + if (part == null) { + parts.addFormData(this.name, value); + } else { + part.setContent(helper.toDataSource(value)); + } + + helper.setMultipartContent(req, parts); + } } catch (Exception e) { if (LOGGER.isDebugEnabled()) { LOGGER.debug(e.getMessage(), e);
--- a/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTest.java Wed Apr 25 14:23:52 2018 +0200 +++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTest.java Thu Apr 26 09:10:02 2018 +0200 @@ -6,10 +6,7 @@ import com.passus.config.validation.Errors; import com.passus.filter.ValueExtractor; import com.passus.net.MimeTypes; -import com.passus.net.http.HttpHeaders; -import com.passus.net.http.HttpMessageHelper; -import com.passus.net.http.HttpRequest; -import com.passus.net.http.HttpRequestBuilder; +import com.passus.net.http.*; import com.passus.st.AppUtils; import static com.passus.st.client.http.HttpConsts.TAG_SESSION_ID; import com.passus.st.client.http.HttpFlowContext; @@ -110,11 +107,29 @@ assertEquals("Header1Value3a", headers.get("Header3").toString()); assertEquals("myCookie1=myValue1;myCookie2=myValue2ab;myCookie4=myValue4@;myCookie6=myValue6@", headers.get("Cookie").toString()); - System.out.println(helper.contentToString(req)); assertEquals("param1=value1¶m5=value5%40¶m6=value6a¶m6=value6b¶m2=value2a%40" + "¶m7={@var7}¶m8={@var8}", helper.contentToString(req)); } + @Test(enabled = true) + public void testFilterOutbound_MultiPart() throws Exception { + HttpRequest req = HttpRequestBuilder.postMultipart("http://test.com/path/test?uParam1=1", "param1=value1¶m2=value2¶m3=value3") + .build(); + + HttpMessageModificationFilter filter = new HttpMessageModificationFilter(); + filter.addOperation(new PostDataSetParamOperation("param1", "value11")); + filter.addOperation(new PostDataRemoveParamOperation("param3")); + filter.addOperation(new PostDataAddParamOperation("param5", "value5")); + filter.addOperation(new PostDataSetParamOperation("param6", "value6")); + filter.addOperation(new PostDataAddParamOperation("param1", "value12")); + + filter.filterOutbound(req, null, null); + + HttpParameters params = helper.decodeMultipartParameters(req); + + assertEquals("param1=value11¶m2=value2¶m5=value5¶m6=value6¶m1=value12", params.toString()); + } + @Test(enabled = true, dataProvider = "contents") public void testFilterOutbound_SetContent(String content, String rule, String newValue, String expectedContent, CharSequence contentType) throws Exception { HttpRequest req = HttpRequestBuilder