changeset 520:31fae01b09d6

HttpMessageWrapperTest
author Devel 1
date Wed, 23 Aug 2017 10:11:02 +0200
parents 7178dc5af601
children dd71d49065ad
files stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageWrapperTest.java
diffstat 1 files changed, 184 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageWrapperTest.java	Wed Aug 23 10:11:02 2017 +0200
@@ -0,0 +1,184 @@
+package com.passus.st.client.http.filter;
+
+import com.passus.data.ByteString;
+import com.passus.filter.BeanValueExtractor;
+import com.passus.filter.MvelValueExtractor;
+import com.passus.filter.UnmutableValueExtractor;
+import com.passus.filter.ValueExtractorParser;
+import com.passus.net.http.HttpRequest;
+import com.passus.net.http.HttpRequestBuilder;
+import com.passus.net.http.HttpResponse;
+import com.passus.net.http.HttpResponseBuilder;
+import com.passus.net.http.HttpStatus;
+import com.passus.net.http.URL;
+import com.passus.st.ParametersBag;
+import static com.passus.st.client.http.HttpConsts.TAG_SESSION_ID;
+import com.passus.st.client.http.HttpFlowContext;
+import com.passus.st.client.http.HttpScopes;
+import java.text.ParseException;
+import static org.testng.AssertJUnit.*;
+import org.testng.annotations.Test;
+
+/**
+ *
+ * @author mikolaj.podbielski
+ */
+public class HttpMessageWrapperTest {
+
+    private static final String SESSION_ID = "sessId";
+    HttpRequest req = HttpRequestBuilder.get("http://example.com/index?action=delete")
+            .header("User-Agent", "stress-tester").cookie("id", "qowdhncdlasndf")
+            .tag(TAG_SESSION_ID, SESSION_ID).build();
+    HttpResponse resp = HttpResponseBuilder.ok().header("Content-Type", "text/plain")
+            .cookie("id", "newid").tag(TAG_SESSION_ID, SESSION_ID).build();
+    HttpScopes scopes = new HttpScopes();
+    HttpFlowContext context = new HttpFlowContext(null, scopes);
+
+    {
+        ParametersBag session = scopes.createSession(SESSION_ID);
+        session.set("Var1", "Val1");
+    }
+
+    HttpMessageWrapper wrapper = new HttpMessageWrapper(req, resp, context);
+    ValueExtractorParser parser = new ValueExtractorParser();
+
+    @Test
+    public void testUnmutableValueExtractor() throws ParseException {
+        UnmutableValueExtractor e;
+        Object value;
+
+        e = val("123");
+        value = e.extract(null);
+        assertEquals(123, value);
+
+        e = val("5000000000");
+        value = e.extract(null);
+        assertEquals(5000000000L, value);
+
+        e = val("123.456");
+        value = e.extract(null);
+        assertEquals(123.456, value);
+
+        e = val("word");
+        value = e.extract(null);
+        assertEquals("word", value);
+    }
+
+    @Test
+    public void testBeanValueExtractor() throws ParseException {
+        BeanValueExtractor e;
+        Object value;
+
+        e = bean("$req.getVersion()");
+        value = e.extract(wrapper);
+        assertTrue(value instanceof ByteString);
+        assertEquals("HTTP/1.1", value.toString());
+
+        e = bean("$req.version");
+        value = e.extract(wrapper);
+        assertEquals("HTTP/1.1", value.toString());
+
+        e = bean("$req.uri");
+        value = e.extract(wrapper);
+        assertEquals("/index?action=delete", value.toString());
+
+        e = bean("$req.url");
+        value = e.extract(wrapper);
+        assertTrue(value instanceof URL);
+
+        e = bean("$req.url.query");
+        value = e.extract(wrapper);
+        assertEquals("action=delete", value.toString());
+
+        e = bean("$req.getHeader('User-Agent')");
+        value = e.extract(wrapper);
+        assertEquals("stress-tester", value.toString());
+
+        e = bean("$resp.status");
+        value = e.extract(wrapper);
+        assertTrue(value instanceof HttpStatus);
+
+        e = bean("$resp.status.code");
+        value = e.extract(wrapper);
+        assertEquals(200, value);
+
+        e = bean("$resp.getHeader('Content-Type')");
+        value = e.extract(wrapper);
+        assertEquals("text/plain", value.toString());
+
+        e = bean("$context");
+        value = e.extract(wrapper);
+        assertNull(value);
+
+        e = bean("$scopes");
+        value = e.extract(wrapper);
+        assertTrue(value instanceof HttpScopes);
+
+        e = bean("$httpSession");
+        value = e.extract(wrapper);
+        assertTrue(value instanceof ParametersBag);
+
+        e = bean("$httpSession.get('Var1')");
+        value = e.extract(wrapper);
+        assertEquals("Val1", value);
+    }
+
+    @Test
+    public void testMvelValueExtractor() throws ParseException {
+        MvelValueExtractor e;
+        Object value;
+
+        e = mvel("$req.getVersion()");
+        value = e.extract(wrapper);
+        assertEquals("HTTP/1.1", value);
+
+        e = mvel("$req.version");
+        value = e.extract(wrapper);
+        assertEquals("HTTP/1.1", value);
+
+//        e = mvel("$req.url");
+//        value = e.extract(wrapper);
+//        // String
+
+        e = mvel("$req.url.query");
+        value = e.extract(wrapper);
+        assertEquals("action=delete", value);
+
+        e = mvel("$req.getHeader('User-Agent')");
+        value = e.extract(wrapper);
+        assertEquals("stress-tester", value);
+
+        e = mvel("$resp.status.code");
+        value = e.extract(wrapper);
+        assertEquals(200, value);
+
+        e = mvel("$httpSession.get('Var1')");
+        value = e.extract(wrapper);
+        assertEquals("Val1", value);
+
+        e = mvel("$httpSession.get('NoSuchVar') or 'defaultValue'");
+        value = e.extract(wrapper);
+        assertEquals("defaultValue", value);
+
+        e = mvel("$httpSession.get('NoSuchVar') or $httpSession.get('Var1')");
+        value = e.extract(wrapper);
+        assertEquals("Val1", value);
+
+        e = mvel("$httpSession.get('Var1') + $httpSession.get('Var1')");
+        value = e.extract(wrapper);
+        assertEquals("Val1Val1", value);
+
+    }
+
+    private UnmutableValueExtractor val(String expr) throws ParseException {
+        return (UnmutableValueExtractor) parser.parse(expr);
+    }
+
+    private BeanValueExtractor bean(String expr) throws ParseException {
+        return (BeanValueExtractor) parser.parse(expr);
+    }
+
+    private MvelValueExtractor mvel(String expr) throws ParseException {
+        return parser.parseExpression(expr);
+    }
+}