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
15 changes: 15 additions & 0 deletions core/src/main/java/com/minekube/connect/module/CommonModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,14 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import lombok.RequiredArgsConstructor;
import okhttp3.OkHttpClient;

@RequiredArgsConstructor
public class CommonModule extends AbstractModule {
private static final long WATCH_PING_INTERVAL_SECONDS = 30;

private final Path dataDirectory;

@Override
Expand Down Expand Up @@ -154,6 +157,18 @@ public OkHttpClient connectOkHttpClient(
.build();
}

@Provides
@Singleton
@Named("watchHttpClient")
public OkHttpClient watchOkHttpClient(
@Named("connectHttpClient") OkHttpClient connectHttpClient
) {
return connectHttpClient.newBuilder()
.readTimeout(0, TimeUnit.MILLISECONDS)
.pingInterval(WATCH_PING_INTERVAL_SECONDS, TimeUnit.SECONDS)
.build();
}

@RequiredArgsConstructor
private static class Token {
@SerializedName("token") final String token;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class WatchClient {
private final ConnectConfig config;

@Inject
public WatchClient(@Named("connectHttpClient") OkHttpClient httpClient, ConnectConfig config) {
public WatchClient(@Named("watchHttpClient") OkHttpClient httpClient, ConnectConfig config) {
this.httpClient = httpClient;
this.config = config;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ class CommonModuleTest {
@Test
void connectHttpClientSendsPluginVersionHeader() throws Exception {
CommonModule module = new CommonModule(tempDir);
PlatformUtils platformUtils = mock(PlatformUtils.class);
when(platformUtils.authType()).thenReturn(PlatformUtils.AuthType.ONLINE);
when(platformUtils.serverImplementationName()).thenReturn("Paper");
when(platformUtils.minecraftVersion()).thenReturn("1.21.11");
when(platformUtils.getPlayerCount()).thenReturn(7);
PlatformUtils platformUtils = platformUtils();

OkHttpClient client = module.connectOkHttpClient(
module.defaultOkHttpClient(),
Expand All @@ -49,8 +45,50 @@ void connectHttpClientSendsPluginVersionHeader() throws Exception {

assertEquals(Constants.VERSION, recorded.getHeader("Connect-Version"));
assertEquals("spigot", recorded.getHeader("Connect-Platform"));
assertEquals("Paper", recorded.getHeaders().values("Connect-Platform").get(1));
assertEquals("test-server-impl", recorded.getHeaders().values("Connect-Platform").get(1));
}
}
}

@Test
void watchHttpClientKeepsConnectHeadersAndUsesWebSocketLiveness() throws Exception {
CommonModule module = new CommonModule(tempDir);
PlatformUtils platformUtils = platformUtils();

OkHttpClient connectClient = module.connectOkHttpClient(
module.defaultOkHttpClient(),
platformUtils,
"spigot",
new SimpleConnectApi(mock(ConnectLogger.class))
);
OkHttpClient watchClient = module.watchOkHttpClient(connectClient);

assertEquals(0, watchClient.readTimeoutMillis());
assertEquals(30_000, watchClient.pingIntervalMillis());

try (MockWebServer server = new MockWebServer()) {
server.enqueue(new MockResponse().setBody("ok"));
server.start();

Request request = new Request.Builder()
.url(server.url("/watch"))
.build();
try (Response ignored = watchClient.newCall(request).execute()) {
RecordedRequest recorded = server.takeRequest();

assertEquals(Constants.VERSION, recorded.getHeader("Connect-Version"));
assertEquals("spigot", recorded.getHeader("Connect-Platform"));
assertEquals("test-server-impl", recorded.getHeaders().values("Connect-Platform").get(1));
}
}
}

private static PlatformUtils platformUtils() {
PlatformUtils platformUtils = mock(PlatformUtils.class);
when(platformUtils.authType()).thenReturn(PlatformUtils.AuthType.ONLINE);
when(platformUtils.serverImplementationName()).thenReturn("test-server-impl");
when(platformUtils.minecraftVersion()).thenReturn("test-minecraft-version");
when(platformUtils.getPlayerCount()).thenReturn(7);
return platformUtils;
}
}
Loading