changeset 944:6dd4491e27b9

HttpSessionBlockerFilter
author Devel 2
date Fri, 04 Jan 2019 15:22:58 +0100
parents ad881e214d8d
children e352cd2f4c82
files stress-tester/src/main/java/com/passus/st/client/http/HttpConsts.java stress-tester/src/main/java/com/passus/st/client/http/filter/HttpSessionBlockerFilter.java stress-tester/src/main/java/com/passus/st/job/TestJob.java stress-tester/src/test/java/com/passus/st/client/http/filter/HttpSessionBlockerFilterTest.java
diffstat 4 files changed, 111 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/http/HttpConsts.java	Wed Sep 26 14:11:08 2018 +0200
+++ b/stress-tester/src/main/java/com/passus/st/client/http/HttpConsts.java	Fri Jan 04 15:22:58 2019 +0100
@@ -1,7 +1,6 @@
 package com.passus.st.client.http;
 
 /**
- *
  * @author Mirosław Hawrot
  */
 public class HttpConsts {
@@ -19,6 +18,7 @@
     public static final String TAG_TIME_START = "timeStart";
     public static final String TAG_TIME_END = "timeEnd";
     public static final String TAG_USERNAME = "userName";
+    public static final String TAG_SESSION_REQUESTS_COUNT = "sessionRequestsCount";
 
     private HttpConsts() {
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpSessionBlockerFilter.java	Fri Jan 04 15:22:58 2019 +0100
@@ -0,0 +1,73 @@
+package com.passus.st.client.http.filter;
+
+import com.passus.commons.annotations.Plugin;
+import com.passus.config.Configuration;
+import com.passus.config.ConfigurationContext;
+import com.passus.config.annotations.NodeDefinitionCreate;
+import com.passus.config.schema.MapNodeDefinition;
+import com.passus.config.schema.NodeDefinitionCreator;
+import com.passus.config.validation.LongValidator;
+import com.passus.net.http.HttpRequest;
+import com.passus.net.http.HttpResponse;
+import com.passus.st.ParametersBag;
+import com.passus.st.client.http.HttpFlowContext;
+import com.passus.st.plugin.PluginConstants;
+
+import static com.passus.config.schema.ConfigurationSchemaBuilder.*;
+import static com.passus.st.client.http.HttpConsts.TAG_SESSION_REQUESTS_COUNT;
+
+@NodeDefinitionCreate(HttpSessionBlockerFilter.HttpSessionBlockerFilterNodeDefinitionCreator.class)
+@Plugin(name = HttpSessionBlockerFilter.TYPE, category = PluginConstants.CATEGORY_HTTP_FILTER)
+public class HttpSessionBlockerFilter extends HttpFilter {
+
+    public static final String TYPE = "sessionBlocker";
+
+    public static final int DEFAULT_MAX_REQUESTS_COUNT = 10;
+
+    private int maxRequestsCount;
+
+    public HttpSessionBlockerFilter() {
+        this(DEFAULT_MAX_REQUESTS_COUNT);
+    }
+
+    public HttpSessionBlockerFilter(int maxRequestsCount) {
+        this.maxRequestsCount = maxRequestsCount;
+    }
+
+    @Override
+    public HttpFilter instanceForWorker(int index) {
+        return new HttpSessionBlockerFilter(maxRequestsCount);
+    }
+
+    @Override
+    public void configure(Configuration config, ConfigurationContext context) {
+        super.configure(config, context);
+        maxRequestsCount = config.getInteger("maxRequestsCount");
+    }
+
+    @Override
+    public int filterOutbound(HttpRequest request, HttpResponse response, HttpFlowContext context) {
+        ParametersBag session = context.scopes().getSession(request, false);
+        if (session != null && request != null) {
+            int requestsCount = (Integer) session.get(TAG_SESSION_REQUESTS_COUNT, 0);
+            requestsCount++;
+            if (requestsCount > maxRequestsCount) {
+                return DENY;
+            }
+
+            session.set(TAG_SESSION_REQUESTS_COUNT, requestsCount);
+        }
+
+        return DUNNO;
+    }
+
+    public static class HttpSessionBlockerFilterNodeDefinitionCreator implements NodeDefinitionCreator {
+
+        @Override
+        public MapNodeDefinition create() {
+            return mapDef(
+                    tupleDef("maxRequestsCount", valueDefInteger().addValidator(LongValidator.GREATER_EQUAL_ZERO))
+            );
+        }
+    }
+}
--- a/stress-tester/src/main/java/com/passus/st/job/TestJob.java	Wed Sep 26 14:11:08 2018 +0200
+++ b/stress-tester/src/main/java/com/passus/st/job/TestJob.java	Fri Jan 04 15:22:58 2019 +0100
@@ -1,9 +1,7 @@
 package com.passus.st.job;
 
 import com.passus.commons.Assert;
-import com.passus.config.Configuration;
 import com.passus.config.ConfigurationContext;
-import com.passus.config.validation.Errors;
 import com.passus.filter.ValueExtractor;
 import com.passus.st.client.Client;
 import com.passus.st.client.http.HttpClient;
@@ -12,7 +10,6 @@
 import com.passus.st.client.http.ReporterDestination;
 import com.passus.st.client.http.filter.HttpFilter;
 import com.passus.st.client.http.filter.HttpVarsFilter;
-import com.passus.st.config.TestJobConfigurator;
 import com.passus.st.emitter.Emitter;
 import com.passus.st.emitter.PassThroughSessionMapper;
 import com.passus.st.emitter.nio.NioEmitter;
@@ -26,9 +23,9 @@
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
+import java.util.Map;
 
 import static com.passus.st.utils.ConfigurationContextConsts.*;
-import java.util.Map;
 
 /**
  * @author Mirosław Hawrot
@@ -129,13 +126,13 @@
         Assert.notNull(context, "context");
         TestJob testJob = new TestJob();
 
-        List<EventSource> cfgEventSources = (List<EventSource>) context.get(EVENT_SOURCE_SOURCES);
+        List<EventSource> cfgEventSources = context.get(EVENT_SOURCE_SOURCES);
         populateIfNotNull(cfgEventSources, testJob.eventSources);
 
-        List<Client> cfgClients = (List<Client>) context.get(CLIENT_CLIENTS);
+        List<Client> cfgClients = context.get(CLIENT_CLIENTS);
         populateIfNotNull(cfgClients, testJob.clients);
 
-        List<Emitter> cfgEmitters = (List<Emitter>) context.get(EMITTER_EMITTERS);
+        List<Emitter> cfgEmitters = context.get(EMITTER_EMITTERS);
         populateIfNotNull(cfgEmitters, testJob.emitters);
 
         testJob.defaultEmitter = (Emitter) context.get(EMITTER_DEFAULT_EMITTER);
@@ -220,7 +217,7 @@
             }
 
             // TODO: czy dodać filtry top-level do klienta???
-            
+
             clients.forEach((client) -> {
                 if (client instanceof HttpClient) {
                     HttpClient hc = (HttpClient) client;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpSessionBlockerFilterTest.java	Fri Jan 04 15:22:58 2019 +0100
@@ -0,0 +1,32 @@
+package com.passus.st.client.http.filter;
+
+import com.passus.net.http.HttpRequest;
+import com.passus.st.client.http.HttpFlowContext;
+import org.testng.annotations.Test;
+
+import static com.passus.net.http.HttpRequestBuilder.get;
+import static com.passus.st.client.http.HttpConsts.TAG_SESSION_ID;
+import static com.passus.st.client.http.filter.HttpFilterTestUtils.createMockContext;
+import static org.testng.AssertJUnit.assertEquals;
+
+public class HttpSessionBlockerFilterTest {
+
+    private HttpRequest createRequest() {
+        HttpRequest req = get("http://test/test").build();
+        req.setTag(TAG_SESSION_ID, "sid1");
+        return req;
+    }
+
+    @Test
+    public void testFilter() {
+        HttpSessionBlockerFilter filter = new HttpSessionBlockerFilter(2);
+        HttpFlowContext mockContext = createMockContext();
+        mockContext.scopes().createSession("sid1");
+        HttpRequest req = get("http://test/test").build();
+        req.setTag(TAG_SESSION_ID, "sid1");
+
+        assertEquals(HttpSessionBlockerFilter.DUNNO, filter.filterOutbound(req, null, mockContext));
+        assertEquals(HttpSessionBlockerFilter.DUNNO, filter.filterOutbound(req, null, mockContext));
+        assertEquals(HttpSessionBlockerFilter.DENY, filter.filterOutbound(req, null, mockContext));
+    }
+}
\ No newline at end of file