Skip to content

Commit

Permalink
Added request headers and ability to define it during testing connect…
Browse files Browse the repository at this point in the history
…ion using `AbstractHofundBasicHttpConnection`
  • Loading branch information
pz2 committed Jul 16, 2024
1 parent 8269ee5 commit 6ac2bc3
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
title: Added request headers and ability to define it during testing connection using `AbstractHofundBasicHttpConnection`
authors:
- name: Peter Zmilczak
nick: marwin1991
url: https://github.com/marwin1991
type: added #[added/changed/deprecated/removed/fixed/security/other]
merge_requests:
- 45
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

@Slf4j
Expand Down Expand Up @@ -42,6 +44,10 @@ protected RequestMethod getRequestMethod() {
return RequestMethod.GET;
}

protected List<RequestHeader> getRequestHeaders() {
return Collections.emptyList();
}

/**
* If your connection can be disabled f.e. by parameter or should be
* active between 9am to 5pm you can override this method and implement it as you wish.
Expand Down Expand Up @@ -91,6 +97,8 @@ private StatusFunction testConnection() {
urlConn.setReadTimeout(getReadTimeout());
urlConn.setRequestMethod(getRequestMethod().getName());

setRequestHeaders(urlConn);

urlConn.connect();
int responseCode = urlConn.getResponseCode();
log.debug("Connection to url: {} status code: {}", getUrl(), responseCode);
Expand All @@ -108,4 +116,16 @@ private StatusFunction testConnection() {
}
};
}

private void setRequestHeaders(HttpURLConnection urlConn) {
List<RequestHeader> requestHeaders = getRequestHeaders();

if (requestHeaders == null || requestHeaders.isEmpty()) {
return;
}

for (RequestHeader header : requestHeaders) {
urlConn.setRequestProperty(header.getName(), header.getValue());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dev.logchange.hofund.connection;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public class RequestHeader {
private final String name;
private final String value;

public static RequestHeader of(String name, String value) {
return new RequestHeader(name, value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

Expand All @@ -24,6 +27,7 @@ private static class TestableAbstractHofundBasicHttpConnection extends AbstractH
private final String url;
private final CheckingStatus checkingStatus;
private final RequestMethod requestMethod;
private final List<RequestHeader> requestHeaders;

@Override
protected String getTarget() {
Expand All @@ -44,6 +48,11 @@ protected CheckingStatus getCheckingStatus() {
protected RequestMethod getRequestMethod() {
return requestMethod;
}

@Override
protected List<RequestHeader> getRequestHeaders() {
return requestHeaders;
}
}

@Test
Expand All @@ -57,7 +66,7 @@ void testGetMethod() {

HttpUrl url = server.url("/api/some/");

TestableAbstractHofundBasicHttpConnection connection = new TestableAbstractHofundBasicHttpConnection("AlaMaKota", url.toString(), CheckingStatus.ACTIVE, RequestMethod.GET);
TestableAbstractHofundBasicHttpConnection connection = new TestableAbstractHofundBasicHttpConnection("AlaMaKota", url.toString(), CheckingStatus.ACTIVE, RequestMethod.GET, Collections.emptyList());

HofundConnection hofundConnection = connection.toHofundConnection();

Expand All @@ -84,7 +93,7 @@ void testPostMethod() {

HttpUrl url = server.url("/api/some/");

TestableAbstractHofundBasicHttpConnection connection = new TestableAbstractHofundBasicHttpConnection("AlaMaKota", url.toString(), CheckingStatus.ACTIVE, RequestMethod.POST);
TestableAbstractHofundBasicHttpConnection connection = new TestableAbstractHofundBasicHttpConnection("AlaMaKota", url.toString(), CheckingStatus.ACTIVE, RequestMethod.POST, Collections.emptyList());

HofundConnection hofundConnection = connection.toHofundConnection();

Expand All @@ -103,7 +112,7 @@ void testPostMethod() {
@Test
void testCheckingStatusInactive() {
// given:
TestableAbstractHofundBasicHttpConnection connection = new TestableAbstractHofundBasicHttpConnection("AlaMaKota", "https://a.b.c.d/", CheckingStatus.INACTIVE, RequestMethod.GET);
TestableAbstractHofundBasicHttpConnection connection = new TestableAbstractHofundBasicHttpConnection("AlaMaKota", "https://a.b.c.d/", CheckingStatus.INACTIVE, RequestMethod.GET, Collections.emptyList());

HofundConnection hofundConnection = connection.toHofundConnection();

Expand All @@ -114,4 +123,32 @@ void testCheckingStatusInactive() {
assertEquals(Status.INACTIVE, status);
}

@Test
void testPostMethodWithRequestHeader() {
try (MockWebServer server = new MockWebServer()) {

// given:
server.enqueue(new MockResponse()
.setBody("hello, world!")
);

HttpUrl url = server.url("/api/some/");

TestableAbstractHofundBasicHttpConnection connection = new TestableAbstractHofundBasicHttpConnection("AlaMaKota", url.toString(), CheckingStatus.ACTIVE, RequestMethod.POST, Arrays.asList(RequestHeader.of("Authorization", "Bearer 12345678")));

HofundConnection hofundConnection = connection.toHofundConnection();

// when:
Status status = hofundConnection.getFun().get().getStatus();

// then:
RecordedRequest request = server.takeRequest();
assertEquals(Status.UP, status);
assertEquals(request.getMethod(), "POST");
assertEquals(request.getHeader("Authorization"), "Bearer 12345678");
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
}

}

0 comments on commit 6ac2bc3

Please sign in to comment.