Skip to content

Commit

Permalink
leveraging buildUrl method in combination with avoidExternalConnectio…
Browse files Browse the repository at this point in the history
…ns to replace placeholders in the provided url (#1158)
  • Loading branch information
giulong authored Nov 2, 2023
1 parent f6479ba commit e677e10
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 36 deletions.
5 changes: 3 additions & 2 deletions src/main/java/io/github/bonigarcia/wdm/WebDriverManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -1269,8 +1269,9 @@ protected String download(String driverVersion) throws IOException {
}

URL url;
if (config.isAvoidExternalConnections()) {
url = URI.create(config.getChromeDownloadUrlPattern()).toURL();
Optional<URL> optionalURL = buildUrl(driverVersion);
if (config.isAvoidExternalConnections() && optionalURL.isPresent()) {
url = optionalURL.get();
downloadedDriverVersion = driverVersion;
} else {
UrlHandler urlHandler = createUrlHandler(driverVersion);
Expand Down
116 changes: 82 additions & 34 deletions src/test/java/io/github/bonigarcia/wdm/WebDriverManagerTest.java
Original file line number Diff line number Diff line change
@@ -1,58 +1,40 @@
package io.github.bonigarcia.wdm;

import io.github.bonigarcia.wdm.config.Config;
import io.github.bonigarcia.wdm.managers.VoidDriverManager;
import io.github.bonigarcia.wdm.config.DriverManagerType;
import io.github.bonigarcia.wdm.online.Downloader;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.junit.jupiter.MockitoExtension;

import java.io.IOException;
import java.net.URI;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class WebDriverManagerTest {

@InjectMocks
private VoidDriverManager webDriverManager;
private static final String DUMMY_URL = "http://not.important";

private MockedStatic<URI> uriMockedStatic;
@InjectMocks
private DummyWebDriverManager webDriverManager;

@Mock
private Config config;

@Mock
private Downloader downloader;

@Mock
private URI uri;

@Mock
private URL url;

@BeforeEach
public void beforeEach() {
uriMockedStatic = mockStatic(URI.class);
}

@AfterEach
public void afterEach() {
uriMockedStatic.close();
}

@Test
@DisplayName("avoidExternalConnections() should fluently set to true the corresponding config parameter")
void avoidExternalConnections() {
Expand All @@ -64,22 +46,88 @@ void avoidExternalConnections() {

@DisplayName("download")
@ParameterizedTest(name = "with driver version {0} should download version {1}")
@ValueSource(strings = { "123", ".123" })
@ValueSource(strings = {"123", ".123"})
void download(String driverVersion) throws IOException {
String chromeDownloadUrlPattern = "https://chromeDownloadUrlPattern";
String expected = "expected";
String cleanDriverVersion = "123";

when(config.isAvoidExternalConnections()).thenReturn(true);
when(config.getChromeDownloadUrlPattern())
.thenReturn(chromeDownloadUrlPattern);

when(URI.create(chromeDownloadUrlPattern)).thenReturn(uri);
when(uri.toURL()).thenReturn(url);

when(downloader.download(url, cleanDriverVersion, "", null))
when(downloader.download(new URL(DUMMY_URL), cleanDriverVersion, "dummy", null))
.thenReturn(expected);

assertEquals(expected, webDriverManager.download(driverVersion));
}
}

private static class DummyWebDriverManager extends WebDriverManager {

@Override
protected List<URL> getDriverUrls(String driverVersion) {
return null;
}

@Override
protected String getDriverName() {
return "dummy";
}

@Override
protected String getDriverVersion() {
return null;
}

@Override
protected void setDriverVersion(String driverVersion) {

}

@Override
protected String getBrowserVersion() {
return null;
}

@Override
protected void setBrowserVersion(String browserVersion) {

}

@Override
protected void setDriverUrl(URL url) {

}

@Override
protected URL getDriverUrl() {
return null;
}

@Override
protected Optional<URL> getMirrorUrl() {
return Optional.empty();
}

@Override
protected Optional<String> getExportParameter() {
return Optional.empty();
}

@Override
public DriverManagerType getDriverManagerType() {
return null;
}

@Override
public WebDriverManager exportParameter(String exportParameter) {
return null;
}

@Override
protected Optional<URL> buildUrl(String driverVersion) {
try {
return Optional.of(new URL(DUMMY_URL));
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
}
}

0 comments on commit e677e10

Please sign in to comment.