changeset 781:80b01b797330

HttpVarsFilterTest
author Devel 1
date Tue, 19 Dec 2017 14:19:04 +0100
parents 6ae3b6a8bd5d
children 2ad0c7dbe571
files stress-tester/src/test/java/com/passus/st/client/http/filter/HttpVarsFilterTest.java
diffstat 1 files changed, 94 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpVarsFilterTest.java	Tue Dec 19 13:30:43 2017 +0100
+++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpVarsFilterTest.java	Tue Dec 19 14:19:04 2017 +0100
@@ -1,17 +1,22 @@
 package com.passus.st.client.http.filter;
 
-import com.passus.data.ByteString;
+import static com.passus.commons.collection.FluentBuilder.*;
 import com.passus.data.DataSourceUtils;
+import com.passus.data.utils.CompressionUtils;
 import com.passus.filter.UnmutableValueExtractor;
 import com.passus.filter.ValueExtractor;
+import com.passus.net.http.HttpConsts;
+import com.passus.net.http.HttpDataContainer;
 import com.passus.net.http.HttpDataPart;
+import com.passus.net.http.HttpHeaders;
 import com.passus.net.http.HttpMessageHelper;
+import com.passus.net.http.HttpMethod;
 import com.passus.net.http.HttpRequest;
+import com.passus.net.http.HttpRequestBuilder;
 import com.passus.st.utils.TestHttpUtils;
 import com.passus.st.vars.VarsValueExtractorResolver;
 import java.io.IOException;
 import java.nio.charset.Charset;
-import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import static org.testng.AssertJUnit.*;
@@ -25,10 +30,22 @@
 
     private static final Charset UTF_8 = Charset.forName("UTF-8");
 
-    private final ValueExtractor value = new UnmutableValueExtractor("value1");
-    private final Map<String, ValueExtractor> vars = Collections.singletonMap("var1", value);
+    private final Map<String, ValueExtractor> vars = map(
+            e("var1", val("value1")),
+            e("name", val("X")),
+            e("type", val("x"))
+    );
+
     private final VarsValueExtractorResolver resolver = new VarsValueExtractorResolver(vars);
 
+    private static UnmutableValueExtractor val(Object o) {
+        return new UnmutableValueExtractor(o);
+    }
+
+    private static String content(HttpDataContainer container, Charset ch) throws IOException {
+        return DataSourceUtils.toString(container.getContent(), ch);
+    }
+
     @Test
     public void testPostUrlEncoded() throws IOException {
         String reqs = "POST /login_check HTTP/1.1\r\n"
@@ -42,12 +59,84 @@
         HttpVarsFilter filter = new HttpVarsFilter(resolver);
 
         filter.filterOutbound(request, null, null);
-        
+
         String content = request.getContentByteString().toString();
         int contentLength = (int) HttpMessageHelper.get().getContentLength(request);
         assertEquals("user=admin&pass=value1", content);
         assertEquals(content.length(), contentLength);
     }
 
+    @Test
+    public void testPostMultipart() throws IOException {
+        String hdrs
+                = "POST /event_processing/edit/53ba69fcc2364828037a6bd7 HTTP/1.1\r\n"
+                + "Host: 172.16.60.159\r\n"
+                + "X-Requested-With: XMLHttpRequest\r\n"
+                + "Content-Length: 359\r\n"
+                + "Content-Type: multipart/form-data; boundary=------------------1456847137\r\n\r\n";
+        String c1
+                = "--------------------1456847137\r\n"
+                + "Content-Disposition: form-data; name=\"name\"\r\n\r\n"
+                + "Filter ${name}\r\n"
+                + "--------------------1456847137\r\n"
+                + "Content-Disposition: form-data; name=\"description\"\r\n\r\n"
+                + "Komputery korzystające z netu bez logowania do domeny\r\n"
+                + "--------------------1456847137\r\n"
+                + "Content-Disposition: form-data; name=\"type\"\r\n\r\n"
+                + "${type}\r\n";
+        String cn = "--------------------1456847137--\r\n";
 
+        byte[] bytes = (hdrs + c1 + cn).getBytes(UTF_8);
+        assertEquals(359, (c1 + cn).getBytes(UTF_8).length);
+
+        HttpRequest request = TestHttpUtils.request(bytes);
+        List<HttpDataPart> parts = HttpMessageHelper.get().decodeMultipart(request);
+        assertEquals(3, parts.size());
+        assertTrue(content(parts.get(1), UTF_8).contains("korzystające"));
+
+        HttpVarsFilter filter = new HttpVarsFilter(resolver);
+
+        filter.filterOutbound(request, null, null);
+
+        parts = HttpMessageHelper.get().decodeMultipart(request);
+        assertEquals(3, parts.size());
+        assertEquals("Filter X", content(parts.get(0), UTF_8));
+        assertTrue(content(parts.get(1), UTF_8).contains("korzystające"));
+        assertEquals("x", content(parts.get(2), UTF_8));
+    }
+
+    @Test
+    public void testPut() throws IOException {
+        HttpRequestBuilder builder = new HttpRequestBuilder("http://test.com", HttpMethod.PUT);
+        builder.content("{\"abc\": \"${var1}\"}", "application/json");
+        HttpRequest request = builder.build();
+
+        HttpVarsFilter filter = new HttpVarsFilter(resolver);
+
+        filter.filterOutbound(request, null, null);
+
+        int contentLength = (int) HttpMessageHelper.get().getContentLength(request);
+        assertEquals("{\"abc\": \"value1\"}", content(request, UTF_8));
+        assertEquals(17, contentLength);
+    }
+
+    @Test
+    public void testPutDeflate() throws IOException {
+        String json = "{\"abc\": \"${var1}\"}";
+        byte[] plain = json.getBytes(); //18
+        byte[] deflate = CompressionUtils.deflate(plain); //26
+
+        HttpRequestBuilder builder = new HttpRequestBuilder("http://test.com", HttpMethod.PUT);
+        builder.content(deflate).header(HttpHeaders.CONTENT_ENCODING, HttpConsts.DEFLATE);
+        HttpRequest request = builder.build();
+
+        HttpVarsFilter filter = new HttpVarsFilter(resolver);
+
+        filter.filterOutbound(request, null, null);
+
+        int contentLength = (int) HttpMessageHelper.get().getContentLength(request);
+        assertEquals("{\"abc\": \"value1\"}", content(request, UTF_8));
+        assertEquals(17, contentLength);
+        assertNull(request.getHeaders().get(HttpHeaders.CONTENT_ENCODING));
+    }
 }