-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added
SimpleHofundHttpConnection
to simplified configuration of HTT…
…P connections and added ability to override `getRequestMethod()` in `AbstractHofundBasicHttpConnection` to allow using `POST` method.
- Loading branch information
pz2
committed
Jul 13, 2024
1 parent
70169e2
commit dc6115b
Showing
8 changed files
with
245 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
title: Added `SimpleHofundHttpConnection` to simplified configuration of HTTP connections and added ability to override | ||
`getRequestMethod()` in `AbstractHofundBasicHttpConnection` to allow using `POST` method. | ||
authors: | ||
- name: Peter Zmilczak | ||
nick: marwin1991 | ||
url: https://github.com/marwin1991 | ||
type: added #[added/changed/deprecated/removed/fixed/security/other] | ||
merge_requests: | ||
- 33 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
hofund-core/src/main/java/dev/logchange/hofund/connection/RequestMethod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package dev.logchange.hofund.connection; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public enum RequestMethod { | ||
|
||
GET("GET"), | ||
POST("POST"), | ||
PATCH("PATH"), | ||
HEAD("HEAD"), | ||
OPTIONS("OPTIONS"), | ||
PUT("PUT"), | ||
DELETE("DELETE"), | ||
TRACE("TRACE"); | ||
|
||
private final String name; | ||
} |
57 changes: 57 additions & 0 deletions
57
hofund-core/src/main/java/dev/logchange/hofund/connection/SimpleHofundHttpConnection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package dev.logchange.hofund.connection; | ||
|
||
public class SimpleHofundHttpConnection extends AbstractHofundBasicHttpConnection { | ||
|
||
private final String target; | ||
private final String url; | ||
private final CheckingStatus checkingStatus; | ||
private final RequestMethod requestMethod; | ||
|
||
public SimpleHofundHttpConnection(String target, String url) { | ||
this.target = target; | ||
this.url = url; | ||
this.checkingStatus = CheckingStatus.ACTIVE; | ||
this.requestMethod = RequestMethod.GET; | ||
} | ||
|
||
public SimpleHofundHttpConnection(String target, String url, CheckingStatus checkingStatus) { | ||
this.target = target; | ||
this.url = url; | ||
this.checkingStatus = checkingStatus; | ||
this.requestMethod = RequestMethod.GET; | ||
} | ||
|
||
public SimpleHofundHttpConnection(String target, String url, RequestMethod requestMethod) { | ||
this.target = target; | ||
this.url = url; | ||
this.checkingStatus = CheckingStatus.ACTIVE; | ||
this.requestMethod = requestMethod; | ||
} | ||
|
||
public SimpleHofundHttpConnection(String target, String url, CheckingStatus checkingStatus, RequestMethod requestMethod) { | ||
this.target = target; | ||
this.url = url; | ||
this.checkingStatus = checkingStatus; | ||
this.requestMethod = requestMethod; | ||
} | ||
|
||
@Override | ||
protected String getTarget() { | ||
return target; | ||
} | ||
|
||
@Override | ||
protected String getUrl() { | ||
return url; | ||
} | ||
|
||
@Override | ||
protected RequestMethod getRequestMethod() { | ||
return requestMethod; | ||
} | ||
|
||
@Override | ||
protected CheckingStatus getCheckingStatus() { | ||
return checkingStatus; | ||
} | ||
} |
117 changes: 117 additions & 0 deletions
117
.../src/test/java/dev/logchange/hofund/connection/AbstractHofundBasicHttpConnectionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package dev.logchange.hofund.connection; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import okhttp3.HttpUrl; | ||
import okhttp3.mockwebserver.MockResponse; | ||
import okhttp3.mockwebserver.MockWebServer; | ||
import okhttp3.mockwebserver.RecordedRequest; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class AbstractHofundBasicHttpConnectionTest { | ||
|
||
/** | ||
* AbstractHofundBasicHttpConnection is an abstract class so to test it | ||
* we need to create below fake class. | ||
*/ | ||
@RequiredArgsConstructor | ||
private static class TestableAbstractHofundBasicHttpConnection extends AbstractHofundBasicHttpConnection { | ||
|
||
private final String target; | ||
private final String url; | ||
private final CheckingStatus checkingStatus; | ||
private final RequestMethod requestMethod; | ||
|
||
@Override | ||
protected String getTarget() { | ||
return target; | ||
} | ||
|
||
@Override | ||
protected String getUrl() { | ||
return url; | ||
} | ||
|
||
@Override | ||
protected CheckingStatus getCheckingStatus() { | ||
return checkingStatus; | ||
} | ||
|
||
@Override | ||
protected RequestMethod getRequestMethod() { | ||
return requestMethod; | ||
} | ||
} | ||
|
||
@Test | ||
void testGetMethod() { | ||
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.GET); | ||
|
||
HofundConnection hofundConnection = connection.toHofundConnection(); | ||
|
||
// when: | ||
Status status = hofundConnection.getFun().get().getStatus(); | ||
|
||
// then: | ||
RecordedRequest request = server.takeRequest(); | ||
assertEquals(Status.UP, status); | ||
assertEquals(request.getMethod(), "GET"); | ||
} catch (IOException | InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Test | ||
void testPostMethod() { | ||
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); | ||
|
||
HofundConnection hofundConnection = connection.toHofundConnection(); | ||
|
||
// when: | ||
Status status = hofundConnection.getFun().get().getStatus(); | ||
|
||
// then: | ||
RecordedRequest request = server.takeRequest(); | ||
assertEquals(Status.UP, status); | ||
assertEquals(request.getMethod(), "POST"); | ||
} catch (IOException | InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Test | ||
void testCheckingStatusInactive() { | ||
// given: | ||
TestableAbstractHofundBasicHttpConnection connection = new TestableAbstractHofundBasicHttpConnection("AlaMaKota", "https://a.b.c.d/", CheckingStatus.INACTIVE, RequestMethod.GET); | ||
|
||
HofundConnection hofundConnection = connection.toHofundConnection(); | ||
|
||
// when: | ||
Status status = hofundConnection.getFun().get().getStatus(); | ||
|
||
// then: | ||
assertEquals(Status.INACTIVE, status); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters