diff --git a/SDK/src/queueit/knownuserv3/sdk/KnownUser.java b/SDK/src/queueit/knownuserv3/sdk/KnownUser.java index d080eee..e02f300 100644 --- a/SDK/src/queueit/knownuserv3/sdk/KnownUser.java +++ b/SDK/src/queueit/knownuserv3/sdk/KnownUser.java @@ -1,4 +1,7 @@ package queueit.knownuserv3.sdk; +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; +import java.net.URLEncoder; import java.util.HashMap; import java.util.Map; import javax.servlet.http.Cookie; @@ -37,10 +40,10 @@ public static RequestValidationResult validateRequestByIntegrationConfig(String try { boolean isDebug = getIsDebug(queueitToken, secretKey); if (isDebug) { - debugEntries.put("configVersion", Integer.toString(customerIntegrationInfo.Version)); - debugEntries.put("pureUrl", currentUrlWithoutQueueITToken); - debugEntries.put("queueitToken", queueitToken); - debugEntries.put("OriginalURL", request.getRequestURL().toString()); + debugEntries.put("ConfigVersion", Integer.toString(customerIntegrationInfo.Version)); + debugEntries.put("PureUrl", currentUrlWithoutQueueITToken); + debugEntries.put("QueueitToken", queueitToken); + debugEntries.put("OriginalUrl", getOriginalUrl(request)); } if (Utils.isNullOrWhiteSpace(currentUrlWithoutQueueITToken)) { @@ -53,14 +56,13 @@ public static RequestValidationResult validateRequestByIntegrationConfig(String Cookie[] cookies = request != null ? request.getCookies() : new Cookie[0]; IntegrationEvaluator configEvaluater = new IntegrationEvaluator(); - String userAgent = request.getHeader("User-Agent"); IntegrationConfigModel matchedConfig = configEvaluater.getMatchedIntegrationConfig( - customerIntegrationInfo, currentUrlWithoutQueueITToken, cookies, userAgent != null ? userAgent : ""); + customerIntegrationInfo, currentUrlWithoutQueueITToken, request); if (isDebug) { String matchedConfigName = (matchedConfig != null) ? matchedConfig.Name : "NULL"; - debugEntries.put("matchedConfig", matchedConfigName); + debugEntries.put("MatchedConfig", matchedConfigName); } if (matchedConfig == null) { @@ -136,10 +138,10 @@ private static RequestValidationResult cancelRequestByLocalConfig( boolean isDebug = getIsDebug(queueitToken, secretKey); if (isDebug) { - debugEntries.put("targetUrl", targetUrl); - debugEntries.put("queueitToken", queueitToken); - debugEntries.put("cancelConfig", cancelConfig != null ? cancelConfig.toString() : "NULL"); - debugEntries.put("OriginalURL", request.getRequestURL().toString()); + debugEntries.put("TargetUrl", targetUrl); + debugEntries.put("QueueitToken", queueitToken); + debugEntries.put("CancelConfig", cancelConfig != null ? cancelConfig.toString() : "NULL"); + debugEntries.put("OriginalUrl", getOriginalUrl(request)); } if (Utils.isNullOrWhiteSpace(targetUrl)) { @@ -189,10 +191,10 @@ private static RequestValidationResult resolveQueueRequestByLocalConfig( boolean isDebug = getIsDebug(queueitToken, secretKey); if (isDebug) { - debugEntries.put("targetUrl", targetUrl); - debugEntries.put("queueitToken", queueitToken); - debugEntries.put("queueConfig", queueConfig != null ? queueConfig.toString() : "NULL"); - debugEntries.put("OriginalURL", request.getRequestURL().toString()); + debugEntries.put("TargetUrl", targetUrl); + debugEntries.put("QueueitToken", queueitToken); + debugEntries.put("QueueConfig", queueConfig != null ? queueConfig.toString() : "NULL"); + debugEntries.put("OriginalUrl", getOriginalUrl(request)); } if (Utils.isNullOrWhiteSpace(customerId)) { @@ -242,17 +244,18 @@ public static void extendQueueCookie(String eventId, userInQueueService.extendQueueCookie(eventId, cookieValidityMinute, cookieDomain, secretKey); } - private static void setDebugCookie(Map debugEntries, HttpServletRequest request, HttpServletResponse response) { + private static void setDebugCookie(Map debugEntries, HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException { if(debugEntries.isEmpty()) return; ICookieManager cookieManager = new CookieManager(request, response); - String cookieValue = ""; + String cookieValue = ""; for (Map.Entry entry : debugEntries.entrySet()) { - cookieValue += (entry.getKey() + "=" + entry.getValue() + "&"); + cookieValue += (entry.getKey() + "=" + entry.getValue() + "|"); } if(!"".equals(cookieValue)) - cookieValue = cookieValue.substring(0, cookieValue.length() - 1); // remove trailing & + cookieValue = cookieValue.substring(0, cookieValue.length() - 1); // remove trailing char + cookieManager.setCookie(QueueITDebugKey, cookieValue, null, null); } @@ -265,6 +268,18 @@ private static boolean getIsDebug(String queueitToken, String secretKey) throws } return false; } + + private static String getOriginalUrl(HttpServletRequest request){ + return (request.getQueryString() != null) + ? String.join("", request.getRequestURL(), "?",request.getQueryString()) + : request.getRequestURL().toString(); + } +} + +interface ICookieManager { + + void setCookie(String cookieName, String cookieValue, Integer expiration, String cookieDomain); + String getCookie(String cookieName); } class CookieManager implements ICookieManager { @@ -287,7 +302,9 @@ public void setCookie(String cookieName, String cookieValue, Integer expiration, if (cookieValue == null) { cookieValue = ""; } - cookie.setValue(cookieValue); + try { + cookie.setValue(URLEncoder.encode(cookieValue, "UTF-8")); + } catch (UnsupportedEncodingException ex) {} if(expiration != null) cookie.setMaxAge(expiration); cookie.setPath("/"); @@ -311,7 +328,9 @@ public String getCookie(String cookieName) { for (Cookie cookie : cookies) { if (cookie.getName().equals(cookieName)) { - return cookie.getValue(); + try { + return URLDecoder.decode(cookie.getValue(), "UTF-8"); + } catch (UnsupportedEncodingException ex) { } } } return null; diff --git a/SDK/src/queueit/knownuserv3/sdk/UserInQueueService.java b/SDK/src/queueit/knownuserv3/sdk/UserInQueueService.java index 1132ddf..c5914e0 100644 --- a/SDK/src/queueit/knownuserv3/sdk/UserInQueueService.java +++ b/SDK/src/queueit/knownuserv3/sdk/UserInQueueService.java @@ -29,7 +29,7 @@ void extendQueueCookie( class UserInQueueService implements IUserInQueueService { - public static final String SDK_VERSION = "3.2.0"; + public static final String SDK_VERSION = "3.3.0"; private final IUserInQueueStateRepository _userInQueueStateRepository; public UserInQueueService( @@ -116,7 +116,7 @@ private RequestValidationResult getVaidationErrorResult( if (!domainAlias.endsWith("/")) { domainAlias = domainAlias + "/"; } - String redirectUrl = "https://" + domainAlias + "error/" + errorCode + "?" + query; + String redirectUrl = "https://" + domainAlias + "error/" + errorCode + "/?" + query; return new RequestValidationResult(ActionType.QUEUE_ACTION, config.getEventId(), null, redirectUrl); } diff --git a/SDK/src/queueit/knownuserv3/sdk/UserInQueueStateCookieRepository.java b/SDK/src/queueit/knownuserv3/sdk/UserInQueueStateCookieRepository.java index c8e9a98..7722742 100644 --- a/SDK/src/queueit/knownuserv3/sdk/UserInQueueStateCookieRepository.java +++ b/SDK/src/queueit/knownuserv3/sdk/UserInQueueStateCookieRepository.java @@ -183,12 +183,6 @@ public void extendQueueCookie( } } -interface ICookieManager { - - void setCookie(String cookieName, String cookieValue, Integer expiration, String cookieDomain); - String getCookie(String cookieName); -} - class StateInfo { private final boolean isValid; diff --git a/SDK/src/queueit/knownuserv3/sdk/integrationconfig/Constants.java b/SDK/src/queueit/knownuserv3/sdk/integrationconfig/Constants.java index b168823..6bec3d5 100644 --- a/SDK/src/queueit/knownuserv3/sdk/integrationconfig/Constants.java +++ b/SDK/src/queueit/knownuserv3/sdk/integrationconfig/Constants.java @@ -5,6 +5,7 @@ final class ValidatorType { public static final String URL_VALIDATOR = "UrlValidator"; public static final String COOKIE_VALIDATOR = "CookieValidator"; public static final String USERAGENT_VALIDATOR = "UserAgentValidator"; + public static final String HTTPHEADER_VALIDATOR = "HttpHeaderValidator"; } final class UrlPartType { diff --git a/SDK/src/queueit/knownuserv3/sdk/integrationconfig/IntegrationEvaluator.java b/SDK/src/queueit/knownuserv3/sdk/integrationconfig/IntegrationEvaluator.java index 8d1d5f6..a439539 100644 --- a/SDK/src/queueit/knownuserv3/sdk/integrationconfig/IntegrationEvaluator.java +++ b/SDK/src/queueit/knownuserv3/sdk/integrationconfig/IntegrationEvaluator.java @@ -3,14 +3,16 @@ import javax.servlet.http.Cookie; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.Map; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; interface IIntegrationEvaluator { IntegrationConfigModel getMatchedIntegrationConfig( CustomerIntegration customerIntegration, String currentPageUrl, - Cookie[] cookies, - String userAgent); + HttpServletRequest request) throws Exception; } public class IntegrationEvaluator implements IIntegrationEvaluator { @@ -19,11 +21,14 @@ public class IntegrationEvaluator implements IIntegrationEvaluator { public IntegrationConfigModel getMatchedIntegrationConfig( CustomerIntegration customerIntegration, String currentPageUrl, - Cookie[] cookies, - String userAgent) { + HttpServletRequest request) throws Exception { + + if(request == null) + throw new Exception("request is null"); + for (IntegrationConfigModel integration : customerIntegration.Integrations) { for (TriggerModel trigger : integration.Triggers) { - if (evaluateTrigger(trigger, currentPageUrl, cookies, userAgent)) { + if (evaluateTrigger(trigger, currentPageUrl, request)) { return integration; } } @@ -34,18 +39,17 @@ public IntegrationConfigModel getMatchedIntegrationConfig( private boolean evaluateTrigger( TriggerModel trigger, String currentPageUrl, - Cookie[] cookies, - String userAgent) { + HttpServletRequest request) { if (trigger.LogicalOperator.equals(LogicalOperatorType.OR)) { for (TriggerPart part : trigger.TriggerParts) { - if (evaluateTriggerPart(part, currentPageUrl, cookies, userAgent)) { + if (evaluateTriggerPart(part, currentPageUrl, request)) { return true; } } return false; } else { for (TriggerPart part : trigger.TriggerParts) { - if (!evaluateTriggerPart(part, currentPageUrl, cookies, userAgent)) { + if (!evaluateTriggerPart(part, currentPageUrl, request)) { return false; } } @@ -53,18 +57,16 @@ private boolean evaluateTrigger( } } - private boolean evaluateTriggerPart( - TriggerPart triggerPart, - String currentPageUrl, - Cookie[] cookies, - String userAgent) { + private boolean evaluateTriggerPart(TriggerPart triggerPart, String currentPageUrl, HttpServletRequest request) { switch (triggerPart.ValidatorType) { case ValidatorType.URL_VALIDATOR: return UrlValidatorHelper.evaluate(triggerPart, currentPageUrl); case ValidatorType.COOKIE_VALIDATOR: - return CookieValidatorHelper.evaluate(triggerPart, cookies); + return CookieValidatorHelper.evaluate(triggerPart, request.getCookies()); case ValidatorType.USERAGENT_VALIDATOR: - return UserAgentValidatorHelper.evaluate(triggerPart, userAgent); + return UserAgentValidatorHelper.evaluate(triggerPart, request.getHeader("User-Agent")); + case ValidatorType.HTTPHEADER_VALIDATOR: + return HttpHeaderValidatorHelper.evaluate(triggerPart, request); default: return false; } @@ -151,6 +153,18 @@ public static boolean evaluate(TriggerPart triggerPart, String userAgent) { triggerPart.ValueToCompare); } } + + final class HttpHeaderValidatorHelper { + public static boolean evaluate(TriggerPart triggerPart, HttpServletRequest request) + { + return ComparisonOperatorHelper.evaluate(triggerPart.Operator, + triggerPart.IsNegative, + triggerPart.IsIgnoreCase, + request.getHeader(triggerPart.HttpHeaderName), + triggerPart.ValueToCompare); + } + } + final class ComparisonOperatorHelper { public static boolean evaluate(String opt, boolean isNegative, boolean isIgnoreCase, String left, String right) { diff --git a/SDK/src/queueit/knownuserv3/sdk/integrationconfig/TriggerPart.java b/SDK/src/queueit/knownuserv3/sdk/integrationconfig/TriggerPart.java index 9e1f1c1..ad1bdb4 100644 --- a/SDK/src/queueit/knownuserv3/sdk/integrationconfig/TriggerPart.java +++ b/SDK/src/queueit/knownuserv3/sdk/integrationconfig/TriggerPart.java @@ -11,4 +11,6 @@ public class TriggerPart { public String UrlPart; //CookieValidator public String CookieName; + //HttpHeaderValidator + public String HttpHeaderName; } \ No newline at end of file diff --git a/SDK/test/queueit/knownuserv3/sdk/KnownUserTest.java b/SDK/test/queueit/knownuserv3/sdk/KnownUserTest.java index 46db154..341333e 100644 --- a/SDK/test/queueit/knownuserv3/sdk/KnownUserTest.java +++ b/SDK/test/queueit/knownuserv3/sdk/KnownUserTest.java @@ -4,6 +4,7 @@ import java.io.IOException; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; import java.security.Principal; import java.util.ArrayList; import java.util.Collection; @@ -134,12 +135,17 @@ public void cancelRequestByLocalConfigDebugCookieLoggingTest() throws Exception KnownUser.cancelRequestByLocalConfig("url", queueittoken, cancelEventConfig, "customerId", requestMock, responseMock, secretKey); - // Assert - String expectedCookieValue = "cancelConfig=EventId:eventid&Version:1&QueueDomain:queuedomain&CookieDomain:cookiedomain&OriginalURL=requestUrl&queueitToken=" + queueittoken + "&targetUrl=url"; - + // Assert assertTrue(responseMock.addedCookies.size() == 1); assertTrue(responseMock.addedCookies.get(0).getName().equals(KnownUser.QueueITDebugKey)); - assertTrue(responseMock.addedCookies.get(0).getValue().equals(expectedCookieValue)); + String decodedCookieValue = URLDecoder.decode(responseMock.addedCookies.get(0).getValue(), "UTF-8"); + assertTrue(decodedCookieValue.contains("OriginalUrl=requestUrl")); + assertTrue(decodedCookieValue.contains("|CancelConfig=EventId:eventid")); + assertTrue(decodedCookieValue.contains("&Version:1")); + assertTrue(decodedCookieValue.contains("&QueueDomain:queuedomain")); + assertTrue(decodedCookieValue.contains("&CookieDomain:cookiedomain")); + assertTrue(decodedCookieValue.contains("|QueueitToken=" + queueittoken)); + assertTrue(decodedCookieValue.contains("|TargetUrl=url")); } @Test @@ -535,11 +541,20 @@ public void resolveQueueRequestByLocalConfigDebugCookieLoggingTest() throws Exce KnownUser.resolveQueueRequestByLocalConfig("targetUrl", queueittoken, queueConfig, "customerId", requestMock, responseMock, secretKey); // Assert - String expectedCookieValue = "OriginalURL=requestUrl&queueConfig=EventId:eventId&Version:12&QueueDomain:queueDomain&CookieDomain:cookieDomain&ExtendCookieValidity:true&CookieValidityMinute:10&LayoutName:layoutName&Culture:culture&queueitToken=" +queueittoken + "&targetUrl=targetUrl"; - assertTrue(responseMock.addedCookies.size() == 1); assertTrue(responseMock.addedCookies.get(0).getName().equals(KnownUser.QueueITDebugKey)); - assertTrue(responseMock.addedCookies.get(0).getValue().equals(expectedCookieValue)); + String decodedCookieValue = URLDecoder.decode(responseMock.addedCookies.get(0).getValue(), "UTF-8"); + assertTrue(decodedCookieValue.contains("OriginalUrl=requestUrl")); + assertTrue(decodedCookieValue.contains("|QueueConfig=EventId:eventId")); + assertTrue(decodedCookieValue.contains("&Version:12")); + assertTrue(decodedCookieValue.contains("&QueueDomain:queueDomain")); + assertTrue(decodedCookieValue.contains("&CookieDomain:cookieDomain")); + assertTrue(decodedCookieValue.contains("&ExtendCookieValidity:true")); + assertTrue(decodedCookieValue.contains("&CookieValidityMinute:10")); + assertTrue(decodedCookieValue.contains("&LayoutName:layoutName")); + assertTrue(decodedCookieValue.contains("&Culture:culture")); + assertTrue(decodedCookieValue.contains("|QueueitToken=" + queueittoken)); + assertTrue(decodedCookieValue.contains("|TargetUrl=targetUrl")); } @Test @@ -692,11 +707,23 @@ public void validateRequestByIntegrationConfigDebugCookieLoggingTest() throws Ex KnownUser.validateRequestByIntegrationConfig("http://test.com?event1=true", queueittoken, customerIntegration, "customerId", requestMock, responseMock, secretKey); // Assert - String expectedCookieValue = "OriginalURL=requestUrl&pureUrl=http://test.com?event1=true&configVersion=3&queueConfig=EventId:event1&Version:3&QueueDomain:knownusertest.queue-it.net&CookieDomain:.test.com&ExtendCookieValidity:true&CookieValidityMinute:20&LayoutName:Christmas Layout by Queue-it&Culture:da-DK&queueitToken=" + queueittoken + "&targetUrl=http://test.com?event1=true&matchedConfig=event1action"; - assertTrue(responseMock.addedCookies.size() == 1); assertTrue(responseMock.addedCookies.get(0).getName().equals(KnownUser.QueueITDebugKey)); - assertTrue(responseMock.addedCookies.get(0).getValue().equals(expectedCookieValue)); + String decodedCookieValue = URLDecoder.decode(responseMock.addedCookies.get(0).getValue(), "UTF-8"); + assertTrue(decodedCookieValue.contains("OriginalUrl=requestUrl")); + assertTrue(decodedCookieValue.contains("|PureUrl=http://test.com?event1=true")); + assertTrue(decodedCookieValue.contains("|ConfigVersion=3")); + assertTrue(decodedCookieValue.contains("|QueueConfig=EventId:event1")); + assertTrue(decodedCookieValue.contains("&Version:3")); + assertTrue(decodedCookieValue.contains("&QueueDomain:knownusertest.queue-it.net")); + assertTrue(decodedCookieValue.contains("&CookieDomain:.test.com")); + assertTrue(decodedCookieValue.contains("&ExtendCookieValidity:true")); + assertTrue(decodedCookieValue.contains("&CookieValidityMinute:20")); + assertTrue(decodedCookieValue.contains("&LayoutName:Christmas Layout by Queue-it")); + assertTrue(decodedCookieValue.contains("&Culture:da-DK")); + assertTrue(decodedCookieValue.contains("|QueueitToken=" + queueittoken)); + assertTrue(decodedCookieValue.contains("|TargetUrl=http://test.com?event1=true")); + assertTrue(decodedCookieValue.contains("|MatchedConfig=event1action")); } @Test @@ -739,11 +766,14 @@ public void validateRequestByIntegrationConfigNotMatchDebugCookieLoggingTest() t KnownUser.validateRequestByIntegrationConfig("http://test.com?event1=true", queueittoken, customerIntegration, "customerId", requestMock, responseMock, secretKey); // Assert - String expectedCookieValue = "OriginalURL=requestUrl&pureUrl=http://test.com?event1=true&configVersion=3&queueitToken=" + queueittoken + "&matchedConfig=NULL"; - assertTrue(responseMock.addedCookies.size() == 1); - assertTrue(responseMock.addedCookies.get(0).getName().equals(KnownUser.QueueITDebugKey)); - assertTrue(responseMock.addedCookies.get(0).getValue().equals(expectedCookieValue)); + assertTrue(responseMock.addedCookies.get(0).getName().equals(KnownUser.QueueITDebugKey)); + String decodedCookieValue = URLDecoder.decode(responseMock.addedCookies.get(0).getValue(), "UTF-8"); + assertTrue(decodedCookieValue.contains("OriginalUrl=requestUrl")); + assertTrue(decodedCookieValue.contains("|PureUrl=http://test.com?event1=true")); + assertTrue(decodedCookieValue.contains("ConfigVersion=3")); + assertTrue(decodedCookieValue.contains("|QueueitToken=" + queueittoken)); + assertTrue(decodedCookieValue.contains("|MatchedConfig=NULL")); } @Test @@ -921,6 +951,7 @@ class HttpServletRequestMock implements HttpServletRequest { public Cookie[] CookiesValue; public String UserAgent; public String RequestURL; + public String QueryString; @Override public String getAuthType() { @@ -981,7 +1012,7 @@ public String getContextPath() { @Override public String getQueryString() { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + return this.QueryString; } @Override diff --git a/SDK/test/queueit/knownuserv3/sdk/UserInQueueServiceTest.java b/SDK/test/queueit/knownuserv3/sdk/UserInQueueServiceTest.java index 144b51f..0ddc8df 100644 --- a/SDK/test/queueit/knownuserv3/sdk/UserInQueueServiceTest.java +++ b/SDK/test/queueit/knownuserv3/sdk/UserInQueueServiceTest.java @@ -193,7 +193,7 @@ public void extendQueueCookie(String eventId, int cookieValidityMinute, String c String targetUrl = "http://test.test.com?b=h"; String knownUserVersion = UserInQueueService.SDK_VERSION; - String expectedErrorUrl = "https://testDomain.com/error/hash?c=testCustomer&e=e1" + String expectedErrorUrl = "https://testDomain.com/error/hash/?c=testCustomer&e=e1" + "&ver=v3-java-" + knownUserVersion + "&cver=100" + "&queueittoken=" + queueitToken @@ -258,7 +258,7 @@ public void extendQueueCookie(String eventId, int cookieValidityMinute, String c String targetUrl = "http://test.test.com?b=h"; String knownUserVersion = UserInQueueService.SDK_VERSION; - String expectedErrorUrl = "https://testDomain.com/error/timestamp?c=testCustomer&e=e1" + String expectedErrorUrl = "https://testDomain.com/error/timestamp/?c=testCustomer&e=e1" + "&ver=v3-java-" + knownUserVersion + "&cver=100" + "&queueittoken=" + queueitToken @@ -323,7 +323,7 @@ public void extendQueueCookie(String eventId, int cookieValidityMinute, String c String targetUrl = "http://test.test.com?b=h"; String knownUserVersion = UserInQueueService.SDK_VERSION; - String expectedErrorUrl = "https://testDomain.com/error/eventid?c=testCustomer&e=e2" + String expectedErrorUrl = "https://testDomain.com/error/eventid/?c=testCustomer&e=e2" + "&ver=v3-java-" + knownUserVersion + "&cver=10" + "&queueittoken=" + queueitToken @@ -606,7 +606,7 @@ public void extendQueueCookie(String eventId, int cookieValidityMinute, String c UserInQueueService testObject = new UserInQueueService(cookieProviderMock); RequestValidationResult result = testObject.validateQueueRequest(targetUrl, "ts_sasa~cv_adsasa~ce_falwwwse~q_944c1f44-60dd-4e37-aabc-f3e4bb1c8895", config, "testCustomer", "key"); assertTrue(result.doRedirect()); - assertTrue(result.getRedirectUrl().startsWith("https://testDomain.com/error/hash?c=testCustomer&e=e1&ver=v3-java-" + knownUserVersion + "&cver=10&l=testlayout&queueittoken=ts_sasa~cv_adsasa~ce_falwwwse~q_944c1f44-60dd-4e37-aabc-f3e4bb1c8895&")); + assertTrue(result.getRedirectUrl().startsWith("https://testDomain.com/error/hash/?c=testCustomer&e=e1&ver=v3-java-" + knownUserVersion + "&cver=10&l=testlayout&queueittoken=ts_sasa~cv_adsasa~ce_falwwwse~q_944c1f44-60dd-4e37-aabc-f3e4bb1c8895&")); assertTrue(!conditions.get("isStoreWasCalled")); assertTrue(config.getEventId().equals(result.getEventId())); } diff --git a/SDK/test/queueit/knownuserv3/sdk/integrationconfig/HttpHeaderHelperTest.java b/SDK/test/queueit/knownuserv3/sdk/integrationconfig/HttpHeaderHelperTest.java new file mode 100644 index 0000000..2e0e485 --- /dev/null +++ b/SDK/test/queueit/knownuserv3/sdk/integrationconfig/HttpHeaderHelperTest.java @@ -0,0 +1,39 @@ +package queueit.knownuserv3.sdk.integrationconfig; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import org.junit.Test; + +public class HttpHeaderHelperTest { + @Test + public void Evaluate_Test() { + HttpServletRequestMock httpContextMock = new HttpServletRequestMock(); + httpContextMock.Headers.put("MyHeaderName", "MyHeaderValue"); + TriggerPart triggerPart = new TriggerPart(); + triggerPart.HttpHeaderName = "MyHeaderName"; + + triggerPart.ValueToCompare = "MyHeaderValue"; + triggerPart.Operator = ComparisonOperatorType.EQUALS; + triggerPart.IsNegative = false; + triggerPart.IsIgnoreCase = false; + assertTrue(HttpHeaderValidatorHelper.evaluate(triggerPart, httpContextMock)); + + triggerPart.ValueToCompare = "Value"; + triggerPart.Operator = ComparisonOperatorType.CONTAINS; + triggerPart.IsNegative = false; + triggerPart.IsIgnoreCase = false; + assertTrue(HttpHeaderValidatorHelper.evaluate(triggerPart, httpContextMock)); + + triggerPart.ValueToCompare = "MyHeaderValue"; + triggerPart.Operator = ComparisonOperatorType.EQUALS; + triggerPart.IsNegative = true; + triggerPart.IsIgnoreCase = false; + assertFalse(HttpHeaderValidatorHelper.evaluate(triggerPart, httpContextMock)); + + triggerPart.ValueToCompare = "myheadervalue"; + triggerPart.Operator = ComparisonOperatorType.EQUALS; + triggerPart.IsNegative = false; + triggerPart.IsIgnoreCase = true; + assertTrue(HttpHeaderValidatorHelper.evaluate(triggerPart, httpContextMock)); + } +} diff --git a/SDK/test/queueit/knownuserv3/sdk/integrationconfig/IntegrationEvaluatorTest.java b/SDK/test/queueit/knownuserv3/sdk/integrationconfig/IntegrationEvaluatorTest.java index 5394fae..bb8dc96 100644 --- a/SDK/test/queueit/knownuserv3/sdk/integrationconfig/IntegrationEvaluatorTest.java +++ b/SDK/test/queueit/knownuserv3/sdk/integrationconfig/IntegrationEvaluatorTest.java @@ -1,13 +1,394 @@ package queueit.knownuserv3.sdk.integrationconfig; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.security.Principal; +import java.util.Collection; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; +import javax.servlet.AsyncContext; +import javax.servlet.DispatcherType; +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.ServletInputStream; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; import static org.junit.Assert.*; import org.junit.Test; import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +import javax.servlet.http.HttpUpgradeHandler; +import javax.servlet.http.Part; + + class HttpServletRequestMock implements HttpServletRequest { + public Cookie[] CookiesValue; + public String UserAgent = ""; + public String RequestURL; + public String QueryString; + public HashMap Headers = new HashMap(); + + @Override + public String getAuthType() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public Cookie[] getCookies() { + return this.CookiesValue; + } + + @Override + public long getDateHeader(String string) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public String getHeader(String key) { + if("User-Agent".equals(key)) + return this.UserAgent; + + if(this.Headers.containsKey(key)) + return this.Headers.get(key); + return ""; + } + + @Override + public Enumeration getHeaders(String string) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public Enumeration getHeaderNames() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public int getIntHeader(String string) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public String getMethod() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public String getPathInfo() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public String getPathTranslated() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public String getContextPath() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public String getQueryString() { + return this.QueryString; + } + + @Override + public String getRemoteUser() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public boolean isUserInRole(String string) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public Principal getUserPrincipal() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public String getRequestedSessionId() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public String getRequestURI() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public StringBuffer getRequestURL() { + return new StringBuffer(this.RequestURL); + } + + @Override + public String getServletPath() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public HttpSession getSession(boolean bln) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public HttpSession getSession() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public String changeSessionId() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public boolean isRequestedSessionIdValid() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public boolean isRequestedSessionIdFromCookie() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public boolean isRequestedSessionIdFromURL() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public boolean isRequestedSessionIdFromUrl() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public boolean authenticate(HttpServletResponse hsr) throws IOException, ServletException { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public void login(String string, String string1) throws ServletException { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public void logout() throws ServletException { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public Collection getParts() throws IOException, ServletException { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public Part getPart(String string) throws IOException, ServletException { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public T upgrade(Class type) throws IOException, ServletException { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public Object getAttribute(String string) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public Enumeration getAttributeNames() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public String getCharacterEncoding() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public void setCharacterEncoding(String string) throws UnsupportedEncodingException { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public int getContentLength() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public long getContentLengthLong() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public String getContentType() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public ServletInputStream getInputStream() throws IOException { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public String getParameter(String string) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public Enumeration getParameterNames() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public String[] getParameterValues(String string) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public Map getParameterMap() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public String getProtocol() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public String getScheme() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public String getServerName() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public int getServerPort() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public BufferedReader getReader() throws IOException { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public String getRemoteAddr() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public String getRemoteHost() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public void setAttribute(String string, Object o) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public void removeAttribute(String string) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public Locale getLocale() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public Enumeration getLocales() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public boolean isSecure() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public RequestDispatcher getRequestDispatcher(String string) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public String getRealPath(String string) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public int getRemotePort() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public String getLocalName() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public String getLocalAddr() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public int getLocalPort() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public ServletContext getServletContext() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public AsyncContext startAsync() throws IllegalStateException { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public AsyncContext startAsync(ServletRequest sr, ServletResponse sr1) throws IllegalStateException { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public boolean isAsyncStarted() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public boolean isAsyncSupported() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public AsyncContext getAsyncContext() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public DispatcherType getDispatcherType() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + } + public class IntegrationEvaluatorTest { @Test - public void GetMatchedIntegrationConfig_OneTrigger_And_NotMatched() { + public void GetMatchedIntegrationConfig_OneTrigger_And_NotMatched() throws Exception { IntegrationEvaluator testObject = new IntegrationEvaluator(); TriggerPart triggerPart1 = new TriggerPart(); @@ -43,13 +424,16 @@ public void GetMatchedIntegrationConfig_OneTrigger_And_NotMatched() { customerIntegration.Integrations = integrationConfigModels; String url = "http://test.tesdomain.com:8080/test?q=2"; - Cookie [] cookies = new Cookie [0]; - IntegrationConfigModel result = testObject.getMatchedIntegrationConfig(customerIntegration, url, cookies, ""); + + HttpServletRequestMock httpContextMock = new HttpServletRequestMock(); + httpContextMock.CookiesValue = new Cookie [0]; + + IntegrationConfigModel result = testObject.getMatchedIntegrationConfig(customerIntegration, url, httpContextMock); assertTrue(result == null); } @Test - public void GetMatchedIntegrationConfig_OneTrigger_And_Matched() { + public void GetMatchedIntegrationConfig_OneTrigger_And_Matched() throws Exception { IntegrationEvaluator testObject = new IntegrationEvaluator(); TriggerPart triggerPart1 = new TriggerPart(); @@ -87,15 +471,17 @@ public void GetMatchedIntegrationConfig_OneTrigger_And_Matched() { customerIntegration.Integrations = integrationConfigModels; String url = "http://test.tesdomain.com:8080/test?q=2"; - Cookie [] cookies = new Cookie [1]; - cookies[0]=new Cookie("c1", "value1"); + + HttpServletRequestMock httpContextMock = new HttpServletRequestMock(); + httpContextMock.CookiesValue = new Cookie [1]; + httpContextMock.CookiesValue[0] = new Cookie("c1", "value1"); - IntegrationConfigModel result = testObject.getMatchedIntegrationConfig(customerIntegration, url, cookies, ""); + IntegrationConfigModel result = testObject.getMatchedIntegrationConfig(customerIntegration, url, httpContextMock); assertTrue(result.Name.equals("integration1")); } @Test - public void GetMatchedIntegrationConfig_OneTrigger_Or_NotMatched() { + public void GetMatchedIntegrationConfig_OneTrigger_Or_NotMatched() throws Exception { IntegrationEvaluator testObject = new IntegrationEvaluator(); TriggerPart triggerPart1 = new TriggerPart(); @@ -134,15 +520,17 @@ public void GetMatchedIntegrationConfig_OneTrigger_Or_NotMatched() { customerIntegration.Integrations = integrationConfigModels; String url = "http://test.tesdomain.com:8080/test?q=2"; - Cookie [] cookies = new Cookie [1]; - cookies[0] = new Cookie("c2", "value1"); - IntegrationConfigModel result = testObject.getMatchedIntegrationConfig(customerIntegration, url, cookies, ""); + HttpServletRequestMock httpContextMock = new HttpServletRequestMock(); + httpContextMock.CookiesValue = new Cookie [1]; + httpContextMock.CookiesValue[0] = new Cookie("c2", "value1"); + + IntegrationConfigModel result = testObject.getMatchedIntegrationConfig(customerIntegration, url, httpContextMock); assertTrue(result == null); } @Test - public void GetMatchedIntegrationConfig_OneTrigger_Or_Matched() { + public void GetMatchedIntegrationConfig_OneTrigger_Or_Matched() throws Exception { IntegrationEvaluator testObject = new IntegrationEvaluator(); TriggerPart triggerPart1 = new TriggerPart(); @@ -179,15 +567,17 @@ public void GetMatchedIntegrationConfig_OneTrigger_Or_Matched() { customerIntegration.Integrations = integrationConfigModels; String url = "http://test.tesdomain.com:8080/test?q=2"; - Cookie [] cookies = new Cookie [1]; - cookies[0]= new Cookie("c1", "value1"); + + HttpServletRequestMock httpContextMock = new HttpServletRequestMock(); + httpContextMock.CookiesValue = new Cookie [1]; + httpContextMock.CookiesValue[0] = new Cookie("c1", "value1"); - IntegrationConfigModel result = testObject.getMatchedIntegrationConfig(customerIntegration, url, cookies, ""); + IntegrationConfigModel result = testObject.getMatchedIntegrationConfig(customerIntegration, url, httpContextMock); assertTrue(result.Name.equals("integration1")); } @Test - public void GetMatchedIntegrationConfig_TwoTriggers_Matched() { + public void GetMatchedIntegrationConfig_TwoTriggers_Matched() throws Exception { IntegrationEvaluator testObject = new IntegrationEvaluator(); TriggerPart triggerPart1 = new TriggerPart(); @@ -231,14 +621,16 @@ public void GetMatchedIntegrationConfig_TwoTriggers_Matched() { customerIntegration.Integrations = integrationConfigModels; String url = "http://test.tesdomain.com:8080/test?q=2"; - Cookie [] cookies = new Cookie [0]; + + HttpServletRequestMock httpContextMock = new HttpServletRequestMock(); + httpContextMock.CookiesValue = new Cookie [0]; - IntegrationConfigModel result = testObject.getMatchedIntegrationConfig(customerIntegration, url, cookies, ""); + IntegrationConfigModel result = testObject.getMatchedIntegrationConfig(customerIntegration, url, httpContextMock); assertTrue(result.Name.equals("integration1")); } @Test - public void GetMatchedIntegrationConfig_TwoTriggers_NotMatched() { + public void GetMatchedIntegrationConfig_TwoTriggers_NotMatched() throws Exception { IntegrationEvaluator testObject = new IntegrationEvaluator(); TriggerPart triggerPart1 = new TriggerPart(); @@ -282,14 +674,16 @@ public void GetMatchedIntegrationConfig_TwoTriggers_NotMatched() { customerIntegration.Integrations = integrationConfigModels; String url = "http://test.tesdomain.com:8080/test?q=2"; - Cookie [] cookies = new Cookie [0]; + + HttpServletRequestMock httpContextMock = new HttpServletRequestMock(); + httpContextMock.CookiesValue = new Cookie [0]; - IntegrationConfigModel result = testObject.getMatchedIntegrationConfig(customerIntegration, url, cookies, ""); + IntegrationConfigModel result = testObject.getMatchedIntegrationConfig(customerIntegration, url, httpContextMock); assertTrue(result == null); } @Test - public void GetMatchedIntegrationConfig_ThreeIntegrationsInOrder_SecondMatched() { + public void GetMatchedIntegrationConfig_ThreeIntegrationsInOrder_SecondMatched() throws Exception { IntegrationEvaluator testObject = new IntegrationEvaluator(); TriggerPart triggerPart0 = new TriggerPart(); @@ -361,17 +755,18 @@ public void GetMatchedIntegrationConfig_ThreeIntegrationsInOrder_SecondMatched() customerIntegration.Integrations = integrationConfigModels; String url = "http://test.tesdomain.com:8080/test?q=2"; - Cookie [] cookies = new Cookie [1]; - cookies[0]= new Cookie("c1", "Value1"); - - IntegrationConfigModel result = testObject.getMatchedIntegrationConfig(customerIntegration, url, cookies, ""); + + HttpServletRequestMock httpContextMock = new HttpServletRequestMock(); + httpContextMock.CookiesValue = new Cookie [1]; + httpContextMock.CookiesValue[0] = new Cookie("c1", "Value1"); + + IntegrationConfigModel result = testObject.getMatchedIntegrationConfig(customerIntegration, url, httpContextMock); assertTrue(result.Name.equals("integration1")); } - @Test - public void GetMatchedIntegrationConfig_OneTrigger_And_NotMatched_UserAgent() - { - IntegrationEvaluator testObject = new IntegrationEvaluator(); + @Test + public void GetMatchedIntegrationConfig_OneTrigger_And_NotMatched_UserAgent() throws Exception { + IntegrationEvaluator testObject = new IntegrationEvaluator(); TriggerPart triggerPart1 = new TriggerPart(); triggerPart1.ValidatorType = ValidatorType.COOKIE_VALIDATOR; @@ -416,10 +811,13 @@ public void GetMatchedIntegrationConfig_OneTrigger_And_NotMatched_UserAgent() customerIntegration.Integrations = integrationConfigModels; String url = "http://test.tesdomain.com:8080/test?q=2"; - Cookie [] cookies = new Cookie [1]; - cookies[0]=new Cookie("c1", "value1"); - - IntegrationConfigModel result = testObject.getMatchedIntegrationConfig(customerIntegration, url, cookies, "Googlebot"); + + HttpServletRequestMock httpContextMock = new HttpServletRequestMock(); + httpContextMock.CookiesValue = new Cookie [1]; + httpContextMock.CookiesValue[0]=new Cookie("c1", "value1"); + httpContextMock.UserAgent = "Googlebot"; + + IntegrationConfigModel result = testObject.getMatchedIntegrationConfig(customerIntegration, url, httpContextMock); assertTrue(result == null); } } \ No newline at end of file