Skip to content

Commit

Permalink
Allow setting external URL explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanyu committed Sep 30, 2023
1 parent dcb5e1d commit 23034c2
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ dependencies {

testImplementation libs.testcontainers.junit.jupiter

testImplementation libs.assertj

testImplementation libs.gcs
testRuntimeOnly libs.slf4j.log4j12
}
Expand Down
2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ dependencyResolutionManagement {
library('junit-jupiter-engine', 'org.junit.jupiter', 'junit-jupiter-engine').versionRef('junit')
library('junit-jupiter-params', 'org.junit.jupiter', 'junit-jupiter-params').versionRef('junit')

library('assertj', 'org.assertj:assertj-core:3.24.2')

library('slf4j-log4j12', 'org.slf4j:slf4j-log4j12:1.7.36')

library('gcs', 'com.google.cloud:google-cloud-storage:2.26.1')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,21 @@
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Objects;

import org.testcontainers.containers.GenericContainer;
import org.testcontainers.utility.DockerImageName;

public class FakeGcsServerContainer extends GenericContainer<FakeGcsServerContainer> {
private static final int PORT = 4443;
public static final int PORT = 4443;

public static final String DEFAULT_VERSION = "1.47.4";

public static final DockerImageName DEFAULT_IMAGE_NAME =
DockerImageName.parse("fsouza/fake-gcs-server:" + DEFAULT_VERSION);

private String externalUrl = null;

public FakeGcsServerContainer() {
this(DEFAULT_IMAGE_NAME);
}
Expand All @@ -47,20 +50,37 @@ public FakeGcsServerContainer(final DockerImageName dockerImageName) {
addExposedPorts(PORT);
}

public FakeGcsServerContainer withExternalURL(final String externalUrl) {
this.externalUrl = externalUrl;
return this;
}

@Override
public void start() {
super.start();
updateExternalUrl();
}

/**
* The URL based on the host name and mapped port.
*/
public String url() {
return "http://" + getHost() + ":" + getMappedPort(PORT);
}

/**
* The URL based on the defined external URL.
*
* <p>Defaults to {@link FakeGcsServerContainer#url()}.
*/
public String externalUrl() {
return Objects.requireNonNullElseGet(externalUrl, this::url);
}

private void updateExternalUrl() {
final String modifyExternalUrlRequestUri = url() + "/_internal/config";
final String updateExternalUrlJson = "{"
+ "\"externalUrl\": \"" + url() + "\""
+ "\"externalUrl\": \"" + externalUrl() + "\""
+ "}";

final HttpRequest req = HttpRequest.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import org.junit.jupiter.api.Named;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.testcontainers.utility.DockerImageName;

import static org.assertj.core.api.Assertions.assertThat;

public class FakeGcsServerContainerTest {
static final BucketInfo TEST_BUCKET = BucketInfo.of("test-bucket");

Expand Down Expand Up @@ -60,4 +63,21 @@ private static Stream<Arguments> provideTestParams() {
new FakeGcsServerContainer(DockerImageName.parse("fsouza/fake-gcs-server:1.47.3"))))
);
}

@Test
void testExternalUrlNotSet() {
try (final var fakeGcsServerContainer = new FakeGcsServerContainer()) {
fakeGcsServerContainer.start();
assertThat(fakeGcsServerContainer.externalUrl()).isEqualTo(fakeGcsServerContainer.url());
}
}

@Test
void testExternalUrlSet() {
try (final var fakeGcsServerContainer = new FakeGcsServerContainer().withExternalURL("http://aaa:1234")) {
fakeGcsServerContainer.start();
assertThat(fakeGcsServerContainer.externalUrl()).isEqualTo("http://aaa:1234");
assertThat(fakeGcsServerContainer.externalUrl()).isNotEqualTo(fakeGcsServerContainer.url());
}
}
}

0 comments on commit 23034c2

Please sign in to comment.