Move okhttp client from http-api

The client has been recreated with a new builder off of the http-api
client for awhile anyway since runelite-client has multiple other
requirements (caching, tls, etc). This fully moves creation of the
okhttp client into both http-service and runelite-client separately.

I've kept the CLIENT field in http-api for now since a few external
plugins depend on it currently.
This commit is contained in:
Adam
2021-12-23 12:36:05 -05:00
parent 37d538f0db
commit 0a501429e6
14 changed files with 162 additions and 179 deletions

View File

@@ -34,11 +34,6 @@
<name>Web API</name> <name>Web API</name>
<artifactId>http-api</artifactId> <artifactId>http-api</artifactId>
<properties>
<git.commit.id.abbrev>nogit</git.commit.id.abbrev>
<git.dirty>false</git.dirty>
</properties>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>com.squareup.okhttp3</groupId> <groupId>com.squareup.okhttp3</groupId>
@@ -77,39 +72,4 @@
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<version>2.2.6</version>
<executions>
<execution>
<id>query-git-info</id>
<goals>
<goal>revision</goal>
</goals>
<configuration>
<failOnNoGitDirectory>false</failOnNoGitDirectory>
<failOnUnableToExtractRepoInfo>false</failOnUnableToExtractRepoInfo>
<gitDescribe>
<skip>true</skip>
</gitDescribe>
<includeOnlyProperties>
<includeOnlyProperty>git.commit.id.abbrev</includeOnlyProperty>
<includeOnlyProperty>git.dirty</includeOnlyProperty>
</includeOnlyProperties>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project> </project>

View File

