|
| 1 | +package io.github.easy4j.hermes.security; |
| 2 | + |
| 3 | +import io.github.easy4j.hermes.HermesHttpClientConfig; |
| 4 | +import io.github.easy4j.hermes.api.HermesHttpClient; |
| 5 | +import okhttp3.mockwebserver.MockResponse; |
| 6 | +import okhttp3.mockwebserver.MockWebServer; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 10 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 11 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 12 | + |
| 13 | +class EndpointPolicyContractTest { |
| 14 | + |
| 15 | + @Test |
| 16 | + void testEp001S1() throws Exception { |
| 17 | + try (MockWebServer server = new MockWebServer()) { |
| 18 | + server.enqueue(new MockResponse() |
| 19 | + .setResponseCode(200) |
| 20 | + .setHeader("Content-Type", "application/json") |
| 21 | + .setBody("{}")); |
| 22 | + server.start(); |
| 23 | + |
| 24 | + int allowedPort = server.getPort(); |
| 25 | + EndpointPolicy policy = EndpointPolicy.trustedLocal("127.0.0.1", allowedPort); |
| 26 | + |
| 27 | + HermesHttpClientConfig allowed = new HermesHttpClientConfig() |
| 28 | + .setEndpointPolicy(policy) |
| 29 | + .setBaseUrl("http://127.0.0.1:" + allowedPort); |
| 30 | + |
| 31 | + try (HermesHttpClient client = new HermesHttpClient(allowed)) { |
| 32 | + assertNotNull(client.health()); |
| 33 | + assertNotNull(server.takeRequest()); |
| 34 | + } |
| 35 | + |
| 36 | + HermesHttpClientConfig differentPort = new HermesHttpClientConfig() |
| 37 | + .setEndpointPolicy(policy); |
| 38 | + assertThrows(IllegalArgumentException.class, () -> differentPort.setBaseUrl( |
| 39 | + "http://127.0.0.1:" + (allowedPort + 1))); |
| 40 | + } |
| 41 | + } |
| 42 | +} |
0 commit comments