Simplify `UserAgentUtil`
This commit is contained in:
parent
f5e49b6db7
commit
05c74f1997
|
@ -5,7 +5,6 @@
|
||||||
|
|
||||||
package org.whispersystems.textsecuregcm.util.ua;
|
package org.whispersystems.textsecuregcm.util.ua;
|
||||||
|
|
||||||
import com.google.common.annotations.VisibleForTesting;
|
|
||||||
import com.vdurmont.semver4j.Semver;
|
import com.vdurmont.semver4j.Semver;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
@ -21,10 +20,10 @@ public class UserAgentUtil {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final UserAgent standardUserAgent = parseStandardUserAgentString(userAgentString);
|
final Matcher matcher = STANDARD_UA_PATTERN.matcher(userAgentString);
|
||||||
|
|
||||||
if (standardUserAgent != null) {
|
if (matcher.matches()) {
|
||||||
return standardUserAgent;
|
return new UserAgent(ClientPlatform.valueOf(matcher.group(1).toUpperCase()), new Semver(matcher.group(2)), StringUtils.stripToNull(matcher.group(4)));
|
||||||
}
|
}
|
||||||
} catch (final Exception e) {
|
} catch (final Exception e) {
|
||||||
throw new UnrecognizedUserAgentException(e);
|
throw new UnrecognizedUserAgentException(e);
|
||||||
|
@ -32,15 +31,4 @@ public class UserAgentUtil {
|
||||||
|
|
||||||
throw new UnrecognizedUserAgentException();
|
throw new UnrecognizedUserAgentException();
|
||||||
}
|
}
|
||||||
|
|
||||||
@VisibleForTesting
|
|
||||||
static UserAgent parseStandardUserAgentString(final String userAgentString) {
|
|
||||||
final Matcher matcher = STANDARD_UA_PATTERN.matcher(userAgentString);
|
|
||||||
|
|
||||||
if (matcher.matches()) {
|
|
||||||
return new UserAgent(ClientPlatform.valueOf(matcher.group(1).toUpperCase()), new Semver(matcher.group(2)), StringUtils.stripToNull(matcher.group(4)));
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,28 +13,20 @@ import java.util.stream.Stream;
|
||||||
import org.junit.jupiter.params.ParameterizedTest;
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
import org.junit.jupiter.params.provider.Arguments;
|
import org.junit.jupiter.params.provider.Arguments;
|
||||||
import org.junit.jupiter.params.provider.MethodSource;
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
class UserAgentUtilTest {
|
class UserAgentUtilTest {
|
||||||
|
|
||||||
@ParameterizedTest
|
@ParameterizedTest
|
||||||
@MethodSource
|
@MethodSource("argumentsForTestParseStandardUserAgentString")
|
||||||
void testParseBogusUserAgentString(final String userAgentString) {
|
void testParseStandardUserAgentString(final String userAgentString, @Nullable final UserAgent expectedUserAgent)
|
||||||
|
throws UnrecognizedUserAgentException {
|
||||||
|
|
||||||
|
if (expectedUserAgent != null) {
|
||||||
|
assertEquals(expectedUserAgent, UserAgentUtil.parseUserAgentString(userAgentString));
|
||||||
|
} else {
|
||||||
assertThrows(UnrecognizedUserAgentException.class, () -> UserAgentUtil.parseUserAgentString(userAgentString));
|
assertThrows(UnrecognizedUserAgentException.class, () -> UserAgentUtil.parseUserAgentString(userAgentString));
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
|
||||||
private static Stream<String> testParseBogusUserAgentString() {
|
|
||||||
return Stream.of(
|
|
||||||
null,
|
|
||||||
"This is obviously not a reasonable User-Agent string.",
|
|
||||||
"Signal-Android/4.6-8.3.unreasonableversionstring-17"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@ParameterizedTest
|
|
||||||
@MethodSource("argumentsForTestParseStandardUserAgentString")
|
|
||||||
void testParseStandardUserAgentString(final String userAgentString, final UserAgent expectedUserAgent) {
|
|
||||||
assertEquals(expectedUserAgent, UserAgentUtil.parseStandardUserAgentString(userAgentString));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Stream<Arguments> argumentsForTestParseStandardUserAgentString() {
|
private static Stream<Arguments> argumentsForTestParseStandardUserAgentString() {
|
||||||
|
|
Loading…
Reference in New Issue