Override config http.userAgent from environment GIT_HTTP_USER_AGENT
According to [1], environment variable GIT_HTTP_USER_AGENT can override a git config http.userAgent. [1] https://git-scm.com/docs/git-config#Documentation/git-config.txt-httpuserAgent Change-Id: I996789dc49faf96339cd7b4e0a682b9bcafb6f70 Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
This commit is contained in:
parent
b0b3250197
commit
f37aa182e1
|
@ -13,7 +13,9 @@
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import org.eclipse.jgit.junit.MockSystemReader;
|
||||||
import org.eclipse.jgit.lib.Config;
|
import org.eclipse.jgit.lib.Config;
|
||||||
|
import org.eclipse.jgit.util.SystemReader;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
@ -96,7 +98,8 @@ public void testMatchCaseSensitivity() throws Exception {
|
||||||
@Test
|
@Test
|
||||||
public void testMatchWithInvalidUriInConfig() throws Exception {
|
public void testMatchWithInvalidUriInConfig() throws Exception {
|
||||||
config.fromText(
|
config.fromText(
|
||||||
DEFAULT + "[http \"///\"]\n" + "\tpostBuffer = 1024\n");
|
DEFAULT + "[http \"///#expectedWarning\"]\n"
|
||||||
|
+ "\tpostBuffer = 1024\n");
|
||||||
HttpConfig http = new HttpConfig(config,
|
HttpConfig http = new HttpConfig(config,
|
||||||
new URIish("http://example.com/path/repo.git"));
|
new URIish("http://example.com/path/repo.git"));
|
||||||
assertEquals(1, http.getPostBuffer());
|
assertEquals(1, http.getPostBuffer());
|
||||||
|
@ -104,7 +107,8 @@ public void testMatchWithInvalidUriInConfig() throws Exception {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMatchWithInvalidAndValidUriInConfig() throws Exception {
|
public void testMatchWithInvalidAndValidUriInConfig() throws Exception {
|
||||||
config.fromText(DEFAULT + "[http \"///\"]\n" + "\tpostBuffer = 1024\n"
|
config.fromText(DEFAULT + "[http \"///#expectedWarning\"]\n"
|
||||||
|
+ "\tpostBuffer = 1024\n"
|
||||||
+ "[http \"http://example.com\"]\n" + "\tpostBuffer = 2048\n");
|
+ "[http \"http://example.com\"]\n" + "\tpostBuffer = 2048\n");
|
||||||
HttpConfig http = new HttpConfig(config,
|
HttpConfig http = new HttpConfig(config,
|
||||||
new URIish("http://example.com/path/repo.git"));
|
new URIish("http://example.com/path/repo.git"));
|
||||||
|
@ -231,6 +235,31 @@ public void testUserAgent() throws Exception {
|
||||||
assertEquals("DummyAgent/4.0", http.getUserAgent());
|
assertEquals("DummyAgent/4.0", http.getUserAgent());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUserAgentEnvOverride() throws Exception {
|
||||||
|
String mockAgent = "jgit-test/5.10.0";
|
||||||
|
SystemReader originalReader = SystemReader.getInstance();
|
||||||
|
SystemReader.setInstance(new MockSystemReader() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getenv(String variable) {
|
||||||
|
if ("GIT_HTTP_USER_AGENT".equals(variable)) {
|
||||||
|
return mockAgent;
|
||||||
|
}
|
||||||
|
return super.getenv(variable);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
try {
|
||||||
|
config.fromText(DEFAULT + "[http \"http://example.com\"]\n"
|
||||||
|
+ "\tuserAgent=DummyAgent/4.0\n");
|
||||||
|
HttpConfig http = new HttpConfig(config,
|
||||||
|
new URIish("http://example.com/"));
|
||||||
|
assertEquals(mockAgent, http.getUserAgent());
|
||||||
|
} finally {
|
||||||
|
SystemReader.setInstance(originalReader);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testUserAgentNonAscii() throws Exception {
|
public void testUserAgentNonAscii() throws Exception {
|
||||||
config.fromText(DEFAULT + "[http \"http://example.com\"]\n"
|
config.fromText(DEFAULT + "[http \"http://example.com\"]\n"
|
||||||
|
|
|
@ -121,6 +121,8 @@ public Integer get() {
|
||||||
}
|
}
|
||||||
}).get().intValue();
|
}).get().intValue();
|
||||||
|
|
||||||
|
private static final String ENV_HTTP_USER_AGENT = "GIT_HTTP_USER_AGENT"; //$NON-NLS-1$
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Config values for http.followRedirect.
|
* Config values for http.followRedirect.
|
||||||
*/
|
*/
|
||||||
|
@ -364,6 +366,11 @@ private void init(Config config, URIish uri) {
|
||||||
saveCookies = config.getBoolean(HTTP, match, SAVE_COOKIES_KEY,
|
saveCookies = config.getBoolean(HTTP, match, SAVE_COOKIES_KEY,
|
||||||
saveCookies);
|
saveCookies);
|
||||||
}
|
}
|
||||||
|
// Environment overrides config
|
||||||
|
agent = SystemReader.getInstance().getenv(ENV_HTTP_USER_AGENT);
|
||||||
|
if (!StringUtils.isEmptyOrNull(agent)) {
|
||||||
|
userAgent = UserAgent.clean(agent);
|
||||||
|
}
|
||||||
postBuffer = postBufferSize;
|
postBuffer = postBufferSize;
|
||||||
sslVerify = sslVerifyFlag;
|
sslVerify = sslVerifyFlag;
|
||||||
followRedirects = followRedirectsMode;
|
followRedirects = followRedirectsMode;
|
||||||
|
|
Loading…
Reference in New Issue