Mercurial > stress-tester
changeset 490:df8db8c1a760
refactoring
line wrap: on
line diff
--- a/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpBasicAuthLoginFilterTest.java Thu Aug 10 11:58:39 2017 +0200 +++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpBasicAuthLoginFilterTest.java Thu Aug 10 14:03:21 2017 +0200 @@ -6,6 +6,7 @@ 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 com.passus.st.client.credentials.Credentials; import com.passus.st.client.credentials.CredentialsProvider; @@ -14,13 +15,10 @@ 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 static com.passus.st.client.http.filter.HttpBasicAuthLoginFilter.decodeUsername; import static com.passus.st.client.http.filter.HttpBasicAuthLoginFilter.encode; import com.passus.st.utils.TestHttpUtils; import java.util.List; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; import static org.testng.Assert.*; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; @@ -32,25 +30,10 @@ */ public class HttpBasicAuthLoginFilterTest extends HttpAbstractLoginFilterTest { - private static final String REQS = "GET /html/basic/index2.html HTTP/1.1\r\n" + private static final String REQS = "GET /basic/index.html HTTP/1.1\r\n" + "Host: 172.16.60.101\r\n" - + "User-Agent: Mozilla/5.0\r\n" - + "Accept: text/html;q=0.9,*/*;q=0.8\r\n" + "Authorization: Basic dXNlcjpwYXNzd29yZA==\r\n\r\n"; - private static final String RESPS = "HTTP/1.1 200 OK\r\n" - + "Last-Modified: Thu, 08 Jun 2017 09:01:56 GMT\r\n" - + "Content-Length: 33\r\n" - + "Content-Type: text/html\r\n\r\n" - + "<html><body>content</body></html>"; - - private final HttpFlowContext mockContext = mock(HttpFlowContext.class); - private final HttpScopes scopes = new HttpScopes(); - - { - when(mockContext.scopes()).thenReturn(scopes); - } - @BeforeClass public static void beforeClass() { AppUtils.registerAll(); @@ -72,49 +55,49 @@ @Test public void testFilter() { HttpRequest request = TestHttpUtils.request(REQS); - HttpResponse response = TestHttpUtils.response(RESPS); + HttpResponse response = HttpResponseBuilder.ok().build(); request.setTag(TAG_SESSION_ID, "sid1"); HttpBasicAuthLoginFilter filter = createFilter(provider(new Credentials("test", "test"))); - scopes.clear(); + HttpFlowContext context = HttpFilterTestUtils.createMockContext(); - filter.filterOutbound(request, null, mockContext); + filter.filterOutbound(request, null, context); assertEquals(request.getHeaders().get(HttpHeaders.AUTHORIZATION).toString(), "Basic dGVzdDp0ZXN0"); - assertEquals(scopes.getConversation(request).get(PARAM_USERNAME), "test"); + assertEquals(context.scopes().getConversation(request).get(PARAM_USERNAME), "test"); - filter.filterInbound(request, response, mockContext); - assertEquals(scopes.getSession(request).get(PARAM_USERNAME), "test"); + filter.filterInbound(request, response, context); + assertEquals(context.scopes().getSession(request).get(PARAM_USERNAME), "test"); } @Test public void testFilterNoCredentials() { HttpRequest request = TestHttpUtils.request(REQS); - HttpResponse response = TestHttpUtils.response(RESPS); + HttpResponse response = HttpResponseBuilder.ok().build(); request.setTag(TAG_SESSION_ID, "sid1"); HttpBasicAuthLoginFilter filter = createFilter(provider(null)); - scopes.clear(); + HttpFlowContext context = HttpFilterTestUtils.createMockContext(); - filter.filterOutbound(request, null, mockContext); + filter.filterOutbound(request, null, context); assertEquals(request.getHeaders().get(HttpHeaders.AUTHORIZATION).toString(), "Basic dXNlcjpwYXNzd29yZA=="); - assertEquals(scopes.getConversation(request).get(PARAM_USERNAME), "user"); + assertEquals(context.scopes().getConversation(request).get(PARAM_USERNAME), "user"); - filter.filterInbound(request, response, mockContext); - assertEquals(scopes.getSession(request).get(PARAM_USERNAME), "user"); + filter.filterInbound(request, response, context); + assertEquals(context.scopes().getSession(request).get(PARAM_USERNAME), "user"); } @Test public void testFilterNoProvider() { HttpRequest request = TestHttpUtils.request(REQS); - HttpResponse response = TestHttpUtils.response(RESPS); + HttpResponse response = HttpResponseBuilder.ok().build(); request.setTag(TAG_SESSION_ID, "sid1"); HttpBasicAuthLoginFilter filter = createFilter(null); - scopes.clear(); + HttpFlowContext context = HttpFilterTestUtils.createMockContext(); - filter.filterOutbound(request, null, mockContext); + filter.filterOutbound(request, null, context); assertEquals(request.getHeaders().get(HttpHeaders.AUTHORIZATION).toString(), "Basic dXNlcjpwYXNzd29yZA=="); - assertEquals(scopes.getConversation(request).get(PARAM_USERNAME), "user"); + assertEquals(context.scopes().getConversation(request).get(PARAM_USERNAME), "user"); - filter.filterInbound(request, response, mockContext); - assertEquals(scopes.getSession(request).get(PARAM_USERNAME), "user"); + filter.filterInbound(request, response, context); + assertEquals(context.scopes().getSession(request).get(PARAM_USERNAME), "user"); } @Test @@ -159,14 +142,15 @@ HttpRequest req = HttpRequestBuilder.get("http://test.com/html/basic/index.html") .header(HttpHeaders.AUTHORIZATION, "Basic dXNlcjpwYXNzd29yZA==") .build(); + HttpFlowContext context = HttpFilterTestUtils.createMockContext(); - filter.filterOutbound(req, null, mockContext); + filter.filterOutbound(req, null, context); assertEquals("Basic dXNlcjpwYXNzd29yZA==", req.getHeaders().get(HttpHeaders.AUTHORIZATION).toString()); req = HttpRequestBuilder.get("http://test.com/html/basic/index2.html") .header(HttpHeaders.AUTHORIZATION, "Basic dXNlcjpwYXNzd29yZA==") .build(); - filter.filterOutbound(req, null, mockContext); + filter.filterOutbound(req, null, context); assertEquals("Basic bG9naW46cGFzcw==", req.getHeaders().get(HttpHeaders.AUTHORIZATION).toString()); } }
--- a/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpCsrfFilterTest.java Thu Aug 10 11:58:39 2017 +0200 +++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpCsrfFilterTest.java Thu Aug 10 14:03:21 2017 +0200 @@ -2,13 +2,11 @@ import com.passus.config.validation.Errors; import com.passus.data.ByteString; -import com.passus.net.http.HttpMessage; 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.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 com.passus.st.client.http.filter.HttpCsrfFilter.CookieExtractor; @@ -32,10 +30,6 @@ */ public class HttpCsrfFilterTest { - private void tagSessionId(HttpMessage msg) { - msg.setTag(TAG_SESSION_ID, "1"); - } - @Test public void testCookieExtractor() { CookieExtractor extractor = new CookieExtractor("x_csrf_token"); @@ -123,9 +117,7 @@ .header("x-csrf-token", "token1") .build(); - tagSessionId(req1); - tagSessionId(resp1); - tagSessionId(req2); + HttpFilterTestUtils.tagMessages(req1, resp1, req2); SessionInfo session = new SessionInfo("1.1.1.1:5000", "2.2.2.2:80"); HttpScopes scopes = new HttpScopes();
--- a/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpCsrfFormFilterTest.java Thu Aug 10 11:58:39 2017 +0200 +++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpCsrfFormFilterTest.java Thu Aug 10 14:03:21 2017 +0200 @@ -3,14 +3,12 @@ import com.passus.config.NodeException; import com.passus.config.validation.Errors; import com.passus.data.ByteBuffDataSource; -import com.passus.net.http.HttpMessage; import com.passus.net.http.HttpMessageHelper; import com.passus.net.http.HttpParameters; 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 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.io.IOException; @@ -37,8 +35,7 @@ .header("Content-Type", "text/html").build(); HttpRequest req2 = HttpRequestBuilder.post("example.com/save").content(post("oldToken")) .header("Content-Type", "application/x-www-form-urlencoded").build(); - tagMessages(req1, resp1Orig, resp1Live, req2); - // sfejkować kontekst i sesję + HttpFilterTestUtils.tagMessages(req1, resp1Orig, resp1Live, req2); when(mockContext.scopes()).thenReturn(new HttpScopes()); HttpCsrfFormFilter filter = new HttpCsrfFormFilter(); @@ -67,12 +64,6 @@ return form; } - private static void tagMessages(HttpMessage... messages) { - for (HttpMessage message : messages) { - message.setTag(TAG_SESSION_ID, "sid1"); - } - } - @Test public void testConfigure() throws IOException, NodeException { String filterConfig = "filters:\n"
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpFilterTestUtils.java Thu Aug 10 14:03:21 2017 +0200 @@ -0,0 +1,29 @@ +package com.passus.st.client.http.filter; + +import com.passus.net.http.HttpMessage; +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 static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * + * @author mikolaj.podbielski + */ +public class HttpFilterTestUtils { + + protected static HttpFlowContext createMockContext() { + HttpFlowContext mockContext = mock(HttpFlowContext.class); + final HttpScopes scopes = new HttpScopes(); + when(mockContext.scopes()).thenReturn(scopes); + return mockContext; + } + + protected static void tagMessages(HttpMessage... messages) { + for (HttpMessage message : messages) { + message.setTag(TAG_SESSION_ID, "sid1"); + } + } + +}
--- a/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpFormLoginFilterTest.java Thu Aug 10 11:58:39 2017 +0200 +++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpFormLoginFilterTest.java Thu Aug 10 14:03:21 2017 +0200 @@ -29,8 +29,6 @@ public static final String REQS = "POST /login_check HTTP/1.1\r\n" + "Host: 172.16.60.23\r\n" - + "User-Agent: Mozilla/5.0\r\n" - + "Accept: text/html;q=0.9,*/*;q=0.8\r\n" + "Referer: http://172.16.60.23/login\r\n" + "Content-Type: application/x-www-form-urlencoded\r\n" + "Content-Length: 32\r\n"
--- a/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTest.java Thu Aug 10 11:58:39 2017 +0200 +++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTest.java Thu Aug 10 14:03:21 2017 +0200 @@ -1,7 +1,6 @@ package com.passus.st.client.http.filter; import com.passus.config.validation.Errors; -import com.passus.filter.BeanValueExtractor; import com.passus.net.http.HttpHeaders; import com.passus.net.http.HttpMessageHelper; import com.passus.net.http.HttpParameters; @@ -10,12 +9,21 @@ import com.passus.st.AppUtils; import com.passus.st.client.http.HttpFlowContext; import com.passus.st.client.http.HttpScopes; -import com.passus.st.client.http.filter.HttpMessageModificationFilter.*; +import com.passus.st.client.http.filter.HttpMessageModificationFilter.AddCookieOperation; +import com.passus.st.client.http.filter.HttpMessageModificationFilter.AddHeaderOperation; +import com.passus.st.client.http.filter.HttpMessageModificationFilter.AddQueryParameterOperation; +import com.passus.st.client.http.filter.HttpMessageModificationFilter.PostDataAddParamOperation; +import com.passus.st.client.http.filter.HttpMessageModificationFilter.PostDataRemoveParamOperation; +import com.passus.st.client.http.filter.HttpMessageModificationFilter.PostDataSetParamOperation; +import com.passus.st.client.http.filter.HttpMessageModificationFilter.RemoveCookieOperation; +import com.passus.st.client.http.filter.HttpMessageModificationFilter.RemoveHeaderOperation; +import com.passus.st.client.http.filter.HttpMessageModificationFilter.RemoveQueryParameterOperation; +import com.passus.st.client.http.filter.HttpMessageModificationFilter.SetCookieOperation; +import com.passus.st.client.http.filter.HttpMessageModificationFilter.SetHeaderOperation; +import com.passus.st.client.http.filter.HttpMessageModificationFilter.SetQueryParameterOperation; import java.util.Arrays; import java.util.List; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import org.mvel2.PropertyAccessor; import static org.testng.AssertJUnit.*; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; @@ -70,7 +78,7 @@ filter.filterOutbound(req, null, null); assertEquals("/path/test?uParam2=2", req.getUri().toString()); - + HttpHeaders headers = req.getHeaders(); assertFalse(headers.contains("Header1")); assertEquals("HeaderValue4", headers.get("Header4").toString()); @@ -98,7 +106,7 @@ + " Header3: Header1Value3a\n" + " $setHeader: \n" + " Header6: \"$scopes.getSession('testId').get('testParam')\"\n"; - + Errors errors = new Errors(); List<HttpFilter> filters = HttpFiltersConfigurator.getFilters(filterConfig, errors); @@ -114,7 +122,7 @@ .build(); HttpMessageModificationFilter filter = (HttpMessageModificationFilter) filters.get(0); - HttpFlowContext mockContext = mock(HttpFlowContext.class); + HttpFlowContext mockContext = HttpFilterTestUtils.createMockContext(); final HttpScopes scopes = new HttpScopes(); scopes.createSession("testId").set("testParam", "exprValue"); when(mockContext.scopes()).thenReturn(scopes);
--- a/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpScopeModificationFilterTest.java Thu Aug 10 11:58:39 2017 +0200 +++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpScopeModificationFilterTest.java Thu Aug 10 14:03:21 2017 +0200 @@ -8,10 +8,14 @@ import com.passus.st.client.http.HttpConsts; import com.passus.st.client.http.HttpFlowContext; import com.passus.st.client.http.HttpScopes; -import static com.passus.st.client.http.filter.HttpScopeModificationFilter.*; +import static com.passus.st.client.http.filter.HttpFilterTestUtils.createMockContext; +import com.passus.st.client.http.filter.HttpScopeModificationFilter.Operation; +import com.passus.st.client.http.filter.HttpScopeModificationFilter.RemoveGlobalParamOperation; +import com.passus.st.client.http.filter.HttpScopeModificationFilter.RemoveSessionOperation; +import com.passus.st.client.http.filter.HttpScopeModificationFilter.RemoveSessionParamOperation; +import com.passus.st.client.http.filter.HttpScopeModificationFilter.SetGlobalParamOperation; +import com.passus.st.client.http.filter.HttpScopeModificationFilter.SetSessionParamOperation; import java.util.List; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; import static org.testng.AssertJUnit.*; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; @@ -41,13 +45,6 @@ .build(); } - private HttpFlowContext createMockContext() { - HttpFlowContext mockContext = mock(HttpFlowContext.class); - final HttpScopes scopes = new HttpScopes(); - when(mockContext.scopes()).thenReturn(scopes); - return mockContext; - } - @Test public void testRemoveSessionOperation() { HttpScopeModificationFilter filter = new HttpScopeModificationFilter(); @@ -80,13 +77,12 @@ @Test public void testSetSessionParamOperation_CheckValueExists_Autocreate() { + HttpFlowContext mockContext = createMockContext(); + HttpScopes scopes = mockContext.scopes(); + HttpRequest req = createRequest(); + HttpScopeModificationFilter filter = new HttpScopeModificationFilter(); filter.addOperation(new SetSessionParamOperation("paramName", new UnmutableValueExtractor("paramValue"), true)); - - HttpFlowContext mockContext = createMockContext(); - HttpScopes scopes = mockContext.scopes(); - - HttpRequest req = createRequest(); filter.filterOutbound(req, null, mockContext); assertEquals("paramValue", scopes.getSession(sessionId, false).get("paramName")); @@ -98,13 +94,12 @@ @Test public void testSetSessionParamOperation_NotCheckValueExists_Autocreate() { + HttpFlowContext mockContext = createMockContext(); + HttpScopes scopes = mockContext.scopes(); + HttpRequest req = createRequest(); + HttpScopeModificationFilter filter = new HttpScopeModificationFilter(); filter.addOperation(new SetSessionParamOperation("paramName", new UnmutableValueExtractor("paramValue"), false)); - - HttpFlowContext mockContext = createMockContext(); - HttpScopes scopes = mockContext.scopes(); - - HttpRequest req = createRequest(); filter.filterOutbound(req, null, mockContext); assertEquals("paramValue", scopes.getSession(sessionId, false).get("paramName")); @@ -131,13 +126,12 @@ @Test public void testSetGlobalParamOperation_CheckValueExists() { + HttpFlowContext mockContext = createMockContext(); + HttpScopes scopes = mockContext.scopes(); + HttpRequest req = createRequest(); + HttpScopeModificationFilter filter = new HttpScopeModificationFilter(); filter.addOperation(new SetGlobalParamOperation("paramName", new UnmutableValueExtractor("paramValue"), true)); - - HttpFlowContext mockContext = createMockContext(); - HttpScopes scopes = mockContext.scopes(); - - HttpRequest req = createRequest(); filter.filterOutbound(req, null, mockContext); assertEquals("paramValue", scopes.getGlobal().get("paramName")); @@ -149,13 +143,12 @@ @Test public void testSetGlobalParamOperation_NotCheckValueExists() { + HttpFlowContext mockContext = createMockContext(); + HttpScopes scopes = mockContext.scopes(); + HttpRequest req = createRequest(); + HttpScopeModificationFilter filter = new HttpScopeModificationFilter(); filter.addOperation(new SetGlobalParamOperation("paramName", new UnmutableValueExtractor("paramValue"), false)); - - HttpFlowContext mockContext = createMockContext(); - HttpScopes scopes = mockContext.scopes(); - - HttpRequest req = createRequest(); filter.filterOutbound(req, null, mockContext); assertEquals("paramValue", scopes.getGlobal().get("paramName"));