From 3b141a746a765700ae1e8222a13dda23b6d46bfb Mon Sep 17 00:00:00 2001 From: AJ Rice <53190766+ajrice6713@users.noreply.github.com> Date: Mon, 7 Aug 2023 13:15:53 -0400 Subject: [PATCH 1/2] SWI-3111 Add the ability to supply your own HTTP client in the IrisClient constructor --- .../com/bandwidth/iris/sdk/IrisClient.java | 14 +++++ .../com/bandwidth/iris/sdk/ProxyTests.java | 62 +++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 src/test/java/com/bandwidth/iris/sdk/ProxyTests.java diff --git a/src/main/java/com/bandwidth/iris/sdk/IrisClient.java b/src/main/java/com/bandwidth/iris/sdk/IrisClient.java index 0966dbb..beb1596 100644 --- a/src/main/java/com/bandwidth/iris/sdk/IrisClient.java +++ b/src/main/java/com/bandwidth/iris/sdk/IrisClient.java @@ -5,12 +5,14 @@ import com.bandwidth.iris.sdk.utils.XmlUtils; import org.apache.commons.lang3.StringUtils; import org.apache.http.Header; +import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.auth.AuthScope; import org.apache.http.auth.Credentials; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.methods.*; import org.apache.http.client.utils.URIBuilder; +import org.apache.http.conn.params.ConnRoutePNames; import org.apache.http.entity.ContentType; import org.apache.http.entity.FileEntity; import org.apache.http.entity.StringEntity; @@ -47,6 +49,17 @@ public IrisClient(String accountId, String userName, String password) { this(defaultUri, accountId, userName, password, defaultVersion); } + public IrisClient(DefaultHttpClient httpClient, String uri, String accountId, String username, String password) { + this.uri = uri; + this.baseUrl = "/" + defaultVersion + "/"; + this.baseAccountUrl = this.baseUrl + "accounts/" + accountId + "/"; + + Credentials credentials = new UsernamePasswordCredentials(username, password); + httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, credentials); + + this.httpClient = httpClient; + } + private void initHttpClient(String userName, String password) { httpClient = new DefaultHttpClient(); Credentials credentials = new UsernamePasswordCredentials(userName, password); @@ -66,6 +79,7 @@ private T processResponse(IrisResponse response, Class returnType) throws } public T get(String uri, Class returnType) throws Exception { + System.out.println(uri); IrisResponse response = get(uri); return processResponse(response, returnType); } diff --git a/src/test/java/com/bandwidth/iris/sdk/ProxyTests.java b/src/test/java/com/bandwidth/iris/sdk/ProxyTests.java new file mode 100644 index 0000000..32ab251 --- /dev/null +++ b/src/test/java/com/bandwidth/iris/sdk/ProxyTests.java @@ -0,0 +1,62 @@ +package com.bandwidth.iris.sdk; + +import com.bandwidth.iris.sdk.model.Order; +import com.bandwidth.iris.sdk.model.OrderResponse; +import com.github.tomakehurst.wiremock.junit.WireMockRule; +import org.apache.http.HttpHost; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.Credentials; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.conn.params.ConnRoutePNames; +import org.apache.http.impl.client.BasicCredentialsProvider; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.impl.client.ProxyAuthenticationStrategy; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import static com.github.tomakehurst.wiremock.client.WireMock.*; +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static org.junit.Assert.assertEquals; + +public class ProxyTests { + + @Rule + public WireMockRule wireMockRule = new WireMockRule(8090); // No-args constructor defaults to port 8080 + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + protected IrisClient getDefaultClient() { + return new IrisClient("https://d815f3b06d79650d7a886f0c2ce1955d.m.pipedream.net", "accountId", "username", "password", "v1.0"); + } + + protected IrisClient getCustomClient() { + DefaultHttpClient client = new DefaultHttpClient(); + HttpHost proxy = new HttpHost("localhost",80); + client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy); + + Credentials credentials = new UsernamePasswordCredentials("userName", "password"); + CredentialsProvider credsProvider = new BasicCredentialsProvider(); + credsProvider.setCredentials( new AuthScope("localhost",8080), credentials); + + client.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy()); + + return new IrisClient(client, "http://localhost:8090", "accountId", "username", "password"); + } + + @Test + public void testWithCustomClient() throws Exception { + String url = "/v1.0/accounts/accountId/orders/someid"; + stubFor(get(urlMatching(url)) + .willReturn(aResponse() + .withStatus(200) + .withBody(IrisClientTestUtils.validOrderResponseXml))); + + OrderResponse orderResponse = Order.get(getCustomClient(), "someid"); + assertEquals(orderResponse.getOrder().getid(), "someid"); + assertEquals(orderResponse.getOrder().getExistingTelephoneNumberOrderType().getTelephoneNumberList().get(0), + "2052865046"); + assertEquals(orderResponse.getOrder().getName(), "A New Order"); + } +} From feeff762cda89dab3344680ae1a896422c89e0ea Mon Sep 17 00:00:00 2001 From: AJ Rice <53190766+ajrice6713@users.noreply.github.com> Date: Wed, 17 Jul 2024 18:46:26 -0400 Subject: [PATCH 2/2] Rework tests and remove debug statement --- .../com/bandwidth/iris/sdk/IrisClient.java | 1 - .../bandwidth/iris/sdk/BaseModelTests.java | 23 +++++++ .../com/bandwidth/iris/sdk/OrderTests.java | 15 +++++ .../com/bandwidth/iris/sdk/ProxyTests.java | 62 ------------------- 4 files changed, 38 insertions(+), 63 deletions(-) delete mode 100644 src/test/java/com/bandwidth/iris/sdk/ProxyTests.java diff --git a/src/main/java/com/bandwidth/iris/sdk/IrisClient.java b/src/main/java/com/bandwidth/iris/sdk/IrisClient.java index beb1596..93f07f0 100644 --- a/src/main/java/com/bandwidth/iris/sdk/IrisClient.java +++ b/src/main/java/com/bandwidth/iris/sdk/IrisClient.java @@ -79,7 +79,6 @@ private T processResponse(IrisResponse response, Class returnType) throws } public T get(String uri, Class returnType) throws Exception { - System.out.println(uri); IrisResponse response = get(uri); return processResponse(response, returnType); } diff --git a/src/test/java/com/bandwidth/iris/sdk/BaseModelTests.java b/src/test/java/com/bandwidth/iris/sdk/BaseModelTests.java index 6454f1c..707b6cd 100644 --- a/src/test/java/com/bandwidth/iris/sdk/BaseModelTests.java +++ b/src/test/java/com/bandwidth/iris/sdk/BaseModelTests.java @@ -1,6 +1,15 @@ package com.bandwidth.iris.sdk; import com.github.tomakehurst.wiremock.junit.WireMockRule; +import org.apache.http.HttpHost; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.Credentials; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.conn.params.ConnRoutePNames; +import org.apache.http.impl.client.BasicCredentialsProvider; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.impl.client.ProxyAuthenticationStrategy; import org.junit.Rule; import org.junit.rules.ExpectedException; @@ -16,6 +25,20 @@ protected IrisClient getDefaultClient() { return new IrisClient("http://localhost:8090", "accountId", "username", "password", "v1.0"); } + protected IrisClient getCustomClient() { + DefaultHttpClient client = new DefaultHttpClient(); + HttpHost proxy = new HttpHost("localhost",8090); + client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy); + + Credentials credentials = new UsernamePasswordCredentials("userName", "password"); + CredentialsProvider credsProvider = new BasicCredentialsProvider(); + credsProvider.setCredentials( new AuthScope("localhost",8080), credentials); + + client.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy()); + + return new IrisClient(client, "http://localhost:8090", "accountId", "username", "password"); + } + public void setMessage(String s) { this.message = s; } diff --git a/src/test/java/com/bandwidth/iris/sdk/OrderTests.java b/src/test/java/com/bandwidth/iris/sdk/OrderTests.java index 35a3286..51b7ebf 100644 --- a/src/test/java/com/bandwidth/iris/sdk/OrderTests.java +++ b/src/test/java/com/bandwidth/iris/sdk/OrderTests.java @@ -49,6 +49,21 @@ public void testGet() throws Exception { } + @Test + public void testGetWithCustomClient() throws Exception { + String url = "/v1.0/accounts/accountId/orders/someid"; + stubFor(get(urlMatching(url)) + .willReturn(aResponse() + .withStatus(200) + .withBody(IrisClientTestUtils.validOrderResponseXml))); + + OrderResponse orderResponse = Order.get(getCustomClient(), "someid"); + assertEquals(orderResponse.getOrder().getid(), "someid"); + assertEquals(orderResponse.getOrder().getExistingTelephoneNumberOrderType().getTelephoneNumberList().get(0), + "2052865046"); + assertEquals(orderResponse.getOrder().getName(), "A New Order"); + } + @Test public void testGetError() throws Exception { String url = "/v1.0/accounts/accountId/orders/errorid"; diff --git a/src/test/java/com/bandwidth/iris/sdk/ProxyTests.java b/src/test/java/com/bandwidth/iris/sdk/ProxyTests.java deleted file mode 100644 index 32ab251..0000000 --- a/src/test/java/com/bandwidth/iris/sdk/ProxyTests.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.bandwidth.iris.sdk; - -import com.bandwidth.iris.sdk.model.Order; -import com.bandwidth.iris.sdk.model.OrderResponse; -import com.github.tomakehurst.wiremock.junit.WireMockRule; -import org.apache.http.HttpHost; -import org.apache.http.auth.AuthScope; -import org.apache.http.auth.Credentials; -import org.apache.http.auth.UsernamePasswordCredentials; -import org.apache.http.client.CredentialsProvider; -import org.apache.http.conn.params.ConnRoutePNames; -import org.apache.http.impl.client.BasicCredentialsProvider; -import org.apache.http.impl.client.DefaultHttpClient; -import org.apache.http.impl.client.ProxyAuthenticationStrategy; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; - -import static com.github.tomakehurst.wiremock.client.WireMock.*; -import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; -import static org.junit.Assert.assertEquals; - -public class ProxyTests { - - @Rule - public WireMockRule wireMockRule = new WireMockRule(8090); // No-args constructor defaults to port 8080 - @Rule - public ExpectedException expectedEx = ExpectedException.none(); - - protected IrisClient getDefaultClient() { - return new IrisClient("https://d815f3b06d79650d7a886f0c2ce1955d.m.pipedream.net", "accountId", "username", "password", "v1.0"); - } - - protected IrisClient getCustomClient() { - DefaultHttpClient client = new DefaultHttpClient(); - HttpHost proxy = new HttpHost("localhost",80); - client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy); - - Credentials credentials = new UsernamePasswordCredentials("userName", "password"); - CredentialsProvider credsProvider = new BasicCredentialsProvider(); - credsProvider.setCredentials( new AuthScope("localhost",8080), credentials); - - client.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy()); - - return new IrisClient(client, "http://localhost:8090", "accountId", "username", "password"); - } - - @Test - public void testWithCustomClient() throws Exception { - String url = "/v1.0/accounts/accountId/orders/someid"; - stubFor(get(urlMatching(url)) - .willReturn(aResponse() - .withStatus(200) - .withBody(IrisClientTestUtils.validOrderResponseXml))); - - OrderResponse orderResponse = Order.get(getCustomClient(), "someid"); - assertEquals(orderResponse.getOrder().getid(), "someid"); - assertEquals(orderResponse.getOrder().getExistingTelephoneNumberOrderType().getTelephoneNumberList().get(0), - "2052865046"); - assertEquals(orderResponse.getOrder().getName(), "A New Order"); - } -}