changeset 527:05d63b5bf1d1

HttpLogoutFilter
author Devel 2
date Thu, 31 Aug 2017 09:55:22 +0200
parents c72b7f546870
children f6b363ed693b
files stress-tester/src/main/java/com/passus/st/client/http/HttpScopes.java stress-tester/src/main/java/com/passus/st/client/http/filter/HttpLogoutFilter.java stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessagePredicate.java stress-tester/src/test/java/com/passus/st/client/http/filter/HttpLogoutFilterTest.java
diffstat 4 files changed, 241 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/http/HttpScopes.java	Tue Aug 29 12:45:42 2017 +0200
+++ b/stress-tester/src/main/java/com/passus/st/client/http/HttpScopes.java	Thu Aug 31 09:55:22 2017 +0200
@@ -66,6 +66,14 @@
         return session;
     }
 
+    public boolean containsSession(String sessionId) {
+        return (getSession(sessionId, ZONE_DEFAULT, false) != null);
+    }
+
+    public boolean containsSession(String sessionId, String zone) {
+        return (getSession(sessionId, zone, false) != null);
+    }
+
     public boolean removeSession(String sessionId) {
         return removeSession(sessionId, ZONE_DEFAULT);
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpLogoutFilter.java	Thu Aug 31 09:55:22 2017 +0200
@@ -0,0 +1,108 @@
+package com.passus.st.client.http.filter;
+
+import com.passus.commons.annotations.Plugin;
+import com.passus.config.Configuration;
+import com.passus.config.annotations.NodeDefinitionCreate;
+import static com.passus.config.schema.ConfigurationSchemaBuilder.mapDef;
+import static com.passus.config.schema.ConfigurationSchemaBuilder.tupleDef;
+import static com.passus.config.schema.ConfigurationSchemaBuilder.valueDefBool;
+import com.passus.config.schema.NodeDefinition;
+import com.passus.config.schema.NodeDefinitionCreator;
+import com.passus.net.http.HttpRequest;
+import com.passus.net.http.HttpResponse;
+import com.passus.st.ParametersBag;
+import com.passus.st.client.credentials.CredentialsProvider;
+import com.passus.st.client.http.HttpConsts;
+import static com.passus.st.client.http.HttpConsts.PARAM_USERNAME;
+import static com.passus.st.client.http.HttpConsts.TAG_SESSION_ID;
+import com.passus.st.client.http.HttpFlowContext;
+import com.passus.st.client.http.filter.HttpFilter;
+import com.passus.st.client.http.filter.HttpFilterMessagePredicateNodeDefinition;
+import com.passus.st.client.http.filter.HttpMessagePredicate;
+import com.passus.st.plugin.PluginConstants;
+
+/**
+ *
+ * @author Mirosław Hawrot
+ */
+@NodeDefinitionCreate(HttpLogoutFilter.HttpLogoutFilterNodeDefinitionCreator.class)
+@Plugin(name = HttpLogoutFilter.TYPE, category = PluginConstants.CATEGORY_HTTP_FILTER)
+public class HttpLogoutFilter extends HttpFilter {
+
+    public static final String TYPE = "logout";
+
+    private boolean invalidateSession = true;
+
+    private HttpMessagePredicate predicate;
+
+    public HttpLogoutFilter() {
+    }
+
+    public HttpLogoutFilter(HttpMessagePredicate predicate, boolean invalidateSession) {
+        this.predicate = predicate;
+        this.invalidateSession = invalidateSession;
+    }
+
+    public boolean isInvalidateSession() {
+        return invalidateSession;
+    }
+
+    public void setInvalidateSession(boolean invalidateSession) {
+        this.invalidateSession = invalidateSession;
+    }
+
+    public HttpMessagePredicate getPredicate() {
+        return predicate;
+    }
+
+    public void setPredicate(HttpMessagePredicate predicate) {
+        this.predicate = predicate;
+    }
+
+    @Override
+    public void configure(Configuration config) {
+        predicate = (HttpMessagePredicate) config.get("applyIf");
+        invalidateSession = config.getBoolean("invalidateSession", true);
+    }
+
+    @Override
+    public int filterOutbound(HttpRequest req, HttpResponse resp, HttpFlowContext context) {
+        if (req != null
+                && predicate != null
+                && predicate.test(req, resp, context)) {
+            ParametersBag session = context.scopes().getSession(req);
+            if (session != null) {
+                session.remove(PARAM_USERNAME);
+
+                if (invalidateSession) {
+                    context.scopes().removeSession(req);
+                    req.removeTag(TAG_SESSION_ID);
+                    if (resp != null) {
+                        resp.removeTag(TAG_SESSION_ID);
+                    }
+                }
+            }
+        }
+
+        return DUNNO;
+    }
+
+    @Override
+    public HttpLogoutFilter instanceForWorker(int index) {
+        return new HttpLogoutFilter(predicate, invalidateSession);
+    }
+
+    public static class HttpLogoutFilterNodeDefinitionCreator implements NodeDefinitionCreator {
+
+        @Override
+        public NodeDefinition create() {
+
+            return mapDef(
+                    tupleDef("invalidateSession", valueDefBool()).setRequired(false),
+                    tupleDef("applyIf", new HttpFilterMessagePredicateNodeDefinition())
+            );
+        }
+
+    }
+
+}
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessagePredicate.java	Tue Aug 29 12:45:42 2017 +0200
+++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessagePredicate.java	Thu Aug 31 09:55:22 2017 +0200
@@ -1,9 +1,14 @@
 package com.passus.st.client.http.filter;
 
 import com.passus.commons.Assert;
+import com.passus.commons.ConversionException;
+import com.passus.config.NodeException;
+import com.passus.filter.config.PredicateNodeTransformer;
 import com.passus.net.http.HttpMessage;
 import com.passus.net.http.HttpRequest;
 import com.passus.net.http.HttpResponse;
+import com.passus.st.client.http.HttpFlowContext;
+import java.io.IOException;
 import java.util.function.Predicate;
 
 /**
@@ -19,6 +24,10 @@
         this.predicate = predicate;
     }
 
+    public boolean test(HttpRequest req, HttpResponse resp, HttpFlowContext context) {
+        return test(new HttpMessageWrapper(req, resp, context));
+    }
+
     public boolean test(HttpMessageWrapper wrapper) {
         return predicate.test(wrapper);
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpLogoutFilterTest.java	Thu Aug 31 09:55:22 2017 +0200
@@ -0,0 +1,116 @@
+package com.passus.st.client.http.filter;
+
+import com.passus.config.validation.Errors;
+import com.passus.filter.config.PredicateNodeTransformer;
+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.st.AppUtils;
+import static com.passus.st.client.http.HttpConsts.PARAM_USERNAME;
+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.util.List;
+import java.util.function.Predicate;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.testng.AssertJUnit.assertEquals;
+import static org.testng.AssertJUnit.assertFalse;
+import static org.testng.AssertJUnit.assertTrue;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+/**
+ *
+ * @author Mirosław Hawrot
+ */
+public class HttpLogoutFilterTest {
+
+    private final PredicateNodeTransformer nodeTransformer = new PredicateNodeTransformer();
+
+    @BeforeClass
+    public static void beforeClass() {
+        AppUtils.registerAll();
+    }
+
+    @AfterClass
+    public static void afterClass() {
+        AppUtils.unregisterAll();
+    }
+
+    private HttpMessagePredicate predicate(String config) {
+        try {
+            Predicate predicate = nodeTransformer.transform(config);
+            return new HttpMessagePredicate(predicate);
+        } catch (Exception e) {
+            throw new RuntimeException(e.getMessage(), e);
+        }
+    }
+
+    @Test
+    public void testFilter_InvalidateSession() {
+        HttpMessagePredicate predicate = predicate("{\"req.uri\": \"/logout\"}");
+        HttpRequest req = HttpRequestBuilder.get("http://test.com/logout").build();
+        HttpResponse resp = HttpResponseBuilder.ok().build();
+        HttpScopes scopes = new HttpScopes();
+        String sessionId = "123";
+        scopes.createSession(sessionId).set(PARAM_USERNAME, "username");
+
+        req.setTag(TAG_SESSION_ID, sessionId);
+        resp.setTag(TAG_SESSION_ID, sessionId);
+
+        HttpFlowContext mockContext = mock(HttpFlowContext.class);
+        when(mockContext.scopes()).thenReturn(scopes);
+        HttpLogoutFilter filter = new HttpLogoutFilter(predicate, true);
+        filter.filterOutbound(req, resp, mockContext);
+
+        assertFalse(req.containsTag(TAG_SESSION_ID));
+        assertFalse(resp.containsTag(TAG_SESSION_ID));
+        assertFalse(scopes.containsSession(sessionId));
+    }
+
+    @Test
+    public void testFilter_NotInvalidateSession() {
+        HttpMessagePredicate predicate = predicate("{\"req.uri\": \"/logout\"}");
+        HttpRequest req = HttpRequestBuilder.get("http://test.com/logout").build();
+        HttpResponse resp = HttpResponseBuilder.ok().build();
+        HttpScopes scopes = new HttpScopes();
+        String sessionId = "123";
+        scopes.createSession(sessionId).set(PARAM_USERNAME, "username");
+
+        req.setTag(TAG_SESSION_ID, sessionId);
+        resp.setTag(TAG_SESSION_ID, sessionId);
+
+        HttpFlowContext mockContext = mock(HttpFlowContext.class);
+        when(mockContext.scopes()).thenReturn(scopes);
+        HttpLogoutFilter filter = new HttpLogoutFilter(predicate, false);
+        filter.filterOutbound(req, resp, mockContext);
+
+        assertTrue(req.containsTag(TAG_SESSION_ID));
+        assertTrue(resp.containsTag(TAG_SESSION_ID));
+        assertTrue(scopes.containsSession(sessionId));
+        assertFalse(scopes.getSession(sessionId).contains(PARAM_USERNAME));
+    }
+
+    @Test
+    public void testConfigure() throws Exception {
+        String filterConfig = "filters:\n"
+                + "    - type: logout\n"
+                + "      invalidateSession: false\n"
+                + "      applyIf: {\"req.uri\": \"/logout\"}\n";
+
+        Errors errors = new Errors();
+        List<HttpFilter> filters = HttpFiltersConfigurator.getFilters(filterConfig, errors);
+
+        assertEquals(0, errors.getErrorCount());
+        assertEquals(1, filters.size());
+        assertTrue(filters.get(0) instanceof HttpLogoutFilter);
+
+        HttpLogoutFilter filter = (HttpLogoutFilter) filters.get(0);
+        assertFalse(filter.isInvalidateSession());
+        assertTrue(filter.getPredicate() != null);
+    }
+
+}