Mercurial > stress-tester
changeset 821:efc9f09afa8d
PostValueExtractor - escape option
author | Devel 1 |
---|---|
date | Thu, 18 Jan 2018 10:53:52 +0100 |
parents | 3b8b53f6ec89 |
children | 8977a1a9087a |
files | stress-tester/src/main/java/com/passus/st/client/http/extractor/ContentReplacer.java stress-tester/src/main/java/com/passus/st/client/http/extractor/PostValueExtractor.java stress-tester/src/test/java/com/passus/st/client/http/extractor/PostValueExtractorTest.java |
diffstat | 3 files changed, 53 insertions(+), 10 deletions(-) [+] |
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/http/extractor/ContentReplacer.java Thu Jan 18 10:22:53 2018 +0100 +++ b/stress-tester/src/main/java/com/passus/st/client/http/extractor/ContentReplacer.java Thu Jan 18 10:53:52 2018 +0100 @@ -21,4 +21,6 @@ public CharSequence replace(CharSequence content, CharSequence value) throws IOException; + public default void setOption(String name, Object value) { + } }
--- a/stress-tester/src/main/java/com/passus/st/client/http/extractor/PostValueExtractor.java Thu Jan 18 10:22:53 2018 +0100 +++ b/stress-tester/src/main/java/com/passus/st/client/http/extractor/PostValueExtractor.java Thu Jan 18 10:53:52 2018 +0100 @@ -17,26 +17,41 @@ private final ByteString fieldName; + private boolean escape = true; + public PostValueExtractor(CharSequence fieldName) { Assert.notNull(fieldName, "fieldName"); this.fieldName = ByteString.create(fieldName); } @Override + public void setOption(String name, Object value) { + switch (name) { + case "escape": + if (Boolean.FALSE.equals(value)) { + escape = false; + } else if (Boolean.TRUE.equals(value)) { + escape = true; + } + break; + } + } + + @Override public void replace(HttpMessage message, CharSequence value) throws IOException { - HttpParameters params = helper.decodeFormUrlencoded(message); + HttpParameters params = helper.decodeFormUrlencoded(message, escape); if (params != null) { params.set(fieldName, value); - helper.setFormUrlencoded(message, params); + helper.setFormUrlencoded(message, params, escape); } } @Override public CharSequence replace(CharSequence content, CharSequence value) throws IOException { - HttpParameters params = helper.decodeFormUrlencoded(content); + HttpParameters params = helper.decodeFormUrlencoded(content, escape); if (params != null) { params.set(fieldName, value); - return params.toString(); + return params.toString(escape); } return null;
--- a/stress-tester/src/test/java/com/passus/st/client/http/extractor/PostValueExtractorTest.java Thu Jan 18 10:22:53 2018 +0100 +++ b/stress-tester/src/test/java/com/passus/st/client/http/extractor/PostValueExtractorTest.java Thu Jan 18 10:53:52 2018 +0100 @@ -1,5 +1,10 @@ package com.passus.st.client.http.extractor; +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 java.io.IOException; import static org.testng.AssertJUnit.*; import org.testng.annotations.Test; @@ -10,14 +15,35 @@ */ public class PostValueExtractorTest { - private static CharSequence replace(String fieldName, String content, String value) throws IOException { - return new PostValueExtractor(fieldName).replace(content, value); + @Test + public void testReplace() throws IOException { + String content = "a=value1&b=value2"; + PostValueExtractor replacer = new PostValueExtractor("b"); + assertEquals("a=value1&b=newValue2", replacer.replace(content, "newValue2")); } @Test - public void testReplace() throws Exception { - String content = "a=value1&b=value2"; - assertEquals("a=value1&b=newValue2", replace("b", content, "newValue2")); + public void testReplaceSpecialCharacters() throws IOException { + String contentString = "a=%40%26%3D&b=value2"; + byte[] content = contentString.getBytes(); + HttpRequest req = HttpRequestBuilder.post("http://www.example.com") + .content(content, MimeTypes.APPLICATION_FORM_URLENCODED) + .header(HttpHeaders.CONTENT_LENGTH, String.valueOf(content.length)) + .build(); + HttpMessageHelper helper = HttpMessageHelper.get(); + + PostValueExtractor replacer = new PostValueExtractor("b"); + String expectedEnc = "a=%40%26%3D&b=%7B%40var%7D"; + String expectedRaw = "a=%40%26%3D&b={@var}"; + + replacer.setOption("escape", true); + replacer.replace(req, "{@var}"); + assertEquals(expectedEnc, helper.contentToString(req)); + assertEquals(expectedEnc, replacer.replace(contentString, "{@var}")); + + replacer.setOption("escape", false); + replacer.replace(req, "{@var}"); + assertEquals(expectedRaw, helper.contentToString(req)); + assertEquals(expectedRaw, replacer.replace(contentString, "{@var}")); } - }