mirror of
https://github.com/tiennm99/tjahzi.git
synced 2026-10-14 14:17:58 +00:00
Re #45: Cleanup and tests.
This commit is contained in:
1 parent
d153e3aad0
commit
94aeb2e277
4 files changed
+160
-36
No files matched your search
@@ -12,9 +12,6 @@ import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
|
||||
import pl.tkowalcz.tjahzi.stats.MonitoringModule;
|
||||
|
||||
import javax.net.ssl.SSLException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.Optional;
|
||||
|
||||
public class BootstrapUtil {
|
||||
|
||||
@@ -65,17 +62,4 @@ public class BootstrapUtil {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static Optional<String> createAuthString(String username, String password) {
|
||||
if (username == null && password == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
String userPassword = username + ":" + password;
|
||||
String authString = "Basic " + Base64
|
||||
.getEncoder()
|
||||
.encodeToString(userPassword.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
return Optional.of(authString);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package pl.tkowalcz.tjahzi.http;
|
||||
|
||||
import io.netty.handler.codec.http.HttpHeaderNames;
|
||||
import io.netty.handler.codec.http.HttpHeaders;
|
||||
import io.netty.handler.codec.http.ReadOnlyHttpHeaders;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Base64;
|
||||
import java.util.Optional;
|
||||
|
||||
public class HttpHeadersFactory {
|
||||
|
||||
public static HttpHeaders createHeaders(ClientConfiguration clientConfiguration, String[] additionalHeaders) {
|
||||
ArrayList<String> newHeaders = new ArrayList<>(Arrays.asList(additionalHeaders));
|
||||
|
||||
Optional<String> maybeAuthString = createAuthString(
|
||||
clientConfiguration.getUsername(),
|
||||
clientConfiguration.getPassword()
|
||||
);
|
||||
|
||||
maybeAuthString.ifPresent(
|
||||
authString -> {
|
||||
newHeaders.add(HttpHeaderNames.AUTHORIZATION.toString());
|
||||
newHeaders.add(authString);
|
||||
}
|
||||
);
|
||||
|
||||
return new ReadOnlyHttpHeaders(
|
||||
true,
|
||||
newHeaders.toArray(new String[0])
|
||||
);
|
||||
}
|
||||
|
||||
public static Optional<String> createAuthString(String username, String password) {
|
||||
if (username == null && password == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
String userPassword = username + ":" + password;
|
||||
String authString = "Basic " + Base64
|
||||
.getEncoder()
|
||||
.encodeToString(userPassword.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
return Optional.of(authString);
|
||||
}
|
||||
}
|
||||
@@ -9,18 +9,17 @@ import pl.tkowalcz.tjahzi.stats.MonitoringModule;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class NettyHttpClient implements Closeable {
|
||||
|
||||
public static final String PROTOBUF_MIME_TYPE = "application/x-protobuf";
|
||||
|
||||
private final ClientConfiguration clientConfiguration;
|
||||
|
||||
private final HttpHeaders headers;
|
||||
private final HttpConnection lokiConnection;
|
||||
|
||||
private final Snappy snappy = new Snappy();
|
||||
private final HttpConnection lokiConnection;
|
||||
|
||||
public NettyHttpClient(
|
||||
ClientConfiguration clientConfiguration,
|
||||
@@ -29,23 +28,8 @@ public class NettyHttpClient implements Closeable {
|
||||
) {
|
||||
this.clientConfiguration = clientConfiguration;
|
||||
|
||||
String[] finalAdditionalHeaders = BootstrapUtil.createAuthString(
|
||||
clientConfiguration.getUsername(),
|
||||
clientConfiguration.getPassword()
|
||||
).map(authString -> {
|
||||
ArrayList<String> newHeaders = new ArrayList<>(Arrays.asList(additionalHeaders));
|
||||
newHeaders.add(HttpHeaderNames.AUTHORIZATION.toString());
|
||||
newHeaders.add(authString);
|
||||
|
||||
return newHeaders.toArray(new String[0]);
|
||||
}).orElse(additionalHeaders);
|
||||
|
||||
this.headers = new ReadOnlyHttpHeaders(
|
||||
true,
|
||||
finalAdditionalHeaders
|
||||
);
|
||||
|
||||
lokiConnection = new HttpConnection(clientConfiguration, monitoringModule);
|
||||
this.headers = HttpHeadersFactory.createHeaders(clientConfiguration, additionalHeaders);
|
||||
this.lokiConnection = new HttpConnection(clientConfiguration, monitoringModule);
|
||||
}
|
||||
|
||||
public void log(ByteBuf dataBuffer) {
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
package pl.tkowalcz.tjahzi.http;
|
||||
|
||||
import io.netty.handler.codec.http.HttpHeaders;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.entry;
|
||||
|
||||
class HttpHeadersFactoryTest {
|
||||
|
||||
@Test
|
||||
void shouldAddAuthHeaderIfCredentialsAreProvided_NoAdditionalHeaders() {
|
||||
// Given
|
||||
ClientConfiguration configuration = ClientConfiguration.builder()
|
||||
.withUsername("foob")
|
||||
.withPassword("ar")
|
||||
.build();
|
||||
|
||||
String[] additionalHeaders = new String[0];
|
||||
|
||||
// When
|
||||
HttpHeaders httpHeaders = HttpHeadersFactory.createHeaders(
|
||||
configuration,
|
||||
additionalHeaders
|
||||
);
|
||||
|
||||
// Then
|
||||
assertThat(httpHeaders.entries())
|
||||
.hasSize(1)
|
||||
.containsExactly(
|
||||
entry("authorization", "Basic Zm9vYjphcg==")
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldAddAuthHeaderIfCredentialsAreProvided_WithAdditionalHeaders() {
|
||||
// Given
|
||||
ClientConfiguration configuration = ClientConfiguration.builder()
|
||||
.withUsername("foob")
|
||||
.withPassword("ar")
|
||||
.build();
|
||||
|
||||
String[] additionalHeaders = new String[]{
|
||||
"droid", "K2SO",
|
||||
"not-droid", "Jyn Erso",
|
||||
};
|
||||
|
||||
// When
|
||||
HttpHeaders httpHeaders = HttpHeadersFactory.createHeaders(
|
||||
configuration,
|
||||
additionalHeaders
|
||||
);
|
||||
|
||||
// Then
|
||||
assertThat(httpHeaders.entries())
|
||||
.hasSize(3)
|
||||
.containsExactlyInAnyOrder(
|
||||
entry("authorization", "Basic Zm9vYjphcg=="),
|
||||
entry("droid", "K2SO"),
|
||||
entry("not-droid", "Jyn Erso")
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotAddAuthHeader_NoAdditionalHeaders() {
|
||||
// Given
|
||||
ClientConfiguration configuration = ClientConfiguration.builder()
|
||||
.build();
|
||||
|
||||
String[] additionalHeaders = new String[0];
|
||||
|
||||
// When
|
||||
HttpHeaders httpHeaders = HttpHeadersFactory.createHeaders(
|
||||
configuration,
|
||||
additionalHeaders
|
||||
);
|
||||
|
||||
// Then
|
||||
assertThat(httpHeaders.entries())
|
||||
.isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNOtAddAuthHeader_WithAdditionalHeaders() {
|
||||
// Given
|
||||
ClientConfiguration configuration = ClientConfiguration.builder()
|
||||
.build();
|
||||
|
||||
String[] additionalHeaders = new String[]{
|
||||
"droid", "K2SO",
|
||||
"not-droid", "Jyn Erso",
|
||||
};
|
||||
|
||||
// When
|
||||
HttpHeaders httpHeaders = HttpHeadersFactory.createHeaders(
|
||||
configuration,
|
||||
additionalHeaders
|
||||
);
|
||||
|
||||
// Then
|
||||
assertThat(httpHeaders.entries())
|
||||
.hasSize(2)
|
||||
.containsExactlyInAnyOrder(
|
||||
entry("droid", "K2SO"),
|
||||
entry("not-droid", "Jyn Erso")
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user