diff --git a/src/main/java/com/bandwidth/iris/sdk/IrisClient.java b/src/main/java/com/bandwidth/iris/sdk/IrisClient.java index 0966dbb..93f07f0 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); 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 cbbdc78..b615d54 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";