diff --git a/README.md b/README.md index 2467b3b..7919988 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ You can install this SDK in a couple of ways: com.queue-it.connector connector - 3.6.2 + 3.7.0 ``` @@ -23,17 +23,17 @@ The KnownUser validation must be done on all requests except requests for static This example is using the *[IntegrationConfigProvider](https://github.com/queueit/KnownUser.V3.JAVA/blob/master/Documentation/IntegrationConfigProvider.java)* to download the queue configuration. The IntegrationConfigProvider.java file is an example of how the download and caching of the configuration can be done. This is just an example, but if you make your own downloader, please cache the result for 5 - 10 minutes to limit number of download requests. **You should NEVER download the configuration as part of the request handling**. The following method is all that is needed to validate that a user has been through the queue: -``` - private void doValidation(HttpServletRequest request, HttpServletResponse response) { +```java + private void doValidation(KnownUserRequestWrapper request, HttpServletResponse response) { try { String customerId = "Your Queue-it customer ID"; String secretKey = "Your 72 char secrete key as specified in Go Queue-it self-service platform"; - String apiKey = "Your api-key as specified in Go Queue-it self-service platform"; + String apiKey = "Your api-key as specified in Go Queue-it self-service platform"; String queueitToken = request.getParameter(KnownUser.QueueITTokenKey); String pureUrl = getPureUrl(request); - - // The pureUrl is used to match Triggers and as the Target url (where to return the users to) + + // The pureUrl is used to match Triggers and as the Target url (where to return the users to) // It is therefor important that the pureUrl is exactly the url of the users browsers. So if your webserver is // e.g. behind a load balancer that modifies the host name or port, reformat the pureUrl before proceeding CustomerIntegration integrationConfig = IntegrationConfigProvider.getCachedIntegrationConfig(customerId, apiKey); @@ -51,6 +51,7 @@ The following method is all that is needed to validate that a user has been thro if (validationResult.isAjaxResult) { //In case of ajax call send the user to the queue by sending a custom queue-it header and redirecting user to queue from javascript response.setHeader(validationResult.getAjaxQueueRedirectHeaderKey(), validationResult.getAjaxRedirectUrl()); + response.setHeader("Access-Control-Expose-Headers", validationResult.getAjaxQueueRedirectHeaderKey()); } else { //Send the user to the queue - either becuase hash was missing or becuase is was invalid response.sendRedirect(validationResult.getRedirectUrl()); @@ -60,16 +61,16 @@ The following method is all that is needed to validate that a user has been thro } else { String queryString = request.getQueryString(); //Request can continue - we remove queueittoken form querystring parameter to avoid sharing of user specific token - if (queryString != null && queryString.contains(KnownUser.QueueITTokenKey) && "Queue".equals(validationResult.getActionType()) ) { - response.sendRedirect(pureUrl); - response.getOutputStream().flush(); - response.getOutputStream().close(); + if (queryString != null && queryString.contains(KnownUser.QueueITTokenKey) && "Queue".equals(validationResult.getActionType()) ) { + response.sendRedirect(pureUrl); + response.getOutputStream().flush(); + response.getOutputStream().close(); } } } catch (Exception ex) { // There was an error validating the request // Use your own logging framework to log the error - // This was a configuration error, so we let the user continue + // This was a configuration error, so we let the user continue } } @@ -90,8 +91,8 @@ Specify the configuration in code without using the Trigger/Action paradigm. In The following is an example of how to specify the configuration in code: -``` - private void doValidationByLocalEventConfig(HttpServletRequest request, HttpServletResponse response) { +```java + private void doValidationByLocalEventConfig(KnownUserRequestWrapper request, HttpServletResponse response) { try { String customerId = "Your Queue-it customer ID"; @@ -118,9 +119,10 @@ The following is an example of how to specify the configuration in code: response.setHeader("Pragma", "no-cache"); response.setHeader("Expires", "Fri, 01 Jan 1990 00:00:00 GMT"); //end - if (validationResult.isAjaxResult) { + if (validationResult.isAjaxResult) { //In case of ajax call send the user to the queue by sending a custom queue-it header and redirecting user to queue from javascript response.setHeader(validationResult.getAjaxQueueRedirectHeaderKey(), validationResult.getAjaxRedirectUrl()); + response.setHeader("Access-Control-Expose-Headers", validationResult.getAjaxQueueRedirectHeaderKey()); } else { //Send the user to the queue - either becuase hash was missing or becuase is was invalid response.sendRedirect(validationResult.getRedirectUrl()); @@ -144,3 +146,125 @@ The following is an example of how to specify the configuration in code: When users are redirected back from queue-it website they carry a QueueITToken with some information which is used to validate their request by SDK. In specific cases you would like to validate, process or extract specfic parameters you can use QueueParameterHelper class in [KnownUserHelper.java](https://github.com/queueit/KnownUser.V3.JAVA/blob/master/Documentation/KnownUserHelper.java). Calling *QueueParameterHelper.getIsTokenValid()* will validate the token and passing QueueITToken to *QueueParameterHelper.extractQueueParams* you will get a QueueUrlParams result containing all parameters found in the token. + +## Request body trigger (advanced) + +The connector supports triggering on request body content. An example could be a POST call with specific item ID where you want end-users to queue up for. +For this to work, you will need to enable request body triggers in your integration settings in your GO Queue-it platform account or contact Queue-it support. +Once enabled you will need to update your integration configuration so request body is available for the connector. + +Request body should be provided by the code which is using this SDK. You can read the request body in your code and provide it to the SDK. This should be done using a subclass of KnownUserRequestWrapper (Please take a look at CustomKnownUserRequestWrapper as an example). The subclass should be used instead of HttpServletRequest similar to the below example. Then the request body can be read many times by using GetRequestBodyAsString() mehod. + +For the Get requests the KnownUserRequestWrapper could be used directly. + +```java + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + KnownUserRequestWrapper requestWrapper = new KnownUserRequestWrapper(request); + processRequest(requestWrapper, response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + CustomKnownUserRequestWrapper requestWrapper = new CustomKnownUserRequestWrapper(request); + processRequest(requestWrapper, response); + } +``` + +Here is an example of implementing CustomKnownUserRequestWrapper subclass. +This is just one example of how to read the request body, you could use your own implementation. + + +```java +public class CustomKnownUserRequestWrapper extends KnownUserRequestWrapper { + + private final String body; + + public CustomKnownUserRequestWrapper(HttpServletRequest request) throws IOException { + super(request); + + int maxBytesToRead = 1024 * 50; + StringBuilder stringBuilder = new StringBuilder(); + try { + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(super.getInputStream())); + char[] charBuffer = new char[1024]; + int bytesRead = -1; + while (((bytesRead = bufferedReader.read(charBuffer)) > 0) && stringBuilder.length() <= maxBytesToRead) { + stringBuilder.append(charBuffer, 0, bytesRead); + } + } catch (IOException ex) { + throw ex; + } + body = stringBuilder.toString(); + } + + @Override + public ServletInputStream getInputStream() throws IOException { + final byte[] myBytes = body.getBytes("UTF-8"); + ServletInputStream servletInputStream = new ServletInputStream() { + private int lastIndexRetrieved = -1; + private ReadListener readListener = null; + + @Override + public boolean isFinished() { + return (lastIndexRetrieved == myBytes.length - 1); + } + + @Override + public boolean isReady() { + return isFinished(); + } + + @Override + public void setReadListener(ReadListener readListener) { + this.readListener = readListener; + if (!isFinished()) { + try { + readListener.onDataAvailable(); + } catch (IOException e) { + readListener.onError(e); + } + } else { + try { + readListener.onAllDataRead(); + } catch (IOException e) { + readListener.onError(e); + } + } + } + + @Override + public int read() throws IOException { + int i; + if (!isFinished()) { + i = myBytes[lastIndexRetrieved + 1]; + lastIndexRetrieved++; + if (isFinished() && (readListener != null)) { + try { + readListener.onAllDataRead(); + } catch (IOException ex) { + readListener.onError(ex); + throw ex; + } + } + return i; + } else { + return -1; + } + } + }; + return servletInputStream; + } + + @Override + public BufferedReader getReader() throws IOException { + return new BufferedReader(new InputStreamReader(this.getInputStream())); + } + + public String GetRequestBodyAsString() { + return this.body; + } +} +``` \ No newline at end of file diff --git a/SDK/.classpath b/SDK/.classpath index 0ca1374..a004e75 100644 --- a/SDK/.classpath +++ b/SDK/.classpath @@ -24,7 +24,7 @@ - + diff --git a/SDK/pom.xml b/SDK/pom.xml index 5a7014f..d4fa366 100644 --- a/SDK/pom.xml +++ b/SDK/pom.xml @@ -8,7 +8,7 @@ 4.0.0 com.queue-it.connector connector - 3.6.2 + 3.7.0 KnownUserV3 SDK for integrating your application with Queue-it https://github.com/queueit/KnownUser.V3.JAVA @@ -122,7 +122,7 @@ - + diff --git a/SDK/src/main/java/com/queue_it/connector/CancelEventConfig.java b/SDK/src/main/java/com/queue_it/connector/CancelEventConfig.java index edf0587..655e4bd 100644 --- a/SDK/src/main/java/com/queue_it/connector/CancelEventConfig.java +++ b/SDK/src/main/java/com/queue_it/connector/CancelEventConfig.java @@ -7,6 +7,8 @@ public class CancelEventConfig { private String cookieDomain; private int version; private String actionName = "unspecified"; + private Boolean isCookieHttpOnly; + private Boolean isCookieSecure; public String getEventId() { return eventId; @@ -52,12 +54,30 @@ public String getActionName() { return actionName; } + public Boolean getIsCookieHttpOnly() { + return this.isCookieHttpOnly; + } + + public void setIsCookieHttpOnly(Boolean isCookieHttpOnly) { + this.isCookieHttpOnly = isCookieHttpOnly; + } + + public Boolean getIsCookieSecure() { + return this.isCookieSecure; + } + + public void setIsCookieSecure(Boolean cookieSecure) { + this.isCookieSecure = cookieSecure; + } + @Override public String toString() { return "EventId:" + eventId + "&Version:" + version + "&QueueDomain:" + queueDomain + "&CookieDomain:" + cookieDomain + + "&IsCookieHttpOnly:" + isCookieHttpOnly + + "&IsCookieSecure:" + isCookieSecure + "&ActionName:" + actionName; } } diff --git a/SDK/src/main/java/com/queue_it/connector/KnownUser.java b/SDK/src/main/java/com/queue_it/connector/KnownUser.java index 92339c7..289ca96 100644 --- a/SDK/src/main/java/com/queue_it/connector/KnownUser.java +++ b/SDK/src/main/java/com/queue_it/connector/KnownUser.java @@ -38,7 +38,7 @@ static void setUserInQueueService(IUserInQueueService mockUserInQueueService) { public static RequestValidationResult validateRequestByIntegrationConfig(String currentUrlWithoutQueueITToken, String queueitToken, CustomerIntegration customerIntegrationInfo, String customerId, - HttpServletRequest request, HttpServletResponse response, String secretKey) throws Exception { + KnownUserRequestWrapper request, HttpServletResponse response, String secretKey) throws Exception { Map debugEntries = new HashMap(); @@ -255,8 +255,14 @@ private static RequestValidationResult resolveQueueRequestByLocalConfig(String t return result; } - public static void extendQueueCookie(String eventId, int cookieValidityMinute, String cookieDomain, - HttpServletRequest request, HttpServletResponse response, String secretKey) throws Exception { + public static void extendQueueCookie(String eventId, + int cookieValidityMinute, + String cookieDomain, + Boolean isCookieHttpOnly, + Boolean isCookieSecure, + HttpServletRequest request, + HttpServletResponse response, + String secretKey) throws Exception { if (Utils.isNullOrWhiteSpace(eventId)) { throw new Exception("eventId can not be null or empty."); @@ -269,7 +275,7 @@ public static void extendQueueCookie(String eventId, int cookieValidityMinute, S } IUserInQueueService userInQueueService = getUserInQueueService(request, response); - userInQueueService.extendQueueCookie(eventId, cookieValidityMinute, cookieDomain, secretKey); + userInQueueService.extendQueueCookie(eventId, cookieValidityMinute, cookieDomain, isCookieHttpOnly, isCookieSecure, secretKey); } private static void setDebugCookie(Map debugEntries, HttpServletRequest request, @@ -287,7 +293,7 @@ private static void setDebugCookie(Map debugEntries, HttpServlet if (!"".equals(cookieValue)) { cookieValue = cookieValue.substring(0, cookieValue.length() - 1); // remove trailing char } - cookieManager.setCookie(QueueITDebugKey, cookieValue, null, null); + cookieManager.setCookie(QueueITDebugKey, cookieValue, null, null, false, false); } private static void logMoreRequestDetails(Map debugEntries, HttpServletRequest request) { @@ -339,6 +345,8 @@ private static RequestValidationResult handleQueueAction(IntegrationConfigModel queueConfig.setLayoutName(matchedConfig.LayoutName); queueConfig.setCookieValidityMinute(matchedConfig.CookieValidityMinute); queueConfig.setCookieDomain(matchedConfig.CookieDomain); + queueConfig.setIsCookieHttpOnly(matchedConfig.IsCookieHttpOnly); + queueConfig.setIsCookieSecure(matchedConfig.IsCookieSecure); queueConfig.setVersion(customerIntegrationInfo.Version); queueConfig.setActionName(matchedConfig.Name); @@ -355,6 +363,8 @@ private static RequestValidationResult handleCancelAction(IntegrationConfigModel cancelConfig.setQueueDomain(matchedConfig.QueueDomain); cancelConfig.setEventId(matchedConfig.EventId); cancelConfig.setCookieDomain(matchedConfig.CookieDomain); + cancelConfig.setIsCookieHttpOnly(matchedConfig.IsCookieHttpOnly); + cancelConfig.setIsCookieSecure(matchedConfig.IsCookieSecure); cancelConfig.setVersion(customerIntegrationInfo.Version); cancelConfig.setActionName(matchedConfig.Name); @@ -397,9 +407,9 @@ public static String GetRuntime() { interface ICookieManager { - void setCookie(String cookieName, String cookieValue, Integer expiration, String cookieDomain); + void setCookie(String name, String value, Integer expiration, String domain, Boolean isHttpOnly, Boolean isSecure); - String getCookie(String cookieName); + String getCookie(String name); } class CookieManager implements ICookieManager { @@ -413,32 +423,35 @@ public CookieManager(HttpServletRequest request, HttpServletResponse response) { } @Override - public void setCookie(String cookieName, String cookieValue, Integer expiration, String cookieDomain) { + public void setCookie(String name, String value, Integer expiration, String domain, Boolean isHttpOnly, Boolean isSecure) { if (response == null) { return; } - Cookie cookie = new Cookie(cookieName, cookieValue); - if (cookieValue == null) { - cookieValue = ""; + Cookie cookie = new Cookie(name, value); + if (value == null) { + value = ""; } try { - cookie.setValue(URLEncoder.encode(cookieValue, "UTF-8")); + cookie.setValue(URLEncoder.encode(value, "UTF-8")); } catch (UnsupportedEncodingException ex) { } if (expiration != null) { cookie.setMaxAge(expiration); } cookie.setPath("/"); - if (!Utils.isNullOrWhiteSpace(cookieDomain)) { - cookie.setDomain(cookieDomain); + if (!Utils.isNullOrWhiteSpace(domain)) { + cookie.setDomain(domain); } + cookie.setHttpOnly(Boolean.TRUE.equals(isHttpOnly)); + cookie.setSecure(Boolean.TRUE.equals(isSecure)); + response.addCookie(cookie); } @Override - public String getCookie(String cookieName) { + public String getCookie(String name) { if (request == null) { return null; } @@ -449,7 +462,7 @@ public String getCookie(String cookieName) { } for (Cookie cookie : cookies) { - if (cookie.getName().equals(cookieName)) { + if (cookie.getName().equals(name)) { try { return URLDecoder.decode(cookie.getValue(), "UTF-8"); } catch (Exception ex) { diff --git a/SDK/src/main/java/com/queue_it/connector/KnownUserRequestWrapper.java b/SDK/src/main/java/com/queue_it/connector/KnownUserRequestWrapper.java new file mode 100644 index 0000000..dac6f12 --- /dev/null +++ b/SDK/src/main/java/com/queue_it/connector/KnownUserRequestWrapper.java @@ -0,0 +1,16 @@ +package com.queue_it.connector; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletRequestWrapper; + + +public class KnownUserRequestWrapper extends HttpServletRequestWrapper { + + public KnownUserRequestWrapper(HttpServletRequest request) { + super(request); + } + + public String GetRequestBodyAsString(){ + return ""; + } +} \ No newline at end of file diff --git a/SDK/src/main/java/com/queue_it/connector/QueueEventConfig.java b/SDK/src/main/java/com/queue_it/connector/QueueEventConfig.java index 57d140d..8120d9c 100644 --- a/SDK/src/main/java/com/queue_it/connector/QueueEventConfig.java +++ b/SDK/src/main/java/com/queue_it/connector/QueueEventConfig.java @@ -11,6 +11,8 @@ public class QueueEventConfig { private String cookieDomain; private int version; private String actionName = "unspecified"; + private Boolean isCookieHttpOnly; + private Boolean isCookieSecure; public String getEventId() { return eventId; @@ -88,12 +90,30 @@ public String getActionName() { return actionName; } + public Boolean getIsCookieHttpOnly() { + return this.isCookieHttpOnly; + } + + public void setIsCookieHttpOnly(Boolean cookieHttpOnly) { + this.isCookieHttpOnly = cookieHttpOnly; + } + + public Boolean getIsCookieSecure() { + return this.isCookieSecure; + } + + public void setIsCookieSecure(Boolean cookieSecure) { + this.isCookieSecure = cookieSecure; + } + @Override public String toString() { return "EventId:" + eventId + "&Version:" + version + "&QueueDomain:" + queueDomain + "&CookieDomain:" + cookieDomain + + "&IsCookieHttpOnly:" + isCookieHttpOnly + + "&IsCookieSecure:" + isCookieSecure + "&ExtendCookieValidity:" + extendCookieValidity + "&CookieValidityMinute:" + cookieValidityMinute + "&LayoutName:" + layoutName diff --git a/SDK/src/main/java/com/queue_it/connector/UserInQueueService.java b/SDK/src/main/java/com/queue_it/connector/UserInQueueService.java index ebd6bb2..2361638 100644 --- a/SDK/src/main/java/com/queue_it/connector/UserInQueueService.java +++ b/SDK/src/main/java/com/queue_it/connector/UserInQueueService.java @@ -21,6 +21,8 @@ void extendQueueCookie( String eventId, int cookieValidityMinutes, String cookieDomain, + Boolean isCookieHttpOnly, + Boolean isCookieSecure, String secretKey); RequestValidationResult getIgnoreActionResult(String actionName); @@ -28,7 +30,7 @@ void extendQueueCookie( class UserInQueueService implements IUserInQueueService { - public static final String SDK_VERSION = "v3-java-" + "3.6.2"; + public static final String SDK_VERSION = "v3-java-" + "3.7.0"; public final IUserInQueueStateRepository _userInQueueStateRepository; public UserInQueueService(IUserInQueueStateRepository queueStateRepository) { @@ -51,8 +53,11 @@ public RequestValidationResult validateQueueRequest( stateInfo.getQueueId(), null, config.getCookieDomain(), + config.getIsCookieHttpOnly(), + config.getIsCookieSecure(), stateInfo.getRedirectType(), - secretKey); + secretKey + ); } return new RequestValidationResult(ActionType.QUEUE_ACTION, config.getEventId(), stateInfo.getQueueId(), null, stateInfo.getRedirectType(), config.getActionName()); } @@ -74,7 +79,7 @@ public RequestValidationResult validateQueueRequest( } if (stateInfo.isFound() && !isTokenValid) { - this._userInQueueStateRepository.cancelQueueCookie(config.getEventId(), config.getCookieDomain()); + this._userInQueueStateRepository.cancelQueueCookie(config.getEventId(), config.getCookieDomain(),config.getIsCookieHttpOnly(), config.getIsCookieSecure()); } return requestValidationResult; @@ -90,8 +95,9 @@ private RequestValidationResult getValidTokenResult( queueParams.getQueueId(), queueParams.getCookieValidityMinutes(), config.getCookieDomain(), - queueParams.getRedirectType(), - secretKey); + config.getIsCookieHttpOnly(), config.getIsCookieSecure(), queueParams.getRedirectType(), + secretKey + ); return new RequestValidationResult( ActionType.QUEUE_ACTION, @@ -187,8 +193,10 @@ public void extendQueueCookie( String eventId, int cookieValidityMinute, String cookieDomain, + Boolean isCookieHttpOnly, + Boolean isCookieSecure, String secretKey) { - this._userInQueueStateRepository.reissueQueueCookie(eventId, cookieValidityMinute, cookieDomain, secretKey); + this._userInQueueStateRepository.reissueQueueCookie(eventId, cookieValidityMinute, cookieDomain, isCookieHttpOnly, isCookieSecure, secretKey); } @Override @@ -201,8 +209,13 @@ public RequestValidationResult validateCancelRequest( StateInfo state = _userInQueueStateRepository.getState(config.getEventId(), -1, secretKey, false); if (state.isValid()) { - this._userInQueueStateRepository.cancelQueueCookie(config.getEventId(), config.getCookieDomain()); - String uriPath = "cancel/" + customerId + "/" + config.getEventId() + "/"; + this._userInQueueStateRepository.cancelQueueCookie(config.getEventId(), config.getCookieDomain(), config.getIsCookieHttpOnly(), config.getIsCookieSecure()); + String uriPath = "cancel/" + customerId + "/" + config.getEventId(); + + String queueId = state.getQueueId(); + if(queueId != null && !queueId.trim().isEmpty()) { + uriPath += "/" + queueId; + } String query = getQueryString(customerId, config.getEventId(), config.getVersion(), config.getActionName(), null, null); diff --git a/SDK/src/main/java/com/queue_it/connector/UserInQueueStateCookieRepository.java b/SDK/src/main/java/com/queue_it/connector/UserInQueueStateCookieRepository.java index d9d87a0..4baf68d 100644 --- a/SDK/src/main/java/com/queue_it/connector/UserInQueueStateCookieRepository.java +++ b/SDK/src/main/java/com/queue_it/connector/UserInQueueStateCookieRepository.java @@ -5,12 +5,14 @@ interface IUserInQueueStateRepository { void store( - String eventId, - String queueId, - Integer fixedCookieValidityMinutes, - String cookieDomain, - String redirectType, - String secretKey) throws Exception; + String eventId, + String queueId, + Integer fixedCookieValidityMinutes, + String cookieDomain, + Boolean isCookieHttpOnly, + Boolean isCookieSecure, + String redirectType, + String secretKey) throws Exception; StateInfo getState(String eventId, int cookieValidityMinutes, @@ -19,13 +21,17 @@ StateInfo getState(String eventId, void cancelQueueCookie( String eventId, - String cookieDomain); + String cookieDomain, + Boolean isCookieHttpOnly, + Boolean isCookieSecure); void reissueQueueCookie( - String eventId, - int cookieValidityMinutes, - String cookieDomain, - String secretKey); + String eventId, + int cookieValidityMinutes, + String cookieDomain, + Boolean isCookieHttpOnly, + Boolean isCookieSecure, + String secretKey); } class UserInQueueStateCookieRepository implements IUserInQueueStateRepository { @@ -49,18 +55,20 @@ public UserInQueueStateCookieRepository(ICookieManager cookieManeger) { @Override public void store( - String eventId, - String queueId, - Integer fixedCookieValidityMinutes, - String cookieDomain, - String redirectType, - String secretKey) throws Exception { + String eventId, + String queueId, + Integer fixedCookieValidityMinutes, + String cookieDomain, + Boolean isCookieHttpOnly, + Boolean isCookieSecure, + String redirectType, + String secretKey) throws Exception { String cookieKey = getCookieKey(eventId); String cookieValue = createCookieValue(eventId, queueId, fixedCookieValidityMinutes, redirectType, secretKey); - this.cookieManager.setCookie(cookieKey, cookieValue, 24 * 60 * 60, cookieDomain); + this.cookieManager.setCookie(cookieKey, cookieValue, 24 * 60 * 60, cookieDomain, isCookieHttpOnly, isCookieSecure); } private String createCookieValue(String eventId, String queueId, Integer fixedCookieValidityMinutes, String redirectType, String secretKey) throws Exception { @@ -175,17 +183,19 @@ public StateInfo getState(String eventId, int cookieValidityMinutes, String secr @Override public void cancelQueueCookie( String eventId, - String cookieDomain) { + String cookieDomain, + Boolean isCookieHttpOnly, + Boolean isCookieSecure) { String cookieKey = getCookieKey(eventId); - cookieManager.setCookie(cookieKey, null, 0, cookieDomain); + cookieManager.setCookie(cookieKey, null, 0, cookieDomain, isCookieHttpOnly, isCookieSecure); } @Override public void reissueQueueCookie( - String eventId, - int cookieValidityMinutes, - String cookieDomain, - String secretKey) { + String eventId, + int cookieValidityMinutes, + String cookieDomain, + Boolean isCookieHttpOnly, Boolean isCookieSecure, String secretKey) { try { String cookieKey = getCookieKey(eventId); String cookieValueOld = this.cookieManager.getCookie(cookieKey); @@ -202,7 +212,7 @@ public void reissueQueueCookie( } String cookieValue = createCookieValue(eventId, cookieNameValueMap.get(QUEUE_ID_KEY), fixedCookieValidityMinutes, cookieNameValueMap.get(REDIRECT_TYPE_KEY), secretKey); - this.cookieManager.setCookie(cookieKey, cookieValue, 24 * 60 * 60, cookieDomain); + this.cookieManager.setCookie(cookieKey, cookieValue, 24 * 60 * 60, cookieDomain, isCookieHttpOnly, isCookieSecure); } catch (Exception ex) { } diff --git a/SDK/src/main/java/com/queue_it/connector/integrationconfig/Constants.java b/SDK/src/main/java/com/queue_it/connector/integrationconfig/Constants.java index ab07e68..ec00408 100644 --- a/SDK/src/main/java/com/queue_it/connector/integrationconfig/Constants.java +++ b/SDK/src/main/java/com/queue_it/connector/integrationconfig/Constants.java @@ -6,6 +6,7 @@ final class ValidatorType { public static final String COOKIE_VALIDATOR = "CookieValidator"; public static final String USERAGENT_VALIDATOR = "UserAgentValidator"; public static final String HTTPHEADER_VALIDATOR = "HttpHeaderValidator"; + public static final String REQUESTBODY_VALIDATOR = "RequestBodyValidator"; } final class UrlPartType { diff --git a/SDK/src/main/java/com/queue_it/connector/integrationconfig/IntegrationConfigModel.java b/SDK/src/main/java/com/queue_it/connector/integrationconfig/IntegrationConfigModel.java index 0e51e5b..2f3d016 100644 --- a/SDK/src/main/java/com/queue_it/connector/integrationconfig/IntegrationConfigModel.java +++ b/SDK/src/main/java/com/queue_it/connector/integrationconfig/IntegrationConfigModel.java @@ -14,4 +14,6 @@ public class IntegrationConfigModel { public String ForcedTargetUrl; public String ActionType; public TriggerModel[] Triggers; + public Boolean IsCookieHttpOnly; + public Boolean IsCookieSecure; } diff --git a/SDK/src/main/java/com/queue_it/connector/integrationconfig/IntegrationEvaluator.java b/SDK/src/main/java/com/queue_it/connector/integrationconfig/IntegrationEvaluator.java index b3192f8..8f85288 100644 --- a/SDK/src/main/java/com/queue_it/connector/integrationconfig/IntegrationEvaluator.java +++ b/SDK/src/main/java/com/queue_it/connector/integrationconfig/IntegrationEvaluator.java @@ -1,17 +1,18 @@ package com.queue_it.connector.integrationconfig; - import java.net.MalformedURLException; import java.net.URL; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; +import com.queue_it.connector.KnownUserRequestWrapper; + interface IIntegrationEvaluator { IntegrationConfigModel getMatchedIntegrationConfig( CustomerIntegration customerIntegration, String currentPageUrl, - HttpServletRequest request) throws Exception; + KnownUserRequestWrapper request) throws Exception; } public class IntegrationEvaluator implements IIntegrationEvaluator { @@ -20,7 +21,7 @@ public class IntegrationEvaluator implements IIntegrationEvaluator { public IntegrationConfigModel getMatchedIntegrationConfig( CustomerIntegration customerIntegration, String currentPageUrl, - HttpServletRequest request) throws Exception { + KnownUserRequestWrapper request) throws Exception { if (request == null) { throw new Exception("request is null"); @@ -39,7 +40,7 @@ public IntegrationConfigModel getMatchedIntegrationConfig( private boolean evaluateTrigger( TriggerModel trigger, String currentPageUrl, - HttpServletRequest request) { + KnownUserRequestWrapper request) { if (trigger.LogicalOperator.equals(LogicalOperatorType.OR)) { for (TriggerPart part : trigger.TriggerParts) { if (evaluateTriggerPart(part, currentPageUrl, request)) { @@ -57,7 +58,7 @@ private boolean evaluateTrigger( } } - private boolean evaluateTriggerPart(TriggerPart triggerPart, String currentPageUrl, HttpServletRequest request) { + private boolean evaluateTriggerPart(TriggerPart triggerPart, String currentPageUrl, KnownUserRequestWrapper request) { if (ValidatorType.URL_VALIDATOR.equals(triggerPart.ValidatorType)) { return UrlValidatorHelper.evaluate(triggerPart, currentPageUrl); } else if (ValidatorType.COOKIE_VALIDATOR.equals(triggerPart.ValidatorType)) { @@ -66,6 +67,8 @@ private boolean evaluateTriggerPart(TriggerPart triggerPart, String currentPageU return UserAgentValidatorHelper.evaluate(triggerPart, request.getHeader("User-Agent")); } else if (ValidatorType.HTTPHEADER_VALIDATOR.equals(triggerPart.ValidatorType)) { return HttpHeaderValidatorHelper.evaluate(triggerPart, request); + } else if (ValidatorType.REQUESTBODY_VALIDATOR.equals(triggerPart.ValidatorType)) { + return RequestBodyValidatorHelper.evaluate(triggerPart, request); } else { return false; } @@ -154,6 +157,19 @@ public static boolean evaluate(TriggerPart triggerPart, HttpServletRequest reque } } +final class RequestBodyValidatorHelper { + + public static boolean evaluate(TriggerPart triggerPart, KnownUserRequestWrapper request) { + String requestsBodyString = request.GetRequestBodyAsString(); + return ComparisonOperatorHelper.evaluate(triggerPart.Operator, + triggerPart.IsNegative, + triggerPart.IsIgnoreCase, + (requestsBodyString == null || requestsBodyString.isEmpty()) ? "" : requestsBodyString, + triggerPart.ValueToCompare, + triggerPart.ValuesToCompare); + } +} + final class ComparisonOperatorHelper { public static boolean evaluate( diff --git a/SDK/src/test/java/com/queue_it/connector/KnownUserRequestWrapperMock.java b/SDK/src/test/java/com/queue_it/connector/KnownUserRequestWrapperMock.java new file mode 100644 index 0000000..2a4707d --- /dev/null +++ b/SDK/src/test/java/com/queue_it/connector/KnownUserRequestWrapperMock.java @@ -0,0 +1,20 @@ +package com.queue_it.connector; + +import javax.servlet.http.HttpServletRequest; + + +public class KnownUserRequestWrapperMock extends KnownUserRequestWrapper { + + String Body = ""; + public KnownUserRequestWrapperMock(HttpServletRequest request) { + super(request); + } + + public void SetRequestBodyAsString(String body){ + this.Body = body; + } + + public String GetRequestBodyAsString(){ + return this.Body; + } +} \ No newline at end of file diff --git a/SDK/src/test/java/com/queue_it/connector/KnownUserTest.java b/SDK/src/test/java/com/queue_it/connector/KnownUserTest.java index 0a7b43b..6e57720 100644 --- a/SDK/src/test/java/com/queue_it/connector/KnownUserTest.java +++ b/SDK/src/test/java/com/queue_it/connector/KnownUserTest.java @@ -39,17 +39,17 @@ public class KnownUserTest { static class UserInQueueServiceMock implements IUserInQueueService { - public ArrayList> validateQueueRequestCalls = new ArrayList<>(); + public ArrayList> validateQueueRequestCalls = new ArrayList>(); public boolean validateQueueRequestRaiseException = false; - public ArrayList> validateCancelRequestCalls = new ArrayList<>(); + public ArrayList> validateCancelRequestCalls = new ArrayList>(); public boolean validateCancelRequestRaiseException = false; - public ArrayList> extendQueueCookieCalls = new ArrayList<>(); - public ArrayList> getIgnoreActionResultCalls = new ArrayList<>(); + public ArrayList> extendQueueCookieCalls = new ArrayList>(); + public ArrayList> getIgnoreActionResultCalls = new ArrayList>(); @Override public RequestValidationResult validateQueueRequest(String targetUrl, String queueitToken, QueueEventConfig config, String customerId, String secretKey) throws Exception { - ArrayList args = new ArrayList<>(); + ArrayList args = new ArrayList(); args.add(targetUrl); args.add(queueitToken); args.add(config.getCookieDomain() + ":" + config.getLayoutName() + ":" + config.getCulture() + ":" @@ -70,7 +70,7 @@ public RequestValidationResult validateQueueRequest(String targetUrl, String que public RequestValidationResult validateCancelRequest(String targetUrl, CancelEventConfig config, String customerId, String secretKey) throws Exception { - ArrayList args = new ArrayList<>(); + ArrayList args = new ArrayList(); args.add(targetUrl); args.add(config.getCookieDomain() + ":" + config.getEventId() + ":" + config.getQueueDomain() + ":" + config.getVersion() + ":" + config.getActionName()); @@ -86,18 +86,20 @@ public RequestValidationResult validateCancelRequest(String targetUrl, CancelEve } @Override - public void extendQueueCookie(String eventId, int cookieValidityMinute, String cookieDomain, String secretKey) { - ArrayList args = new ArrayList<>(); + public void extendQueueCookie(String eventId, int cookieValidityMinute, String cookieDomain, Boolean isCookieHttpOnly, Boolean isCookieSecure, String secretKey) { + ArrayList args = new ArrayList(); args.add(eventId); args.add(Integer.toString(cookieValidityMinute)); args.add(cookieDomain); args.add(secretKey); + args.add(Boolean.toString(isCookieHttpOnly)); + args.add(Boolean.toString(isCookieSecure)); extendQueueCookieCalls.add(args); } @Override public RequestValidationResult getIgnoreActionResult(String actionName) { - ArrayList args = new ArrayList<>(); + ArrayList args = new ArrayList(); args.add(actionName); getIgnoreActionResultCalls.add(args); return new RequestValidationResult("Ignore", "", "", "", "", ""); @@ -347,7 +349,7 @@ public void extendQueueCookieNullEventIdTest() { // Act try { - KnownUser.extendQueueCookie(null, 0, null, null, null, null); + KnownUser.extendQueueCookie(null, 0, null, null, null, null, null, null); } catch (Exception ex) { exceptionWasThrown = "eventId can not be null or empty.".equals(ex.getMessage()); } @@ -366,7 +368,7 @@ public void extendQueueCookieInvalidCookieValidityMinutesTest() { // Act try { - KnownUser.extendQueueCookie("eventId", 0, null, null, null, null); + KnownUser.extendQueueCookie("eventId", 0, null, null, null, null, null,null); } catch (Exception ex) { exceptionWasThrown = "cookieValidityMinute should be greater than 0.".equals(ex.getMessage()); } @@ -385,7 +387,7 @@ public void extendQueueCookieNullSecretKeyTest() { // Act try { - KnownUser.extendQueueCookie("eventId", 20, null, null, null, null); + KnownUser.extendQueueCookie("eventId", 20, null, null, null, null, null,null); } catch (Exception ex) { exceptionWasThrown = "secretKey can not be null or empty.".equals(ex.getMessage()); } @@ -402,13 +404,15 @@ public void extendQueueCookieTest() throws Exception { KnownUser.setUserInQueueService(mock); // Act - KnownUser.extendQueueCookie("eventId", 20, "cookieDomain", null, null, "secretKey"); + KnownUser.extendQueueCookie("eventId", 20, "cookieDomain", true, false, null, null, "secretKey"); // Assert assertEquals("eventId", mock.extendQueueCookieCalls.get(0).get(0)); assertEquals("20", mock.extendQueueCookieCalls.get(0).get(1)); assertEquals("cookieDomain", mock.extendQueueCookieCalls.get(0).get(2)); assertEquals("secretKey", mock.extendQueueCookieCalls.get(0).get(3)); + assertTrue(Boolean.parseBoolean(mock.extendQueueCookieCalls.get(0).get(4))); + assertFalse(Boolean.parseBoolean(mock.extendQueueCookieCalls.get(0).get(5))); } @Test @@ -714,7 +718,7 @@ public void validateRequestByIntegrationConfigEmptyIntegrationsConfigTest() { // Act try { KnownUser.validateRequestByIntegrationConfig("currentUrl", "queueitToken", null, null, - new HttpServletRequestMock(), null, null); + new KnownUserRequestWrapper(new HttpServletRequestMock()), null, null); } catch (Exception ex) { exceptionWasThrown = "customerIntegrationInfo can not be null.".equals(ex.getMessage()); } @@ -766,12 +770,13 @@ public void validateRequestByIntegrationConfigQueueActionTest() throws Exception CustomerIntegration customerIntegration = new CustomerIntegration(); customerIntegration.Integrations = new IntegrationConfigModel[]{config}; customerIntegration.Version = 3; - HttpServletRequestMock httpContextMock = new HttpServletRequestMock(); - httpContextMock.UserAgent = "googlebot"; + HttpServletRequestMock requestMock = new HttpServletRequestMock(); + requestMock.UserAgent = "googlebot"; + KnownUserRequestWrapper wrappedRequest = new KnownUserRequestWrapper(requestMock); // Act RequestValidationResult result = KnownUser.validateRequestByIntegrationConfig("http://test.com?event1=true", - "queueitToken", customerIntegration, "customerId", httpContextMock, null, "secretKey"); + "queueitToken", customerIntegration, "customerId", wrappedRequest, null, "secretKey"); // Assert assertEquals(1, mock.validateQueueRequestCalls.size()); @@ -829,10 +834,11 @@ public void validateRequestByIntegrationConfigQueueActionAjaxCallTest() throws E HttpServletRequestMock requestMock = new HttpServletRequestMock(); requestMock.UserAgent = "googlebot"; requestMock.Headers.put("x-queueit-ajaxpageurl", "http%3A%2F%2Furl"); + KnownUserRequestWrapper wrappedRequest = new KnownUserRequestWrapper(requestMock); // Act RequestValidationResult result = KnownUser.validateRequestByIntegrationConfig("http://test.com?event1=true", - "queueitToken", customerIntegration, "customerId", requestMock, null, "secretKey"); + "queueitToken", customerIntegration, "customerId", wrappedRequest, null, "secretKey"); // Assert assertEquals(1, mock.validateQueueRequestCalls.size()); @@ -891,6 +897,7 @@ public void validateRequestByIntegrationConfigDebugCookieLoggingTest() throws Ex HttpServletRequestMock requestMock = new HttpServletRequestMock(); requestMock.UserAgent = "googlebot"; requestMock.RequestURL = "requestUrl"; + KnownUserRequestWrapper wrappedRequest = new KnownUserRequestWrapper(requestMock); HttpServletResponseMock responseMock = new HttpServletResponseMock(); @@ -901,7 +908,7 @@ public void validateRequestByIntegrationConfigDebugCookieLoggingTest() throws Ex String queueittoken = UserInQueueServiceTest.QueueITTokenGenerator.generateToken(date, "eventId", true, 20, secretKey, "debug"); KnownUser.validateRequestByIntegrationConfig("http://test.com?event1=true", - queueittoken, customerIntegration, "customerId", requestMock, responseMock, secretKey); + queueittoken, customerIntegration, "customerId", wrappedRequest, responseMock, secretKey); // Assert assertEquals(1, responseMock.addedCookies.size()); @@ -936,7 +943,7 @@ public void validateRequestByIntegrationConfigNotMatchTest() throws Exception { // Act RequestValidationResult result = KnownUser.validateRequestByIntegrationConfig("http://test.com?event1=true", - "queueitToken", customerIntegration, "customerId", new HttpServletRequestMock(), null, "secretKey"); + "queueitToken", customerIntegration, "customerId", new KnownUserRequestWrapper(new HttpServletRequestMock()), null, "secretKey"); // Assert assertTrue(mock.validateQueueRequestCalls.isEmpty()); @@ -961,6 +968,7 @@ public void validateRequestByIntegrationConfigNotMatchDebugCookieLoggingTest() t requestMock.Headers.put("x-forwarded-for", "129.78.138.66, 129.78.64.103"); requestMock.Headers.put("x-forwarded-host", "en.wikipedia.org:8080"); requestMock.Headers.put("x-forwarded-proto", "https"); + KnownUserRequestWrapper wrappedRequest = new KnownUserRequestWrapper(requestMock); HttpServletResponseMock responseMock = new HttpServletResponseMock(); @@ -971,7 +979,7 @@ public void validateRequestByIntegrationConfigNotMatchDebugCookieLoggingTest() t String queueittoken = UserInQueueServiceTest.QueueITTokenGenerator.generateToken(date, "eventId", true, 20, secretKey, "debug"); KnownUser.validateRequestByIntegrationConfig("http://test.com?event1=true", queueittoken, customerIntegration, - "customerId", requestMock, responseMock, secretKey); + "customerId", wrappedRequest, responseMock, secretKey); // Assert assertEquals(1, responseMock.addedCookies.size()); @@ -1028,7 +1036,7 @@ public void validateRequestByIntegrationConfigForcedTargeturlTest() throws Excep // Act KnownUser.validateRequestByIntegrationConfig("http://test.com?event1=true", "queueitToken", customerIntegration, - "customerId", new HttpServletRequestMock(), null, "secretKey"); + "customerId", new KnownUserRequestWrapper(new HttpServletRequestMock()), null, "secretKey"); // Assert assertEquals(1, mock.validateQueueRequestCalls.size()); @@ -1073,7 +1081,7 @@ public void validateRequestByIntegrationConfigForecedTargeturlTest() throws Exce // Act KnownUser.validateRequestByIntegrationConfig("http://test.com?event1=true", "queueitToken", customerIntegration, - "customerId", new HttpServletRequestMock(), null, "secretKey"); + "customerId", new KnownUserRequestWrapper(new HttpServletRequestMock()), null, "secretKey"); // Assert assertEquals(1, mock.validateQueueRequestCalls.size()); @@ -1117,7 +1125,7 @@ public void validateRequestByIntegrationConfigEventTargetUrl() throws Exception // Act KnownUser.validateRequestByIntegrationConfig("http://test.com?event1=true", "queueitToken", customerIntegration, - "customerId", new HttpServletRequestMock(), null, "secretKey"); + "customerId", new KnownUserRequestWrapper(new HttpServletRequestMock()), null, "secretKey"); // Assert assertEquals(1, mock.validateQueueRequestCalls.size()); @@ -1156,7 +1164,7 @@ public void validateRequestByIntegrationConfigIgnoreAction() throws Exception { // Act RequestValidationResult result = KnownUser.validateRequestByIntegrationConfig("http://test.com?event1=true", - "queueitToken", customerIntegration, "customerId", new HttpServletRequestMock(), null, "secretKey"); + "queueitToken", customerIntegration, "customerId", new KnownUserRequestWrapper(new HttpServletRequestMock()), null, "secretKey"); // Assert assertEquals(1, mock.getIgnoreActionResultCalls.size()); @@ -1194,13 +1202,14 @@ public void validateRequestByIntegrationConfigAjaxCallIgnoreAction() throws Exce HttpServletRequestMock requestMock = new HttpServletRequestMock(); requestMock.Headers.put("x-queueit-ajaxpageurl", "url"); + KnownUserRequestWrapper wrappedRequest = new KnownUserRequestWrapper(requestMock); UserInQueueServiceMock mock = new UserInQueueServiceMock(); KnownUser.setUserInQueueService(mock); // Act RequestValidationResult result = KnownUser.validateRequestByIntegrationConfig("http://test.com?event1=true", - "queueitToken", customerIntegration, "customerId", requestMock, null, "secretKey"); + "queueitToken", customerIntegration, "customerId", wrappedRequest, null, "secretKey"); // Assert assertEquals(1, mock.getIgnoreActionResultCalls.size()); @@ -1240,7 +1249,7 @@ public void validateRequestByIntegrationConfigCancelAction() throws Exception { // Act RequestValidationResult result = KnownUser.validateRequestByIntegrationConfig("http://test.com?event1=true", - "queueitToken", customerIntegration, "customerId", new HttpServletRequestMock(), null, "secretKey"); + "queueitToken", customerIntegration, "customerId", new KnownUserRequestWrapper(new HttpServletRequestMock()), null, "secretKey"); // Assert assertEquals("http://test.com?event1=true", mock.validateCancelRequestCalls.get(0).get(0)); @@ -1279,13 +1288,14 @@ public void validateRequestByIntegrationConfigAjaxCallCancelAction() throws Exce HttpServletRequestMock requestMock = new HttpServletRequestMock(); requestMock.Headers.put("x-queueit-ajaxpageurl", "http%3A%2F%2Furl"); + KnownUserRequestWrapper wrappedRequest = new KnownUserRequestWrapper(requestMock); UserInQueueServiceMock mock = new UserInQueueServiceMock(); KnownUser.setUserInQueueService(mock); // Act RequestValidationResult result = KnownUser.validateRequestByIntegrationConfig("http://test.com?event1=true", - "queueitToken", customerIntegration, "customerId", requestMock, null, "secretKey"); + "queueitToken", customerIntegration, "customerId", wrappedRequest, null, "secretKey"); // Assert assertEquals("http://url", mock.validateCancelRequestCalls.get(0).get(0)); @@ -1340,6 +1350,7 @@ public void validateRequestByIntegrationConfig_Debug() throws Exception { requestMock.Headers.put("x-forwarded-for", "129.78.138.66, 129.78.64.103"); requestMock.Headers.put("x-forwarded-host", "en.wikipedia.org:8080"); requestMock.Headers.put("x-forwarded-proto", "https"); + KnownUserRequestWrapper wrappedRequest = new KnownUserRequestWrapper(requestMock); HttpServletResponseMock responseMock = new HttpServletResponseMock(); @@ -1350,7 +1361,7 @@ public void validateRequestByIntegrationConfig_Debug() throws Exception { String queueittoken = UserInQueueServiceTest.QueueITTokenGenerator.generateToken(date, "eventId", true, 20, secretKey, "debug"); KnownUser.validateRequestByIntegrationConfig("http://test.com?event1=true", - queueittoken, customerIntegration, "customerId", requestMock, responseMock, secretKey); + queueittoken, customerIntegration, "customerId", wrappedRequest, responseMock, secretKey); // Assert assertEquals(1, responseMock.addedCookies.size()); @@ -1393,6 +1404,7 @@ public void ValidateRequestByIntegrationConfig_Debug_WithoutMatch() throws Excep requestMock.Headers.put("x-forwarded-for", "129.78.138.66, 129.78.64.103"); requestMock.Headers.put("x-forwarded-host", "en.wikipedia.org:8080"); requestMock.Headers.put("x-forwarded-proto", "https"); + KnownUserRequestWrapper wrappedRequest = new KnownUserRequestWrapper(requestMock); // endregion HttpServletResponseMock responseMock = new HttpServletResponseMock(); @@ -1404,7 +1416,7 @@ public void ValidateRequestByIntegrationConfig_Debug_WithoutMatch() throws Excep String queueittoken = UserInQueueServiceTest.QueueITTokenGenerator.generateToken(date, "event1", true, null, secretKey, "debug"); KnownUser.validateRequestByIntegrationConfig("http://test.com?event1=true", - queueittoken, customerIntegration, "customerId", requestMock, responseMock, secretKey); + queueittoken, customerIntegration, "customerId", wrappedRequest, responseMock, secretKey); // Assert assertEquals(1, responseMock.addedCookies.size()); @@ -1464,8 +1476,9 @@ public void ValidateRequestByIntegrationConfig_Exception_NoDebugToken_NoDebugCoo customerIntegration.Version = 3; //endregion - HttpServletRequestMock httpContextMock = new HttpServletRequestMock(); - httpContextMock.UserAgent = "googlebot"; + HttpServletRequestMock requestMock = new HttpServletRequestMock(); + requestMock.UserAgent = "googlebot"; + KnownUserRequestWrapper wrappedRequest = new KnownUserRequestWrapper(requestMock); HttpServletResponseMock responseMock = new HttpServletResponseMock(); @@ -1474,7 +1487,7 @@ public void ValidateRequestByIntegrationConfig_Exception_NoDebugToken_NoDebugCoo // Act try { KnownUser.validateRequestByIntegrationConfig("http://test.com?event1=true", - "queueitToken", customerIntegration, "customerId", httpContextMock, responseMock, "secretKey"); + "queueitToken", customerIntegration, "customerId", wrappedRequest, responseMock, "secretKey"); } catch (Exception ex) { assertEquals("exception", ex.getMessage()); } @@ -1584,6 +1597,7 @@ public void validateRequestByIntegrationConfig_Debug_NullConfig() throws Excepti requestMock.Headers.put("x-forwarded-for", "129.78.138.66, 129.78.64.103"); requestMock.Headers.put("x-forwarded-host", "en.wikipedia.org:8080"); requestMock.Headers.put("x-forwarded-proto", "https"); + KnownUserRequestWrapper wrappedRequest = new KnownUserRequestWrapper(requestMock); HttpServletResponseMock responseMock = new HttpServletResponseMock(); @@ -1595,7 +1609,7 @@ public void validateRequestByIntegrationConfig_Debug_NullConfig() throws Excepti try { KnownUser.validateRequestByIntegrationConfig("http://test.com?event1=true", queueittoken, null, - "customerId", requestMock, responseMock, secretKey); + "customerId", wrappedRequest, responseMock, secretKey); } catch (Exception ex) { exceptionWasThrown = "customerIntegrationInfo can not be null.".equals(ex.getMessage()); } @@ -1623,6 +1637,7 @@ public void ValidateRequestByIntegrationConfig_Debug_Missing_CustomerId() throws HttpServletResponseMock responseMock = new HttpServletResponseMock(); HttpServletRequestMock requestMock = new HttpServletRequestMock(); + KnownUserRequestWrapper wrappedRequest = new KnownUserRequestWrapper(requestMock); UserInQueueServiceMock mock = new UserInQueueServiceMock(); KnownUser.setUserInQueueService(mock); @@ -1637,7 +1652,7 @@ public void ValidateRequestByIntegrationConfig_Debug_Missing_CustomerId() throws String queueittoken = UserInQueueServiceTest.QueueITTokenGenerator.generateToken(date, "event1", true, null, secretKey, "debug"); RequestValidationResult result = KnownUser.validateRequestByIntegrationConfig("http://test.com?event1=true", - queueittoken, customerIntegration, null, requestMock, responseMock, secretKey); + queueittoken, customerIntegration, null, wrappedRequest, responseMock, secretKey); // Assert assertEquals("https://api2.queue-it.net/diagnostics/connector/error/?code=setup", result.getRedirectUrl()); @@ -1651,6 +1666,7 @@ public void ValidateRequestByIntegrationConfig_Debug_Missing_Secretkey() throws HttpServletResponseMock responseMock = new HttpServletResponseMock(); HttpServletRequestMock requestMock = new HttpServletRequestMock(); + KnownUserRequestWrapper wrappedRequest = new KnownUserRequestWrapper(requestMock); UserInQueueServiceMock mock = new UserInQueueServiceMock(); KnownUser.setUserInQueueService(mock); @@ -1665,7 +1681,7 @@ public void ValidateRequestByIntegrationConfig_Debug_Missing_Secretkey() throws String queueittoken = UserInQueueServiceTest.QueueITTokenGenerator.generateToken(date, "event1", true, null, secretKey, "debug"); RequestValidationResult result = KnownUser.validateRequestByIntegrationConfig("http://test.com?event1=true", - queueittoken, customerIntegration, "customerId", requestMock, responseMock, null); + queueittoken, customerIntegration, "customerId", wrappedRequest, responseMock, null); // Assert assertEquals("https://api2.queue-it.net/diagnostics/connector/error/?code=setup", result.getRedirectUrl()); @@ -1680,6 +1696,7 @@ public void ValidateRequestByIntegrationConfig_Debug_ExpiredToken() throws Excep HttpServletResponseMock responseMock = new HttpServletResponseMock(); HttpServletRequestMock requestMock = new HttpServletRequestMock(); + KnownUserRequestWrapper wrappedRequest = new KnownUserRequestWrapper(requestMock); UserInQueueServiceMock mock = new UserInQueueServiceMock(); KnownUser.setUserInQueueService(mock); @@ -1694,7 +1711,7 @@ public void ValidateRequestByIntegrationConfig_Debug_ExpiredToken() throws Excep String queueittoken = UserInQueueServiceTest.QueueITTokenGenerator.generateToken(date, "event1", true, null, secretKey, "debug"); RequestValidationResult result = KnownUser.validateRequestByIntegrationConfig("http://test.com?event1=true", - queueittoken, customerIntegration, "customerId", requestMock, responseMock, secretKey); + queueittoken, customerIntegration, "customerId", wrappedRequest, responseMock, secretKey); // Assert assertEquals("https://customerId.api2.queue-it.net/customerId/diagnostics/connector/error/?code=timestamp", result.getRedirectUrl()); @@ -1708,6 +1725,7 @@ public void ValidateRequestByIntegrationConfig_Debug_ModifiedToken() throws Exce HttpServletResponseMock responseMock = new HttpServletResponseMock(); HttpServletRequestMock requestMock = new HttpServletRequestMock(); + KnownUserRequestWrapper wrappedRequest = new KnownUserRequestWrapper(requestMock); UserInQueueServiceMock mock = new UserInQueueServiceMock(); KnownUser.setUserInQueueService(mock); @@ -1722,7 +1740,7 @@ public void ValidateRequestByIntegrationConfig_Debug_ModifiedToken() throws Exce String queueittoken = UserInQueueServiceTest.QueueITTokenGenerator.generateToken(date, "event1", true, null, secretKey, "debug") + "invalid-hash"; RequestValidationResult result = KnownUser.validateRequestByIntegrationConfig("http://test.com?event1=true", - queueittoken, customerIntegration, "customerId", requestMock, responseMock, secretKey); + queueittoken, customerIntegration, "customerId", wrappedRequest, responseMock, secretKey); // Assert assertEquals("https://customerId.api2.queue-it.net/customerId/diagnostics/connector/error/?code=hash", result.getRedirectUrl()); @@ -1753,6 +1771,8 @@ public void ResolveQueueRequestByLocalConfig_Debug() throws Exception { eventConfig.setCulture("culture"); eventConfig.setEventId("eventId"); eventConfig.setQueueDomain("queueDomain"); + eventConfig.setIsCookieHttpOnly(true); + eventConfig.setIsCookieSecure(false); eventConfig.setExtendCookieValidity(true); eventConfig.setCookieValidityMinute(10); eventConfig.setVersion(12); @@ -1775,7 +1795,7 @@ public void ResolveQueueRequestByLocalConfig_Debug() throws Exception { decodedCookieValue.contains("OriginalUrl=http://test.com/?event1=true&queueittoken=queueittokenvalue")); assertTrue(decodedCookieValue.contains("TargetUrl=http://test.com?event1=true")); assertTrue(decodedCookieValue.contains( - "QueueConfig=EventId:eventId&Version:12&QueueDomain:queueDomain&CookieDomain:cookieDomain&ExtendCookieValidity:true&CookieValidityMinute:10&LayoutName:layoutName&Culture:culture&ActionName:" + "QueueConfig=EventId:eventId&Version:12&QueueDomain:queueDomain&CookieDomain:cookieDomain&IsCookieHttpOnly:true&IsCookieSecure:false&ExtendCookieValidity:true&CookieValidityMinute:10&LayoutName:layoutName&Culture:culture&ActionName:" + eventConfig.getActionName())); assertTrue(decodedCookieValue.contains("SdkVersion=" + UserInQueueService.SDK_VERSION)); assertTrue(decodedCookieValue.contains("Runtime=" + GetRuntimeVersion())); @@ -2009,6 +2029,8 @@ public void CancelRequestByLocalConfig_Debug() throws Exception { cancelEventConfig.setQueueDomain("queuedomain"); cancelEventConfig.setVersion(12); cancelEventConfig.setActionName("cancelAction"); + cancelEventConfig.setIsCookieHttpOnly(true); + cancelEventConfig.setIsCookieSecure(false); // Act String secretKey = "secretkey"; @@ -2027,7 +2049,7 @@ public void CancelRequestByLocalConfig_Debug() throws Exception { decodedCookieValue.contains("OriginalUrl=http://test.com/?event1=true&queueittoken=queueittokenvalue")); assertTrue(decodedCookieValue.contains("TargetUrl=http://test.com?event1=true")); - String configvalues = "CancelConfig=EventId:eventId&Version:12&QueueDomain:queuedomain&CookieDomain:cookiedomain&ActionName:" + String configvalues = "CancelConfig=EventId:eventId&Version:12&QueueDomain:queuedomain&CookieDomain:cookiedomain&IsCookieHttpOnly:true&IsCookieSecure:false&ActionName:" + cancelEventConfig.getActionName(); assertTrue(decodedCookieValue.contains(configvalues)); } @@ -2229,7 +2251,7 @@ static class HttpServletRequestMock implements HttpServletRequest { public HashMap Headers; public HttpServletRequestMock() { - this.Headers = new HashMap<>(); + this.Headers = new HashMap(); } @Override @@ -2575,7 +2597,7 @@ public DispatcherType getDispatcherType() { static class HttpServletResponseMock implements HttpServletResponse { - ArrayList addedCookies = new ArrayList<>(); + ArrayList addedCookies = new ArrayList(); @Override public void addCookie(Cookie cookie) { diff --git a/SDK/src/test/java/com/queue_it/connector/UserInQueueServiceTest.java b/SDK/src/test/java/com/queue_it/connector/UserInQueueServiceTest.java index e057ca9..aff44db 100644 --- a/SDK/src/test/java/com/queue_it/connector/UserInQueueServiceTest.java +++ b/SDK/src/test/java/com/queue_it/connector/UserInQueueServiceTest.java @@ -26,19 +26,19 @@ public void validateQueueRequest_ValidState_ExtendableCookie_NoCookieExtensionFr config.setExtendCookieValidity(false); config.setActionName("QueueAction"); - final HashMap conditions = new HashMap<>(); + final HashMap conditions = new HashMap(); conditions.put("isStoreWasCalled", false); IUserInQueueStateRepository cookieProviderMock = new IUserInQueueStateRepository() { @Override - public void cancelQueueCookie(String eventId, String cookieDomain) { + public void cancelQueueCookie(String eventId, String cookieDomain, Boolean isCookieHttpOnly, Boolean isCookieSecure) { throw new UnsupportedOperationException("Not supported yet."); } @Override public void store(String eventId, String queueId, Integer fixedCookieValidityMinutes, - String cookieDomainString, String redirectType, String customerSecretKey) { + String cookieDomainString, Boolean isCookieHttpOnly, Boolean isCookieSecure, String redirectType, String customerSecretKey) { conditions.put("isStoreWasCalled", true); } @@ -50,7 +50,7 @@ public StateInfo getState(String eventId, int cookieValidityMinutes, String cust @Override public void reissueQueueCookie(String eventId, int cookieValidityMinutes, String cookieDomain, - String secretKey) { + Boolean isCookieHttpOnly, Boolean isCookieSecure, String secretKey) { throw new UnsupportedOperationException("Not supported yet."); } }; @@ -76,20 +76,22 @@ public void validateQueueRequest_ValidState_ExtendableCookie_CookieExtensionFrom config.setExtendCookieValidity(true); config.setCookieDomain(".testdomain.com"); config.setActionName("QueueAction"); - final HashMap> callInfo = new HashMap<>(); - callInfo.put("firstCall", new HashMap<>()); + final HashMap> callInfo = new HashMap>(); + callInfo.put("firstCall", new HashMap()); IUserInQueueStateRepository cookieProviderMock = new IUserInQueueStateRepository() { @Override public void store(String eventId, String queueId, Integer fixedCookieValidityMinutes, String cookieDomain, - String redirectType, String customerSecretKey) { - HashMap info = new HashMap<>(); + Boolean isCookieHttpOnly, Boolean isCookieSecure, String redirectType, String customerSecretKey) { + HashMap info = new HashMap(); info.put("eventId", eventId); info.put("fixedCookieValidityMinutes", fixedCookieValidityMinutes); info.put("redirectType", redirectType); info.put("cookieDomain", cookieDomain); info.put("queueId", queueId); info.put("customerSecretKey", customerSecretKey); + info.put("isCookieHttpOnly", isCookieHttpOnly); + info.put("isCookieSecure", isCookieSecure); callInfo.put("firstCall", info); } @@ -101,13 +103,13 @@ public StateInfo getState(String eventId, int cookieValidityMinutes, String cust } @Override - public void cancelQueueCookie(String eventId, String cookieDomain) { + public void cancelQueueCookie(String eventId, String cookieDomain, Boolean isCookieHttpOnly, Boolean isCookieSecure) { throw new UnsupportedOperationException("Not supported yet."); } @Override public void reissueQueueCookie(String eventId, int cookieValidityMinutes, String cookieDomain, - String secretKey) { + Boolean isCookieHttpOnly, Boolean isCookieSecure, String secretKey) { throw new UnsupportedOperationException("Not supported yet."); } }; @@ -135,14 +137,14 @@ public void validateQueueRequest_ValidState_NoExtendableCookie_DoNotRedirect_DoN config.setExtendCookieValidity(true); config.setActionName("QueueAction"); - final HashMap conditions = new HashMap<>(); + final HashMap conditions = new HashMap(); conditions.put("isStoreWasCalled", false); IUserInQueueStateRepository cookieProviderMock = new IUserInQueueStateRepository() { @Override public void store(String eventId, String queueId, Integer fixedCookieValidityMinutes, String cookieDomain, - String redirectType, String customerSecretKey) { + Boolean isCookieHttpOnly, Boolean isCookieSecure, String redirectType, String customerSecretKey) { conditions.put("isStoreWasCalled", true); } @@ -153,13 +155,13 @@ public StateInfo getState(String eventId, int cookieValidityMinutes, String cust } @Override - public void cancelQueueCookie(String eventId, String cookieDomain) { + public void cancelQueueCookie(String eventId, String cookieDomain, Boolean isCookieHttpOnly, Boolean isCookieSecure) { throw new UnsupportedOperationException("Not supported yet."); } @Override public void reissueQueueCookie(String eventId, int cookieValidityMinutes, String cookieDomain, - String secretKey) { + Boolean isCookieHttpOnly, Boolean isCookieSecure, String secretKey) { throw new UnsupportedOperationException("Not supported yet."); } }; @@ -185,19 +187,19 @@ public void ValidateQueueRequest_NoCookie_TampredToken_RedirectToErrorPageWithHa config.setActionName("QueueAction"); config.setCookieDomain("TestDomain"); - final HashMap conditions = new HashMap<>(); + final HashMap conditions = new HashMap(); conditions.put("isStoreWasCalled", false); IUserInQueueStateRepository cookieProviderMock = new IUserInQueueStateRepository() { @Override - public void cancelQueueCookie(String eventId, String cookieDomain) { + public void cancelQueueCookie(String eventId, String cookieDomain, Boolean isCookieHttpOnly, Boolean isCookieSecure) { conditions.put("cancelQueueCookieWasCalled", true); } @Override public void store(String eventId, String queueId, Integer fixedCookieValidityMinutes, String cookieDomain, - String redirectType, String customerSecretKey) { + Boolean isCookieHttpOnly, Boolean isCookieSecure, String redirectType, String customerSecretKey) { conditions.put("isStoreWasCalled", true); } @@ -209,7 +211,7 @@ public StateInfo getState(String eventId, int cookieValidityMinutes, String cust @Override public void reissueQueueCookie(String eventId, int cookieValidityMinutes, String cookieDomain, - String secretKey) { + Boolean isCookieHttpOnly, Boolean isCookieSecure, String secretKey) { throw new UnsupportedOperationException("Not supported yet."); } }; @@ -257,19 +259,19 @@ public void ValidateQueueRequest_NoCookie_ExpiredTimeStampInToken_RedirectToErro config.setCookieDomain("testDomain"); String customerKey = "4e1db821-a825-49da-acd0-5d376f2068db"; - final HashMap conditions = new HashMap<>(); + final HashMap conditions = new HashMap(); conditions.put("isStoreWasCalled", false); IUserInQueueStateRepository cookieProviderMock = new IUserInQueueStateRepository() { @Override - public void cancelQueueCookie(String eventId, String cookieDomain) { + public void cancelQueueCookie(String eventId, String cookieDomain, Boolean isCookieHttpOnly, Boolean isCookieSecure) { conditions.put("cancelQueueCookie", true); } @Override public void store(String eventId, String queueId, Integer fixedCookieValidityMinutes, String cookieDomain, - String redirectType, String customerSecretKey) { + Boolean isCookieHttpOnly, Boolean isCookieSecure, String redirectType, String customerSecretKey) { conditions.put("isStoreWasCalled", true); } @@ -281,7 +283,7 @@ public StateInfo getState(String eventId, int cookieValidityMinutes, String cust @Override public void reissueQueueCookie(String eventId, int cookieValidityMinutes, String cookieDomain, - String secretKey) { + Boolean isCookieHttpOnly, Boolean isCookieSecure, String secretKey) { throw new UnsupportedOperationException("Not supported yet."); } }; @@ -329,19 +331,19 @@ public void ValidateQueueRequest_NoCookie_EventIdMismatch_RedirectToErrorPageWit config.setCookieDomain("testDomain"); String customerKey = "4e1db821-a825-49da-acd0-5d376f2068db"; - final HashMap conditions = new HashMap<>(); + final HashMap conditions = new HashMap(); conditions.put("isStoreWasCalled", false); IUserInQueueStateRepository cookieProviderMock = new IUserInQueueStateRepository() { @Override - public void cancelQueueCookie(String eventId, String cookieDomain) { + public void cancelQueueCookie(String eventId, String cookieDomain, Boolean isCookieHttpOnly, Boolean isCookieSecure) { conditions.put("cancelQueueCookie", true); } @Override public void store(String eventId, String queueId, Integer fixedCookieValidityMinutes, String cookieDomain, - String redirectType, String customerSecretKey) { + Boolean isCookieHttpOnly, Boolean isCookieSecure, String redirectType, String customerSecretKey) { conditions.put("isStoreWasCalled", true); } @@ -353,7 +355,7 @@ public StateInfo getState(String eventId, int cookieValidityMinutes, String cust @Override public void reissueQueueCookie(String eventId, int cookieValidityMinutes, String cookieDomain, - String secretKey) { + Boolean isCookieHttpOnly, Boolean isCookieSecure, String secretKey) { throw new UnsupportedOperationException("Not supported yet."); } }; @@ -399,19 +401,21 @@ public void ValidateQueueRequest_NoCookie_ValidToken_ExtendableCookie_DoNotRedir config.setActionName("QueueAction"); String customerKey = "4e1db821-a825-49da-acd0-5d376f2068db"; - final HashMap> callInfo = new HashMap<>(); - callInfo.put("firstCall", new HashMap<>()); + final HashMap> callInfo = new HashMap>(); + callInfo.put("firstCall", new HashMap()); IUserInQueueStateRepository cookieProviderMock = new IUserInQueueStateRepository() { @Override public void store(String eventId, String queueId, Integer fixedCookieValidityMinutes, String cookieDomain, - String redirectType, String customerSecretKey) { - HashMap info = new HashMap<>(); + Boolean isCookieHttpOnly, Boolean isCookieSecure, String redirectType, String customerSecretKey) { + HashMap info = new HashMap(); info.put("eventId", eventId); info.put("fixedCookieValidityMinutes", fixedCookieValidityMinutes); info.put("cookieDomain", cookieDomain); info.put("redirectType", redirectType); info.put("customerSecretKey", customerSecretKey); + info.put("isCookieHttpOnly", isCookieHttpOnly); + info.put("isCookieSecure", isCookieSecure); callInfo.put("firstCall", info); } @@ -422,16 +426,18 @@ public StateInfo getState(String eventId, int cookieValidityMinutes, String cust } @Override - public void cancelQueueCookie(String eventId, String cookieDomain) { - HashMap obj = new HashMap<>(); + public void cancelQueueCookie(String eventId, String cookieDomain, Boolean isCookieHttpOnly, Boolean isCookieSecure) { + HashMap obj = new HashMap(); obj.put("eventId", eventId); obj.put("cookieDomain", cookieDomain); + obj.put("isCookieHttpOnly", isCookieHttpOnly); + obj.put("isCookieSecure", isCookieSecure); callInfo.put("cancelQueueCookieWasCalled", obj); } @Override public void reissueQueueCookie(String eventId, int cookieValidityMinutes, String cookieDomain, - String secretKey) { + Boolean isCookieHttpOnly, Boolean isCookieSecure, String secretKey) { throw new UnsupportedOperationException("Not supported yet."); } }; @@ -466,19 +472,21 @@ public void ValidateQueueRequest_NoCookie_ValidToken_CookieValidityMinuteFromTok config.setCookieDomain("testDomain"); String customerKey = "secretekeyofuser"; - final HashMap> callInfo = new HashMap<>(); - callInfo.put("firstCall", new HashMap<>()); + final HashMap> callInfo = new HashMap>(); + callInfo.put("firstCall", new HashMap()); IUserInQueueStateRepository cookieProviderMock = new IUserInQueueStateRepository() { @Override public void store(String eventId, String queueId, Integer fixedCookieValidityMinutes, String cookieDomain, - String redirectType, String customerSecretKey) { - HashMap info = new HashMap<>(); + Boolean isCookieHttpOnly, Boolean isCookieSecure, String redirectType, String customerSecretKey) { + HashMap info = new HashMap(); info.put("eventId", eventId); info.put("fixedCookieValidityMinutes", fixedCookieValidityMinutes); info.put("cookieDomain", cookieDomain); info.put("redirectType", redirectType); info.put("customerSecretKey", customerSecretKey); + info.put("isCookieHttpOnly", isCookieHttpOnly); + info.put("isCookieSecure", isCookieSecure); callInfo.put("firstCall", info); } @@ -489,16 +497,18 @@ public StateInfo getState(String eventId, int cookieValidityMinutes, String cust } @Override - public void cancelQueueCookie(String eventId, String cookieDomain) { - HashMap obj = new HashMap<>(); + public void cancelQueueCookie(String eventId, String cookieDomain, Boolean isCookieHttpOnly, Boolean isCookieSecure) { + HashMap obj = new HashMap(); obj.put("eventId", eventId); obj.put("cookieDomain", cookieDomain); + obj.put("isCookieHttpOnly", isCookieHttpOnly); + obj.put("isCookieSecure", isCookieSecure); callInfo.put("cancelQueueCookieWasCalled", obj); } @Override public void reissueQueueCookie(String eventId, int cookieValidityMinutes, String cookieDomain, - String secretKey) { + Boolean isCookieHttpOnly, Boolean isCookieSecure, String secretKey) { throw new UnsupportedOperationException("Not supported yet."); } }; @@ -531,19 +541,19 @@ public void ValidateQueueRequest_NoCookie_WithoutToken_RedirectToQueue() throws config.setActionName("QueueAction"); config.setQueueDomain("testDomain.com"); - final HashMap conditions = new HashMap<>(); + final HashMap conditions = new HashMap(); conditions.put("isStoreWasCalled", false); IUserInQueueStateRepository cookieProviderMock = new IUserInQueueStateRepository() { @Override - public void cancelQueueCookie(String eventId, String cookieDomain) { + public void cancelQueueCookie(String eventId, String cookieDomain, Boolean isCookieHttpOnly, Boolean isCookieSecure) { conditions.put("cancelQueueCookie", true); } @Override public void store(String eventId, String queueId, Integer fixedCookieValidityMinutes, String cookieDomain, - String redirectType, String customerSecretKey) { + Boolean isCookieHttpOnly, Boolean isCookieSecure, String redirectType, String customerSecretKey) { conditions.put("isStoreWasCalled", true); } @@ -555,7 +565,7 @@ public StateInfo getState(String eventId, int cookieValidityMinutes, String cust @Override public void reissueQueueCookie(String eventId, int cookieValidityMinutes, String cookieDomain, - String secretKey) { + Boolean isCookieHttpOnly, Boolean isCookieSecure, String secretKey) { throw new UnsupportedOperationException("Not supported yet."); } }; @@ -588,19 +598,19 @@ public void ValidateQueueRequest_NoCookie_WithoutToken_RedirectToQueue_NoTargetU config.setActionName("QueueAction"); config.setCookieDomain("testDomain"); - final HashMap conditions = new HashMap<>(); + final HashMap conditions = new HashMap(); conditions.put("isStoreWasCalled", false); IUserInQueueStateRepository cookieProviderMock = new IUserInQueueStateRepository() { @Override - public void cancelQueueCookie(String eventId, String cookieDomain) { + public void cancelQueueCookie(String eventId, String cookieDomain, Boolean isCookieHttpOnly, Boolean isCookieSecure) { conditions.put("cancelQueueCookie", true); } @Override public void store(String eventId, String queueId, Integer fixedCookieValidityMinutes, String cookieDomain, - String redirectType, String customerSecretKey) { + Boolean isCookieHttpOnly, Boolean isCookieSecure, String redirectType, String customerSecretKey) { conditions.put("isStoreWasCalled", true); } @@ -612,7 +622,7 @@ public StateInfo getState(String eventId, int cookieValidityMinutes, String cust @Override public void reissueQueueCookie(String eventId, int cookieValidityMinutes, String cookieDomain, - String secretKey) { + Boolean isCookieHttpOnly, Boolean isCookieSecure, String secretKey) { throw new UnsupportedOperationException("Not supported yet."); } }; @@ -643,19 +653,19 @@ public void ValidateQueueRequest_InvalidCookie_WithoutToken_RedirectToQueue_NoTa config.setActionName("QueueAction"); config.setCookieDomain("testDomain"); - final HashMap conditions = new HashMap<>(); + final HashMap conditions = new HashMap(); conditions.put("isStoreWasCalled", false); IUserInQueueStateRepository cookieProviderMock = new IUserInQueueStateRepository() { @Override - public void cancelQueueCookie(String eventId, String cookieDomain) { + public void cancelQueueCookie(String eventId, String cookieDomain, Boolean isCookieHttpOnly, Boolean isCookieSecure) { conditions.put("cancelQueueCookie", true); } @Override public void store(String eventId, String queueId, Integer fixedCookieValidityMinutes, String cookieDomain, - String redirectType, String customerSecretKey) { + Boolean isCookieHttpOnly, Boolean isCookieSecure, String redirectType, String customerSecretKey) { conditions.put("isStoreWasCalled", true); } @@ -667,7 +677,7 @@ public StateInfo getState(String eventId, int cookieValidityMinutes, String cust @Override public void reissueQueueCookie(String eventId, int cookieValidityMinutes, String cookieDomain, - String secretKey) { + Boolean isCookieHttpOnly, Boolean isCookieSecure, String secretKey) { throw new UnsupportedOperationException("Not supported yet."); } }; @@ -698,19 +708,19 @@ public void ValidateQueueRequest_NoCookie_InValidToken() throws Exception { config.setActionName("QueueAction"); config.setCookieDomain("testDomain"); - final HashMap conditions = new HashMap<>(); + final HashMap conditions = new HashMap(); conditions.put("isStoreWasCalled", false); IUserInQueueStateRepository cookieProviderMock = new IUserInQueueStateRepository() { @Override - public void cancelQueueCookie(String eventId, String cookieDomain) { + public void cancelQueueCookie(String eventId, String cookieDomain, Boolean isCookieHttpOnly, Boolean isCookieSecure) { conditions.put("cancelQueueCookie", true); } @Override public void store(String eventId, String queueId, Integer fixedCookieValidityMinutes, String cookieDomain, - String redirectType, String customerSecretKey) { + Boolean isCookieHttpOnly, Boolean isCookieSecure, String redirectType, String customerSecretKey) { conditions.put("isStoreWasCalled", true); } @@ -722,7 +732,7 @@ public StateInfo getState(String eventId, int cookieValidityMinutes, String cust @Override public void reissueQueueCookie(String eventId, int cookieValidityMinutes, String cookieDomain, - String secretKey) { + Boolean isCookieHttpOnly, Boolean isCookieSecure, String secretKey) { throw new UnsupportedOperationException("Not supported yet."); } }; @@ -754,19 +764,19 @@ public void ValidateRequest_InvalidCookie_InValidToken() throws Exception { config.setActionName("QueueAction"); config.setCookieDomain("testDomain"); - final HashMap conditions = new HashMap<>(); + final HashMap conditions = new HashMap(); conditions.put("isStoreWasCalled", false); IUserInQueueStateRepository cookieProviderMock = new IUserInQueueStateRepository() { @Override - public void cancelQueueCookie(String eventId, String cookieDomain) { + public void cancelQueueCookie(String eventId, String cookieDomain, Boolean isCookieHttpOnly, Boolean isCookieSecure) { conditions.put("cancelQueueCookie", true); } @Override public void store(String eventId, String queueId, Integer fixedCookieValidityMinutes, String cookieDomain, - String redirectType, String customerSecretKey) { + Boolean isCookieHttpOnly, Boolean isCookieSecure, String redirectType, String customerSecretKey) { conditions.put("isStoreWasCalled", true); } @@ -778,7 +788,7 @@ public StateInfo getState(String eventId, int cookieValidityMinutes, String cust @Override public void reissueQueueCookie(String eventId, int cookieValidityMinutes, String cookieDomain, - String secretKey) { + Boolean isCookieHttpOnly, Boolean isCookieSecure, String secretKey) { throw new UnsupportedOperationException("Not supported yet."); } }; @@ -800,6 +810,7 @@ public void reissueQueueCookie(String eventId, int cookieValidityMinutes, String @Test public void validateCancelRequest() throws Exception { + String QueueId = "queueId"; CancelEventConfig config = new CancelEventConfig(); config.setEventId("e1"); config.setQueueDomain("testDomain.com"); @@ -807,13 +818,13 @@ public void validateCancelRequest() throws Exception { config.setVersion(10); config.setActionName("Queue Action (._~-) &!*|'\""); - final HashMap conditions = new HashMap<>(); + final HashMap conditions = new HashMap(); IUserInQueueStateRepository cookieProviderMock = new IUserInQueueStateRepository() { @Override public void store(String eventId, String queueId, Integer fixedCookieValidityMinutes, String cookieDomain, - String redirectType, String customerSecretKey) { + Boolean isCookieHttpOnly, Boolean isCookieSecure, String redirectType, String customerSecretKey) { } @Override @@ -828,18 +839,18 @@ public StateInfo getState(String eventId, int cookieValidityMinutes, String cust @Override public void reissueQueueCookie(String eventId, int cookieValidityMinutes, String cookieDomain, - String secretKey) { + Boolean isCookieHttpOnly, Boolean isCookieSecure, String secretKey) { throw new UnsupportedOperationException("Not supported yet."); } @Override - public void cancelQueueCookie(String eventId, String cookieDomain) { + public void cancelQueueCookie(String eventId, String cookieDomain, Boolean isCookieHttpOnly, Boolean isCookieSecure) { conditions.put("cancelQueueCookieWasCalled", "eventId:" + eventId + ",cookieDomain:" + cookieDomain); } }; String knownUserVersion = UserInQueueService.SDK_VERSION; String expectedMan = "Queue%20Action%20%28._%7E-%29%20%26%21%2A%7C%27%22"; - String expectedUrl = "https://testDomain.com/cancel/testCustomer/e1/?c=testCustomer&e=e1" + "&ver=" + String expectedUrl = "https://testDomain.com/cancel/testCustomer/e1/" + QueueId + "?c=testCustomer&e=e1" + "&ver=" + knownUserVersion + "&cver=10" + "&man=" + expectedMan + "&r=url"; UserInQueueService testObject = new UserInQueueService(cookieProviderMock); @@ -847,7 +858,7 @@ public void cancelQueueCookie(String eventId, String cookieDomain) { assertEquals("eventId:e1,cookieDomain:testdomain", conditions.get("cancelQueueCookieWasCalled")); assertTrue(result.doRedirect()); - assertEquals("queueId", result.getQueueId()); + assertEquals(QueueId, result.getQueueId()); String expUrl = expectedUrl.toLowerCase(); String rdrUrl = result.getRedirectUrl().toLowerCase(); assertEquals(expUrl, rdrUrl); @@ -872,7 +883,7 @@ public static class QueueITTokenGenerator { public static String generateToken(Date timeStamp, String eventId, boolean extendableCookie, Integer cookieValidityMinute, String secretKey, String redirectType) { - ArrayList paramList = new ArrayList<>(); + ArrayList paramList = new ArrayList(); paramList.add(QueueParameterHelper.TimeStampKey + QueueParameterHelper.KeyValueSeparatorChar + GetUnixTimestamp(timeStamp)); diff --git a/SDK/src/test/java/com/queue_it/connector/UserInQueueStateCookieRepositoryTest.java b/SDK/src/test/java/com/queue_it/connector/UserInQueueStateCookieRepositoryTest.java index c3e57ab..bb3d47f 100644 --- a/SDK/src/test/java/com/queue_it/connector/UserInQueueStateCookieRepositoryTest.java +++ b/SDK/src/test/java/com/queue_it/connector/UserInQueueStateCookieRepositoryTest.java @@ -16,27 +16,29 @@ public void store_getState_ExtendableCookie_CookieIsSaved() throws Exception { String queueId = "528f01d4-30f9-4753-95b3-2c8c33966abc"; String cookieKey = UserInQueueStateCookieRepository.getCookieKey(eventId); int cookieValidity = 10; - final HashMap> cookies = new HashMap<>(); - cookies.put(cookieKey, new HashMap<>()); + final HashMap> cookies = new HashMap>(); + cookies.put(cookieKey, new HashMap()); ICookieManager cookieManager = new ICookieManager() { @Override - public void setCookie(String cookieName, String cookieValue, Integer expiration, String cookieDomain) { - HashMap cookie = cookies.get(cookieName); - cookie.put("cookieValue", cookieValue); + public void setCookie(String name, String value, Integer expiration, String domain, Boolean isHttpOnly, Boolean isSecure) { + HashMap cookie = cookies.get(name); + cookie.put("cookieValue", value); cookie.put("expiration", expiration); - cookie.put("cookieDomain", cookieDomain); + cookie.put("cookieDomain", domain); + cookie.put("isCookieHttpOnly", isHttpOnly); + cookie.put("isCookieSecure", isSecure); } @Override - public String getCookie(String cookieName) { - return String.valueOf(cookies.get(cookieName).get("cookieValue")); + public String getCookie(String name) { + return String.valueOf(cookies.get(name).get("cookieValue")); } }; UserInQueueStateCookieRepository testObject = new UserInQueueStateCookieRepository(cookieManager); - testObject.store(eventId, queueId, null, cookieDomain, "Queue", secretKey); + testObject.store(eventId, queueId, null, cookieDomain, true, false, "Queue", secretKey); StateInfo state = testObject.getState(eventId, cookieValidity, secretKey, true); assertTrue(state.isValid()); @@ -47,6 +49,8 @@ public String getCookie(String cookieName) { assertTrue(Math.abs(System.currentTimeMillis() / 1000L - issueTime) < 2); assertEquals(Integer.parseInt(cookies.get(cookieKey).get("expiration").toString()), 24 * 60 * 60); assertEquals(cookies.get(cookieKey).get("cookieDomain"), cookieDomain); + assertEquals(cookies.get(cookieKey).get("isCookieHttpOnly"), true); + assertEquals(cookies.get(cookieKey).get("isCookieSecure"), false); } @Test @@ -57,23 +61,23 @@ public void store_getState_TamperedCookie_StateIsNotValid() throws Exception { String queueId = "528f01d4-30f9-4753-95b3-2c8c33966abc"; String cookieKey = UserInQueueStateCookieRepository.getCookieKey(eventId); int cookieValidity = 10; - final HashMap cookies = new HashMap<>(); + final HashMap cookies = new HashMap(); ICookieManager cookieManager = new ICookieManager() { @Override - public void setCookie(String cookieName, String cookieValue, Integer expiration, String cookieDomain) { - cookies.put(cookieName, cookieValue); + public void setCookie(String name, String value, Integer expiration, String domain, Boolean isHttpOnly, Boolean isSecure) { + cookies.put(name, value); } @Override - public String getCookie(String cookieName) { - return cookies.get(cookieName); + public String getCookie(String name) { + return cookies.get(name); } }; UserInQueueStateCookieRepository testObject = new UserInQueueStateCookieRepository(cookieManager); - testObject.store(eventId, queueId, cookieValidity, cookieDomain, "Queue", secretKey); + testObject.store(eventId, queueId, cookieValidity, cookieDomain, true, true, "Queue", secretKey); StateInfo state = testObject.getState(eventId, 10, secretKey, true); assertTrue(state.isValid()); @@ -91,22 +95,22 @@ public void store_getState_ExpiredCookie_StateIsNotValid_Queue() throws Exceptio String secretKey = "4e1db821-a825-49da-acd0-5d376f2068db"; String cookieDomain = ".test.com"; String queueId = "528f01d4-30f9-4753-95b3-2c8c33966abc"; - final HashMap cookies = new HashMap<>(); + final HashMap cookies = new HashMap(); ICookieManager cookieManager = new ICookieManager() { @Override - public void setCookie(String cookieName, String cookieValue, Integer expiration, String cookieDomain) { - cookies.put(cookieName, cookieValue); + public void setCookie(String name, String value, Integer expiration, String domain, Boolean isHttpOnly, Boolean isSecure) { + cookies.put(name, value); } @Override - public String getCookie(String cookieName) { - return cookies.get(cookieName); + public String getCookie(String name) { + return cookies.get(name); } }; UserInQueueStateCookieRepository testObject = new UserInQueueStateCookieRepository(cookieManager); - testObject.store(eventId, queueId, null, cookieDomain, "Queue", secretKey); + testObject.store(eventId, queueId, null, cookieDomain, true, true, "Queue", secretKey); StateInfo state = testObject.getState(eventId, -1, secretKey, true); assertFalse(state.isValid()); @@ -118,22 +122,22 @@ public void store_getState_ExpiredCookie_StateIsNotValid_Idle() throws Exception String secretKey = "4e1db821-a825-49da-acd0-5d376f2068db"; String cookieDomain = ".test.com"; String queueId = "528f01d4-30f9-4753-95b3-2c8c33966abc"; - final HashMap cookies = new HashMap<>(); + final HashMap cookies = new HashMap(); ICookieManager cookieManager = new ICookieManager() { @Override - public void setCookie(String cookieName, String cookieValue, Integer expiration, String cookieDomain) { - cookies.put(cookieName, cookieValue); + public void setCookie(String name, String value, Integer expiration, String domain, Boolean isHttpOnly, Boolean isSecure) { + cookies.put(name, value); } @Override - public String getCookie(String cookieName) { - return cookies.get(cookieName); + public String getCookie(String name) { + return cookies.get(name); } }; UserInQueueStateCookieRepository testObject = new UserInQueueStateCookieRepository(cookieManager); - testObject.store(eventId, queueId, -1, cookieDomain, "Idle", secretKey); + testObject.store(eventId, queueId, -1, cookieDomain, true, true, "Idle", secretKey); StateInfo state = testObject.getState(eventId, 10, secretKey, true); assertFalse(state.isValid()); @@ -146,22 +150,22 @@ public void store_getState_DifferentEventId_StateIsNotValid() throws Exception { String cookieDomain = ".test.com"; String queueId = "528f01d4-30f9-4753-95b3-2c8c33966abc"; - final HashMap cookies = new HashMap<>(); + final HashMap cookies = new HashMap(); ICookieManager cookieManager = new ICookieManager() { @Override - public void setCookie(String cookieName, String cookieValue, Integer expiration, String cookieDomain) { - cookies.put(cookieName, cookieValue); + public void setCookie(String name, String value, Integer expiration, String domain, Boolean isHttpOnly, Boolean isSecure) { + cookies.put(name, value); } @Override - public String getCookie(String cookieName) { - return cookies.get(cookieName); + public String getCookie(String name) { + return cookies.get(name); } }; UserInQueueStateCookieRepository testObject = new UserInQueueStateCookieRepository(cookieManager); - testObject.store(eventId, queueId, null, cookieDomain, "Queue", secretKey); + testObject.store(eventId, queueId, null, cookieDomain, true, true, "Queue", secretKey); StateInfo state = testObject.getState(eventId, 10, secretKey, true); assertTrue(state.isValid()); @@ -179,17 +183,17 @@ public void store_getState_InvalidCookie_StateIsNotValid() throws Exception { ICookieManager cookieManager = new ICookieManager() { @Override - public void setCookie(String cookieName, String cookieValue, Integer expiration, String cookieDomain) { + public void setCookie(String name, String value, Integer expiration, String domain, Boolean isHttpOnly, Boolean isSecure) { } @Override - public String getCookie(String cookieName) { + public String getCookie(String name) { return "FixedValidityMins=ooOOO&Expires=|||&QueueId=000&Hash=23232$$$"; } }; UserInQueueStateCookieRepository testObject = new UserInQueueStateCookieRepository(cookieManager); - testObject.store(eventId, queueId, null, cookieDomain, "Queue", secretKey); + testObject.store(eventId, queueId, null, cookieDomain, true, true, "Queue", secretKey); StateInfo state = testObject.getState(eventId, 10, secretKey, true); assertFalse(state.isValid()); } @@ -202,35 +206,37 @@ public void cancelQueueCookie_Test() throws Exception { String cookieDomain = "testDomain"; String cookieKey = UserInQueueStateCookieRepository.getCookieKey(eventId); - final HashMap> cookies = new HashMap<>(); - cookies.put(cookieKey + "1", new HashMap<>()); - cookies.put(cookieKey + "2", new HashMap<>()); + final HashMap> cookies = new HashMap>(); + cookies.put(cookieKey + "1", new HashMap()); + cookies.put(cookieKey + "2", new HashMap()); ICookieManager cookieManager = new ICookieManager() { public int setCookieCallNumber = 0; @Override - public void setCookie(String cookieName, String cookieValue, Integer expiration, String cookieDomain) { + public void setCookie(String name, String value, Integer expiration, String domain, Boolean isHttpOnly, Boolean isSecure) { setCookieCallNumber++; - HashMap cookie = cookies.get(cookieName + setCookieCallNumber); - cookie.put("cookieValue", cookieValue); + HashMap cookie = cookies.get(name + setCookieCallNumber); + cookie.put("cookieValue", value); cookie.put("expiration", expiration); - cookie.put("cookieDomain", cookieDomain); + cookie.put("cookieDomain", domain); + cookie.put("isCookieHttpOnly", isHttpOnly); + cookie.put("isCookieSecure", isSecure); } @Override - public String getCookie(String cookieName) { - return String.valueOf(cookies.get(cookieName + setCookieCallNumber).get("cookieValue")); + public String getCookie(String name) { + return String.valueOf(cookies.get(name + setCookieCallNumber).get("cookieValue")); } }; UserInQueueStateCookieRepository testObject = new UserInQueueStateCookieRepository(cookieManager); - testObject.store(eventId, queueId, -1, "cookieDomain", "Idle", secretKey); + testObject.store(eventId, queueId, -1, "cookieDomain", true, true, "Idle", secretKey); assertTrue(testObject.getState(eventId, 10, secretKey, false).isValid()); - testObject.cancelQueueCookie(eventId, cookieDomain); + testObject.cancelQueueCookie(eventId, cookieDomain, true, true); assertEquals(0, Integer.parseInt(cookies.get(cookieKey + "2").get("expiration").toString())); assertNull(cookies.get(cookieKey + "2").get("cookieValue")); @@ -243,26 +249,28 @@ public void extendQueueCookie_CookieExists_Test() { String eventId = "event1"; String secretKey = "secretKey"; String queueId = "528f01d4-30f9-4753-95b3-2c8c33966abc"; - final HashMap cookie = new HashMap<>(); + final HashMap cookie = new HashMap(); long issueTime = (System.currentTimeMillis() / 1000L - 120); String hash = HashHelper.generateSHA256Hash(secretKey, eventId + queueId + "3" + "idle" + issueTime); - final String cookieValue = "EventId=" + eventId + "&QueueId=" + queueId + "&FixedValidityMins=3&RedirectType=idle&IssueTime=" + issueTime + "&Hash=" + hash; + final String cookieValue = "EventId=" + eventId + "&QueueId=" + queueId + "&FixedValidityMins=3&RedirectType=idle&IssueTime=" + issueTime + "&IsCookieHttpOnly=True&IsCookieSecure=True&Hash=" + hash; ICookieManager cookieManager = new ICookieManager() { boolean isSetCookieCalled = false; @Override - public void setCookie(String cookieName, String cookieValue, Integer expiration, String cookieDomain) { - cookie.put("cookieValue", cookieValue); + public void setCookie(String name, String value, Integer expiration, String domain, Boolean isHttpOnly, Boolean isSecure) { + cookie.put("cookieValue", value); cookie.put("expiration", expiration); - cookie.put("cookieDomain", cookieDomain); + cookie.put("cookieDomain", domain); + cookie.put("isCookieHttpOnly", isHttpOnly); + cookie.put("isCookieSecure", isSecure); isSetCookieCalled = true; } @Override - public String getCookie(String cookieName) { + public String getCookie(String name) { if (!isSetCookieCalled) { return cookieValue; } @@ -273,7 +281,7 @@ public String getCookie(String cookieName) { UserInQueueStateCookieRepository testObject = new UserInQueueStateCookieRepository(cookieManager); assertTrue(testObject.getState(eventId, 10, secretKey, true).isValid()); - testObject.reissueQueueCookie(eventId, 12, "cookieDomain", secretKey); + testObject.reissueQueueCookie(eventId, 12, "cookieDomain", true, true, secretKey); StateInfo state = testObject.getState(eventId, 10, secretKey, true); @@ -285,6 +293,8 @@ public String getCookie(String cookieName) { assertTrue(Math.abs(System.currentTimeMillis() / 1000L - newIssueTime) < 2); assertEquals(Integer.parseInt(cookie.get("expiration").toString()), 24 * 60 * 60); assertEquals("cookieDomain", cookie.get("cookieDomain")); + assertTrue((Boolean)cookie.get("isCookieHttpOnly")); + assertTrue((Boolean)cookie.get("isCookieSecure")); } @Test @@ -292,23 +302,23 @@ public void extendQueueCookie_CookieDoesNotExist_Test() { String eventId = "event1"; String secretKey = "secretKey"; - final HashMap conditions = new HashMap<>(); + final HashMap conditions = new HashMap(); conditions.put("isSetCookieCalled", false); ICookieManager cookieManager = new ICookieManager() { @Override - public void setCookie(String cookieName, String cookieValue, Integer expiration, String cookieDomain) { + public void setCookie(String name, String value, Integer expiration, String domain, Boolean isHttpOnly, Boolean isSecure) { conditions.put("isSetCookieCalled", true); } @Override - public String getCookie(String cookieName) { + public String getCookie(String name) { return null; } }; UserInQueueStateCookieRepository testObject = new UserInQueueStateCookieRepository(cookieManager); - testObject.reissueQueueCookie(eventId, 12, "queueDomain", secretKey); + testObject.reissueQueueCookie(eventId, 12, "queueDomain", true, true, secretKey); assertFalse(conditions.get("isSetCookieCalled")); } @@ -325,12 +335,12 @@ public void getState_ValidCookieFormat_Extendable_Test() { ICookieManager cookieManager = new ICookieManager() { @Override - public void setCookie(String cookieName, String cookieValue, Integer expiration, String cookieDomain) { + public void setCookie(String name, String value, Integer expiration, String domain, Boolean isHttpOnly, Boolean isSecure) { } @Override - public String getCookie(String cookieName) { - if (cookieName.endsWith(cookieKey)) { + public String getCookie(String name) { + if (name.endsWith(cookieKey)) { return cookieValue; } return null; @@ -358,12 +368,12 @@ public void getState_ValidCookieFormat_NonExtendable_Test() { ICookieManager cookieManager = new ICookieManager() { @Override - public void setCookie(String cookieName, String cookieValue, Integer expiration, String cookieDomain) { + public void setCookie(String name, String value, Integer expiration, String domain, Boolean isHttpOnly, Boolean isSecure) { } @Override - public String getCookie(String cookieName) { - if (cookieName.equals(cookieKey)) { + public String getCookie(String name) { + if (name.equals(cookieKey)) { return cookieValue; } return null; @@ -391,12 +401,12 @@ public void getState_OldCookie_InValid_ExpiredCookie_Extendable_Test() { ICookieManager cookieManager = new ICookieManager() { @Override - public void setCookie(String cookieName, String cookieValue, Integer expiration, String cookieDomain) { + public void setCookie(String name, String value, Integer expiration, String domain, Boolean isHttpOnly, Boolean isSecure) { } @Override - public String getCookie(String cookieName) { - if (cookieName.endsWith(cookieKey)) { + public String getCookie(String name) { + if (name.endsWith(cookieKey)) { return cookieValue; } return null; @@ -420,12 +430,12 @@ public void getState_OldCookie_InValid_ExpiredCookie_NonExtendable_Test() { ICookieManager cookieManager = new ICookieManager() { @Override - public void setCookie(String cookieName, String cookieValue, Integer expiration, String cookieDomain) { + public void setCookie(String name, String value, Integer expiration, String domain, Boolean isHttpOnly, Boolean isSecure) { } @Override - public String getCookie(String cookieName) { - if (cookieName.endsWith(cookieKey)) { + public String getCookie(String name) { + if (name.endsWith(cookieKey)) { return cookieValue; } return null; @@ -445,11 +455,11 @@ public void getState_NoCookie_Test() { ICookieManager cookieManager = new ICookieManager() { @Override - public void setCookie(String cookieName, String cookieValue, Integer expiration, String cookieDomain) { + public void setCookie(String name, String value, Integer expiration, String domain, Boolean isHttpOnly, Boolean isSecure) { } @Override - public String getCookie(String cookieName) { + public String getCookie(String name) { return null; } }; diff --git a/SDK/src/test/java/com/queue_it/connector/integrationconfig/IntegrationEvaluatorTest.java b/SDK/src/test/java/com/queue_it/connector/integrationconfig/IntegrationEvaluatorTest.java index 9281a5b..c8ac8d8 100644 --- a/SDK/src/test/java/com/queue_it/connector/integrationconfig/IntegrationEvaluatorTest.java +++ b/SDK/src/test/java/com/queue_it/connector/integrationconfig/IntegrationEvaluatorTest.java @@ -6,6 +6,9 @@ import javax.servlet.*; import javax.servlet.http.*; + +import com.queue_it.connector.KnownUserRequestWrapper; + import java.io.BufferedReader; import java.security.Principal; import java.util.*; @@ -17,7 +20,7 @@ class HttpServletRequestMock implements HttpServletRequest { public String UserAgent = ""; public String RequestURL; public String QueryString; - public HashMap Headers = new HashMap<>(); + public HashMap Headers = new HashMap(); @Override public String getAuthType() { @@ -397,10 +400,12 @@ public void GetMatchedIntegrationConfig_OneTrigger_And_NotMatched() throws Excep String url = "http://test.tesdomain.com:8080/test?q=2"; - HttpServletRequestMock httpContextMock = new HttpServletRequestMock(); - httpContextMock.CookiesValue = new Cookie[0]; + HttpServletRequestMock requestMock = new HttpServletRequestMock(); + requestMock.CookiesValue = new Cookie[0]; + + KnownUserRequestWrapper wrappedRequest = new KnownUserRequestWrapper(requestMock); - IntegrationConfigModel result = testObject.getMatchedIntegrationConfig(customerIntegration, url, httpContextMock); + IntegrationConfigModel result = testObject.getMatchedIntegrationConfig(customerIntegration, url, wrappedRequest); assertNull(result); } @@ -444,11 +449,13 @@ public void GetMatchedIntegrationConfig_OneTrigger_And_Matched() throws Exceptio String url = "http://test.tesdomain.com:8080/test?q=2"; - HttpServletRequestMock httpContextMock = new HttpServletRequestMock(); - httpContextMock.CookiesValue = new Cookie[1]; - httpContextMock.CookiesValue[0] = new Cookie("c1", "value1"); + HttpServletRequestMock requestMock = new HttpServletRequestMock(); + requestMock.CookiesValue = new Cookie[1]; + requestMock.CookiesValue[0] = new Cookie("c1", "value1"); - IntegrationConfigModel result = testObject.getMatchedIntegrationConfig(customerIntegration, url, httpContextMock); + KnownUserRequestWrapper wrappedRequest = new KnownUserRequestWrapper(requestMock); + + IntegrationConfigModel result = testObject.getMatchedIntegrationConfig(customerIntegration, url, wrappedRequest); assertEquals("integration1", result.Name); } @@ -493,11 +500,13 @@ public void GetMatchedIntegrationConfig_OneTrigger_Or_NotMatched() throws Except String url = "http://test.tesdomain.com:8080/test?q=2"; - HttpServletRequestMock httpContextMock = new HttpServletRequestMock(); - httpContextMock.CookiesValue = new Cookie[1]; - httpContextMock.CookiesValue[0] = new Cookie("c2", "value1"); + HttpServletRequestMock requestMock = new HttpServletRequestMock(); + requestMock.CookiesValue = new Cookie[1]; + requestMock.CookiesValue[0] = new Cookie("c2", "value1"); + + KnownUserRequestWrapper wrappedRequest = new KnownUserRequestWrapper(requestMock); - IntegrationConfigModel result = testObject.getMatchedIntegrationConfig(customerIntegration, url, httpContextMock); + IntegrationConfigModel result = testObject.getMatchedIntegrationConfig(customerIntegration, url, wrappedRequest); assertNull(result); } @@ -540,11 +549,13 @@ public void GetMatchedIntegrationConfig_OneTrigger_Or_Matched() throws Exception String url = "http://test.tesdomain.com:8080/test?q=2"; - HttpServletRequestMock httpContextMock = new HttpServletRequestMock(); - httpContextMock.CookiesValue = new Cookie[1]; - httpContextMock.CookiesValue[0] = new Cookie("c1", "value1"); + HttpServletRequestMock requestMock = new HttpServletRequestMock(); + requestMock.CookiesValue = new Cookie[1]; + requestMock.CookiesValue[0] = new Cookie("c1", "value1"); - IntegrationConfigModel result = testObject.getMatchedIntegrationConfig(customerIntegration, url, httpContextMock); + KnownUserRequestWrapper wrappedRequest = new KnownUserRequestWrapper(requestMock); + + IntegrationConfigModel result = testObject.getMatchedIntegrationConfig(customerIntegration, url, wrappedRequest); assertEquals("integration1", result.Name); } @@ -594,10 +605,12 @@ public void GetMatchedIntegrationConfig_TwoTriggers_Matched() throws Exception { String url = "http://test.tesdomain.com:8080/test?q=2"; - HttpServletRequestMock httpContextMock = new HttpServletRequestMock(); - httpContextMock.CookiesValue = new Cookie[0]; + HttpServletRequestMock requestMock = new HttpServletRequestMock(); + requestMock.CookiesValue = new Cookie[0]; + + KnownUserRequestWrapper wrappedRequest = new KnownUserRequestWrapper(requestMock); - IntegrationConfigModel result = testObject.getMatchedIntegrationConfig(customerIntegration, url, httpContextMock); + IntegrationConfigModel result = testObject.getMatchedIntegrationConfig(customerIntegration, url, wrappedRequest); assertEquals("integration1", result.Name); } @@ -647,10 +660,12 @@ public void GetMatchedIntegrationConfig_TwoTriggers_NotMatched() throws Exceptio String url = "http://test.tesdomain.com:8080/test?q=2"; - HttpServletRequestMock httpContextMock = new HttpServletRequestMock(); - httpContextMock.CookiesValue = new Cookie[0]; + HttpServletRequestMock requestMock = new HttpServletRequestMock(); + requestMock.CookiesValue = new Cookie[0]; + + KnownUserRequestWrapper wrappedRequest = new KnownUserRequestWrapper(requestMock); - IntegrationConfigModel result = testObject.getMatchedIntegrationConfig(customerIntegration, url, httpContextMock); + IntegrationConfigModel result = testObject.getMatchedIntegrationConfig(customerIntegration, url, wrappedRequest); assertNull(result); } @@ -728,11 +743,13 @@ public void GetMatchedIntegrationConfig_ThreeIntegrationsInOrder_SecondMatched() String url = "http://test.tesdomain.com:8080/test?q=2"; - HttpServletRequestMock httpContextMock = new HttpServletRequestMock(); - httpContextMock.CookiesValue = new Cookie[1]; - httpContextMock.CookiesValue[0] = new Cookie("c1", "Value1"); + HttpServletRequestMock requestMock = new HttpServletRequestMock(); + requestMock.CookiesValue = new Cookie[1]; + requestMock.CookiesValue[0] = new Cookie("c1", "Value1"); - IntegrationConfigModel result = testObject.getMatchedIntegrationConfig(customerIntegration, url, httpContextMock); + KnownUserRequestWrapper wrappedRequest = new KnownUserRequestWrapper(requestMock); + + IntegrationConfigModel result = testObject.getMatchedIntegrationConfig(customerIntegration, url, wrappedRequest); assertEquals("integration1", result.Name); } @@ -784,12 +801,14 @@ public void GetMatchedIntegrationConfig_OneTrigger_And_NotMatched_UserAgent() th String url = "http://test.tesdomain.com:8080/test?q=2"; - HttpServletRequestMock httpContextMock = new HttpServletRequestMock(); - httpContextMock.CookiesValue = new Cookie[1]; - httpContextMock.CookiesValue[0] = new Cookie("c1", "value1"); - httpContextMock.UserAgent = "Googlebot"; + HttpServletRequestMock requestMock = new HttpServletRequestMock(); + requestMock.CookiesValue = new Cookie[1]; + requestMock.CookiesValue[0] = new Cookie("c1", "value1"); + requestMock.UserAgent = "Googlebot"; + + KnownUserRequestWrapper wrappedRequest = new KnownUserRequestWrapper(requestMock); - IntegrationConfigModel result = testObject.getMatchedIntegrationConfig(customerIntegration, url, httpContextMock); + IntegrationConfigModel result = testObject.getMatchedIntegrationConfig(customerIntegration, url, wrappedRequest); assertNull(result); } } diff --git a/SDK/src/test/java/com/queue_it/connector/integrationconfig/RequestBodyValidatorHelperTest.java b/SDK/src/test/java/com/queue_it/connector/integrationconfig/RequestBodyValidatorHelperTest.java new file mode 100644 index 0000000..d432c8c --- /dev/null +++ b/SDK/src/test/java/com/queue_it/connector/integrationconfig/RequestBodyValidatorHelperTest.java @@ -0,0 +1,42 @@ +package com.queue_it.connector.integrationconfig; + +import org.junit.Test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import com.queue_it.connector.KnownUserRequestWrapperMock; + +public class RequestBodyValidatorHelperTest { + @Test + public void Evaluate_Test() { + TriggerPart triggerPart = new TriggerPart(); + triggerPart.Operator = ComparisonOperatorType.CONTAINS; + triggerPart.ValueToCompare = "test body"; + HttpServletRequestMock requestMock = new HttpServletRequestMock(); + KnownUserRequestWrapperMock wrappedRequest = new KnownUserRequestWrapperMock(requestMock); + + assertFalse(RequestBodyValidatorHelper.evaluate(triggerPart, wrappedRequest)); + + wrappedRequest.SetRequestBodyAsString("test body"); + + assertTrue(RequestBodyValidatorHelper.evaluate(triggerPart, wrappedRequest)); + + triggerPart.ValueToCompare = "ZZZ"; + assertFalse(RequestBodyValidatorHelper.evaluate(triggerPart, wrappedRequest)); + + triggerPart.ValueToCompare = "Test"; + triggerPart.IsIgnoreCase = true; + assertTrue(RequestBodyValidatorHelper.evaluate(triggerPart, wrappedRequest)); + + triggerPart.ValueToCompare = "Test"; + triggerPart.IsIgnoreCase = true; + triggerPart.IsNegative = true; + assertFalse(RequestBodyValidatorHelper.evaluate(triggerPart, wrappedRequest)); + + triggerPart.ValueToCompare = "Test"; + triggerPart.IsIgnoreCase = true; + triggerPart.IsNegative = true; + assertFalse(RequestBodyValidatorHelper.evaluate(triggerPart, wrappedRequest)); + } +}