diff options
2 files changed, 74 insertions, 10 deletions
diff --git a/org.eclipse.mylyn.gerrit.core.tests/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritHttpClientTest.java b/org.eclipse.mylyn.gerrit.core.tests/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritHttpClientTest.java index 8a1c23293..1d578dac7 100644 --- a/org.eclipse.mylyn.gerrit.core.tests/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritHttpClientTest.java +++ b/org.eclipse.mylyn.gerrit.core.tests/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritHttpClientTest.java @@ -13,20 +13,34 @@ package org.eclipse.mylyn.internal.gerrit.core.client; import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.isA; +import static org.mockito.Mockito.inOrder; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.io.IOException; import org.apache.commons.httpclient.HttpMethodBase; +import org.apache.commons.httpclient.HttpStatus; +import org.apache.commons.httpclient.methods.GetMethod; +import org.apache.commons.httpclient.methods.PostMethod; import org.eclipse.core.runtime.AssertionFailedException; import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.mylyn.commons.net.AbstractWebLocation; +import org.eclipse.mylyn.commons.net.AuthenticationCredentials; +import org.eclipse.mylyn.commons.net.AuthenticationType; import org.eclipse.mylyn.internal.gerrit.core.client.GerritHttpClient.JsonEntity; import org.eclipse.mylyn.internal.gerrit.core.client.GerritHttpClient.Request; import org.eclipse.mylyn.internal.gerrit.core.client.GerritHttpClient.Request.HttpMethod; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.InOrder; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; @@ -39,6 +53,30 @@ import com.google.gson.reflect.TypeToken; */ @RunWith(MockitoJUnitRunner.class) public class GerritHttpClientTest { + public class TestGerritHttpClient extends GerritHttpClient { + private final int code; + + private TestGerritHttpClient(AbstractWebLocation location, int code) { + super(location); + this.code = code; + } + + @Override + public int execute(org.apache.commons.httpclient.HttpMethod method, IProgressMonitor monitor) + throws IOException { + return code; + } + + @Override + void requestCredentials(IProgressMonitor monitor, AuthenticationType authenticationType) + throws GerritLoginException { + } + + @Override + String getUrl() { + return "http://mock"; + } + } @Mock AbstractWebLocation abstractWebLocation; @@ -97,4 +135,26 @@ public class GerritHttpClientTest { assertArrayEquals(binary, result); } + @Test + public void authenticateForm() throws IOException, GerritException { + GerritHttpClient client = spy(new TestGerritHttpClient(abstractWebLocation, HttpStatus.SC_BAD_REQUEST)); + int result = client.authenticateForm(new AuthenticationCredentials("", ""), new NullProgressMonitor()); + assertEquals(HttpStatus.SC_NOT_FOUND, result); + InOrder inOrder = inOrder(client); + inOrder.verify(client).execute(isA(PostMethod.class), any(IProgressMonitor.class)); + inOrder.verify(client).execute(isA(GetMethod.class), any(IProgressMonitor.class)); + + client = spy(new TestGerritHttpClient(abstractWebLocation, HttpStatus.SC_METHOD_NOT_ALLOWED)); + result = client.authenticateForm(new AuthenticationCredentials("", ""), new NullProgressMonitor()); + assertEquals(HttpStatus.SC_NOT_FOUND, result); + inOrder = inOrder(client); + inOrder.verify(client).execute(isA(PostMethod.class), any(IProgressMonitor.class)); + inOrder.verify(client).execute(isA(GetMethod.class), any(IProgressMonitor.class)); + + client = spy(new TestGerritHttpClient(abstractWebLocation, HttpStatus.SC_UNAUTHORIZED)); + result = client.authenticateForm(new AuthenticationCredentials("", ""), new NullProgressMonitor()); + assertEquals(-1, result); + verify(client).execute(isA(PostMethod.class), any(IProgressMonitor.class)); + verify(client, never()).execute(isA(GetMethod.class), any(IProgressMonitor.class)); + } } diff --git a/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritHttpClient.java b/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritHttpClient.java index 02dc3cb12..436e10e72 100644 --- a/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritHttpClient.java +++ b/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritHttpClient.java @@ -336,7 +336,7 @@ public class GerritHttpClient { } try { // Execute the method. - WebUtil.execute(httpClient, hostConfiguration, method, monitor); + execute(method, monitor); } catch (IOException e) { WebUtil.releaseConnection(method, monitor); throw e; @@ -386,7 +386,7 @@ public class GerritHttpClient { method.setFollowRedirects(false); int code; try { - code = WebUtil.execute(httpClient, hostConfiguration, method, monitor); + code = execute(method, monitor); if (code == HttpStatus.SC_OK) { InputStream in = WebUtil.getResponseBodyAsStream(method, monitor); try { @@ -412,7 +412,7 @@ public class GerritHttpClient { GetMethod method = new GetMethod(getUrl() + serviceUri); try { // Execute the method. - WebUtil.execute(httpClient, hostConfiguration, method, monitor); + execute(method, monitor); return method; } catch (IOException e) { WebUtil.releaseConnection(method, monitor); @@ -497,7 +497,7 @@ public class GerritHttpClient { JsonRequest jsonRequest = new JsonRequest(GerritConnector.GERRIT_RPC_URI + "OpenIdService", entity); //$NON-NLS-1$ PostMethod method = jsonRequest.createMethod(); try { - int code = WebUtil.execute(httpClient, hostConfiguration, method, monitor); + int code = execute(method, monitor); if (needsReauthentication(code, monitor)) { return -1; } @@ -540,7 +540,7 @@ public class GerritHttpClient { GetMethod validateMethod = new GetMethod(openIdResponse.getResponseUrl()); try { // Execute the method. - WebUtil.execute(httpClient, hostConfiguration, validateMethod, monitor); + execute(validateMethod, monitor); } catch (IOException e) { WebUtil.releaseConnection(method, monitor); throw e; @@ -574,7 +574,7 @@ public class GerritHttpClient { JsonRequest jsonRequest = new JsonRequest(GerritConnector.GERRIT_RPC_URI + "UserPassAuthService", entity); //$NON-NLS-1$ PostMethod method = jsonRequest.createMethod(); try { - int code = WebUtil.execute(httpClient, hostConfiguration, method, monitor); + int code = execute(method, monitor); if (needsReauthentication(code, monitor)) { return -1; } @@ -618,7 +618,7 @@ public class GerritHttpClient { GetMethod method = new GetMethod(requestPath); method.setFollowRedirects(false); try { - int code = WebUtil.execute(httpClient, hostConfiguration, method, monitor); + int code = execute(method, monitor); if (needsReauthentication(code, monitor)) { return -1; } @@ -641,7 +641,7 @@ public class GerritHttpClient { return HttpStatus.SC_NOT_FOUND; } - private int authenticateForm(AuthenticationCredentials credentials, IProgressMonitor monitor) throws IOException, + int authenticateForm(AuthenticationCredentials credentials, IProgressMonitor monitor) throws IOException, GerritException { // try standard basic/digest/ntlm authentication first String repositoryUrl = getUrl(); @@ -654,8 +654,8 @@ public class GerritHttpClient { for (HttpMethodBase method : methods) { int code; try { - code = WebUtil.execute(httpClient, hostConfiguration, method, monitor); - if (code == HttpStatus.SC_METHOD_NOT_ALLOWED) { + code = execute(method, monitor); + if (code == HttpStatus.SC_BAD_REQUEST || code == HttpStatus.SC_METHOD_NOT_ALLOWED) { continue; // try next http method } else if (needsReauthentication(code, monitor)) { return -1; @@ -681,6 +681,10 @@ public class GerritHttpClient { return HttpStatus.SC_NOT_FOUND; } + int execute(org.apache.commons.httpclient.HttpMethod method, IProgressMonitor monitor) throws IOException { + return WebUtil.execute(httpClient, hostConfiguration, method, monitor); + } + private HttpMethodBase[] getFormAuthMethods(String repositoryUrl, AuthenticationCredentials credentials) { PostMethod post = new PostMethod(WebUtil.getRequestPath(repositoryUrl + LOGIN_URL)); post.setParameter("username", credentials.getUserName()); //$NON-NLS-1$ |