Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mapbox.core.utils;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.mapbox.core.constants.Constants;

Expand All @@ -12,6 +13,8 @@
*/
public final class ApiCallHelper {

private static final String ONLY_PRINTABLE_CHARS = "[^\\p{ASCII}]";

private ApiCallHelper() {
// Private constructor preventing instances of class
}
Expand All @@ -32,10 +35,31 @@ public static String getHeaderUserAgent(@Nullable String clientAppName) {
if (TextUtils.isEmpty(osName) || TextUtils.isEmpty(osVersion) || TextUtils.isEmpty(osArch)) {
return Constants.HEADER_USER_AGENT;
} else {
return getHeaderUserAgent(clientAppName, osName, osVersion, osArch);
}
}

/**
* Computes a full user agent header of the form:
* {@code MapboxJava/1.2.0 Mac OS X/10.11.5 (x86_64)}.
*
* @param clientAppName Application Name
* @param osName OS name
* @param osVersion OS version
* @param osArch OS Achitecture
* @return {@link String} representing the header user agent
* @since 1.0.0
*/
public static String getHeaderUserAgent(@Nullable String clientAppName,
@NonNull String osName, @NonNull String osVersion, @NonNull String osArch) {

osName = osName.replaceAll(ONLY_PRINTABLE_CHARS, "");
osVersion = osVersion.replaceAll(ONLY_PRINTABLE_CHARS, "");
osArch = osArch.replaceAll(ONLY_PRINTABLE_CHARS, "");
String baseUa = String.format(
Locale.US, "%s %s/%s (%s)", Constants.HEADER_USER_AGENT, osName, osVersion, osArch);

return TextUtils.isEmpty(clientAppName) ? baseUa : String.format(Locale.US, "%s %s",
clientAppName, baseUa);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package com.mapbox.core.utils;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import com.mapbox.core.constants.Constants;

import org.junit.Assert;
import org.junit.Test;

import java.util.Locale;

public class ApiCallHelperTest {

@Test
Expand All @@ -14,4 +20,22 @@ public void getHeaderUserAgent_formatsStringCorrectly() throws Exception {
assertTrue(ApiCallHelper.getHeaderUserAgent(
"AppName").startsWith("AppName"));
}

@Test
public void getHeaderUserAgent_nonAsciiCharsRemoved() {

String osName = "os.name";
String osVersion = "os.version";
String osArch = "os.arch";

String userAgent = String.format(
Locale.US, "%s %s/%s (%s)", Constants.HEADER_USER_AGENT, osName, osVersion, osArch);

String userAgentWithExtraChars = ApiCallHelper.getHeaderUserAgent(null,
osName + '\u00A9', // copyright
osVersion + '\u00AE', // Registered Sign
osArch + '\u2122'); // TM

Assert.assertEquals(userAgent, userAgentWithExtraChars);
}
}