|
31 | 31 | import ai.philterd.philter.model.exceptions.ServiceUnavailableException; |
32 | 32 | import ai.philterd.philter.model.exceptions.UnauthorizedException; |
33 | 33 | import okhttp3.HttpUrl; |
| 34 | +import okhttp3.OkHttpClient; |
34 | 35 | import okhttp3.mockwebserver.MockResponse; |
35 | 36 | import okhttp3.mockwebserver.MockWebServer; |
36 | 37 | import okhttp3.mockwebserver.RecordedRequest; |
@@ -177,6 +178,28 @@ public void noAuthorizationHeaderWhenApiKeyAbsent() throws Exception { |
177 | 178 | Assert.assertNull(request.getHeader("Authorization")); |
178 | 179 | } |
179 | 180 |
|
| 181 | + @Test |
| 182 | + public void suppliedOkHttpClientBuilderIsUsedAndAuthIsLayeredOnTop() throws Exception { |
| 183 | + |
| 184 | + server.enqueue(new MockResponse().setResponseCode(200).setBody("[]")); |
| 185 | + |
| 186 | + // An interceptor on the supplied builder proves the builder is actually used; the API key |
| 187 | + // proves the Authorization interceptor is still layered on top of the supplied builder. |
| 188 | + final OkHttpClient.Builder supplied = new OkHttpClient.Builder() |
| 189 | + .addInterceptor(chain -> chain.proceed( |
| 190 | + chain.request().newBuilder().header("X-Custom", "custom-value").build())); |
| 191 | + |
| 192 | + final PhilterClient client = clientBuilder() |
| 193 | + .withOkHttpClientBuilder(supplied) |
| 194 | + .withApiKey("secret-key") |
| 195 | + .build(); |
| 196 | + client.getPolicies(); |
| 197 | + |
| 198 | + final RecordedRequest request = server.takeRequest(); |
| 199 | + Assert.assertEquals("custom-value", request.getHeader("X-Custom")); |
| 200 | + Assert.assertEquals("secret-key", request.getHeader("Authorization")); |
| 201 | + } |
| 202 | + |
180 | 203 | // Status. |
181 | 204 |
|
182 | 205 | @Test |
@@ -398,6 +421,7 @@ public void wireContract() throws Exception { |
398 | 421 |
|
399 | 422 | // Contexts. |
400 | 423 | verify(c, "GET", "/api/contexts", Map.of(), "[]", c::getContexts); |
| 424 | + verify(c, "GET", "/api/contexts/c1", Map.of(), "{}", () -> c.getContext("c1")); |
401 | 425 | verify(c, "PUT", "/api/contexts/c1", Map.of("entity_type_disambiguation", "true", "ledger", "false"), "{}", |
402 | 426 | () -> c.updateContext("c1", true, false)); |
403 | 427 | verify(c, "DELETE", "/api/contexts/c1", Map.of(), "{}", () -> c.deleteContext("c1")); |
@@ -426,6 +450,7 @@ public void wireContract() throws Exception { |
426 | 450 | verify(c, "GET", "/api/ledger/d1/valid", Map.of(), "true", () -> c.isLedgerValid("d1")); |
427 | 451 |
|
428 | 452 | // Custom lists. |
| 453 | + verify(c, "GET", "/api/lists", Map.of(), "[]", c::getLists); |
429 | 454 | verify(c, "DELETE", "/api/lists/l1", Map.of(), "", () -> c.deleteList("l1")); |
430 | 455 |
|
431 | 456 | // Redact lists. |
|
0 commit comments