From e5b8f362f03ff66e5422de43c6f2502535f753f9 Mon Sep 17 00:00:00 2001 From: PasinduYeshan Date: Thu, 3 Aug 2023 21:52:05 +0530 Subject: [PATCH 01/18] Add http header feature to httpPost and httpGet methods --- .../pom.xml | 3 + .../functions/http/AbstractHTTPFunction.java | 23 +++++- .../auth/functions/http/HTTPGetFunction.java | 6 +- .../functions/http/HTTPGetFunctionImpl.java | 19 ++++- .../auth/functions/http/HTTPPostFunction.java | 8 ++- .../functions/http/HTTPPostFunctionImpl.java | 72 ++++++++++++++++--- 6 files changed, 113 insertions(+), 18 deletions(-) diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/pom.xml b/components/org.wso2.carbon.identity.conditional.auth.functions.http/pom.xml index 93b3ec1a..95fdeac0 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/pom.xml +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/pom.xml @@ -133,6 +133,7 @@ javax.servlet.*; version="${imp.pkg.version.javax.servlet}", + org.apache.commons.collections, org.apache.commons.io, org.apache.commons.lang, org.apache.commons.logging, @@ -140,10 +141,12 @@ org.apache.http.impl, org.apache.http.util, org.apache.http.client, + org.apache.http.client.entity, org.apache.http.client.methods, org.apache.http.client.config, org.apache.http.impl.client, org.apache.http.conn, + org.apache.http.message, org.json.simple, org.json.simple.parser, org.apache.http, diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/AbstractHTTPFunction.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/AbstractHTTPFunction.java index 3552338e..bf6d1855 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/AbstractHTTPFunction.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/AbstractHTTPFunction.java @@ -20,6 +20,8 @@ import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.http.Header; +import org.apache.http.HttpEntity; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpUriRequest; @@ -49,6 +51,8 @@ public abstract class AbstractHTTPFunction { private static final Log LOG = LogFactory.getLog(AbstractHTTPFunction.class); protected static final String TYPE_APPLICATION_JSON = "application/json"; + protected static final String TYPE_APPLICATION_FORM_URLENCODED = "application/x-www-form-urlencoded"; + protected static final String TYPE_APPLICATION_XML = "application/xml"; private static final char DOMAIN_SEPARATOR = '.'; private final List allowedDomains; @@ -88,11 +92,24 @@ protected void executeHttpMethod(HttpUriRequest request, Map eve try (CloseableHttpResponse response = client.execute(request)) { responseCode = response.getStatusLine().getStatusCode(); + Header contentType = response.getEntity().getContentType(); if (responseCode >= 200 && responseCode < 300) { outcome = Constants.OUTCOME_SUCCESS; - String jsonString = EntityUtils.toString(response.getEntity()); - JSONParser parser = new JSONParser(); - json = (JSONObject) parser.parse(jsonString); + HttpEntity entity = response.getEntity(); + + if (entity != null) { + String responseBody = EntityUtils.toString(entity); + if (contentType != null && contentType.getValue().contains("application/json")) { + JSONParser parser = new JSONParser(); + json = (JSONObject) parser.parse(responseBody); + } else if (contentType != null && contentType.getValue().contains("text/plain")) { + // For 'text/plain', put the response body into the JSON object as a single field. + json = new JSONObject(); + json.put("response", responseBody); + } + } + + outcome = Constants.OUTCOME_SUCCESS; } else { outcome = Constants.OUTCOME_FAIL; } diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunction.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunction.java index 17942af1..4ae7a031 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunction.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunction.java @@ -30,7 +30,9 @@ public interface HTTPGetFunction { * POST data to the given endpoint. * * @param epUrl Endpoint url. - * @param eventHandlers event handlers. + * @param params Parameters. + * 1. headers headers (optional). + * 2. eventHandlers event handlers. */ - void httpGet(String epUrl, Map eventHandlers); + void httpGet(String epUrl, Object... params); } diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImpl.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImpl.java index 27edf666..40117b05 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImpl.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImpl.java @@ -22,6 +22,7 @@ import org.apache.commons.logging.LogFactory; import org.apache.http.client.methods.HttpGet; +import java.util.HashMap; import java.util.Map; import static org.apache.http.HttpHeaders.ACCEPT; @@ -39,9 +40,25 @@ public HTTPGetFunctionImpl() { } @Override - public void httpGet(String epUrl, Map eventHandlers) { + public void httpGet(String epUrl, Object... params) { + + Map eventHandlers = new HashMap<>(); + Map headers = new HashMap<>(); + + if (params.length == 1 && params[0] instanceof Map) { + eventHandlers = (Map) params[0]; + } else if (params.length == 2 && params[0] instanceof Map && params[1] instanceof Map) { + headers = (Map) params[0]; + eventHandlers = (Map) params[1]; + } else { + LOG.error("Invalid number of parameters."); + return; + } HttpGet request = new HttpGet(epUrl); + for (Map.Entry dataElements : headers.entrySet()) { + request.setHeader(dataElements.getKey(), dataElements.getValue()); + } request.setHeader(ACCEPT, TYPE_APPLICATION_JSON); executeHttpMethod(request, eventHandlers); } diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunction.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunction.java index 4749c268..d19318e1 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunction.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunction.java @@ -31,8 +31,10 @@ public interface HTTPPostFunction { * POST data to the given endpoint. * * @param epUrl Endpoint url. - * @param payloadData payload data. - * @param eventHandlers event handlers. + * @param params parameters. + * 1. payloadData payload data. + * 2. headers headers (optional). + * 3. eventHandlers event handlers. */ - void httpPost(String epUrl, Map payloadData, Map eventHandlers); + void httpPost(String epUrl, Object... params); } diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImpl.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImpl.java index 923375be..ee6a24b8 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImpl.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImpl.java @@ -18,13 +18,20 @@ package org.wso2.carbon.identity.conditional.auth.functions.http; +import org.apache.commons.collections.MapUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.http.NameValuePair; +import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; +import org.apache.http.message.BasicNameValuePair; import org.json.simple.JSONObject; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; import java.util.Map; import static org.apache.http.HttpHeaders.ACCEPT; @@ -44,17 +51,64 @@ public HTTPPostFunctionImpl() { } @Override - public void httpPost(String epUrl, Map payloadData, Map eventHandlers) { + public void httpPost(String epUrl, Object... params) { - HttpPost request = new HttpPost(epUrl); - request.setHeader(ACCEPT, TYPE_APPLICATION_JSON); - request.setHeader(CONTENT_TYPE, TYPE_APPLICATION_JSON); + Map eventHandlers = new HashMap<>(); + Map payloadData = new HashMap<>(); + Map headers = new HashMap<>(); - JSONObject jsonObject = new JSONObject(); - for (Map.Entry dataElements : payloadData.entrySet()) { - jsonObject.put(dataElements.getKey(), dataElements.getValue()); + switch (params.length) { + case 1: + if (params[0] instanceof Map) { + eventHandlers = (Map) params[0]; + } + break; + case 2: + if (params[0] instanceof Map && params[1] instanceof Map) { + payloadData = (Map) params[0]; + eventHandlers = (Map) params[1]; + } + break; + case 3: + if (params[0] instanceof Map && params[1] instanceof Map && params[2] instanceof Map) { + payloadData = (Map) params[0]; + headers = (Map) params[1]; + eventHandlers = (Map) params[2]; + } + break; + default: + LOG.error("Invalid number of parameters."); + return; + } + + HttpPost request = new HttpPost(epUrl); + request.setHeader(ACCEPT, TYPE_APPLICATION_JSON); + + if (headers == null) { + headers = new HashMap<>(); + } + headers.putIfAbsent(CONTENT_TYPE, TYPE_APPLICATION_JSON); + headers.forEach(request::setHeader); + + /* + For the header "Content-Type : application/x-www-form-urlencoded" + request body data is set to UrlEncodedFormEntity format. + */ + if (MapUtils.isNotEmpty(payloadData)) { + if (TYPE_APPLICATION_FORM_URLENCODED.equals(headers.get(CONTENT_TYPE))) { + List entities = new ArrayList(); + for (Map.Entry dataElements : payloadData.entrySet()) { + entities.add(new BasicNameValuePair(dataElements.getKey(), dataElements.getValue().toString())); + } + request.setEntity(new UrlEncodedFormEntity(entities, StandardCharsets.UTF_8)); + } else { + JSONObject jsonObject = new JSONObject(); + for (Map.Entry dataElements : payloadData.entrySet()) { + jsonObject.put(dataElements.getKey(), dataElements.getValue()); + } + request.setEntity(new StringEntity(jsonObject.toJSONString(), StandardCharsets.UTF_8)); } - request.setEntity(new StringEntity(jsonObject.toJSONString(), StandardCharsets.UTF_8)); - executeHttpMethod(request, eventHandlers); + } + executeHttpMethod(request, eventHandlers); } } From ddfaeeace0cf5aeb3d123b532c1e2a25326eccf4 Mon Sep 17 00:00:00 2001 From: PasinduYeshan Date: Thu, 3 Aug 2023 22:05:02 +0530 Subject: [PATCH 02/18] Refactor code --- .../auth/functions/http/AbstractHTTPFunction.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/AbstractHTTPFunction.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/AbstractHTTPFunction.java index bf6d1855..48ee234d 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/AbstractHTTPFunction.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/AbstractHTTPFunction.java @@ -95,10 +95,8 @@ protected void executeHttpMethod(HttpUriRequest request, Map eve Header contentType = response.getEntity().getContentType(); if (responseCode >= 200 && responseCode < 300) { outcome = Constants.OUTCOME_SUCCESS; - HttpEntity entity = response.getEntity(); - - if (entity != null) { - String responseBody = EntityUtils.toString(entity); + if (response.getEntity() != null) { + String responseBody = EntityUtils.toString(response.getEntity()); if (contentType != null && contentType.getValue().contains("application/json")) { JSONParser parser = new JSONParser(); json = (JSONObject) parser.parse(responseBody); @@ -108,8 +106,6 @@ protected void executeHttpMethod(HttpUriRequest request, Map eve json.put("response", responseBody); } } - - outcome = Constants.OUTCOME_SUCCESS; } else { outcome = Constants.OUTCOME_FAIL; } From fe5915353851ce5c74e7e72562fbbd96d1f346e0 Mon Sep 17 00:00:00 2001 From: PasinduYeshan Date: Thu, 3 Aug 2023 23:20:00 +0530 Subject: [PATCH 03/18] Fix for PR build issue --- .github/workflows/pr-builder.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pr-builder.yml b/.github/workflows/pr-builder.yml index 513da675..15192940 100644 --- a/.github/workflows/pr-builder.yml +++ b/.github/workflows/pr-builder.yml @@ -22,7 +22,7 @@ jobs: - name: Set up Adopt JDK 11 uses: actions/setup-java@v2 with: - java-version: "11" + java-version: [ 11.0.19+7 ] distribution: "adopt" - name: Cache local Maven repository id: cache-maven-m2 From 5786aab08dccf3e97ecf69d25fdbbe233db0b073 Mon Sep 17 00:00:00 2001 From: PasinduYeshan Date: Fri, 4 Aug 2023 11:46:22 +0530 Subject: [PATCH 04/18] Refactor code --- .../auth/functions/http/HTTPGetFunctionImpl.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImpl.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImpl.java index 40117b05..697b9d99 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImpl.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImpl.java @@ -56,10 +56,10 @@ public void httpGet(String epUrl, Object... params) { } HttpGet request = new HttpGet(epUrl); - for (Map.Entry dataElements : headers.entrySet()) { - request.setHeader(dataElements.getKey(), dataElements.getValue()); - } + // Set default ACCEPT header to application/json request.setHeader(ACCEPT, TYPE_APPLICATION_JSON); + headers.forEach(request::setHeader); + executeHttpMethod(request, eventHandlers); } } From 4a5330868bef5e867c1e0cf3225ba32b4ab35fb6 Mon Sep 17 00:00:00 2001 From: PasinduYeshan Date: Fri, 4 Aug 2023 16:49:53 +0530 Subject: [PATCH 05/18] Add test for header feature --- .../http/HTTPGetFunctionImplTest.java | 46 ++++++++--- .../http/HTTPPostFunctionImplTest.java | 74 ++++++++++++++--- .../functions/http/http-get-test-headers.xml | 78 ++++++++++++++++++ .../http/http-post-test-event-handlers.xml | 72 +++++++++++++++++ .../functions/http/http-post-test-headers.xml | 81 +++++++++++++++++++ 5 files changed, 328 insertions(+), 23 deletions(-) create mode 100644 components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/resources/org/wso2/carbon/identity/conditional/auth/functions/http/http-get-test-headers.xml create mode 100644 components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/resources/org/wso2/carbon/identity/conditional/auth/functions/http/http-post-test-event-handlers.xml create mode 100644 components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/resources/org/wso2/carbon/identity/conditional/auth/functions/http/http-post-test-headers.xml diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImplTest.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImplTest.java index 97261250..c4552457 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImplTest.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImplTest.java @@ -44,9 +44,8 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import javax.ws.rs.GET; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; +import javax.ws.rs.*; +import javax.ws.rs.core.Context; import static org.testng.Assert.assertEquals; @@ -58,11 +57,13 @@ public class HTTPGetFunctionImplTest extends JsSequenceHandlerAbstractTest { private static final String TEST_SP_CONFIG = "http-get-test-sp.xml"; + private static final String TEST_HEADERS = "http-get-test-headers.xml"; private static final String TENANT_DOMAIN = "carbon.super"; private static final String STATUS = "status"; private static final String SUCCESS = "SUCCESS"; private static final String FAILED = "FAILED"; private static final String ALLOWED_DOMAIN = "abc"; + private static final String AUTHORIZATION = "Authorization"; @InjectMicroservicePort private int microServicePort; @@ -89,8 +90,8 @@ protected void tearDown() { @Test public void testHttpGetMethod() throws JsTestException { - String requestUrl = getRequestUrl(); - String result = executeHttpGetFunction(requestUrl); + String requestUrl = getRequestUrl("dummy-get"); + String result = executeHttpGetFunction(requestUrl, TEST_SP_CONFIG); assertEquals(result, SUCCESS, "The http get request was not successful. Result from request: " + result); } @@ -100,13 +101,22 @@ public void testHttpGetMethodUrlValidation() throws JsTestException, NoSuchField sequenceHandlerRunner.registerJsFunction("httpGet", new HTTPGetFunctionImpl()); setAllowedDomain(ALLOWED_DOMAIN); - String requestUrl = getRequestUrl(); - String result = executeHttpGetFunction(requestUrl); + String requestUrl = getRequestUrl("dummy-get"); + String result = executeHttpGetFunction(requestUrl, TEST_SP_CONFIG); assertEquals(result, FAILED, "The http get request should fail but it was successful. Result from request: " + result); } + @Test + public void testHttpGetMethodWithHeaders() throws JsTestException { + + String requestUrl = getRequestUrl("dummy-get-with-headers"); + String result = executeHttpGetFunction(requestUrl, TEST_HEADERS); + + assertEquals(result, SUCCESS, "The http get request was not successful. Result from request: " + result); + } + private void setAllowedDomain(String domain) { ConfigProvider.getInstance().getAllowedDomainsForHttpFunctions().add(domain); @@ -117,14 +127,14 @@ private void unsetAllowedDomains() { ConfigProvider.getInstance().getAllowedDomainsForHttpFunctions().clear(); } - private String getRequestUrl() { + private String getRequestUrl(String path) { - return "http://localhost:" + microServicePort + "/dummy-get"; + return "http://localhost:" + microServicePort + "/" + path; } - private String executeHttpGetFunction(String requestUrl) throws JsTestException { + private String executeHttpGetFunction(String requestUrl, String adaptiveAuthScript) throws JsTestException { - ServiceProvider sp = sequenceHandlerRunner.loadServiceProviderFromResource(TEST_SP_CONFIG, this); + ServiceProvider sp = sequenceHandlerRunner.loadServiceProviderFromResource(adaptiveAuthScript, this); updateSPAuthScriptRequestUrl(sp, requestUrl); AuthenticationContext context = sequenceHandlerRunner.createAuthenticationContext(sp); @@ -163,4 +173,18 @@ public Map dummyGet() { response.put(STATUS, SUCCESS); return response; } + + @GET + @Path("/dummy-get-with-headers") + @Produces("application/json") + public Map dummyGetWithHeaders(@HeaderParam(AUTHORIZATION) String authorization) { + + Map response = new HashMap<>(); + if (authorization != null) { + response.put(STATUS, SUCCESS); + } else { + response.put(STATUS, FAILED); + } + return response; + } } diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImplTest.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImplTest.java index 41ffcc8b..70c296e7 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImplTest.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImplTest.java @@ -39,16 +39,18 @@ import org.wso2.carbon.identity.conditional.auth.functions.test.utils.sequence.JsTestException; import org.wso2.carbon.identity.core.util.IdentityTenantUtil; + import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import javax.ws.rs.Consumes; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; +import javax.ws.rs.*; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.MultivaluedMap; +import static org.mockito.MockitoAnnotations.initMocks; import static org.testng.Assert.assertEquals; @WithCarbonHome @@ -59,12 +61,15 @@ public class HTTPPostFunctionImplTest extends JsSequenceHandlerAbstractTest { private static final String TEST_SP_CONFIG = "http-post-test-sp.xml"; + private static final String TEST_EVENT_HANDLERS = "http-post-test-event-handlers.xml"; + private static final String TEST_HEADERS = "http-post-test-headers.xml"; private static final String TENANT_DOMAIN = "carbon.super"; private static final String STATUS = "status"; private static final String SUCCESS = "SUCCESS"; private static final String FAILED = "FAILED"; private static final String EMAIL = "email"; private static final String ALLOWED_DOMAIN = "abc"; + private static final String AUTHORIZATION = "Authorization"; @InjectMicroservicePort private int microServicePort; @@ -80,6 +85,7 @@ protected void initClass() throws Exception { new LongWaitStatusStoreService(cacheBackedDao, connectionTimeout); FrameworkServiceDataHolder.getInstance().setLongWaitStatusStoreService(longWaitStatusStoreService); sequenceHandlerRunner.registerJsFunction("httpPost", new HTTPPostFunctionImpl()); + initMocks(this); } @AfterClass @@ -91,8 +97,8 @@ protected void tearDown() { @Test public void testHttpPostMethod() throws JsTestException { - String requestUrl = getRequestUrl(); - String result = executeHttpPostFunction(requestUrl); + String requestUrl = getRequestUrl("dummy-post"); + String result = executeHttpPostFunction(requestUrl, TEST_SP_CONFIG); assertEquals(result, SUCCESS, "The http post request was not successful. Result from request: " + result); } @@ -101,13 +107,31 @@ public void testHttpPostMethod() throws JsTestException { public void testHttpPostMethodUrlValidation() throws JsTestException, NoSuchFieldException, IllegalAccessException { setAllowedDomain(ALLOWED_DOMAIN); - String requestUrl = getRequestUrl(); - String result = executeHttpPostFunction(requestUrl); + String requestUrl = getRequestUrl("dummy-post"); + String result = executeHttpPostFunction(requestUrl, TEST_SP_CONFIG); assertEquals(result, FAILED, "The http post request should fail but it was successful. Result from request: " + result); } + @Test + public void testHttpPostWithEventHandlersOnly() throws JsTestException { + + String requestUrl = getRequestUrl("dummy-post-event-handlers"); + String result = executeHttpPostFunction(requestUrl, TEST_EVENT_HANDLERS); + assertEquals(result, SUCCESS, "The http post request was not successful. Result from request: " + + result); + } + + @Test + public void testHttpPostWithHeaders() throws JsTestException { + + String requestUrl = getRequestUrl("dummy-post-headers"); + String result = executeHttpPostFunction(requestUrl, TEST_HEADERS); + assertEquals(result, SUCCESS, "The http post request was not successful. Result from request: " + + result); + } + private void setAllowedDomain(String domain) { ConfigProvider.getInstance().getAllowedDomainsForHttpFunctions().add(domain); @@ -118,14 +142,14 @@ private void unsetAllowedDomains() { ConfigProvider.getInstance().getAllowedDomainsForHttpFunctions().clear(); } - private String getRequestUrl() { + private String getRequestUrl(String path) { - return "http://localhost:" + microServicePort + "/dummy-post"; + return "http://localhost:" + microServicePort + "/" + path; } - private String executeHttpPostFunction(String requestUrl) throws JsTestException { + private String executeHttpPostFunction(String requestUrl, String adaptiveAuthScript) throws JsTestException { - ServiceProvider sp = sequenceHandlerRunner.loadServiceProviderFromResource(TEST_SP_CONFIG, this); + ServiceProvider sp = sequenceHandlerRunner.loadServiceProviderFromResource(adaptiveAuthScript, this); updateSPAuthScriptRequestUrl(sp, requestUrl); AuthenticationContext context = sequenceHandlerRunner.createAuthenticationContext(sp); @@ -169,4 +193,30 @@ public Map dummyPost(Map data) { } return response; } + + @POST + @Path("/dummy-post-event-handlers") + @Produces("application/json") + @Consumes("application/json") + public Map dummyPost() { + + Map response = new HashMap<>(); + response.put(STATUS, SUCCESS); + return response; + } + + @POST + @Path("/dummy-post-headers") + @Produces("application/json") + @Consumes("application/json") + public Map dummyPostWithHeaders(@HeaderParam(AUTHORIZATION) String authorization, Map data) { + + Map response = new HashMap<>(); + if (data.containsKey(EMAIL) && authorization != null) { + response.put(STATUS, SUCCESS); + } else { + response.put(STATUS, FAILED); + } + return response; + } } diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/resources/org/wso2/carbon/identity/conditional/auth/functions/http/http-get-test-headers.xml b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/resources/org/wso2/carbon/identity/conditional/auth/functions/http/http-get-test-headers.xml new file mode 100644 index 00000000..5464fa20 --- /dev/null +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/resources/org/wso2/carbon/identity/conditional/auth/functions/http/http-get-test-headers.xml @@ -0,0 +1,78 @@ + + + 1 + default + Default Service Provider + + + + default + + + + + + + + + 1 + + + BasicMockAuthenticator + basicauth + true + + + true + true + + + + flow + + + + + + true + + + diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/resources/org/wso2/carbon/identity/conditional/auth/functions/http/http-post-test-event-handlers.xml b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/resources/org/wso2/carbon/identity/conditional/auth/functions/http/http-post-test-event-handlers.xml new file mode 100644 index 00000000..7fc36eb2 --- /dev/null +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/resources/org/wso2/carbon/identity/conditional/auth/functions/http/http-post-test-event-handlers.xml @@ -0,0 +1,72 @@ + + + 1 + default + Default Service Provider + + + + default + + + + + + + + + 1 + + + BasicMockAuthenticator + basicauth + true + + + true + true + + + + flow + + + + + + true + + + diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/resources/org/wso2/carbon/identity/conditional/auth/functions/http/http-post-test-headers.xml b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/resources/org/wso2/carbon/identity/conditional/auth/functions/http/http-post-test-headers.xml new file mode 100644 index 00000000..16eab1e7 --- /dev/null +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/resources/org/wso2/carbon/identity/conditional/auth/functions/http/http-post-test-headers.xml @@ -0,0 +1,81 @@ + + + 1 + default + Default Service Provider + + + + default + + + + + + + + + 1 + + + BasicMockAuthenticator + basicauth + true + + + true + true + + + + flow + + + + + + true + + + From 5c6675a1ffc31da90759d7c9d91b78245907d636 Mon Sep 17 00:00:00 2001 From: PasinduYeshan Date: Fri, 4 Aug 2023 16:56:16 +0530 Subject: [PATCH 06/18] Refactor code --- .../auth/functions/http/HTTPGetFunctionImplTest.java | 6 ++++-- .../auth/functions/http/HTTPPostFunctionImplTest.java | 10 ++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImplTest.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImplTest.java index c4552457..32730724 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImplTest.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImplTest.java @@ -44,8 +44,10 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import javax.ws.rs.*; -import javax.ws.rs.core.Context; +import javax.ws.rs.GET; +import javax.ws.rs.HeaderParam; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; import static org.testng.Assert.assertEquals; diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImplTest.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImplTest.java index 70c296e7..9221ab69 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImplTest.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImplTest.java @@ -45,10 +45,12 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import javax.ws.rs.*; -import javax.ws.rs.core.Context; -import javax.ws.rs.core.HttpHeaders; -import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.Consumes; +import javax.ws.rs.HeaderParam; +import javax.ws.rs.Path; +import javax.ws.rs.POST; +import javax.ws.rs.Produces; + import static org.mockito.MockitoAnnotations.initMocks; import static org.testng.Assert.assertEquals; From 3f11ffa8852b4ba05824342dea8d2806cb739abd Mon Sep 17 00:00:00 2001 From: PasinduYeshan Date: Sun, 6 Aug 2023 23:51:11 +0530 Subject: [PATCH 07/18] Add parameter type check --- .../functions/http/HTTPGetFunctionImpl.java | 33 +++++++++++++++---- .../functions/http/HTTPPostFunctionImpl.java | 15 +++++++++ 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImpl.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImpl.java index 697b9d99..e394332b 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImpl.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImpl.java @@ -18,6 +18,7 @@ package org.wso2.carbon.identity.conditional.auth.functions.http; +import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.http.client.methods.HttpGet; @@ -45,16 +46,34 @@ public void httpGet(String epUrl, Object... params) { Map eventHandlers = new HashMap<>(); Map headers = new HashMap<>(); - if (params.length == 1 && params[0] instanceof Map) { - eventHandlers = (Map) params[0]; - } else if (params.length == 2 && params[0] instanceof Map && params[1] instanceof Map) { - headers = (Map) params[0]; - eventHandlers = (Map) params[1]; - } else { - LOG.error("Invalid number of parameters."); + if (StringUtils.isBlank(epUrl)) { + LOG.error("Endpoint URL cannot be empty."); return; } + switch (params.length) { + case 1: + if (params[0] instanceof Map) { + eventHandlers = (Map) params[0]; + } else { + LOG.error("Invalid parameter type."); + return; + } + break; + case 2: + if (params[0] instanceof Map && params[1] instanceof Map ) { + headers = (Map) params[0]; + eventHandlers = (Map) params[1]; + } else { + LOG.error("Invalid parameter type."); + return; + } + break; + default: + LOG.error("Invalid number of parameters."); + return; + } + HttpGet request = new HttpGet(epUrl); // Set default ACCEPT header to application/json request.setHeader(ACCEPT, TYPE_APPLICATION_JSON); diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImpl.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImpl.java index ee6a24b8..1b8eeb88 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImpl.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImpl.java @@ -19,6 +19,7 @@ package org.wso2.carbon.identity.conditional.auth.functions.http; import org.apache.commons.collections.MapUtils; +import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.http.NameValuePair; @@ -57,16 +58,27 @@ public void httpPost(String epUrl, Object... params) { Map payloadData = new HashMap<>(); Map headers = new HashMap<>(); + if (StringUtils.isBlank(epUrl)) { + LOG.error("Endpoint URL cannot be empty."); + return; + } + switch (params.length) { case 1: if (params[0] instanceof Map) { eventHandlers = (Map) params[0]; + } else { + LOG.error("Invalid parameter type."); + return; } break; case 2: if (params[0] instanceof Map && params[1] instanceof Map) { payloadData = (Map) params[0]; eventHandlers = (Map) params[1]; + } else { + LOG.error("Invalid parameter type."); + return; } break; case 3: @@ -74,6 +86,9 @@ public void httpPost(String epUrl, Object... params) { payloadData = (Map) params[0]; headers = (Map) params[1]; eventHandlers = (Map) params[2]; + } else { + LOG.error("Invalid parameter type."); + return; } break; default: From 96b37694cd59e8d2b9859c167d108d01f085fbd0 Mon Sep 17 00:00:00 2001 From: PasinduYeshan Date: Mon, 7 Aug 2023 17:18:20 +0530 Subject: [PATCH 08/18] Add null checks for headers and payload key values --- .../functions/http/HTTPGetFunctionImpl.java | 61 +++++---- .../functions/http/HTTPPostFunctionImpl.java | 125 ++++++++++-------- .../http/http-post-test-event-handlers.xml | 72 ---------- 3 files changed, 103 insertions(+), 155 deletions(-) delete mode 100644 components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/resources/org/wso2/carbon/identity/conditional/auth/functions/http/http-post-test-event-handlers.xml diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImpl.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImpl.java index e394332b..86dd4f98 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImpl.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImpl.java @@ -51,34 +51,43 @@ public void httpGet(String epUrl, Object... params) { return; } - switch (params.length) { - case 1: - if (params[0] instanceof Map) { - eventHandlers = (Map) params[0]; - } else { - LOG.error("Invalid parameter type."); + try { + switch (params.length) { + case 1: + if (params[0] instanceof Map) { + eventHandlers = (Map) params[0]; + } else { + LOG.error("Invalid parameter type."); + return; + } + break; + case 2: + if (params[0] instanceof Map && params[1] instanceof Map ) { + headers = (Map) params[0]; + eventHandlers = (Map) params[1]; + } else { + LOG.error("Invalid parameter type."); + return; + } + break; + default: + LOG.error("Invalid number of parameters."); return; - } - break; - case 2: - if (params[0] instanceof Map && params[1] instanceof Map ) { - headers = (Map) params[0]; - eventHandlers = (Map) params[1]; - } else { - LOG.error("Invalid parameter type."); - return; - } - break; - default: - LOG.error("Invalid number of parameters."); - return; - } + } - HttpGet request = new HttpGet(epUrl); - // Set default ACCEPT header to application/json - request.setHeader(ACCEPT, TYPE_APPLICATION_JSON); - headers.forEach(request::setHeader); + HttpGet request = new HttpGet(epUrl); + // Set default ACCEPT header to application/json + request.setHeader(ACCEPT, TYPE_APPLICATION_JSON); - executeHttpMethod(request, eventHandlers); + headers.entrySet().stream() + .filter(entry -> entry.getKey() != null) + .forEach(entry -> request.setHeader(entry.getKey(), entry.getValue())); + + executeHttpMethod(request, eventHandlers); + } catch (IllegalArgumentException e) { + LOG.error("Invalid parameter type.", e); + } catch (Exception e) { + LOG.error("Error while executing http get.", e); + } } } diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImpl.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImpl.java index 1b8eeb88..bea3d66f 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImpl.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImpl.java @@ -63,67 +63,78 @@ public void httpPost(String epUrl, Object... params) { return; } - switch (params.length) { - case 1: - if (params[0] instanceof Map) { - eventHandlers = (Map) params[0]; - } else { - LOG.error("Invalid parameter type."); - return; - } - break; - case 2: - if (params[0] instanceof Map && params[1] instanceof Map) { - payloadData = (Map) params[0]; - eventHandlers = (Map) params[1]; - } else { - LOG.error("Invalid parameter type."); + try { + switch (params.length) { + case 1: + if (params[0] instanceof Map) { + eventHandlers = (Map) params[0]; + } else { + LOG.error("Invalid parameter type."); + return; + } + break; + case 2: + if (params[0] instanceof Map && params[1] instanceof Map) { + payloadData = (Map) params[0]; + eventHandlers = (Map) params[1]; + } else { + LOG.error("Invalid parameter type."); + return; + } + break; + case 3: + if (params[0] instanceof Map && params[1] instanceof Map && params[2] instanceof Map) { + payloadData = (Map) params[0]; + headers = (Map) params[1]; + eventHandlers = (Map) params[2]; + } else { + LOG.error("Invalid parameter type."); + return; + } + break; + default: + LOG.error("Invalid number of parameters."); return; - } - break; - case 3: - if (params[0] instanceof Map && params[1] instanceof Map && params[2] instanceof Map) { - payloadData = (Map) params[0]; - headers = (Map) params[1]; - eventHandlers = (Map) params[2]; - } else { - LOG.error("Invalid parameter type."); - return; - } - break; - default: - LOG.error("Invalid number of parameters."); - return; - } - - HttpPost request = new HttpPost(epUrl); - request.setHeader(ACCEPT, TYPE_APPLICATION_JSON); + } - if (headers == null) { - headers = new HashMap<>(); - } - headers.putIfAbsent(CONTENT_TYPE, TYPE_APPLICATION_JSON); - headers.forEach(request::setHeader); - - /* - For the header "Content-Type : application/x-www-form-urlencoded" - request body data is set to UrlEncodedFormEntity format. - */ - if (MapUtils.isNotEmpty(payloadData)) { - if (TYPE_APPLICATION_FORM_URLENCODED.equals(headers.get(CONTENT_TYPE))) { - List entities = new ArrayList(); - for (Map.Entry dataElements : payloadData.entrySet()) { - entities.add(new BasicNameValuePair(dataElements.getKey(), dataElements.getValue().toString())); - } - request.setEntity(new UrlEncodedFormEntity(entities, StandardCharsets.UTF_8)); - } else { - JSONObject jsonObject = new JSONObject(); - for (Map.Entry dataElements : payloadData.entrySet()) { - jsonObject.put(dataElements.getKey(), dataElements.getValue()); + HttpPost request = new HttpPost(epUrl); + request.setHeader(ACCEPT, TYPE_APPLICATION_JSON); + + headers.putIfAbsent(CONTENT_TYPE, TYPE_APPLICATION_JSON); + headers.entrySet().stream() + .filter(entry -> entry.getKey() != null) + .forEach(entry -> request.setHeader(entry.getKey(), entry.getValue())); + + + //For the header "Content-Type : application/x-www-form-urlencoded" request body data is set to + // UrlEncodedFormEntity format. For the other cases request body data is set to StringEntity format. + if (MapUtils.isNotEmpty(payloadData)) { + if (TYPE_APPLICATION_FORM_URLENCODED.equals(headers.get(CONTENT_TYPE))) { + List entities = new ArrayList<>(); + for (Map.Entry dataElements : payloadData.entrySet()) { + if (!StringUtils.isEmpty(dataElements.getKey())) { + String value = (dataElements.getValue() != null) ? dataElements.getValue().toString() : null; + entities.add(new BasicNameValuePair(dataElements.getKey(), value)); + } + } + request.setEntity(new UrlEncodedFormEntity(entities, StandardCharsets.UTF_8)); + } else { + JSONObject jsonObject = new JSONObject(); + for (Map.Entry dataElements : payloadData.entrySet()) { + if (!StringUtils.isEmpty(dataElements.getKey())) { + Object value = (dataElements.getValue() != null) ? dataElements.getValue() : null; + jsonObject.put(dataElements.getKey(), value); + } + } + request.setEntity(new StringEntity(jsonObject.toJSONString(), StandardCharsets.UTF_8)); } - request.setEntity(new StringEntity(jsonObject.toJSONString(), StandardCharsets.UTF_8)); } + executeHttpMethod(request, eventHandlers); + + } catch (IllegalArgumentException e) { + LOG.error("Invalid parameter type.", e); + } catch (Exception e) { + LOG.error("Error while executing http post request.", e); } - executeHttpMethod(request, eventHandlers); } } diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/resources/org/wso2/carbon/identity/conditional/auth/functions/http/http-post-test-event-handlers.xml b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/resources/org/wso2/carbon/identity/conditional/auth/functions/http/http-post-test-event-handlers.xml deleted file mode 100644 index 7fc36eb2..00000000 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/resources/org/wso2/carbon/identity/conditional/auth/functions/http/http-post-test-event-handlers.xml +++ /dev/null @@ -1,72 +0,0 @@ - - - 1 - default - Default Service Provider - - - - default - - - - - - - - - 1 - - - BasicMockAuthenticator - basicauth - true - - - true - true - - - - flow - - - - - - true - - - From 2ae2e63ea9d91433fc3912ffabcd3fab5fc6599f Mon Sep 17 00:00:00 2001 From: PasinduYeshan Date: Tue, 8 Aug 2023 07:52:38 +0530 Subject: [PATCH 09/18] Add tests for null values --- .../http/HTTPGetFunctionImplTest.java | 67 ++++++++++ .../http/HTTPPostFunctionImplTest.java | 122 ++++++++++++++---- 2 files changed, 165 insertions(+), 24 deletions(-) diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImplTest.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImplTest.java index 32730724..1eaa3836 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImplTest.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImplTest.java @@ -17,7 +17,9 @@ package org.wso2.carbon.identity.conditional.auth.functions.http; +import org.apache.http.client.methods.HttpGet; import org.testng.annotations.AfterClass; +import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import org.wso2.carbon.identity.application.authentication.framework.config.model.SequenceConfig; @@ -49,6 +51,14 @@ import javax.ws.rs.Path; import javax.ws.rs.Produces; +import static org.apache.http.HttpHeaders.ACCEPT; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.reset; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.doNothing; import static org.testng.Assert.assertEquals; @WithCarbonHome @@ -66,6 +76,7 @@ public class HTTPGetFunctionImplTest extends JsSequenceHandlerAbstractTest { private static final String FAILED = "FAILED"; private static final String ALLOWED_DOMAIN = "abc"; private static final String AUTHORIZATION = "Authorization"; + private HTTPGetFunctionImpl httpGetFunction; @InjectMicroservicePort private int microServicePort; @@ -81,6 +92,10 @@ protected void initClass() throws Exception { new LongWaitStatusStoreService(cacheBackedDao, connectionTimeout); FrameworkServiceDataHolder.getInstance().setLongWaitStatusStoreService(longWaitStatusStoreService); sequenceHandlerRunner.registerJsFunction("httpGet", new HTTPGetFunctionImpl()); + + // Mocking the executeHttpMethod method to avoid actual http calls. + httpGetFunction = spy(new HTTPGetFunctionImpl()); + doNothing().when(httpGetFunction).executeHttpMethod(any(), any()); } @AfterClass @@ -89,6 +104,12 @@ protected void tearDown() { unsetAllowedDomains(); } + @AfterMethod + protected void tearDownMethod() { + + reset(httpGetFunction); + } + @Test public void testHttpGetMethod() throws JsTestException { @@ -110,6 +131,11 @@ public void testHttpGetMethodUrlValidation() throws JsTestException, NoSuchField + result); } + /** + * Test http get method with headers. + * Check if the headers are sent with the request. + * @throws JsTestException + */ @Test public void testHttpGetMethodWithHeaders() throws JsTestException { @@ -119,6 +145,47 @@ public void testHttpGetMethodWithHeaders() throws JsTestException { assertEquals(result, SUCCESS, "The http get request was not successful. Result from request: " + result); } + /** + * Tests the behavior of the httpPost function when provided with null headers. + * executeHttpMethod method should not be called. + * @throws JsTestException + */ + @Test + public void testHttpGetWithNullHeaders() { + Map headers = null; + Map eventHandlers = new HashMap<>(); + httpGetFunction.httpGet(getRequestUrl("dummy-get"), headers, eventHandlers); + verify(httpGetFunction, times(0)).executeHttpMethod(any(HttpGet.class), eq(eventHandlers)); + } + + /** + * Tests the behavior of the httpPost function when provided with null values in headers. + * executeHttpMethod method should be called. + */ + @Test + public void testHttpGetWithNullValuesInHeaders() { + Map headers = new HashMap<>(); + headers.put(ACCEPT, "application/json"); + headers.put("test", null); + Map eventHandlers = new HashMap<>(); + httpGetFunction.httpGet(getRequestUrl("dummy-get"), headers, eventHandlers); + verify(httpGetFunction, times(1)).executeHttpMethod(any(HttpGet.class), eq(eventHandlers)); + } + + /** + * Tests the behavior of the httpPost function when provided with null keys in headers. + * executeHttpMethod method should be called and null keys should be ignored. + */ + @Test + public void testHttpGetWithNullKeysInHeaders() { + Map headers = new HashMap<>(); + headers.put(ACCEPT, "application/json"); + headers.put(null, null); + Map eventHandlers = new HashMap<>(); + httpGetFunction.httpGet(getRequestUrl("dummy-get"), headers, eventHandlers); + verify(httpGetFunction, times(1)).executeHttpMethod(any(HttpGet.class), eq(eventHandlers)); + } + private void setAllowedDomain(String domain) { ConfigProvider.getInstance().getAllowedDomainsForHttpFunctions().add(domain); diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImplTest.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImplTest.java index 9221ab69..3974a51e 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImplTest.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImplTest.java @@ -17,7 +17,9 @@ package org.wso2.carbon.identity.conditional.auth.functions.http; +import org.apache.http.client.methods.HttpGet; import org.testng.annotations.AfterClass; +import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import org.wso2.carbon.identity.application.authentication.framework.config.model.SequenceConfig; @@ -39,7 +41,6 @@ import org.wso2.carbon.identity.conditional.auth.functions.test.utils.sequence.JsTestException; import org.wso2.carbon.identity.core.util.IdentityTenantUtil; - import java.util.HashMap; import java.util.Map; @@ -51,8 +52,14 @@ import javax.ws.rs.POST; import javax.ws.rs.Produces; - -import static org.mockito.MockitoAnnotations.initMocks; +import static org.apache.http.HttpHeaders.ACCEPT; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.reset; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.doNothing; import static org.testng.Assert.assertEquals; @WithCarbonHome @@ -63,7 +70,6 @@ public class HTTPPostFunctionImplTest extends JsSequenceHandlerAbstractTest { private static final String TEST_SP_CONFIG = "http-post-test-sp.xml"; - private static final String TEST_EVENT_HANDLERS = "http-post-test-event-handlers.xml"; private static final String TEST_HEADERS = "http-post-test-headers.xml"; private static final String TENANT_DOMAIN = "carbon.super"; private static final String STATUS = "status"; @@ -72,6 +78,7 @@ public class HTTPPostFunctionImplTest extends JsSequenceHandlerAbstractTest { private static final String EMAIL = "email"; private static final String ALLOWED_DOMAIN = "abc"; private static final String AUTHORIZATION = "Authorization"; + private HTTPPostFunctionImpl httpPostFunction; @InjectMicroservicePort private int microServicePort; @@ -87,7 +94,10 @@ protected void initClass() throws Exception { new LongWaitStatusStoreService(cacheBackedDao, connectionTimeout); FrameworkServiceDataHolder.getInstance().setLongWaitStatusStoreService(longWaitStatusStoreService); sequenceHandlerRunner.registerJsFunction("httpPost", new HTTPPostFunctionImpl()); - initMocks(this); + + // Mocking the executeHttpMethod method to avoid actual http calls. + httpPostFunction = spy(new HTTPPostFunctionImpl()); + doNothing().when(httpPostFunction).executeHttpMethod(any(), any()); } @AfterClass @@ -96,6 +106,12 @@ protected void tearDown() { unsetAllowedDomains(); } + @AfterMethod + protected void tearDownTest() { + + reset(httpPostFunction); + } + @Test public void testHttpPostMethod() throws JsTestException { @@ -116,22 +132,84 @@ public void testHttpPostMethodUrlValidation() throws JsTestException, NoSuchFiel + result); } + /** + * Test http post with headers. + * Check if the headers are sent with the request. + * @throws JsTestException + */ @Test - public void testHttpPostWithEventHandlersOnly() throws JsTestException { + public void testHttpPostWithHeaders() throws JsTestException { - String requestUrl = getRequestUrl("dummy-post-event-handlers"); - String result = executeHttpPostFunction(requestUrl, TEST_EVENT_HANDLERS); + String requestUrl = getRequestUrl("dummy-post-headers"); + String result = executeHttpPostFunction(requestUrl, TEST_HEADERS); assertEquals(result, SUCCESS, "The http post request was not successful. Result from request: " + result); } + /** + * Test http post only with event handlers. + * executeHttpMethod method should be called with event handlers. + * @throws JsTestException + */ @Test - public void testHttpPostWithHeaders() throws JsTestException { + public void testHttpPostWithEventHandlersOnly() throws JsTestException { - String requestUrl = getRequestUrl("dummy-post-headers"); - String result = executeHttpPostFunction(requestUrl, TEST_HEADERS); - assertEquals(result, SUCCESS, "The http post request was not successful. Result from request: " - + result); + Map eventHandlers = new HashMap<>(); + httpPostFunction.httpPost(getRequestUrl("dummy-get"), eventHandlers); + verify(httpPostFunction, times(1)).executeHttpMethod(any(HttpGet.class), eq(eventHandlers)); + } + + /** + * Test http post without headers. + * executeHttpMethod method should be called without headers. + * @throws JsTestException + */ + @Test + public void testHttpPostWithoutHeaders() throws JsTestException { + + Map payload = new HashMap<>(); + Map eventHandlers = new HashMap<>(); + httpPostFunction.httpPost(getRequestUrl("dummy-get"), payload, eventHandlers); + verify(httpPostFunction, times(1)).executeHttpMethod(any(HttpGet.class), eq(eventHandlers)); + } + + /** + * Tests the behavior of the httpPost function when provided with null headers and payload. + * @throws JsTestException + */ + @Test + public void testHttpPostWithNullHeaderAndNullPayload() throws JsTestException { + Map payload = null; + Map headers = null; + Map eventHandlers = new HashMap<>(); + httpPostFunction.httpPost(getRequestUrl("dummy-get"), payload, headers, eventHandlers); + verify(httpPostFunction, times(0)).executeHttpMethod(any(HttpGet.class), eq(eventHandlers)); + } + + /** + * Tests the behavior of the httpPost function when provided with null values inside headers or payload data. + * @throws JsTestException + */ + @Test + public void testHttpPostWithNullValuesInParams() throws JsTestException { + Map payload = new HashMap<>(); + Map headers = new HashMap<>(); + headers.put(ACCEPT, null); + payload.put(EMAIL, null); + Map eventHandlers = new HashMap<>(); + httpPostFunction.httpPost(getRequestUrl("dummy-get"), payload, headers, eventHandlers); + verify(httpPostFunction, times(1)).executeHttpMethod(any(HttpGet.class), eq(eventHandlers)); + } + + @Test + public void testHttpPostWithNullKeysInParams() throws JsTestException { + Map payload = new HashMap<>(); + Map headers = new HashMap<>(); + headers.put(null, "something"); + payload.put(null, "something"); + Map eventHandlers = new HashMap<>(); + httpPostFunction.httpPost(getRequestUrl("dummy-get"), payload, headers, eventHandlers); + verify(httpPostFunction, times(1)).executeHttpMethod(any(HttpGet.class), eq(eventHandlers)); } private void setAllowedDomain(String domain) { @@ -196,17 +274,13 @@ public Map dummyPost(Map data) { return response; } - @POST - @Path("/dummy-post-event-handlers") - @Produces("application/json") - @Consumes("application/json") - public Map dummyPost() { - - Map response = new HashMap<>(); - response.put(STATUS, SUCCESS); - return response; - } - + /** + * Dummy post method to test payload and headers. + * Check if the payload data and headers are sent with the request. + * @param authorization + * @param data + * @return + */ @POST @Path("/dummy-post-headers") @Produces("application/json") From eb5905c515c2b7bb23dd0a9204a3d1a81084cccf Mon Sep 17 00:00:00 2001 From: PasinduYeshan Date: Tue, 8 Aug 2023 08:01:53 +0530 Subject: [PATCH 10/18] refactor code --- .../conditional/auth/functions/http/HTTPPostFunctionImpl.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImpl.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImpl.java index bea3d66f..0d3942fb 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImpl.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImpl.java @@ -122,8 +122,7 @@ public void httpPost(String epUrl, Object... params) { JSONObject jsonObject = new JSONObject(); for (Map.Entry dataElements : payloadData.entrySet()) { if (!StringUtils.isEmpty(dataElements.getKey())) { - Object value = (dataElements.getValue() != null) ? dataElements.getValue() : null; - jsonObject.put(dataElements.getKey(), value); + jsonObject.put(dataElements.getKey(), dataElements.getValue()); } } request.setEntity(new StringEntity(jsonObject.toJSONString(), StandardCharsets.UTF_8)); From 1ed40baf063069b5a3a0e0fa857ed21f7a366299 Mon Sep 17 00:00:00 2001 From: PasinduYeshan Date: Wed, 9 Aug 2023 09:00:11 +0530 Subject: [PATCH 11/18] refactor code --- .../conditional/auth/functions/http/AbstractHTTPFunction.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/AbstractHTTPFunction.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/AbstractHTTPFunction.java index 48ee234d..bab402ff 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/AbstractHTTPFunction.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/AbstractHTTPFunction.java @@ -92,10 +92,10 @@ protected void executeHttpMethod(HttpUriRequest request, Map eve try (CloseableHttpResponse response = client.execute(request)) { responseCode = response.getStatusLine().getStatusCode(); - Header contentType = response.getEntity().getContentType(); if (responseCode >= 200 && responseCode < 300) { outcome = Constants.OUTCOME_SUCCESS; if (response.getEntity() != null) { + Header contentType = response.getEntity().getContentType(); String responseBody = EntityUtils.toString(response.getEntity()); if (contentType != null && contentType.getValue().contains("application/json")) { JSONParser parser = new JSONParser(); From 20f2692f573acf34d029778ce81fae857ae257cc Mon Sep 17 00:00:00 2001 From: PasinduYeshan Date: Wed, 9 Aug 2023 09:14:26 +0530 Subject: [PATCH 12/18] refactor code --- .../auth/functions/http/AbstractHTTPFunction.java | 12 ++++++------ .../auth/functions/http/HTTPGetFunctionImpl.java | 3 +-- .../auth/functions/http/HTTPPostFunctionImpl.java | 6 +++--- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/AbstractHTTPFunction.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/AbstractHTTPFunction.java index bab402ff..7ad1cd5a 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/AbstractHTTPFunction.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/AbstractHTTPFunction.java @@ -96,14 +96,14 @@ protected void executeHttpMethod(HttpUriRequest request, Map eve outcome = Constants.OUTCOME_SUCCESS; if (response.getEntity() != null) { Header contentType = response.getEntity().getContentType(); - String responseBody = EntityUtils.toString(response.getEntity()); - if (contentType != null && contentType.getValue().contains("application/json")) { - JSONParser parser = new JSONParser(); - json = (JSONObject) parser.parse(responseBody); - } else if (contentType != null && contentType.getValue().contains("text/plain")) { + String jsonString = EntityUtils.toString(response.getEntity()); + if (contentType != null && contentType.getValue().contains("text/plain")) { // For 'text/plain', put the response body into the JSON object as a single field. json = new JSONObject(); - json.put("response", responseBody); + json.put("response", jsonString); + } else { + JSONParser parser = new JSONParser(); + json = (JSONObject) parser.parse(jsonString); } } } else { diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImpl.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImpl.java index 86dd4f98..9b54d0ed 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImpl.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImpl.java @@ -43,7 +43,7 @@ public HTTPGetFunctionImpl() { @Override public void httpGet(String epUrl, Object... params) { - Map eventHandlers = new HashMap<>(); + Map eventHandlers; Map headers = new HashMap<>(); if (StringUtils.isBlank(epUrl)) { @@ -76,7 +76,6 @@ public void httpGet(String epUrl, Object... params) { } HttpGet request = new HttpGet(epUrl); - // Set default ACCEPT header to application/json request.setHeader(ACCEPT, TYPE_APPLICATION_JSON); headers.entrySet().stream() diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImpl.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImpl.java index 0d3942fb..38dbd3df 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImpl.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImpl.java @@ -54,7 +54,7 @@ public HTTPPostFunctionImpl() { @Override public void httpPost(String epUrl, Object... params) { - Map eventHandlers = new HashMap<>(); + Map eventHandlers; Map payloadData = new HashMap<>(); Map headers = new HashMap<>(); @@ -106,9 +106,9 @@ public void httpPost(String epUrl, Object... params) { .forEach(entry -> request.setHeader(entry.getKey(), entry.getValue())); - //For the header "Content-Type : application/x-www-form-urlencoded" request body data is set to - // UrlEncodedFormEntity format. For the other cases request body data is set to StringEntity format. if (MapUtils.isNotEmpty(payloadData)) { + //For the header "Content-Type : application/x-www-form-urlencoded" request body data is set to + // UrlEncodedFormEntity format. For the other cases request body data is set to StringEntity format. if (TYPE_APPLICATION_FORM_URLENCODED.equals(headers.get(CONTENT_TYPE))) { List entities = new ArrayList<>(); for (Map.Entry dataElements : payloadData.entrySet()) { From fca4c6855908b1d2424d9f36b2450fd37ee8e8fd Mon Sep 17 00:00:00 2001 From: PasinduYeshan Date: Tue, 22 Aug 2023 15:45:47 +0530 Subject: [PATCH 13/18] Fix error handling issue and update unit tests --- .../functions/http/AbstractHTTPFunction.java | 2 - .../functions/http/HTTPGetFunctionImpl.java | 62 ++++----- .../functions/http/HTTPPostFunctionImpl.java | 122 ++++++++---------- .../http/HTTPGetFunctionImplTest.java | 44 ++----- .../http/HTTPPostFunctionImplTest.java | 74 ++--------- 5 files changed, 98 insertions(+), 206 deletions(-) diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/AbstractHTTPFunction.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/AbstractHTTPFunction.java index 7ad1cd5a..0dcc92d1 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/AbstractHTTPFunction.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/AbstractHTTPFunction.java @@ -21,7 +21,6 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.http.Header; -import org.apache.http.HttpEntity; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpUriRequest; @@ -52,7 +51,6 @@ public abstract class AbstractHTTPFunction { private static final Log LOG = LogFactory.getLog(AbstractHTTPFunction.class); protected static final String TYPE_APPLICATION_JSON = "application/json"; protected static final String TYPE_APPLICATION_FORM_URLENCODED = "application/x-www-form-urlencoded"; - protected static final String TYPE_APPLICATION_XML = "application/xml"; private static final char DOMAIN_SEPARATOR = '.'; private final List allowedDomains; diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImpl.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImpl.java index 9b54d0ed..2c7d269d 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImpl.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImpl.java @@ -46,47 +46,33 @@ public void httpGet(String epUrl, Object... params) { Map eventHandlers; Map headers = new HashMap<>(); - if (StringUtils.isBlank(epUrl)) { - LOG.error("Endpoint URL cannot be empty."); - return; + switch (params.length) { + case 1: + if (params[0] instanceof Map) { + eventHandlers = (Map) params[0]; + } else { + throw new IllegalArgumentException("Invalid argument type."); + } + break; + case 2: + if (params[0] instanceof Map && params[1] instanceof Map ) { + headers = (Map) params[0]; + eventHandlers = (Map) params[1]; + } else { + throw new IllegalArgumentException("Invalid argument type."); + } + break; + default: + throw new IllegalArgumentException("Invalid number of argument."); } - try { - switch (params.length) { - case 1: - if (params[0] instanceof Map) { - eventHandlers = (Map) params[0]; - } else { - LOG.error("Invalid parameter type."); - return; - } - break; - case 2: - if (params[0] instanceof Map && params[1] instanceof Map ) { - headers = (Map) params[0]; - eventHandlers = (Map) params[1]; - } else { - LOG.error("Invalid parameter type."); - return; - } - break; - default: - LOG.error("Invalid number of parameters."); - return; - } + HttpGet request = new HttpGet(epUrl); - HttpGet request = new HttpGet(epUrl); - request.setHeader(ACCEPT, TYPE_APPLICATION_JSON); + headers.putIfAbsent(ACCEPT, TYPE_APPLICATION_JSON); + headers.entrySet().stream() + .filter(entry -> !StringUtils.isBlank(entry.getKey()) && !entry.getKey().equals("null")) + .forEach(entry -> request.setHeader(entry.getKey(), entry.getValue())); - headers.entrySet().stream() - .filter(entry -> entry.getKey() != null) - .forEach(entry -> request.setHeader(entry.getKey(), entry.getValue())); - - executeHttpMethod(request, eventHandlers); - } catch (IllegalArgumentException e) { - LOG.error("Invalid parameter type.", e); - } catch (Exception e) { - LOG.error("Error while executing http get.", e); - } + executeHttpMethod(request, eventHandlers); } } diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImpl.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImpl.java index 38dbd3df..921ede79 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImpl.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImpl.java @@ -58,82 +58,62 @@ public void httpPost(String epUrl, Object... params) { Map payloadData = new HashMap<>(); Map headers = new HashMap<>(); - if (StringUtils.isBlank(epUrl)) { - LOG.error("Endpoint URL cannot be empty."); - return; + switch (params.length) { + case 1: + if (params[0] instanceof Map) { + eventHandlers = (Map) params[0]; + } else { + throw new IllegalArgumentException("Invalid argument type."); + } + break; + case 2: + if (params[0] instanceof Map && params[1] instanceof Map) { + payloadData = (Map) params[0]; + eventHandlers = (Map) params[1]; + } else { + throw new IllegalArgumentException("Invalid argument type."); + } + break; + case 3: + if (params[0] instanceof Map && params[1] instanceof Map && params[2] instanceof Map) { + payloadData = (Map) params[0]; + headers = (Map) params[1]; + eventHandlers = (Map) params[2]; + } else { + throw new IllegalArgumentException("Invalid argument type."); + } + break; + default: + throw new IllegalArgumentException("Invalid number of argument."); } - try { - switch (params.length) { - case 1: - if (params[0] instanceof Map) { - eventHandlers = (Map) params[0]; - } else { - LOG.error("Invalid parameter type."); - return; - } - break; - case 2: - if (params[0] instanceof Map && params[1] instanceof Map) { - payloadData = (Map) params[0]; - eventHandlers = (Map) params[1]; - } else { - LOG.error("Invalid parameter type."); - return; - } - break; - case 3: - if (params[0] instanceof Map && params[1] instanceof Map && params[2] instanceof Map) { - payloadData = (Map) params[0]; - headers = (Map) params[1]; - eventHandlers = (Map) params[2]; - } else { - LOG.error("Invalid parameter type."); - return; - } - break; - default: - LOG.error("Invalid number of parameters."); - return; - } + HttpPost request = new HttpPost(epUrl); - HttpPost request = new HttpPost(epUrl); - request.setHeader(ACCEPT, TYPE_APPLICATION_JSON); - - headers.putIfAbsent(CONTENT_TYPE, TYPE_APPLICATION_JSON); - headers.entrySet().stream() - .filter(entry -> entry.getKey() != null) - .forEach(entry -> request.setHeader(entry.getKey(), entry.getValue())); - - - if (MapUtils.isNotEmpty(payloadData)) { - //For the header "Content-Type : application/x-www-form-urlencoded" request body data is set to - // UrlEncodedFormEntity format. For the other cases request body data is set to StringEntity format. - if (TYPE_APPLICATION_FORM_URLENCODED.equals(headers.get(CONTENT_TYPE))) { - List entities = new ArrayList<>(); - for (Map.Entry dataElements : payloadData.entrySet()) { - if (!StringUtils.isEmpty(dataElements.getKey())) { - String value = (dataElements.getValue() != null) ? dataElements.getValue().toString() : null; - entities.add(new BasicNameValuePair(dataElements.getKey(), value)); - } - } - request.setEntity(new UrlEncodedFormEntity(entities, StandardCharsets.UTF_8)); - } else { - JSONObject jsonObject = new JSONObject(); - for (Map.Entry dataElements : payloadData.entrySet()) { - if (!StringUtils.isEmpty(dataElements.getKey())) { - jsonObject.put(dataElements.getKey(), dataElements.getValue()); - } - } - request.setEntity(new StringEntity(jsonObject.toJSONString(), StandardCharsets.UTF_8)); + headers.putIfAbsent(CONTENT_TYPE, TYPE_APPLICATION_JSON); + headers.putIfAbsent(ACCEPT, TYPE_APPLICATION_JSON); + headers.entrySet().stream() + .filter(entry -> !StringUtils.isBlank(entry.getKey()) && !entry.getKey().equals("null")) + .forEach(entry -> request.setHeader(entry.getKey(), entry.getValue())); + + + if (MapUtils.isNotEmpty(payloadData)) { + //For the header "Content-Type : application/x-www-form-urlencoded" request body data is set to + // UrlEncodedFormEntity format. For the other cases request body data is set to StringEntity format. + if (TYPE_APPLICATION_FORM_URLENCODED.equals(headers.get(CONTENT_TYPE))) { + List entities = new ArrayList<>(); + for (Map.Entry dataElements : payloadData.entrySet()) { + String value = (dataElements.getValue() != null) ? dataElements.getValue().toString() : null; + entities.add(new BasicNameValuePair(dataElements.getKey(), value)); } + request.setEntity(new UrlEncodedFormEntity(entities, StandardCharsets.UTF_8)); + } else { + JSONObject jsonObject = new JSONObject(); + for (Map.Entry dataElements : payloadData.entrySet()) { + jsonObject.put(dataElements.getKey(), dataElements.getValue()); + } + request.setEntity(new StringEntity(jsonObject.toJSONString(), StandardCharsets.UTF_8)); } - executeHttpMethod(request, eventHandlers); - - } catch (IllegalArgumentException e) { - LOG.error("Invalid parameter type.", e); - } catch (Exception e) { - LOG.error("Error while executing http post request.", e); } + executeHttpMethod(request, eventHandlers); } } diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImplTest.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImplTest.java index 1eaa3836..7ab5a62c 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImplTest.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImplTest.java @@ -17,7 +17,6 @@ package org.wso2.carbon.identity.conditional.auth.functions.http; -import org.apache.http.client.methods.HttpGet; import org.testng.annotations.AfterClass; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeClass; @@ -51,13 +50,9 @@ import javax.ws.rs.Path; import javax.ws.rs.Produces; -import static org.apache.http.HttpHeaders.ACCEPT; import static org.mockito.Matchers.any; -import static org.mockito.Matchers.eq; import static org.mockito.Mockito.reset; import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; import static org.mockito.Mockito.doNothing; import static org.testng.Assert.assertEquals; @@ -146,44 +141,23 @@ public void testHttpGetMethodWithHeaders() throws JsTestException { } /** - * Tests the behavior of the httpPost function when provided with null headers. - * executeHttpMethod method should not be called. - * @throws JsTestException + * Tests the behavior of the httpGet function when provided with null headers. + * @throws IllegalArgumentException if the provided arguments are not valid. */ - @Test + @Test(expectedExceptions = IllegalArgumentException.class) public void testHttpGetWithNullHeaders() { - Map headers = null; Map eventHandlers = new HashMap<>(); - httpGetFunction.httpGet(getRequestUrl("dummy-get"), headers, eventHandlers); - verify(httpGetFunction, times(0)).executeHttpMethod(any(HttpGet.class), eq(eventHandlers)); + httpGetFunction.httpGet(getRequestUrl("dummy-get"), null, eventHandlers); } /** - * Tests the behavior of the httpPost function when provided with null values in headers. - * executeHttpMethod method should be called. + * Tests the behavior of the httpGet function when invalid number of arguments are provided. + * @throws IllegalArgumentException if the provided arguments are not valid. */ - @Test - public void testHttpGetWithNullValuesInHeaders() { - Map headers = new HashMap<>(); - headers.put(ACCEPT, "application/json"); - headers.put("test", null); - Map eventHandlers = new HashMap<>(); - httpGetFunction.httpGet(getRequestUrl("dummy-get"), headers, eventHandlers); - verify(httpGetFunction, times(1)).executeHttpMethod(any(HttpGet.class), eq(eventHandlers)); - } - - /** - * Tests the behavior of the httpPost function when provided with null keys in headers. - * executeHttpMethod method should be called and null keys should be ignored. - */ - @Test - public void testHttpGetWithNullKeysInHeaders() { - Map headers = new HashMap<>(); - headers.put(ACCEPT, "application/json"); - headers.put(null, null); + @Test(expectedExceptions = IllegalArgumentException.class) + public void testHttpGetWithInvalidNumberOfArguments() { Map eventHandlers = new HashMap<>(); - httpGetFunction.httpGet(getRequestUrl("dummy-get"), headers, eventHandlers); - verify(httpGetFunction, times(1)).executeHttpMethod(any(HttpGet.class), eq(eventHandlers)); + httpGetFunction.httpGet(getRequestUrl("dummy-get"), eventHandlers, eventHandlers, eventHandlers); } private void setAllowedDomain(String domain) { diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImplTest.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImplTest.java index 3974a51e..c9dcf2e7 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImplTest.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImplTest.java @@ -17,7 +17,6 @@ package org.wso2.carbon.identity.conditional.auth.functions.http; -import org.apache.http.client.methods.HttpGet; import org.testng.annotations.AfterClass; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeClass; @@ -52,13 +51,9 @@ import javax.ws.rs.POST; import javax.ws.rs.Produces; -import static org.apache.http.HttpHeaders.ACCEPT; import static org.mockito.Matchers.any; -import static org.mockito.Matchers.eq; import static org.mockito.Mockito.reset; import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; import static org.mockito.Mockito.doNothing; import static org.testng.Assert.assertEquals; @@ -133,7 +128,7 @@ public void testHttpPostMethodUrlValidation() throws JsTestException, NoSuchFiel } /** - * Test http post with headers. + * Test httpPost with headers. * Check if the headers are sent with the request. * @throws JsTestException */ @@ -147,69 +142,28 @@ public void testHttpPostWithHeaders() throws JsTestException { } /** - * Test http post only with event handlers. - * executeHttpMethod method should be called with event handlers. - * @throws JsTestException + * Tests the behavior of the httpPost function when provided with null headers. + * @throws IllegalArgumentException if the provided arguments are not valid. */ - @Test - public void testHttpPostWithEventHandlersOnly() throws JsTestException { - - Map eventHandlers = new HashMap<>(); - httpPostFunction.httpPost(getRequestUrl("dummy-get"), eventHandlers); - verify(httpPostFunction, times(1)).executeHttpMethod(any(HttpGet.class), eq(eventHandlers)); - } - - /** - * Test http post without headers. - * executeHttpMethod method should be called without headers. - * @throws JsTestException - */ - @Test - public void testHttpPostWithoutHeaders() throws JsTestException { - - Map payload = new HashMap<>(); - Map eventHandlers = new HashMap<>(); - httpPostFunction.httpPost(getRequestUrl("dummy-get"), payload, eventHandlers); - verify(httpPostFunction, times(1)).executeHttpMethod(any(HttpGet.class), eq(eventHandlers)); - } + @Test(expectedExceptions = IllegalArgumentException.class) + public void testHttpPostWithNullHeaders() { - /** - * Tests the behavior of the httpPost function when provided with null headers and payload. - * @throws JsTestException - */ - @Test - public void testHttpPostWithNullHeaderAndNullPayload() throws JsTestException { - Map payload = null; - Map headers = null; + Map payloadData = new HashMap<>(); Map eventHandlers = new HashMap<>(); - httpPostFunction.httpPost(getRequestUrl("dummy-get"), payload, headers, eventHandlers); - verify(httpPostFunction, times(0)).executeHttpMethod(any(HttpGet.class), eq(eventHandlers)); + httpPostFunction.httpPost(getRequestUrl("dummy-post"), payloadData, null, eventHandlers); } /** - * Tests the behavior of the httpPost function when provided with null values inside headers or payload data. - * @throws JsTestException + * Tests the behavior of the httpPost function when provided with invalid number of arguments. + * @throws IllegalArgumentException if the provided arguments are not valid. */ - @Test - public void testHttpPostWithNullValuesInParams() throws JsTestException { - Map payload = new HashMap<>(); - Map headers = new HashMap<>(); - headers.put(ACCEPT, null); - payload.put(EMAIL, null); - Map eventHandlers = new HashMap<>(); - httpPostFunction.httpPost(getRequestUrl("dummy-get"), payload, headers, eventHandlers); - verify(httpPostFunction, times(1)).executeHttpMethod(any(HttpGet.class), eq(eventHandlers)); - } + @Test(expectedExceptions = IllegalArgumentException.class) + public void testHttpPostWithInvalidNumberOfArguments() { - @Test - public void testHttpPostWithNullKeysInParams() throws JsTestException { - Map payload = new HashMap<>(); - Map headers = new HashMap<>(); - headers.put(null, "something"); - payload.put(null, "something"); + Map payloadData = new HashMap<>(); + Map headers = new HashMap<>(); Map eventHandlers = new HashMap<>(); - httpPostFunction.httpPost(getRequestUrl("dummy-get"), payload, headers, eventHandlers); - verify(httpPostFunction, times(1)).executeHttpMethod(any(HttpGet.class), eq(eventHandlers)); + httpPostFunction.httpPost(getRequestUrl("dummy-post"), payloadData, headers, eventHandlers, eventHandlers); } private void setAllowedDomain(String domain) { From a50bfce18d16c4888b94b750fd6a44bdff5666a5 Mon Sep 17 00:00:00 2001 From: PasinduYeshan Date: Tue, 22 Aug 2023 16:22:38 +0530 Subject: [PATCH 14/18] Refactor code --- .../conditional/auth/functions/http/AbstractHTTPFunction.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/AbstractHTTPFunction.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/AbstractHTTPFunction.java index 0dcc92d1..bad35da8 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/AbstractHTTPFunction.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/AbstractHTTPFunction.java @@ -51,6 +51,7 @@ public abstract class AbstractHTTPFunction { private static final Log LOG = LogFactory.getLog(AbstractHTTPFunction.class); protected static final String TYPE_APPLICATION_JSON = "application/json"; protected static final String TYPE_APPLICATION_FORM_URLENCODED = "application/x-www-form-urlencoded"; + protected static final String TYPE_TEXT_PLAIN = "text/plain"; private static final char DOMAIN_SEPARATOR = '.'; private final List allowedDomains; @@ -95,7 +96,7 @@ protected void executeHttpMethod(HttpUriRequest request, Map eve if (response.getEntity() != null) { Header contentType = response.getEntity().getContentType(); String jsonString = EntityUtils.toString(response.getEntity()); - if (contentType != null && contentType.getValue().contains("text/plain")) { + if (contentType != null && contentType.getValue().contains(TYPE_TEXT_PLAIN)) { // For 'text/plain', put the response body into the JSON object as a single field. json = new JSONObject(); json.put("response", jsonString); From f529f9879dc8d5b7ac378837a5f1c06d1a1c4a2a Mon Sep 17 00:00:00 2001 From: PasinduYeshan Date: Thu, 24 Aug 2023 09:51:57 +0530 Subject: [PATCH 15/18] Improve error logs, change epUrl->endpointURL, refactor --- .../functions/http/AbstractHTTPFunction.java | 42 ++++++++++++++++--- .../auth/functions/http/HTTPGetFunction.java | 4 +- .../functions/http/HTTPGetFunctionImpl.java | 21 +++++----- .../auth/functions/http/HTTPPostFunction.java | 4 +- .../functions/http/HTTPPostFunctionImpl.java | 36 ++++++++-------- 5 files changed, 68 insertions(+), 39 deletions(-) diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/AbstractHTTPFunction.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/AbstractHTTPFunction.java index bad35da8..4db3af73 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/AbstractHTTPFunction.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/AbstractHTTPFunction.java @@ -43,6 +43,8 @@ import java.util.List; import java.util.Map; +import static org.apache.http.HttpHeaders.ACCEPT; + /** * Abstract class for handling http calls. */ @@ -76,15 +78,15 @@ protected void executeHttpMethod(HttpUriRequest request, Map eve JSONObject json = null; int responseCode; String outcome; - String epUrl = null; + String endpointURL = null; if (request.getURI() != null) { - epUrl = request.getURI().toString(); + endpointURL = request.getURI().toString(); } if (!isValidRequestDomain(request.getURI())) { outcome = Constants.OUTCOME_FAIL; - LOG.error("Provided Url does not contain a allowed domain. Invalid Url: " + epUrl); + LOG.error("Provided Url does not contain a allowed domain. Invalid Url: " + endpointURL); asyncReturn.accept(context, Collections.emptyMap(), outcome); return; } @@ -110,13 +112,13 @@ protected void executeHttpMethod(HttpUriRequest request, Map eve } } catch (IllegalArgumentException e) { - LOG.error("Invalid Url: " + epUrl, e); + LOG.error("Invalid Url: " + endpointURL, e); outcome = Constants.OUTCOME_FAIL; } catch (ConnectTimeoutException e) { - LOG.error("Error while waiting to connect to " + epUrl, e); + LOG.error("Error while waiting to connect to " + endpointURL, e); outcome = Constants.OUTCOME_TIMEOUT; } catch (SocketTimeoutException e) { - LOG.error("Error while waiting for data from " + epUrl, e); + LOG.error("Error while waiting for data from " + endpointURL, e); outcome = Constants.OUTCOME_TIMEOUT; } catch (IOException e) { LOG.error("Error while calling endpoint. ", e); @@ -184,4 +186,32 @@ private String getParentDomainFromUrl(URI url) { } return parentDomain; } + + /** + * Validate the headers and return a Map of headers. + * @param headers Map of headers. + */ + protected Map validateHeaders(Map headers) { + + for (Map.Entry entry : headers.entrySet()) { + if (!(entry.getValue() instanceof String)) { + throw new IllegalArgumentException("Header values must be of type String"); + } + } + return (Map) headers; + } + + /** + * Set headers to the request. + * Default Accept header is set to application/json. + * @param request HttpUriRequest. + * @param headers Map of headers. + */ + protected void setHeaders(HttpUriRequest request, Map headers) { + + headers.putIfAbsent(ACCEPT, TYPE_APPLICATION_JSON); + headers.entrySet().stream() + .filter(entry -> StringUtils.isNotBlank(entry.getKey()) && !entry.getKey().equals("null")) + .forEach(entry -> request.setHeader(entry.getKey(), entry.getValue())); + } } diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunction.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunction.java index 4ae7a031..19e23932 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunction.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunction.java @@ -29,10 +29,10 @@ public interface HTTPGetFunction { /** * POST data to the given endpoint. * - * @param epUrl Endpoint url. + * @param endpointURL Endpoint url. * @param params Parameters. * 1. headers headers (optional). * 2. eventHandlers event handlers. */ - void httpGet(String epUrl, Object... params); + void httpGet(String endpointURL, Object... params); } diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImpl.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImpl.java index 2c7d269d..d85e0be0 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImpl.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImpl.java @@ -41,7 +41,7 @@ public HTTPGetFunctionImpl() { } @Override - public void httpGet(String epUrl, Object... params) { + public void httpGet(String endpointURL, Object... params) { Map eventHandlers; Map headers = new HashMap<>(); @@ -51,27 +51,26 @@ public void httpGet(String epUrl, Object... params) { if (params[0] instanceof Map) { eventHandlers = (Map) params[0]; } else { - throw new IllegalArgumentException("Invalid argument type."); + throw new IllegalArgumentException("Invalid argument type. Expected eventHandlers " + + "(Map)."); } break; case 2: if (params[0] instanceof Map && params[1] instanceof Map ) { - headers = (Map) params[0]; + headers = validateHeaders((Map) params[0]); eventHandlers = (Map) params[1]; } else { - throw new IllegalArgumentException("Invalid argument type."); + throw new IllegalArgumentException("Invalid argument types. Expected headers (Map) " + + "and eventHandlers (Map) respectively."); } break; default: - throw new IllegalArgumentException("Invalid number of argument."); + throw new IllegalArgumentException("Invalid number of arguments. Expected 1 or 2, but got: " + + params.length + "."); } - HttpGet request = new HttpGet(epUrl); - - headers.putIfAbsent(ACCEPT, TYPE_APPLICATION_JSON); - headers.entrySet().stream() - .filter(entry -> !StringUtils.isBlank(entry.getKey()) && !entry.getKey().equals("null")) - .forEach(entry -> request.setHeader(entry.getKey(), entry.getValue())); + HttpGet request = new HttpGet(endpointURL); + setHeaders(request, headers); executeHttpMethod(request, eventHandlers); } diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunction.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunction.java index d19318e1..7ec750ce 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunction.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunction.java @@ -30,11 +30,11 @@ public interface HTTPPostFunction { /** * POST data to the given endpoint. * - * @param epUrl Endpoint url. + * @param endpointURL Endpoint url. * @param params parameters. * 1. payloadData payload data. * 2. headers headers (optional). * 3. eventHandlers event handlers. */ - void httpPost(String epUrl, Object... params); + void httpPost(String endpointURL, Object... params); } diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImpl.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImpl.java index 921ede79..b00084ce 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImpl.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImpl.java @@ -52,7 +52,7 @@ public HTTPPostFunctionImpl() { } @Override - public void httpPost(String epUrl, Object... params) { + public void httpPost(String endpointURL, Object... params) { Map eventHandlers; Map payloadData = new HashMap<>(); @@ -63,7 +63,8 @@ public void httpPost(String epUrl, Object... params) { if (params[0] instanceof Map) { eventHandlers = (Map) params[0]; } else { - throw new IllegalArgumentException("Invalid argument type."); + throw new IllegalArgumentException("Invalid argument type. Expected eventHandlers " + + "(Map)."); } break; case 2: @@ -71,34 +72,35 @@ public void httpPost(String epUrl, Object... params) { payloadData = (Map) params[0]; eventHandlers = (Map) params[1]; } else { - throw new IllegalArgumentException("Invalid argument type."); + throw new IllegalArgumentException("Invalid argument types. Expected payloadData and eventHandlers " + + "(both of type Map) respectively."); } break; case 3: if (params[0] instanceof Map && params[1] instanceof Map && params[2] instanceof Map) { payloadData = (Map) params[0]; - headers = (Map) params[1]; + headers = validateHeaders((Map) params[1]); eventHandlers = (Map) params[2]; } else { - throw new IllegalArgumentException("Invalid argument type."); + throw new IllegalArgumentException("Invalid argument type. Expected payloadData " + + "(Map), headers (Map), and eventHandlers " + + "(Map) respectively."); } break; default: - throw new IllegalArgumentException("Invalid number of argument."); + throw new IllegalArgumentException("Invalid number of arguments. Expected 1, 2, or 3. Found: " + + params.length + "."); } - HttpPost request = new HttpPost(epUrl); - + HttpPost request = new HttpPost(endpointURL); headers.putIfAbsent(CONTENT_TYPE, TYPE_APPLICATION_JSON); - headers.putIfAbsent(ACCEPT, TYPE_APPLICATION_JSON); - headers.entrySet().stream() - .filter(entry -> !StringUtils.isBlank(entry.getKey()) && !entry.getKey().equals("null")) - .forEach(entry -> request.setHeader(entry.getKey(), entry.getValue())); - + setHeaders(request, headers); if (MapUtils.isNotEmpty(payloadData)) { - //For the header "Content-Type : application/x-www-form-urlencoded" request body data is set to - // UrlEncodedFormEntity format. For the other cases request body data is set to StringEntity format. + /* + * For the header "Content-Type : application/x-www-form-urlencoded" request body data is set to + * UrlEncodedFormEntity format. For the other cases request body data is set to StringEntity format. + */ if (TYPE_APPLICATION_FORM_URLENCODED.equals(headers.get(CONTENT_TYPE))) { List entities = new ArrayList<>(); for (Map.Entry dataElements : payloadData.entrySet()) { @@ -108,9 +110,7 @@ public void httpPost(String epUrl, Object... params) { request.setEntity(new UrlEncodedFormEntity(entities, StandardCharsets.UTF_8)); } else { JSONObject jsonObject = new JSONObject(); - for (Map.Entry dataElements : payloadData.entrySet()) { - jsonObject.put(dataElements.getKey(), dataElements.getValue()); - } + jsonObject.putAll(payloadData); request.setEntity(new StringEntity(jsonObject.toJSONString(), StandardCharsets.UTF_8)); } } From 7465abc79edd169b160f7775c9c14e342000b2e7 Mon Sep 17 00:00:00 2001 From: PasinduYeshan Date: Thu, 24 Aug 2023 17:25:36 +0530 Subject: [PATCH 16/18] Change copyrights --- .../auth/functions/http/http-get-test-headers.xml | 5 +++-- .../auth/functions/http/http-post-test-headers.xml | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/resources/org/wso2/carbon/identity/conditional/auth/functions/http/http-get-test-headers.xml b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/resources/org/wso2/carbon/identity/conditional/auth/functions/http/http-get-test-headers.xml index 5464fa20..742a7f3e 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/resources/org/wso2/carbon/identity/conditional/auth/functions/http/http-get-test-headers.xml +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/resources/org/wso2/carbon/identity/conditional/auth/functions/http/http-get-test-headers.xml @@ -1,7 +1,7 @@ + 1 default diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/resources/org/wso2/carbon/identity/conditional/auth/functions/http/http-post-test-headers.xml b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/resources/org/wso2/carbon/identity/conditional/auth/functions/http/http-post-test-headers.xml index 16eab1e7..5599c869 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/resources/org/wso2/carbon/identity/conditional/auth/functions/http/http-post-test-headers.xml +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/resources/org/wso2/carbon/identity/conditional/auth/functions/http/http-post-test-headers.xml @@ -1,7 +1,7 @@ + 1 default From 18d4d523c10f5b8ff40f9b8b11af4994466aa245 Mon Sep 17 00:00:00 2001 From: PasinduYeshan Date: Tue, 5 Sep 2023 11:03:14 +0530 Subject: [PATCH 17/18] Change license header --- .../conditional/auth/functions/http/AbstractHTTPFunction.java | 2 +- .../conditional/auth/functions/http/HTTPGetFunction.java | 2 +- .../conditional/auth/functions/http/HTTPGetFunctionImpl.java | 2 +- .../conditional/auth/functions/http/HTTPPostFunction.java | 2 +- .../conditional/auth/functions/http/HTTPPostFunctionImpl.java | 2 +- .../functions/http/internal/HTTPFunctionsServiceComponent.java | 2 +- .../auth/functions/http/HTTPGetFunctionImplTest.java | 2 +- .../auth/functions/http/HTTPPostFunctionImplTest.java | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/AbstractHTTPFunction.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/AbstractHTTPFunction.java index 4db3af73..641f99cd 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/AbstractHTTPFunction.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/AbstractHTTPFunction.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.com). + * Copyright (c) 2021, WSO2 LLC. (http://www.wso2.com). * * WSO2 Inc. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunction.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunction.java index 19e23932..87d04fab 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunction.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunction.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * Copyright (c) 2018, WSO2 LLC. (http://www.wso2.org) All Rights Reserved. * * WSO2 Inc. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImpl.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImpl.java index d85e0be0..5d2016c3 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImpl.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * Copyright (c) 2018, WSO2 LLC. (http://www.wso2.org) All Rights Reserved. * * WSO2 Inc. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunction.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunction.java index 7ec750ce..ec1efbc7 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunction.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunction.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * Copyright (c) 2018, WSO2 LLC. (http://www.wso2.org) All Rights Reserved. * * WSO2 Inc. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImpl.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImpl.java index b00084ce..86018a7e 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImpl.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * Copyright (c) 2018, WSO2 LLC. (http://www.wso2.org) All Rights Reserved. * * WSO2 Inc. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/internal/HTTPFunctionsServiceComponent.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/internal/HTTPFunctionsServiceComponent.java index f2335bea..2c60b636 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/internal/HTTPFunctionsServiceComponent.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/internal/HTTPFunctionsServiceComponent.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * Copyright (c) 2018, WSO2 LLC. (http://www.wso2.org) All Rights Reserved. * * WSO2 Inc. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImplTest.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImplTest.java index 7ab5a62c..4bde47d3 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImplTest.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImplTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.com). + * Copyright (c) 2021, WSO2 LLC. (http://www.wso2.com). * * WSO2 Inc. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImplTest.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImplTest.java index c9dcf2e7..66ec283b 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImplTest.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImplTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.com). + * Copyright (c) 2021, WSO2 LLC. (http://www.wso2.com). * * WSO2 Inc. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except From 053e760da0fb8c3528bc8ee40d08eb2b92243320 Mon Sep 17 00:00:00 2001 From: Pasindu Yeshan <61885844+PasinduYeshan@users.noreply.github.com> Date: Wed, 6 Sep 2023 14:05:14 +0530 Subject: [PATCH 18/18] Refactor comments. --- .../auth/functions/http/AbstractHTTPFunction.java | 8 ++++++-- .../auth/functions/http/HTTPPostFunctionImpl.java | 4 ++-- .../auth/functions/http/HTTPGetFunctionImplTest.java | 3 +++ .../auth/functions/http/HTTPPostFunctionImplTest.java | 3 +++ 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/AbstractHTTPFunction.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/AbstractHTTPFunction.java index 641f99cd..58fb58aa 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/AbstractHTTPFunction.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/AbstractHTTPFunction.java @@ -55,6 +55,7 @@ public abstract class AbstractHTTPFunction { protected static final String TYPE_APPLICATION_FORM_URLENCODED = "application/x-www-form-urlencoded"; protected static final String TYPE_TEXT_PLAIN = "text/plain"; private static final char DOMAIN_SEPARATOR = '.'; + private static final String RESPONSE = "response"; private final List allowedDomains; private CloseableHttpClient client; @@ -101,7 +102,7 @@ protected void executeHttpMethod(HttpUriRequest request, Map eve if (contentType != null && contentType.getValue().contains(TYPE_TEXT_PLAIN)) { // For 'text/plain', put the response body into the JSON object as a single field. json = new JSONObject(); - json.put("response", jsonString); + json.put(RESPONSE, jsonString); } else { JSONParser parser = new JSONParser(); json = (JSONObject) parser.parse(jsonString); @@ -188,8 +189,10 @@ private String getParentDomainFromUrl(URI url) { } /** - * Validate the headers and return a Map of headers. + * Validate the headers. + * * @param headers Map of headers. + * @return Map of headers. */ protected Map validateHeaders(Map headers) { @@ -204,6 +207,7 @@ protected Map validateHeaders(Map headers) { /** * Set headers to the request. * Default Accept header is set to application/json. + * * @param request HttpUriRequest. * @param headers Map of headers. */ diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImpl.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImpl.java index 86018a7e..f8becd3f 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImpl.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImpl.java @@ -98,8 +98,8 @@ public void httpPost(String endpointURL, Object... params) { if (MapUtils.isNotEmpty(payloadData)) { /* - * For the header "Content-Type : application/x-www-form-urlencoded" request body data is set to - * UrlEncodedFormEntity format. For the other cases request body data is set to StringEntity format. + For the header "Content-Type : application/x-www-form-urlencoded" request body data is set to + UrlEncodedFormEntity format. For the other cases request body data is set to StringEntity format. */ if (TYPE_APPLICATION_FORM_URLENCODED.equals(headers.get(CONTENT_TYPE))) { List entities = new ArrayList<>(); diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImplTest.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImplTest.java index 4bde47d3..cf5ebe68 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImplTest.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPGetFunctionImplTest.java @@ -129,6 +129,7 @@ public void testHttpGetMethodUrlValidation() throws JsTestException, NoSuchField /** * Test http get method with headers. * Check if the headers are sent with the request. + * * @throws JsTestException */ @Test @@ -142,6 +143,7 @@ public void testHttpGetMethodWithHeaders() throws JsTestException { /** * Tests the behavior of the httpGet function when provided with null headers. + * * @throws IllegalArgumentException if the provided arguments are not valid. */ @Test(expectedExceptions = IllegalArgumentException.class) @@ -152,6 +154,7 @@ public void testHttpGetWithNullHeaders() { /** * Tests the behavior of the httpGet function when invalid number of arguments are provided. + * * @throws IllegalArgumentException if the provided arguments are not valid. */ @Test(expectedExceptions = IllegalArgumentException.class) diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImplTest.java b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImplTest.java index 66ec283b..963f3e8d 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImplTest.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.http/src/test/java/org/wso2/carbon/identity/conditional/auth/functions/http/HTTPPostFunctionImplTest.java @@ -130,6 +130,7 @@ public void testHttpPostMethodUrlValidation() throws JsTestException, NoSuchFiel /** * Test httpPost with headers. * Check if the headers are sent with the request. + * * @throws JsTestException */ @Test @@ -143,6 +144,7 @@ public void testHttpPostWithHeaders() throws JsTestException { /** * Tests the behavior of the httpPost function when provided with null headers. + * * @throws IllegalArgumentException if the provided arguments are not valid. */ @Test(expectedExceptions = IllegalArgumentException.class) @@ -155,6 +157,7 @@ public void testHttpPostWithNullHeaders() { /** * Tests the behavior of the httpPost function when provided with invalid number of arguments. + * * @throws IllegalArgumentException if the provided arguments are not valid. */ @Test(expectedExceptions = IllegalArgumentException.class)