Skip to content

Commit

Permalink
Fixing issue when setting nonProxyHosts (#12071)
Browse files Browse the repository at this point in the history
  • Loading branch information
HiranyaKavishani authored Aug 7, 2023
1 parent 32761fa commit 09ac714
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1025,9 +1025,7 @@ void populateHttpClientConfiguration() {
int proxyPort = Integer.parseInt(configuration.getFirstProperty(APIConstants.PROXY_PORT));
String proxyUsername = configuration.getFirstProperty(APIConstants.PROXY_USERNAME);
String proxyPassword = configuration.getFirstProperty(APIConstants.PROXY_PASSWORD);
String nonProxyHostsString = configuration.getFirstProperty(APIConstants.NON_PROXY_HOSTS);
String[] nonProxyHosts = configuration.getFirstProperty(nonProxyHostsString) != null ?
nonProxyHostsString.split("\\|") : null;
String[] nonProxyHosts = getNonProxyHostsListByNonProxyHostsStringConfiguration(configuration);
String proxyProtocol = configuration.getFirstProperty(APIConstants.PROXY_PROTOCOL);
builder = builder.withProxy(proxyHost, proxyPort, proxyUsername, proxyPassword, proxyProtocol,
nonProxyHosts);
Expand Down Expand Up @@ -1071,6 +1069,18 @@ public boolean verify(String urlHostName, SSLSession session) {
configuration.setHttpClientConfiguration(builder.withConnectionParams(maxTotal, defaultMaxPerRoute)
.withSSLContext(sslContext, hostnameVerifier).build());
}

/**
* Populate list of NonProxyHosts for given nonProxyHostsString through APIManagerConfiguration
*
* @param config APIManagerConfiguration
* @return String array of proxy list
*/
String[] getNonProxyHostsListByNonProxyHostsStringConfiguration(APIManagerConfiguration config) {
String nonProxyHostsString = config.getFirstProperty(APIConstants.NON_PROXY_HOSTS);
return nonProxyHostsString != null ? nonProxyHostsString.split("\\|") : null;
}

@Reference(
name = "apim.workflow.task.service",
service = org.wso2.carbon.apimgt.api.model.WorkflowTaskService.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.wso2.carbon.apimgt.impl.APIConstants;
import org.wso2.carbon.apimgt.impl.APIManagerConfiguration;
import org.wso2.carbon.apimgt.impl.APIManagerConfigurationService;
import org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO;
Expand Down Expand Up @@ -133,6 +134,22 @@ public void testShouldActivateWhenAllPrerequisitesMet() throws Exception {
}
}

@Test
public void testShouldReturnNotNullValueForNotNullNonProxyHostsString() {

APIManagerConfiguration configuration = Mockito.mock(APIManagerConfiguration.class);
Registry registry = Mockito.mock(Registry.class);
APIManagerComponent apiManagerComponent = new APIManagerComponentWrapper(registry);
Mockito.when(configuration.getFirstProperty(APIConstants.NON_PROXY_HOSTS)).thenReturn("localhost");

try {
Assert.assertNotNull(
apiManagerComponent.getNonProxyHostsListByNonProxyHostsStringConfiguration(configuration));
} catch (Exception ex) {
Assert.fail("Unexpected exception was thrown");
}
}

@Test
public void testShouldNotContinueWhenConfigurationUnAvailable() throws Exception {
PowerMockito.mockStatic(APIUtil.class);
Expand All @@ -154,10 +171,8 @@ public void testShouldNotContinueWhenConfigurationUnAvailable() throws Exception
}
}



@AfterClass
public static void destroy() {
System.clearProperty("carbon.home");
}
}
}

0 comments on commit 09ac714

Please sign in to comment.