Skip to content

Commit

Permalink
[Misc] Add a test to validate that the credntial are taken into accou…
Browse files Browse the repository at this point in the history
…nt for an HTTP maven repository
  • Loading branch information
tmortagne committed Nov 27, 2024
1 parent c80ccf2 commit 1da04f3
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@
<artifactId>wiremock-standalone</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.xwiki.commons</groupId>
<artifactId>xwiki-commons-tool-test-component</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.xwiki.commons</groupId>
<artifactId>xwiki-commons-environment-standard</artifactId>
Expand Down Expand Up @@ -146,6 +152,18 @@
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>test-jar</id>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -34,7 +35,13 @@
import org.xwiki.test.junit5.mockito.InjectMockComponents;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.BasicCredentials;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
import static com.github.tomakehurst.wiremock.matching.RequestPatternBuilder.allRequests;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -46,25 +53,45 @@
*/
@AllComponents
@ComponentTest
class SystemHTTPProxyTest
class HTTPTest
{
@InjectMockComponents
private AetherExtensionRepositoryFactory repositoryFactory;

private WireMockServer wireMockServer;

private String proxyHost;

private String proxyPort;

@BeforeEach
void proxyToWireMock()
void beforeEach()
{
this.wireMockServer = new WireMockServer(8888);
this.wireMockServer = new WireMockServer(options().dynamicPort());
this.wireMockServer.start();

// Remember the standard system properties
this.proxyHost = System.getProperty("http.proxyHost");
this.proxyPort = System.getProperty("http.proxyPort");
}

@AfterEach
void noMoreWireMock()
void afterEach()
{
this.wireMockServer.stop();
this.wireMockServer = null;

// Cleanup any leftover in System properties
if (this.proxyHost != null) {
System.setProperty("http.proxyHost", this.proxyHost);
} else {
System.clearProperty("http.proxyHost");
}
if (this.proxyPort != null) {
System.setProperty("http.proxyPort", this.proxyPort);
} else {
System.clearProperty("http.proxyPort");
}
}

@Test
Expand All @@ -80,12 +107,12 @@ void proxy() throws ExtensionRepositoryException, URISyntaxException
// We don't really care if the target artifact exist
}

assertTrue(this.wireMockServer.findAll(allRequests()).isEmpty(), "The repository did not request the "
+ "proxy server");
assertTrue(this.wireMockServer.findAll(allRequests()).isEmpty(),
"The repository did not request the " + "proxy server");

// Set the proxy to be wiremock so that it answers.
System.setProperty("http.proxyHost", "localhost");
System.setProperty("http.proxyPort", "8888");
System.setProperty("http.proxyPort", String.valueOf(this.wireMockServer.port()));

try {
repository.resolveVersions("groupid:artifactid", 0, -1);
Expand All @@ -96,4 +123,28 @@ void proxy() throws ExtensionRepositoryException, URISyntaxException
assertFalse(this.wireMockServer.findAll(allRequests()).isEmpty(),
"The repository did not request the proxy server");
}

@Test
void credentials() throws ExtensionRepositoryException, URISyntaxException
{
ExtensionRepository repository =
this.repositoryFactory.createRepository(new DefaultExtensionRepositoryDescriptor("id", "maven",
new URI("http://localhost:" + this.wireMockServer.port()),
Map.of("auth.user", "user", "auth.password", "password")));

this.wireMockServer.stubFor(get(urlEqualTo("/groupid/artifactid/maven-metadata.xml"))
.willReturn(aResponse().withStatus(401).withHeader("WWW-Authenticate", "Basic")));

try {
repository.resolveVersions("groupid:artifactid", 0, -1);
} catch (ResolveException e) {
// We don't really care if the target artifact exist
}

assertFalse(this.wireMockServer.findAll(allRequests()).isEmpty(),
"The repository did not request the proxy server");

this.wireMockServer.verify(getRequestedFor(urlEqualTo("/groupid/artifactid/maven-metadata.xml"))
.withBasicAuth(new BasicCredentials("user", "password")));
}
}

0 comments on commit 1da04f3

Please sign in to comment.