mirror of
https://github.com/tiennm99/tjahzi.git
synced 2026-10-15 04:18:55 +00:00
Re #87: Testing ClientConfigurationBuilder and fixing bugs.
This commit is contained in:
1 parent
a747d66525
commit
a576a83f99
2 files changed
+114
-4
No files matched your search
@@ -6,7 +6,9 @@ import java.net.URL;
|
||||
public class ClientConfigurationBuilder {
|
||||
|
||||
public static final int HTTPS_PORT = 443;
|
||||
public static final int HTTP_PORT = 80;
|
||||
public static final String HTTPS_STRING = "https";
|
||||
public static final String HTTP_STRING = "http";
|
||||
|
||||
public static final String DEFAULT_LOG_ENDPOINT = "/loki/api/v1/push";
|
||||
|
||||
@@ -17,7 +19,7 @@ public class ClientConfigurationBuilder {
|
||||
|
||||
public static final int DEFAULT_MAX_RETRIES = 0;
|
||||
|
||||
private String logEndpoint = DEFAULT_LOG_ENDPOINT;
|
||||
private String logEndpoint;
|
||||
|
||||
private String url;
|
||||
|
||||
@@ -117,20 +119,38 @@ public class ClientConfigurationBuilder {
|
||||
"Current configuration sets url to '" + url + "' and host to '" + host + "'");
|
||||
}
|
||||
|
||||
if (url == null && host == null) {
|
||||
throw new IllegalArgumentException("One of 'url' or 'host' must be configured.");
|
||||
}
|
||||
|
||||
if (host != null) {
|
||||
validateAndConfigureConnectionParametersNoUrl();
|
||||
} else if (url != null) {
|
||||
validateAndConfigureConnectionParametersWithUrl();
|
||||
}
|
||||
|
||||
validateAndConfigureConnectionParametersWithUrl();
|
||||
}
|
||||
|
||||
private void validateAndConfigureConnectionParametersWithUrl() {
|
||||
try {
|
||||
URL parsedUrl = new URL(url);
|
||||
|
||||
String protocolScheme = parsedUrl.getProtocol();
|
||||
if (!HTTPS_STRING.equalsIgnoreCase(protocolScheme) && !HTTP_STRING.equalsIgnoreCase(protocolScheme)) {
|
||||
throw new IllegalArgumentException("Unknown protocol scheme, must be one of 'https' or 'http' (case insensitive). Provided: '" + protocolScheme + "'");
|
||||
}
|
||||
|
||||
host = parsedUrl.getHost();
|
||||
port = parsedUrl.getPort();
|
||||
|
||||
if (parsedUrl.getPath() != null) {
|
||||
if (port == -1) {
|
||||
if (parsedUrl.getProtocol().equalsIgnoreCase(HTTPS_STRING)) {
|
||||
port = HTTPS_PORT;
|
||||
} else {
|
||||
port = HTTP_PORT;
|
||||
}
|
||||
}
|
||||
|
||||
if (parsedUrl.getPath() != null && !parsedUrl.getPath().isEmpty()) {
|
||||
if (logEndpoint != null) {
|
||||
throw new IllegalArgumentException("If Loki connection URL contains path part then you cannot at the " +
|
||||
"same time define log endpoint. Url: '" + url + "', log endpoint: '" + logEndpoint + "'");
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
package pl.tkowalcz.tjahzi.http;
|
||||
|
||||
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 java.util.stream.Stream;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class ClientConfigurationBuilderTest {
|
||||
|
||||
static Stream<Arguments> configOptions() {
|
||||
return Stream.of(
|
||||
Arguments.of(
|
||||
"Should take arguments from url",
|
||||
"https://example.net:8443/monitoring/loki/api/v1/push",
|
||||
"example.net",
|
||||
8443,
|
||||
"/monitoring/loki/api/v1/push",
|
||||
true
|
||||
),
|
||||
Arguments.of(
|
||||
"Should not enable ssl if prefix is not https",
|
||||
"http://example.net:8443/monitoring/loki/api/v1/push",
|
||||
"example.net",
|
||||
8443,
|
||||
"/monitoring/loki/api/v1/push",
|
||||
false
|
||||
),
|
||||
Arguments.of(
|
||||
"Should use default logEndpoint is none is provided",
|
||||
"http://example.net:8443",
|
||||
"example.net",
|
||||
8443,
|
||||
"/loki/api/v1/push",
|
||||
false
|
||||
),
|
||||
Arguments.of(
|
||||
"Should use default logEndpoint is none is provided",
|
||||
"http://example.net",
|
||||
"example.net",
|
||||
8443,
|
||||
"/loki/api/v1/push",
|
||||
false
|
||||
),
|
||||
Arguments.of(
|
||||
"Should reject unknown protocol",
|
||||
"loki://example.net"
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "{0}")
|
||||
@MethodSource("configOptions")
|
||||
void shouldGetConnectionParamsFromUrl(
|
||||
String testCase,
|
||||
String url,
|
||||
String expectedHost,
|
||||
int expectedPort,
|
||||
String expectedLogEndpoint,
|
||||
boolean expectedUseSSL
|
||||
) {
|
||||
// Given
|
||||
ClientConfigurationBuilder configurationBuilder = new ClientConfigurationBuilder();
|
||||
|
||||
// When
|
||||
ClientConfiguration configuration = configurationBuilder
|
||||
.withUrl(url)
|
||||
.build();
|
||||
|
||||
// Then
|
||||
assertThat(configuration.getHost()).isEqualTo(expectedHost);
|
||||
assertThat(configuration.getPort()).isEqualTo(expectedPort);
|
||||
assertThat(configuration.getLogEndpoint()).isEqualTo(expectedLogEndpoint);
|
||||
assertThat(configuration.isUseSSL()).isEqualTo(expectedUseSSL);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRejectUnknownProtocolScheme() {
|
||||
// Given
|
||||
ClientConfigurationBuilder configurationBuilder = new ClientConfigurationBuilder();
|
||||
|
||||
// When
|
||||
ClientConfiguration configuration = configurationBuilder
|
||||
.withUrl("loki://example.net:8443/monitoring/loki/api/v1/push")
|
||||
.build();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user