|
1 | 1 | package com.google.genai; |
2 | 2 |
|
| 3 | +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; |
| 4 | +import static com.github.tomakehurst.wiremock.client.WireMock.post; |
| 5 | +import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; |
| 6 | +import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching; |
| 7 | +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; |
3 | 8 | import static org.junit.jupiter.api.Assertions.assertEquals; |
4 | 9 | import static org.junit.jupiter.api.Assertions.assertFalse; |
5 | 10 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
6 | 11 | import static org.junit.jupiter.api.Assertions.assertTrue; |
7 | 12 |
|
| 13 | +import com.github.tomakehurst.wiremock.WireMockServer; |
| 14 | +import com.github.tomakehurst.wiremock.client.WireMock; |
8 | 15 | import com.google.auth.oauth2.GoogleCredentials; |
| 16 | +import com.google.common.collect.ImmutableList; |
| 17 | +import com.google.genai.types.Candidate; |
| 18 | +import com.google.genai.types.Content; |
| 19 | +import com.google.genai.types.GenerateContentConfig; |
| 20 | +import com.google.genai.types.GenerateContentResponse; |
9 | 21 | import com.google.genai.types.HttpOptions; |
| 22 | +import com.google.genai.types.Part; |
10 | 23 | import java.lang.reflect.Field; |
11 | 24 | import java.util.Optional; |
12 | 25 | import org.apache.http.client.config.RequestConfig; |
@@ -113,4 +126,42 @@ public void testHttpClientWithCustomCredentials() throws Exception { |
113 | 126 | Optional.of(httpOptions)); |
114 | 127 | assertEquals(credentials, client.credentials.get()); |
115 | 128 | } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void testProxySetup() throws Exception { |
| 132 | + WireMockServer wireMockServer = null; |
| 133 | + try { |
| 134 | + wireMockServer = new WireMockServer(options().dynamicPort()); |
| 135 | + wireMockServer.start(); |
| 136 | + WireMock.configureFor("localhost", wireMockServer.port()); |
| 137 | + String expectedText = "This is Proxy speaking, Hello, World!"; |
| 138 | + Part part = Part.builder().setText(expectedText).build(); |
| 139 | + Content content = Content.builder().setParts(ImmutableList.of(part)).build(); |
| 140 | + Candidate candidate = Candidate.builder().setContent(content).build(); |
| 141 | + GenerateContentResponse fakeResponse = |
| 142 | + GenerateContentResponse.builder().setCandidates(ImmutableList.of(candidate)).build(); |
| 143 | + stubFor( |
| 144 | + post(urlMatching(".*")) |
| 145 | + .willReturn( |
| 146 | + aResponse() |
| 147 | + .withStatus(200) |
| 148 | + .withHeader("Content-Type", "application/json") |
| 149 | + .withBody(fakeResponse.toJson()))); |
| 150 | + |
| 151 | + HttpOptions httpOptions = |
| 152 | + HttpOptions.builder() |
| 153 | + .setBaseUrl("http://localhost:" + wireMockServer.port()) |
| 154 | + .setApiVersion("v1beta") |
| 155 | + .build(); |
| 156 | + Client client = Client.builder().setHttpOptions(httpOptions).build(); |
| 157 | + |
| 158 | + GenerateContentConfig config = GenerateContentConfig.builder().build(); |
| 159 | + GenerateContentResponse response = |
| 160 | + client.models.generateContent("gemini-2.0-flash-exp", "What is your name?", config); |
| 161 | + |
| 162 | + assertEquals(response.text().get(), expectedText); |
| 163 | + } finally { |
| 164 | + wireMockServer.stop(); |
| 165 | + } |
| 166 | + } |
116 | 167 | } |
0 commit comments