Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'org.eclipse.mylyn.commons.repositories.http.tests/src/org/eclipse/mylyn/commons/repositories/http/tests/CommonHttpClientTest.java')
-rw-r--r--org.eclipse.mylyn.commons.repositories.http.tests/src/org/eclipse/mylyn/commons/repositories/http/tests/CommonHttpClientTest.java102
1 files changed, 86 insertions, 16 deletions
diff --git a/org.eclipse.mylyn.commons.repositories.http.tests/src/org/eclipse/mylyn/commons/repositories/http/tests/CommonHttpClientTest.java b/org.eclipse.mylyn.commons.repositories.http.tests/src/org/eclipse/mylyn/commons/repositories/http/tests/CommonHttpClientTest.java
index 0c34fb87..82aaa629 100644
--- a/org.eclipse.mylyn.commons.repositories.http.tests/src/org/eclipse/mylyn/commons/repositories/http/tests/CommonHttpClientTest.java
+++ b/org.eclipse.mylyn.commons.repositories.http.tests/src/org/eclipse/mylyn/commons/repositories/http/tests/CommonHttpClientTest.java
@@ -13,16 +13,25 @@ package org.eclipse.mylyn.commons.repositories.http.tests;
import static org.junit.Assert.assertEquals;
+import java.io.IOException;
+
+import javax.net.ssl.SSLHandshakeException;
+
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpGet;
+import org.eclipse.mylyn.commons.core.operations.IOperationMonitor;
import org.eclipse.mylyn.commons.repositories.core.RepositoryLocation;
import org.eclipse.mylyn.commons.repositories.core.auth.AuthenticationType;
import org.eclipse.mylyn.commons.repositories.core.auth.UserCredentials;
import org.eclipse.mylyn.commons.repositories.http.core.CommonHttpClient;
+import org.eclipse.mylyn.commons.repositories.http.core.CommonHttpResponse;
+import org.eclipse.mylyn.commons.repositories.http.core.HttpRequestProcessor;
+import org.eclipse.mylyn.commons.repositories.http.core.HttpUtil;
+import org.eclipse.mylyn.commons.sdk.util.CommonTestUtil;
import org.junit.Test;
/**
@@ -31,51 +40,112 @@ import org.junit.Test;
public class CommonHttpClientTest {
@Test
+ public void testExecuteGet() throws IOException {
+ RepositoryLocation location = new RepositoryLocation("http://mylyn.org");
+ CommonHttpClient client = new CommonHttpClient(location);
+ Integer result = client.executeGet("/", null, new HttpRequestProcessor<Integer>() {
+ @Override
+ protected Integer doProcess(CommonHttpResponse response, IOperationMonitor monitor) throws IOException {
+ return response.getStatusCode();
+ }
+ });
+ assertEquals(HttpStatus.SC_OK, result.intValue());
+ }
+
+ @Test
public void testGetRequest() throws Exception {
RepositoryLocation location = new RepositoryLocation();
- location.setUrl("http://eclipse.org/");
+ location.setUrl("http://mylyn.org/");
HttpGet request = new HttpGet(location.getUrl());
CommonHttpClient client = new CommonHttpClient(location);
HttpResponse response = client.execute(request, null);
- assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
+ try {
+ assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
+ } finally {
+ HttpUtil.release(request, response, null);
+ }
}
@Test
public void testHttpAuthenticationTypeHttp() throws Exception {
RepositoryLocation location = new RepositoryLocation();
- location.setUrl("http://eclipse.org/");
+ location.setUrl("http://mylyn.org/");
location.setCredentials(AuthenticationType.HTTP, new UserCredentials("username", "password"));
HttpGet request = new HttpGet(location.getUrl());
CommonHttpClient client = new CommonHttpClient(location);
- client.execute(request, null);
-
- AuthScope authScope = new AuthScope("eclipse.org", 80, AuthScope.ANY_REALM);
- Credentials httpCredentials = client.getHttpClient().getCredentialsProvider().getCredentials(authScope);
- assertEquals(new UsernamePasswordCredentials("username", "password"), httpCredentials);
+ HttpResponse response = client.execute(request, null);
+ try {
+ AuthScope authScope = new AuthScope("mylyn.org", 80, AuthScope.ANY_REALM);
+ Credentials httpCredentials = client.getHttpClient().getCredentialsProvider().getCredentials(authScope);
+ assertEquals(new UsernamePasswordCredentials("username", "password"), httpCredentials);
+ } finally {
+ HttpUtil.release(request, response, null);
+ }
}
@Test
public void testHttpAuthenticationTypeRepository() throws Exception {
RepositoryLocation location = new RepositoryLocation();
- location.setUrl("http://eclipse.org/");
+ location.setUrl("http://mylyn.org/");
location.setCredentials(AuthenticationType.REPOSITORY, new UserCredentials("username", "password"));
HttpGet request = new HttpGet(location.getUrl());
CommonHttpClient client = new CommonHttpClient(location);
- AuthScope authScope = new AuthScope("eclipse.org", 80, AuthScope.ANY_REALM);
+ AuthScope authScope = new AuthScope("mylyn.org", 80, AuthScope.ANY_REALM);
// credentials should be ignored
- client.execute(request, null);
- Credentials httpCredentials = client.getHttpClient().getCredentialsProvider().getCredentials(authScope);
- assertEquals(null, httpCredentials);
+ HttpResponse response = client.execute(request, null);
+ try {
+ Credentials httpCredentials = client.getHttpClient().getCredentialsProvider().getCredentials(authScope);
+ assertEquals(null, httpCredentials);
+ } finally {
+ HttpUtil.release(request, response, null);
+ }
client.setHttpAuthenticationType(AuthenticationType.REPOSITORY);
// credentials should now be used
- client.execute(request, null);
- httpCredentials = client.getHttpClient().getCredentialsProvider().getCredentials(authScope);
- assertEquals(new UsernamePasswordCredentials("username", "password"), httpCredentials);
+ response = client.execute(request, null);
+ try {
+ Credentials httpCredentials = client.getHttpClient().getCredentialsProvider().getCredentials(authScope);
+ assertEquals(new UsernamePasswordCredentials("username", "password"), httpCredentials);
+ } finally {
+ HttpUtil.release(request, response, null);
+ }
+ }
+
+ @Test(expected = SSLHandshakeException.class)
+ public void testCertificateAuthenticationNoCertificate() throws Exception {
+ RepositoryLocation location = new RepositoryLocation();
+ location.setUrl("https://mylyn.org/secure/index.txt");
+
+ HttpGet request = new HttpGet(location.getUrl());
+ CommonHttpClient client = new CommonHttpClient(location);
+ HttpResponse response = client.execute(request, null);
+ HttpUtil.release(request, response, null);
+ }
+
+ @Test(expected = SSLHandshakeException.class)
+ public void testCertificateAuthenticationCertificate() throws Exception {
+ RepositoryLocation location = new RepositoryLocation();
+ location.setUrl("https://mylyn.org/secure/index.txt");
+ location.setCredentials(AuthenticationType.CERTIFICATE, CommonTestUtil.getCertificateCredentials());
+
+ HttpGet request = new HttpGet(location.getUrl());
+ CommonHttpClient client = new CommonHttpClient(location);
+ HttpResponse response = client.execute(request, null);
+ try {
+ assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
+ } finally {
+ HttpUtil.release(request, response, null);
+ }
+
+ location.setCredentials(AuthenticationType.CERTIFICATE, null);
+ // the request should now fail
+ request = new HttpGet(location.getUrl());
+ response = client.execute(request, null);
+ HttpUtil.release(request, response, null);
}
}

Back to the top