mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-06 13:35:44 +01:00
improved structure of GitUserAgentProvider and added more unit tests
This commit is contained in:
@@ -41,18 +41,14 @@ import java.util.Locale;
|
||||
import sonia.scm.plugin.ext.Extension;
|
||||
|
||||
/**
|
||||
*
|
||||
* UserAgent provider for git related clients.
|
||||
* @author Sebastian Sdorra <sebastian.sdorra@gmail.com>
|
||||
* @since 1.45
|
||||
*/
|
||||
@Extension
|
||||
public class GitUserAgentProvider implements UserAgentProvider
|
||||
{
|
||||
public class GitUserAgentProvider implements UserAgentProvider {
|
||||
|
||||
private static final String PREFIX_JGIT = "jgit/";
|
||||
private static final String PREFIX_REGULAR = "git/";
|
||||
private static final String PREFIX_LFS = "git-lfs/";
|
||||
private static final String SUFFIX_MSYSGIT = "msysgit";
|
||||
|
||||
@VisibleForTesting
|
||||
static final UserAgent JGIT = UserAgent.builder("JGit")
|
||||
@@ -60,11 +56,15 @@ public class GitUserAgentProvider implements UserAgentProvider
|
||||
.basicAuthenticationCharset(Charsets.UTF_8)
|
||||
.build();
|
||||
|
||||
private static final String PREFIX_REGULAR = "git/";
|
||||
|
||||
@VisibleForTesting
|
||||
static final UserAgent GIT = UserAgent.builder("Git")
|
||||
.browser(false)
|
||||
.basicAuthenticationCharset(Charsets.UTF_8)
|
||||
.build();
|
||||
|
||||
private static final String PREFIX_LFS = "git-lfs/";
|
||||
|
||||
@VisibleForTesting
|
||||
static final UserAgent GIT_LFS = UserAgent.builder("Git Lfs")
|
||||
@@ -72,6 +72,8 @@ public class GitUserAgentProvider implements UserAgentProvider
|
||||
.basicAuthenticationCharset(Charsets.UTF_8)
|
||||
.build();
|
||||
|
||||
private static final String SUFFIX_MSYSGIT = "msysgit";
|
||||
|
||||
@VisibleForTesting
|
||||
static final UserAgent MSYSGIT = UserAgent.builder("msysGit")
|
||||
.browser(false)
|
||||
@@ -83,8 +85,7 @@ public class GitUserAgentProvider implements UserAgentProvider
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
@Override
|
||||
public UserAgent parseUserAgent(String userAgentString)
|
||||
{
|
||||
public UserAgent parseUserAgent(String userAgentString) {
|
||||
String lowerUserAgent = toLower(userAgentString);
|
||||
|
||||
if (isJGit(lowerUserAgent)) {
|
||||
|
||||
@@ -41,22 +41,17 @@ import static org.junit.Assert.*;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
*
|
||||
* Unit tests for {@link GitUserAgentProvider}.
|
||||
*
|
||||
* @author Sebastian Sdorra <sebastian.sdorra@triology.de>
|
||||
*/
|
||||
public class GitUserAgentProviderTest
|
||||
{
|
||||
public class GitUserAgentProviderTest {
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*/
|
||||
private final GitUserAgentProvider provider = new GitUserAgentProvider();
|
||||
|
||||
@Test
|
||||
public void testParseUserAgent()
|
||||
{
|
||||
public void testParseUserAgent() {
|
||||
assertEquals(GitUserAgentProvider.GIT, parse("git/1.7.9.5"));
|
||||
assertEquals(GitUserAgentProvider.JGIT, parse("jgit/4.5.2"));
|
||||
assertEquals(GitUserAgentProvider.GIT_LFS, parse("git-lfs/2.0.1 (GitHub; windows amd64; go 1.8; git 678cdbd4)"));
|
||||
@@ -64,22 +59,22 @@ public class GitUserAgentProviderTest
|
||||
assertNull(parse("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param v
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private UserAgent parse(String v)
|
||||
{
|
||||
return provider.parseUserAgent(
|
||||
Strings.nullToEmpty(v).toLowerCase(Locale.ENGLISH));
|
||||
@Test
|
||||
public void testParseUserAgentCaseSensitive() {
|
||||
assertEquals(GitUserAgentProvider.GIT, parse("Git/1.7.9.5"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseUserAgentWithEmptyValue() {
|
||||
assertNull(parse(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseUserAgentWithNullValue() {
|
||||
assertNull(parse(null));
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private final GitUserAgentProvider provider = new GitUserAgentProvider();
|
||||
private UserAgent parse(String v) {
|
||||
return provider.parseUserAgent(v);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user