aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.http.test/tst/org/eclipse
diff options
context:
space:
mode:
authorThomas Wolf <thomas.wolf@paranor.ch>2020-11-29 16:22:39 +0100
committerThomas Wolf <thomas.wolf@paranor.ch>2021-01-14 16:23:44 +0100
commit224aaa0be755d74deb0b58afe1e1e4c06bdce0b8 (patch)
treef4f20a5cb6af08f5b6345c1705436563fd5c6416 /org.eclipse.jgit.http.test/tst/org/eclipse
parent312ab4f7f65e4f7658e307e0fd143642fa95a98a (diff)
downloadjgit-224aaa0be755d74deb0b58afe1e1e4c06bdce0b8.tar.gz
jgit-224aaa0be755d74deb0b58afe1e1e4c06bdce0b8.zip
TransportHttp: make the connection factory configurable
Previously, TransportHttp always used the globally set connection factory. This is problematic if that global factory is changed in the middle of a fetch or push operation. Initialize the factory to use in the constructor, then use that factory for all HTTP requests made through this transport. Provide a setter and a getter for it so that client code can customize the factory, if needed, in a TransportConfigCallback. Once a factory has been used on a TransportHttp instance it cannot be changed anymore. Make the global static factory reference volatile. Change-Id: I7c6ee16680407d3724e901c426db174a3125ba1c Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
Diffstat (limited to 'org.eclipse.jgit.http.test/tst/org/eclipse')
-rw-r--r--org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/SmartClientSmartServerTest.java64
1 files changed, 64 insertions, 0 deletions
diff --git a/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/SmartClientSmartServerTest.java b/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/SmartClientSmartServerTest.java
index def52ff6bb..89a254184d 100644
--- a/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/SmartClientSmartServerTest.java
+++ b/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/SmartClientSmartServerTest.java
@@ -23,12 +23,15 @@ import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.ByteArrayOutputStream;
+import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
+import java.net.Proxy;
import java.net.URI;
import java.net.URISyntaxException;
+import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.MessageFormat;
import java.util.Collections;
@@ -52,6 +55,8 @@ import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
+import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.api.TransportConfigCallback;
import org.eclipse.jgit.errors.RemoteRepositoryException;
import org.eclipse.jgit.errors.TransportException;
import org.eclipse.jgit.errors.UnsupportedCredentialItem;
@@ -91,6 +96,8 @@ import org.eclipse.jgit.transport.TransportHttp;
import org.eclipse.jgit.transport.URIish;
import org.eclipse.jgit.transport.UploadPack;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
+import org.eclipse.jgit.transport.http.HttpConnection;
+import org.eclipse.jgit.transport.http.HttpConnectionFactory;
import org.eclipse.jgit.util.HttpSupport;
import org.eclipse.jgit.util.SystemReader;
import org.junit.Before;
@@ -638,6 +645,63 @@ public class SmartClientSmartServerTest extends AllProtocolsHttpTestCase {
.getResponseHeader(HDR_CONTENT_TYPE));
}
+ @Test
+ public void test_CloneWithCustomFactory() throws Exception {
+ HttpConnectionFactory globalFactory = HttpTransport
+ .getConnectionFactory();
+ HttpConnectionFactory failingConnectionFactory = new HttpConnectionFactory() {
+
+ @Override
+ public HttpConnection create(URL url) throws IOException {
+ throw new IOException("Should not be reached");
+ }
+
+ @Override
+ public HttpConnection create(URL url, Proxy proxy)
+ throws IOException {
+ throw new IOException("Should not be reached");
+ }
+ };
+ HttpTransport.setConnectionFactory(failingConnectionFactory);
+ try {
+ File tmp = createTempDirectory("cloneViaApi");
+ boolean[] localFactoryUsed = { false };
+ TransportConfigCallback callback = new TransportConfigCallback() {
+
+ @Override
+ public void configure(Transport transport) {
+ if (transport instanceof TransportHttp) {
+ ((TransportHttp) transport).setHttpConnectionFactory(
+ new HttpConnectionFactory() {
+
+ @Override
+ public HttpConnection create(URL url)
+ throws IOException {
+ localFactoryUsed[0] = true;
+ return globalFactory.create(url);
+ }
+
+ @Override
+ public HttpConnection create(URL url,
+ Proxy proxy) throws IOException {
+ localFactoryUsed[0] = true;
+ return globalFactory.create(url, proxy);
+ }
+ });
+ }
+ }
+ };
+ try (Git git = Git.cloneRepository().setDirectory(tmp)
+ .setTransportConfigCallback(callback)
+ .setURI(remoteURI.toPrivateString()).call()) {
+ assertTrue("Should have used the local HttpConnectionFactory",
+ localFactoryUsed[0]);
+ }
+ } finally {
+ HttpTransport.setConnectionFactory(globalFactory);
+ }
+ }
+
private void initialClone_Redirect(int nofRedirects, int code)
throws Exception {
initialClone_Redirect(nofRedirects, code, false);