2024-09-27 11:45:54 +00:00
|
|
|
package nu.marginalia.livecapture;
|
|
|
|
|
2025-02-04 12:36:49 +00:00
|
|
|
import com.github.tomakehurst.wiremock.WireMockServer;
|
|
|
|
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
|
|
|
|
import nu.marginalia.WmsaHome;
|
2024-09-27 11:45:54 +00:00
|
|
|
import org.junit.jupiter.api.Assertions;
|
|
|
|
import org.junit.jupiter.api.BeforeAll;
|
2025-01-09 13:51:11 +00:00
|
|
|
import org.junit.jupiter.api.Tag;
|
2024-09-27 11:45:54 +00:00
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import org.testcontainers.containers.GenericContainer;
|
|
|
|
import org.testcontainers.junit.jupiter.Testcontainers;
|
|
|
|
import org.testcontainers.utility.DockerImageName;
|
|
|
|
|
2025-02-04 12:36:49 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.SocketException;
|
2024-09-27 11:45:54 +00:00
|
|
|
import java.net.URI;
|
2025-02-04 12:36:49 +00:00
|
|
|
import java.util.Map;import static com.github.tomakehurst.wiremock.client.WireMock.*;
|
|
|
|
import static java.net.NetworkInterface.getNetworkInterfaces;
|
2024-09-27 11:45:54 +00:00
|
|
|
|
|
|
|
@Testcontainers
|
2025-01-09 13:51:11 +00:00
|
|
|
@Tag("slow")
|
2024-09-27 11:45:54 +00:00
|
|
|
public class BrowserlessClientTest {
|
2025-01-09 13:51:11 +00:00
|
|
|
static GenericContainer<?> container = new GenericContainer<>(DockerImageName.parse("browserless/chrome"))
|
|
|
|
.withEnv(Map.of("TOKEN", "BROWSERLESS_TOKEN"))
|
2025-02-04 12:36:49 +00:00
|
|
|
.withNetworkMode("bridge")
|
2025-01-09 13:51:11 +00:00
|
|
|
.withExposedPorts(3000);
|
2024-09-27 11:45:54 +00:00
|
|
|
|
2025-02-04 12:36:49 +00:00
|
|
|
static WireMockServer wireMockServer = new WireMockServer(WireMockConfiguration.wireMockConfig().port(18089));
|
|
|
|
|
|
|
|
static String localIp;
|
2024-09-27 11:45:54 +00:00
|
|
|
@BeforeAll
|
2025-02-04 12:36:49 +00:00
|
|
|
public static void setup() throws IOException {
|
2024-09-27 11:45:54 +00:00
|
|
|
container.start();
|
2025-02-04 12:36:49 +00:00
|
|
|
|
|
|
|
wireMockServer.start();
|
|
|
|
wireMockServer.stubFor(get("/").willReturn(aResponse().withStatus(200).withBody("Ok")));
|
|
|
|
|
|
|
|
localIp = findLocalIp();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String findLocalIp() throws SocketException {
|
|
|
|
var interfaces = getNetworkInterfaces();
|
|
|
|
while (interfaces.hasMoreElements()) {
|
|
|
|
var iface = interfaces.nextElement();
|
|
|
|
if (iface.isLoopback())
|
|
|
|
continue;
|
|
|
|
else if (iface.isVirtual())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
var addresses = iface.getInetAddresses();
|
|
|
|
|
|
|
|
while (addresses.hasMoreElements()) {
|
|
|
|
var address = addresses.nextElement();
|
|
|
|
|
|
|
|
if (!address.isSiteLocalAddress()) continue;
|
|
|
|
|
|
|
|
return address.getHostAddress();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "127.0.0.1";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Tag("flaky")
|
|
|
|
@Test
|
|
|
|
public void testInspectContentUA__Flaky() throws Exception {
|
|
|
|
try (var client = new BrowserlessClient(URI.create("http://" + container.getHost() + ":" + container.getMappedPort(3000)))) {
|
|
|
|
client.content("http://" + localIp + ":18089/", BrowserlessClient.GotoOptions.defaultValues());
|
|
|
|
}
|
|
|
|
|
|
|
|
wireMockServer.verify(getRequestedFor(urlEqualTo("/")).withHeader("User-Agent", equalTo(WmsaHome.getUserAgent().uaString())));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Tag("flaky")
|
|
|
|
@Test
|
|
|
|
public void testInspectScreenshotUA__Flaky() throws Exception {
|
|
|
|
try (var client = new BrowserlessClient(URI.create("http://" + container.getHost() + ":" + container.getMappedPort(3000)))) {
|
|
|
|
client.screenshot("http://" + localIp + ":18089/", BrowserlessClient.GotoOptions.defaultValues(), BrowserlessClient.ScreenshotOptions.defaultValues());
|
|
|
|
}
|
|
|
|
|
|
|
|
wireMockServer.verify(getRequestedFor(urlEqualTo("/")).withHeader("User-Agent", equalTo(WmsaHome.getUserAgent().uaString())));
|
2024-09-27 11:45:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testContent() throws Exception {
|
|
|
|
try (var client = new BrowserlessClient(URI.create("http://" + container.getHost() + ":" + container.getMappedPort(3000)))) {
|
|
|
|
var content = client.content("https://www.marginalia.nu/", BrowserlessClient.GotoOptions.defaultValues());
|
|
|
|
Assertions.assertNotNull(content, "Content should not be null");
|
|
|
|
Assertions.assertFalse(content.isBlank(), "Content should not be empty");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testScreenshot() throws Exception {
|
|
|
|
try (var client = new BrowserlessClient(URI.create("http://" + container.getHost() + ":" + container.getMappedPort(3000)))) {
|
|
|
|
var screenshot = client.screenshot("https://www.marginalia.nu/", BrowserlessClient.GotoOptions.defaultValues(), BrowserlessClient.ScreenshotOptions.defaultValues());
|
|
|
|
Assertions.assertNotNull(screenshot, "Screenshot should not be null");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|