Skip to content

Commit

Permalink
Fix #223: Improve User-Agent parsing and add rests
Browse files Browse the repository at this point in the history
  • Loading branch information
petrdvorak authored and banterCZ committed Dec 11, 2023
1 parent 75fed3d commit 34cb223
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 13 deletions.
5 changes: 5 additions & 0 deletions http-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private UserAgent() {
private static final Pattern patternPrefix = Pattern.compile("^PowerAuthNetworking/(?<networkVersion>[0-9]+\\.[0-9]+\\.[0-9]+).*");
private static final Pattern patternV1 = Pattern.compile("^PowerAuthNetworking/(?<networkVersion>[0-9]+\\.[0-9]+\\.[0-9]+) " +
"\\((?<language>[a-zA-Z]{2}); (?<connection>[a-zA-Z0-9]+)\\) " +
"(?<product>[a-zA-Z0-9-_.]+)/(?<version>[0-9.]+) .*" +
"(?<product>[a-zA-Z0-9-_.]+)/(?<version>[0-9.]+(-[^ ]*)?) .*" +
"\\((?<platform>[^;]+); (?<os>[^/]+)/(?<osVersion>[^;]+); (?<model>[^)]+)\\)$");

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@
*/
package com.wultra.core.http.common.headers;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;

import java.util.Optional;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand All @@ -29,21 +33,92 @@
*/
class UserAgentTest {

private static final String[] USER_AGENTS = new String[] {
"PowerAuthNetworking/1.1.7 (en; cellular) com.wultra.app.Mobile-Token.wultra_test/2.0.0 (Apple; iOS/16.6.1; iphone12,3)",
"PowerAuthNetworking/1.2.1 (uk; wifi) com.wultra.android.mtoken.gdnexttest/1.0.0-gdnexttest (samsung; Android/13; SM-A047F)",
"PowerAuthNetworking/1.1.7 (en; unknown) com.wultra.app.MobileToken.wtest/2.0.0 (Apple; iOS/16.6.1; iphone10,6)",
"PowerAuthNetworking/1.1.7 (en; wifi) com.wultra.app.MobileToken.wtest/2.0.0 (Apple; iOS/16.7.1; iphone10,6)"
};

private static final String DEVICES = """
[
{
"networkVersion": "1.1.7",
"language": "en",
"connection": "cellular",
"product": "com.wultra.app.Mobile-Token.wultra_test",
"version": "2.0.0",
"platform": "Apple",
"os": "iOS",
"osVersion": "16.6.1",
"model": "iphone12,3"
},
{
"networkVersion": "1.2.1",
"language": "uk",
"connection": "wifi",
"product": "com.wultra.android.mtoken.gdnexttest",
"version": "1.0.0-gdnexttest",
"platform": "samsung",
"os": "Android",
"osVersion": "13",
"model": "SM-A047F"
},
{
"networkVersion": "1.1.7",
"language": "en",
"connection": "unknown",
"product": "com.wultra.app.MobileToken.wtest",
"version": "2.0.0",
"platform": "Apple",
"os": "iOS",
"osVersion": "16.6.1",
"model": "iphone10,6"
},
{
"networkVersion": "1.1.7",
"language": "en",
"connection": "wifi",
"product": "com.wultra.app.MobileToken.wtest",
"version": "2.0.0",
"platform": "Apple",
"os": "iOS",
"osVersion": "16.7.1",
"model": "iphone10,6"
}
]
""";

@Test
void parse() {
final String sample = "PowerAuthNetworking/1.1.7 (en; cellular) com.wultra.app.Mobile-Token.wultra_test/2.0.0 (Apple; iOS/16.6.1; iphone12,3)";
final Optional<UserAgent.Device> deviceOptional = UserAgent.parse(sample);
assertTrue(deviceOptional.isPresent());
final UserAgent.Device[] expectedDevices = readDevices();
for (int i = 0; i < USER_AGENTS.length; i++) {
final Optional<UserAgent.Device> deviceOptional = UserAgent.parse(USER_AGENTS[i]);
assertTrue(deviceOptional.isPresent());

final UserAgent.Device device = deviceOptional.get();
assertEquals("1.1.7", device.getNetworkVersion());
assertEquals("en", device.getLanguage());
assertEquals("cellular", device.getConnection());
assertEquals("com.wultra.app.Mobile-Token.wultra_test", device.getProduct());
assertEquals("2.0.0", device.getVersion());
assertEquals("Apple", device.getPlatform());
assertEquals("iOS", device.getOs());
assertEquals("16.6.1", device.getOsVersion());
assertEquals("iphone12,3", device.getModel());
final UserAgent.Device expectedDevice = expectedDevices[i];
assertEquals(expectedDevice, device);
}
//printCurrentDevices(devices);
}

private static UserAgent.Device[] readDevices() {
try {
final ObjectMapper om = new ObjectMapper();
return om.readValue(UserAgentTest.DEVICES, UserAgent.Device[].class);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}

private static void printCurrentDevices(List<UserAgent.Device> devices) {
try {
final ObjectMapper om = new ObjectMapper();
final String s = om.writeValueAsString(devices);
System.out.println(s);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
}

0 comments on commit 34cb223

Please sign in to comment.