mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-03-05 20:00:55 +01:00
added userAgentStartsWith method to HttpUtil
This commit is contained in:
@@ -53,6 +53,7 @@ import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLDecoder;
|
||||
import java.net.URLEncoder;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@@ -79,6 +80,9 @@ public final class HttpUtil
|
||||
*/
|
||||
public static final String HEADER_SCM_CLIENT = "X-SCM-Client";
|
||||
|
||||
/** Field description */
|
||||
public static final String HEADER_USERAGENT = "User-Agent";
|
||||
|
||||
/** authentication header */
|
||||
public static final String HEADER_WWW_AUTHENTICATE = "WWW-Authenticate";
|
||||
|
||||
@@ -522,6 +526,26 @@ public final class HttpUtil
|
||||
STATUS_UNAUTHORIZED_MESSAGE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the User-Agent header of the current request starts with
|
||||
* the given string.
|
||||
*
|
||||
*
|
||||
* @param request http request
|
||||
* @param userAgent string to test against the header
|
||||
*
|
||||
* @return true if the header starts with the given string
|
||||
*
|
||||
* @since 1.37
|
||||
*/
|
||||
public static boolean userAgentStartsWith(HttpServletRequest request,
|
||||
String userAgent)
|
||||
{
|
||||
return Strings.nullToEmpty(request.getHeader(HEADER_USERAGENT)).toLowerCase(
|
||||
Locale.ENGLISH).startsWith(
|
||||
Strings.nullToEmpty(userAgent).toLowerCase(Locale.ENGLISH));
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
|
||||
@@ -184,6 +184,23 @@ public class HttpUtilTest
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public void userAgentStartsWithTest()
|
||||
{
|
||||
HttpServletRequest request = mock(HttpServletRequest.class);
|
||||
|
||||
when(request.getHeader(HttpUtil.HEADER_USERAGENT)).thenReturn(
|
||||
"git/1.7.10.5997.gaa4aa");
|
||||
assertTrue(HttpUtil.userAgentStartsWith(request, "git/"));
|
||||
assertTrue(HttpUtil.userAgentStartsWith(request, "GIT/"));
|
||||
assertFalse(HttpUtil.userAgentStartsWith(request, "git/a"));
|
||||
assertFalse(HttpUtil.userAgentStartsWith(request, "sobbo/"));
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user