-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance ShadowingInterceptor by adding exchange construction method
Refactor cloneRequestAndSend to accept an Exchange parameter and use a new buildExchange method for creating requests. Update tests to validate the new functionality in ShadowingInterceptor.
- Loading branch information
1 parent
1bd5a25
commit 6b37d6a
Showing
2 changed files
with
52 additions
and
34 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
55 changes: 29 additions & 26 deletions
55
...c/test/java/com/predic8/membrane/core/interceptor/shadowing/ShadowingInterceptorTest.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 |
---|---|---|
@@ -1,37 +1,40 @@ | ||
package com.predic8.membrane.core.interceptor.shadowing; | ||
|
||
import com.predic8.membrane.core.exchange.Exchange; | ||
import com.predic8.membrane.core.http.Body; | ||
import com.predic8.membrane.core.http.MimeType; | ||
import com.predic8.membrane.core.http.Request; | ||
import com.predic8.membrane.core.rules.AbstractServiceProxy.Target; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
import java.io.IOException; | ||
import java.net.URISyntaxException; | ||
import java.net.http.HttpHeaders; | ||
|
||
import static com.predic8.membrane.core.http.Header.CONTENT_TYPE; | ||
import static com.predic8.membrane.core.http.MimeType.APPLICATION_JSON; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.when; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
class ShadowingInterceptorTest { | ||
|
||
@Mock | ||
private Target mockTarget; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
} | ||
|
||
@Test | ||
void testGetDestFromTarget_WithUrl() { | ||
when(mockTarget.getUrl()).thenReturn("http://example.com"); | ||
String result = ShadowingInterceptor.getDestFromTarget(mockTarget, "/path"); | ||
assertEquals("http://example.com", result); | ||
void buildExchangeTest() throws URISyntaxException, IOException { | ||
Exchange exc = ShadowingInterceptor.buildExchange( | ||
new Body("foo".getBytes()), | ||
new Request.Builder() | ||
.post("https://www.google.com") | ||
.header(CONTENT_TYPE, APPLICATION_JSON) | ||
.buildExchange(), | ||
new Target() {{ | ||
setUrl("https://www.predic8.com:9000/foo"); | ||
}} | ||
); | ||
|
||
assertNotNull(exc); | ||
assertEquals("POST", exc.getRequest().getMethod()); | ||
assertEquals("/foo", exc.getRequest().getUri()); | ||
assertEquals("https://www.predic8.com:9000/foo", exc.getDestinations().get(0)); | ||
assertEquals(APPLICATION_JSON, exc.getRequest().getHeader().getContentType()); | ||
} | ||
|
||
@Test | ||
void testGetDestFromTarget_WithoutUrl() { | ||
when(mockTarget.getHost()).thenReturn("localhost"); | ||
when(mockTarget.getPort()).thenReturn(8080); | ||
String result = ShadowingInterceptor.getDestFromTarget(mockTarget, "/path"); | ||
assertEquals("http://localhost:8080/path", result); | ||
} | ||
|
||
} | ||
} |