@@ -27,78 +27,25 @@ package net.runelite.http.api;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import java.awt.Color; import java.awt.Color;
import java.io.IOException;
import java.io.InputStream;
import java.time.Instant; import java.time.Instant;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import lombok.Getter;
import net.runelite.http.api.gson.ColorTypeAdapter; import net.runelite.http.api.gson.ColorTypeAdapter;
import net.runelite.http.api.gson.IllegalReflectionExclusion; import net.runelite.http.api.gson.IllegalReflectionExclusion;
import net.runelite.http.api.gson.InstantTypeAdapter; import net.runelite.http.api.gson.InstantTypeAdapter;
import okhttp3.Interceptor;
import okhttp3.MediaType; import okhttp3.MediaType;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class RuneLiteAPI public class RuneLiteAPI
{ {
private static final Logger logger = LoggerFactory.getLogger(RuneLiteAPI.class);
public static final String RUNELITE_AUTH = "RUNELITE-AUTH"; public static final String RUNELITE_AUTH = "RUNELITE-AUTH";
public static final String RUNELITE_MACHINEID = "RUNELITE-MACHINEID"; public static final String RUNELITE_MACHINEID = "RUNELITE-MACHINEID";
public static final OkHttpClient CLIENT; @Deprecated
public static OkHttpClient CLIENT;
public static final Gson GSON; public static final Gson GSON;
public static final MediaType JSON = MediaType.parse("application/json"); public static final MediaType JSON = MediaType.parse("application/json");
public static String userAgent;
private static final Properties properties = new Properties();
@Getter
private static String version;
static static
{ {
try
{
InputStream in = RuneLiteAPI.class.getResourceAsStream("/runelite.properties");
properties.load(in);
version = properties.getProperty("runelite.version");
String commit = properties.getProperty("runelite.commit");
boolean dirty = Boolean.parseBoolean(properties.getProperty("runelite.dirty"));
userAgent = "RuneLite/" + version + "-" + commit + (dirty ? "+" : "");
}
catch (NumberFormatException e)
{
throw new RuntimeException("Version string has not been substituted; Re-run maven");
}
catch (IOException ex)
{
logger.error(null, ex);
}
CLIENT = new OkHttpClient.Builder()
.pingInterval(30, TimeUnit.SECONDS)
.addNetworkInterceptor(new Interceptor()
{
@Override
public Response intercept(Chain chain) throws IOException
{
Request userAgentRequest = chain.request()
.newBuilder()
.header("User-Agent", userAgent)
.build();
return chain.proceed(userAgentRequest);
}
})
.build();
GsonBuilder gsonBuilder = new GsonBuilder(); GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder gsonBuilder

View File

@@ -1,3 +0,0 @@
runelite.version=${project.version}
runelite.commit=${git.commit.id.abbrev}
runelite.dirty=${git.dirty}

View File

@@ -1,58 +0,0 @@
/*
* Copyright (c) 2018, Adam <Adam@sigterm.info>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.http.api;
import java.io.IOException;
import okhttp3.Request;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
public class RuneLiteAPITest
{
@Rule
public final MockWebServer server = new MockWebServer();
@Before
public void before() throws IOException
{
server.enqueue(new MockResponse().setBody("OK"));
}
@Test
public void testUserAgent() throws IOException, InterruptedException
{
Request request = new Request.Builder()
.url(server.url("/").url())
.build();
RuneLiteAPI.CLIENT.newCall(request).execute().close();
// rest of UA depends on if git is found
assertTrue(server.takeRequest().getHeader("User-Agent").startsWith("RuneLite/" + RuneLiteAPI.getVersion()));
}
}

View File

@@ -39,6 +39,8 @@
<properties> <properties>
<spring.boot.version>1.5.6.RELEASE</spring.boot.version> <spring.boot.version>1.5.6.RELEASE</spring.boot.version>
<git.commit.id.abbrev>nogit</git.commit.id.abbrev>
<git.dirty>false</git.dirty>
</properties> </properties>
<dependencies> <dependencies>
@@ -154,6 +156,13 @@
<build> <build>
<finalName>runelite-${project.version}</finalName> <finalName>runelite-${project.version}</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
@@ -164,6 +173,10 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId> <artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version> <version>${spring.boot.version}</version>
<configuration>
<!-- Prevent reloading resources since it doesn't work when the resources are also filtered -->
<addResources>false</addResources>
</configuration>
</plugin> </plugin>
<plugin> <plugin>
<groupId>com.github.kongchen</groupId> <groupId>com.github.kongchen</groupId>
@@ -215,6 +228,42 @@
</execution> </execution>
</executions> </executions>
</plugin> </plugin>
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<version>2.2.6</version>
<executions>
<execution>
<id>query-git-info</id>
<goals>
<goal>revision</goal>
</goals>
<configuration>
<failOnNoGitDirectory>false</failOnNoGitDirectory>
<failOnUnableToExtractRepoInfo>false</failOnUnableToExtractRepoInfo>
<gitDescribe>
<skip>true</skip>
</gitDescribe>
<includeOnlyProperties>
<includeOnlyProperty>git.commit.id.abbrev</includeOnlyProperty>
<includeOnlyProperty>git.dirty</includeOnlyProperty>
</includeOnlyProperties>
</configuration>
</execution>
</executions>
</plugin>
<!-- Automatic expansion of properties in configuration https://docs.spring.io/spring-boot/docs/2.0.0.M7/reference/html/howto-properties-and-configuration.html#howto-automatic-expansion-maven -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<configuration>
<delimiters>
<delimiter>@</delimiter>
</delimiters>
<useDefaultDelimiters>false</useDefaultDelimiters>
</configuration>
</plugin>
</plugins> </plugins>
</build> </build>
</project> </project>

View File

@@ -32,6 +32,7 @@ import java.io.IOException;
import java.time.Instant; import java.time.Instant;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.naming.NamingException; import javax.naming.NamingException;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextEvent;
@@ -39,10 +40,10 @@ import javax.servlet.ServletContextListener;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.sql.DataSource; import javax.sql.DataSource;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.runelite.http.api.RuneLiteAPI;
import net.runelite.http.service.util.InstantConverter; import net.runelite.http.service.util.InstantConverter;
import okhttp3.Cache; import okhttp3.Cache;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
import okhttp3.Request;
import org.slf4j.ILoggerFactory; import org.slf4j.ILoggerFactory;
import org.slf4j.impl.StaticLoggerBinder; import org.slf4j.impl.StaticLoggerBinder;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
@@ -200,9 +201,24 @@ public class SpringBootWebApplication extends SpringBootServletInitializer
} }
@Bean @Bean
public OkHttpClient okHttpClient() public OkHttpClient okHttpClient(
@Value("${runelite.version}") String version,
@Value("${runelite.commit}") String commit,
@Value("${runelite.dirty}") boolean dirty
)
{ {
return RuneLiteAPI.CLIENT; final String userAgent = "RuneLite/" + version + "-" + commit + (dirty ? "+" : "");
return new OkHttpClient.Builder()
.pingInterval(30, TimeUnit.SECONDS)
.addNetworkInterceptor(chain ->
{
Request userAgentRequest = chain.request()
.newBuilder()
.header("User-Agent", userAgent)
.build();
return chain.proceed(userAgentRequest);
})
.build();
} }
public static void main(String[] args) public static void main(String[] args)

View File

@@ -98,6 +98,7 @@ public class AccountService
private final String oauthClientId; private final String oauthClientId;
private final String oauthClientSecret; private final String oauthClientSecret;
private final String oauthCallback; private final String oauthCallback;
private final String runeliteVersion;
private final AuthFilter auth; private final AuthFilter auth;
private final RedisPool jedisPool; private final RedisPool jedisPool;
@@ -107,6 +108,7 @@ public class AccountService
@Value("${oauth.client-id}") String oauthClientId, @Value("${oauth.client-id}") String oauthClientId,
@Value("${oauth.client-secret}") String oauthClientSecret, @Value("${oauth.client-secret}") String oauthClientSecret,
@Value("${oauth.callback}") String oauthCallback, @Value("${oauth.callback}") String oauthCallback,
@Value("${runelite.version}") String runeliteVersion,
AuthFilter auth, AuthFilter auth,
RedisPool jedisPool RedisPool jedisPool
) )
@@ -115,6 +117,7 @@ public class AccountService
this.oauthClientId = oauthClientId; this.oauthClientId = oauthClientId;
this.oauthClientSecret = oauthClientSecret; this.oauthClientSecret = oauthClientSecret;
this.oauthCallback = oauthCallback; this.oauthCallback = oauthCallback;
this.runeliteVersion = runeliteVersion;
this.auth = auth; this.auth = auth;
this.jedisPool = jedisPool; this.jedisPool = jedisPool;
@@ -143,7 +146,7 @@ public class AccountService
{ {
State state = new State(); State state = new State();
state.setUuid(uuid); state.setUuid(uuid);
state.setApiVersion(RuneLiteAPI.getVersion()); state.setApiVersion(runeliteVersion);
OAuth20Service service = new ServiceBuilder() OAuth20Service service = new ServiceBuilder()
.apiKey(oauthClientId) .apiKey(oauthClientId)

View File

@@ -33,6 +33,9 @@ mongo:
database: runelite database: runelite
runelite: runelite:
version: @project.version@
commit: @git.commit.id.abbrev@
dirty: @git.dirty@
# Twitter client for feed # Twitter client for feed
twitter: twitter:
consumerkey: consumerkey:

View File

@@ -38,6 +38,8 @@
<properties> <properties>
<jarsigner.skip>true</jarsigner.skip> <jarsigner.skip>true</jarsigner.skip>
<pmd.skip>true</pmd.skip> <pmd.skip>true</pmd.skip>
<git.commit.id.abbrev>nogit</git.commit.id.abbrev>
<git.dirty>false</git.dirty>
</properties> </properties>
<dependencies> <dependencies>
@@ -474,6 +476,30 @@
</execution> </execution>
</executions> </executions>
</plugin> </plugin>
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<version>2.2.6</version>
<executions>
<execution>
<id>query-git-info</id>
<goals>
<goal>revision</goal>
</goals>
<configuration>
<failOnNoGitDirectory>false</failOnNoGitDirectory>
<failOnUnableToExtractRepoInfo>false</failOnUnableToExtractRepoInfo>
<gitDescribe>
<skip>true</skip>
</gitDescribe>
<includeOnlyProperties>
<includeOnlyProperty>git.commit.id.abbrev</includeOnlyProperty>
<includeOnlyProperty>git.dirty</includeOnlyProperty>
</includeOnlyProperties>
</configuration>
</execution>
</executions>
</plugin>
</plugins> </plugins>
</build> </build>
</project> </project>

View File

@@ -44,6 +44,7 @@ import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.security.cert.X509Certificate; import java.security.cert.X509Certificate;
import java.util.Locale; import java.util.Locale;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream; import java.util.stream.Stream;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.inject.Provider; import javax.inject.Provider;
@@ -80,6 +81,7 @@ import net.runelite.client.ui.overlay.worldmap.WorldMapOverlay;
import net.runelite.http.api.RuneLiteAPI; import net.runelite.http.api.RuneLiteAPI;
import okhttp3.Cache; import okhttp3.Cache;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response; import okhttp3.Response;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -97,6 +99,7 @@ public class RuneLite
public static final File DEFAULT_CONFIG_FILE = new File(RUNELITE_DIR, "settings.properties"); public static final File DEFAULT_CONFIG_FILE = new File(RUNELITE_DIR, "settings.properties");
private static final int MAX_OKHTTP_CACHE_SIZE = 20 * 1024 * 1024; // 20mb private static final int MAX_OKHTTP_CACHE_SIZE = 20 * 1024 * 1024; // 20mb
public static String USER_AGENT = "RuneLite/" + RuneLiteProperties.getVersion() + "-" + RuneLiteProperties.getCommit() + (RuneLiteProperties.isDirty() ? "+" : "");
@Getter @Getter
private static Injector injector; private static Injector injector;
@@ -203,16 +206,8 @@ public class RuneLite
} }
}); });
OkHttpClient.Builder okHttpClientBuilder = RuneLiteAPI.CLIENT.newBuilder(); final OkHttpClient okHttpClient = buildHttpClient(options.has("insecure-skip-tls-verification"));
setupCache(okHttpClientBuilder, new File(CACHE_DIR, "okhttp")); RuneLiteAPI.CLIENT = okHttpClient;
final boolean insecureSkipTlsVerification = options.has("insecure-skip-tls-verification");
if (insecureSkipTlsVerification || RuneLiteProperties.isInsecureSkipTlsVerification())
{
setupInsecureTrustManager(okHttpClientBuilder);
}
final OkHttpClient okHttpClient = okHttpClientBuilder.build();
SplashScreen.init(); SplashScreen.init();
SplashScreen.stage(0, "Retrieving client", ""); SplashScreen.stage(0, "Retrieving client", "");
@@ -416,9 +411,20 @@ public class RuneLite
} }
@VisibleForTesting @VisibleForTesting
static void setupCache(OkHttpClient.Builder builder, File cacheDir) static OkHttpClient buildHttpClient(boolean insecureSkipTlsVerification)
{ {
builder.cache(new Cache(cacheDir, MAX_OKHTTP_CACHE_SIZE)) OkHttpClient.Builder builder = new OkHttpClient.Builder()
.pingInterval(30, TimeUnit.SECONDS)
.addNetworkInterceptor(chain ->
{
Request userAgentRequest = chain.request()
.newBuilder()
.header("User-Agent", USER_AGENT)
.build();
return chain.proceed(userAgentRequest);
})
// Setup cache
.cache(new Cache(new File(CACHE_DIR, "okhttp"), MAX_OKHTTP_CACHE_SIZE))
.addNetworkInterceptor(chain -> .addNetworkInterceptor(chain ->
{ {
// This has to be a network interceptor so it gets hit before the cache tries to store stuff // This has to be a network interceptor so it gets hit before the cache tries to store stuff
@@ -432,6 +438,13 @@ public class RuneLite
} }
return res; return res;
}); });
if (insecureSkipTlsVerification || RuneLiteProperties.isInsecureSkipTlsVerification())
{
setupInsecureTrustManager(builder);
}
return builder.build();
} }
private static void setupInsecureTrustManager(OkHttpClient.Builder okHttpClientBuilder) private static void setupInsecureTrustManager(OkHttpClient.Builder okHttpClientBuilder)

View File

@@ -35,6 +35,8 @@ import okhttp3.HttpUrl;
public class RuneLiteProperties public class RuneLiteProperties
{ {
private static final String RUNELITE_VERSION = "runelite.version"; private static final String RUNELITE_VERSION = "runelite.version";
private static final String RUNELITE_COMMIT = "runelite.commit";
private static final String RUNELITE_DIRTY = "runelite.dirty";
private static final String DISCORD_INVITE = "runelite.discord.invite"; private static final String DISCORD_INVITE = "runelite.discord.invite";
private static final String LAUNCHER_VERSION_PROPERTY = "runelite.launcher.version"; private static final String LAUNCHER_VERSION_PROPERTY = "runelite.launcher.version";
private static final String INSECURE_SKIP_TLS_VERIFICATION_PROPERTY = "runelite.insecure-skip-tls-verification"; private static final String INSECURE_SKIP_TLS_VERIFICATION_PROPERTY = "runelite.insecure-skip-tls-verification";
@@ -67,6 +69,16 @@ public class RuneLiteProperties
return properties.getProperty(RUNELITE_VERSION); return properties.getProperty(RUNELITE_VERSION);
} }
public static String getCommit()
{
return properties.getProperty(RUNELITE_COMMIT);
}
public static boolean isDirty()
{
return Boolean.parseBoolean(properties.getProperty(RUNELITE_DIRTY));
}
public static String getDiscordInvite() public static String getDiscordInvite()
{ {
return properties.getProperty(DISCORD_INVITE); return properties.getProperty(DISCORD_INVITE);

View File

@@ -35,8 +35,8 @@ import javax.inject.Named;
import javax.inject.Singleton; import javax.inject.Singleton;
import lombok.Getter; import lombok.Getter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.runelite.client.RuneLite;
import net.runelite.client.eventbus.EventBus; import net.runelite.client.eventbus.EventBus;
import net.runelite.http.api.RuneLiteAPI;
import net.runelite.http.api.ws.WebsocketGsonFactory; import net.runelite.http.api.ws.WebsocketGsonFactory;
import net.runelite.http.api.ws.WebsocketMessage; import net.runelite.http.api.ws.WebsocketMessage;
import net.runelite.http.api.ws.messages.Handshake; import net.runelite.http.api.ws.messages.Handshake;
@@ -106,7 +106,7 @@ public class WSClient extends WebSocketListener implements AutoCloseable
Request request = new Request.Builder() Request request = new Request.Builder()
.url(runeliteWs) .url(runeliteWs)
.header("User-Agent", RuneLiteAPI.userAgent) .header("User-Agent", RuneLite.USER_AGENT)
.build(); .build();
webSocket = okHttpClient.newWebSocket(request, this); webSocket = okHttpClient.newWebSocket(request, this);

View File

@@ -1,5 +1,7 @@
runelite.title=RuneLite runelite.title=RuneLite
runelite.version=${project.version} runelite.version=${project.version}
runelite.commit=${git.commit.id.abbrev}
runelite.dirty=${git.dirty}
runelite.discord.appid=409416265891971072 runelite.discord.appid=409416265891971072
runelite.discord.invite=https://discord.gg/ArdAhnN runelite.discord.invite=https://discord.gg/ArdAhnN
runelite.github.link=https://github.com/runelite runelite.github.link=https://github.com/runelite

View File

@@ -30,7 +30,6 @@ import java.time.ZoneId;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Locale; import java.util.Locale;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import net.runelite.http.api.RuneLiteAPI;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
import okhttp3.Request; import okhttp3.Request;
import okhttp3.Response; import okhttp3.Response;
@@ -38,11 +37,12 @@ import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer; import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest; import okhttp3.mockwebserver.RecordedRequest;
import org.junit.Assert; import org.junit.Assert;
import static org.junit.Assert.assertTrue;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.TemporaryFolder; import org.junit.rules.TemporaryFolder;
public class OkHttpCacheSanityTest public class OkHttpTest
{ {
@Rule @Rule
public TemporaryFolder cacheFolder = new TemporaryFolder(); public TemporaryFolder cacheFolder = new TemporaryFolder();
@@ -71,9 +71,7 @@ public class OkHttpCacheSanityTest
@Test @Test
public void testCacheSanity() throws IOException, InterruptedException public void testCacheSanity() throws IOException, InterruptedException
{ {
OkHttpClient.Builder builder = RuneLiteAPI.CLIENT.newBuilder(); OkHttpClient client = RuneLite.buildHttpClient(false);
RuneLite.setupCache(builder, cacheFolder.getRoot());
OkHttpClient client = builder.build();
Instant lastModified = Instant.now().minusSeconds(20); Instant lastModified = Instant.now().minusSeconds(20);
@@ -122,4 +120,19 @@ public class OkHttpCacheSanityTest
Assert.assertNotNull("cache did not make a conditional request", req); Assert.assertNotNull("cache did not make a conditional request", req);
Assert.assertNotNull(req.getHeader("If-Modified-Since")); Assert.assertNotNull(req.getHeader("If-Modified-Since"));
} }
@Test
public void testUserAgent() throws IOException, InterruptedException
{
server.enqueue(new MockResponse().setBody("OK"));
Request request = new Request.Builder()
.url(server.url("/"))
.build();
RuneLite.buildHttpClient(false)
.newCall(request).execute().close();
// rest of UA depends on if git is found
assertTrue(server.takeRequest().getHeader("User-Agent").startsWith("RuneLite/" + RuneLiteProperties.getVersion()));
}
} }