Skip to content

Commit

Permalink
Refactor test to use java 8.
Browse files Browse the repository at this point in the history
Run tests with Java 8.
  • Loading branch information
unverbraucht committed Jan 25, 2024
1 parent 084ae1d commit 953b031
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 26 deletions.
10 changes: 0 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
<skipTests>true</skipTests>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<netty.version>4.1.105.Final</netty.version>
<testCompileSource>11</testCompileSource>
</properties>

<profiles>
Expand Down Expand Up @@ -374,19 +373,12 @@
</excludes>
</configuration>
</execution>
<!-- We'd like to use JDK11 APIs for the tests -->
<execution>
<id>default-testCompile</id>
<phase>process-test-sources</phase>
<goals>
<goal>testCompile</goal>
</goals>
<configuration>
<source>${testCompileSource}</source>
<target>${testCompileSource}</target>
<release>${testCompileSource}</release>
<compilerArgs>--add-modules=java.net.http</compilerArgs>
</configuration>
</execution>
</executions>
<!-- defaults for compile and testCompile -->
Expand Down Expand Up @@ -417,8 +409,6 @@
<configuration>
<argLine>
-javaagent:"${settings.localRepository}"/org/jmockit/jmockit/1.46/jmockit-1.46.jar
--add-reads netty.socketio=java.net.http
--add-modules=java.net.http
</argLine>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.netty.channel.ChannelHandlerContext;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.ServerSocket;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -122,31 +123,43 @@ private URI createTestServerUri(final String query) throws URISyntaxException {
query, null);
}

private HttpResponse<String> makeSocketIoRequest(final String sessionId, final String bodyForPost)
private String makeSocketIoRequest(final String sessionId, final String bodyForPost)
throws URISyntaxException, IOException, InterruptedException {
final URI uri = createTestServerUri("EIO=4&transport=polling&t=Oqd9eWh" + (sessionId == null ? "" : "&sid=" + sessionId));
HttpClient client = HttpClient.newHttpClient();
final var builder = HttpRequest.newBuilder()
.uri(uri);

URLConnection con = uri.toURL().openConnection();
HttpURLConnection http = (HttpURLConnection)con;
if (bodyForPost != null) {
http.setRequestMethod("POST"); // PUT is another valid option
http.setDoOutput(true);
}

if (bodyForPost != null) {
builder.POST(BodyPublishers.ofString(bodyForPost));
byte[] out = bodyForPost.toString().getBytes(StandardCharsets.UTF_8);
http.setFixedLengthStreamingMode(out.length);
http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
http.connect();
try (OutputStream os = http.getOutputStream()) {
os.write(out);
}
} else {
builder.GET();
http.connect();
}

try (BufferedReader reader = new BufferedReader(new InputStreamReader(http.getInputStream(), StandardCharsets.UTF_8))) {
return reader.lines().collect(Collectors.joining("\n"));
}
return client.send(builder.build(), BodyHandlers.ofString());
}

private void postMessage(final String sessionId, final String body)
throws URISyntaxException, IOException, InterruptedException {
HttpResponse<String> response = makeSocketIoRequest(sessionId, body);
final String responseStr = response.body();
final String responseStr = makeSocketIoRequest(sessionId, body);
Assert.assertEquals(responseStr, "ok");
}

private String[] pollForListOfResponses(final String sessionId)
throws URISyntaxException, IOException, InterruptedException {
HttpResponse<String> response = makeSocketIoRequest(sessionId, null);
final String responseStr = response.body();
final String responseStr = makeSocketIoRequest(sessionId, null);
return responseStr.split(packetSeparator);
}

Expand Down

0 comments on commit 953b031

Please sign in to comment.