changeset 580:a521874e90ea

HttpMatchFilterTest - more tests
author Devel 1
date Fri, 29 Sep 2017 16:35:53 +0200
parents 100d4bff9f82
children 618d4bc8e7a1
files stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMatchFilterTest.java
diffstat 1 files changed, 42 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMatchFilterTest.java	Fri Sep 29 16:35:29 2017 +0200
+++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMatchFilterTest.java	Fri Sep 29 16:35:53 2017 +0200
@@ -7,6 +7,10 @@
 import java.util.List;
 import org.testng.annotations.Test;
 import com.passus.filter.config.PredicateNodeTransformer;
+import com.passus.net.http.HttpMessage;
+import com.passus.net.http.HttpResponse;
+import com.passus.net.http.HttpResponseBuilder;
+import com.passus.net.http.HttpStatus;
 import com.passus.st.AppUtils;
 import java.util.Arrays;
 import java.util.function.Predicate;
@@ -28,6 +32,12 @@
             HttpRequestBuilder.get("http://test2.com/test2").build()
     );
 
+    private final List<HttpResponse> responses = Arrays.asList(
+            HttpResponseBuilder.ok().build(),
+            HttpResponseBuilder.ok().status(HttpStatus.CREATED).build(),
+            HttpResponseBuilder.ok().status(HttpStatus.ACCEPTED).build()
+    );
+
     private HttpMessagePredicate predicate(String config) {
         try {
             Predicate predicate = nodeTransformer.transform(config);
@@ -37,16 +47,16 @@
         }
     }
 
-    private List<HttpRequest> filter(List<HttpRequest> requests, String config) {
+    private <T extends HttpMessage> List<T> filter(List<T> messages, String config) {
         HttpMessagePredicate predicate = predicate(config);
-        return filter(requests, predicate);
+        return filter(messages, predicate);
     }
 
-    private List<HttpRequest> filter(List<HttpRequest> requests, HttpMessagePredicate predicate) {
-        List<HttpRequest> out = new ArrayList<>(requests.size());
-        for (HttpRequest req : requests) {
-            if (predicate.test(req)) {
-                out.add(req);
+    private <T extends HttpMessage> List<T> filter(List<T> messages, HttpMessagePredicate predicate) {
+        List<T> out = new ArrayList<>(messages.size());
+        for (T message : messages) {
+            if (predicate.test(message)) {
+                out.add(message);
             }
         }
 
@@ -65,17 +75,28 @@
 
     @Test
     public void testFilterOutbound() {
-        List<HttpRequest> res = filter(requests, "{req.url: {$contains: \"test2\"}}");
-        assertEquals(1, res.size());
-        assertEquals("/test2", res.get(0).getUri().toString());
+        List<HttpRequest> reqs;
+        reqs = filter(requests, "{req.url: {$contains: \"test2\"}}");
+        assertEquals(1, reqs.size());
+        assertEquals("/test2", reqs.get(0).getUri().toString());
 
-        res = filter(requests, "{req.uri: {$contains: \"test2\"}}");
-        assertEquals(1, res.size());
-        assertEquals("/test2", res.get(0).getUri().toString());
+        reqs = filter(requests, "{req.uri: {$contains: \"test2\"}}");
+        assertEquals(1, reqs.size());
+        assertEquals("/test2", reqs.get(0).getUri().toString());
 
-        res = filter(requests, "{\"req.getHeader('Header')\": {$contains: \"Val\"}}");
-        assertEquals(1, res.size());
-        assertEquals("/test", res.get(0).getUri().toString());
+        reqs = filter(requests, "{\"req.getHeader('Header')\": {$contains: \"Val\"}}");
+        assertEquals(1, reqs.size());
+        assertEquals("/test", reqs.get(0).getUri().toString());
+
+        List<HttpResponse> resps;
+        resps = filter(responses, "{resp.status.code: 200}");
+        assertEquals(1, resps.size());
+
+        resps = filter(responses, "{resp.status.code: '200'}");
+        assertEquals(1, resps.size());
+
+        resps = filter(responses, "{resp.status.code: {$gt: 200}}");
+        assertEquals(2, resps.size());
     }
 
     @Test
@@ -83,7 +104,8 @@
         String filterConfig = "filters:\n"
                 + "    - type: matcher\n"
                 + "      matches:\n"
-                + "          \"req.uri\": {$contains: \"test\"}\n";
+                + "          req.uri: {$contains: 'test'}\n"
+                + "          resp.status.code: 200\n";
 
         Errors errors = new Errors();
         List<HttpFilter> filters = HttpFiltersConfigurator.getFilters(filterConfig, errors);
@@ -92,6 +114,9 @@
         assertEquals(1, filters.size());
         assertTrue(filters.get(0) instanceof HttpMatchFilter);
 
+        HttpMatchFilter filter = (HttpMatchFilter) filters.get(0);
+        HttpMessagePredicate predicate = filter.getPredicate();
+
         List<HttpRequest> res = filter(requests, "{req.url: {$contains: \"test2\"}}");
         assertEquals(1, res.size());
         assertEquals("/test2", res.get(0).getUri().toString());