Skip to content

Commit fc19261

Browse files
BenjaminKazemicopybara-github
authored andcommitted
test: Validate the proxy configurations.
PiperOrigin-RevId: 720621739
1 parent c1e31b8 commit fc19261

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@
8888
<artifactId>mockito-core</artifactId>
8989
<version>${mockito.version}</version>
9090
</dependency>
91+
<dependency>
92+
<groupId>com.github.tomakehurst</groupId>
93+
<artifactId>wiremock-jre8</artifactId>
94+
<version>2.35.0</version>
95+
<scope>test</scope>
96+
</dependency>
9197
</dependencies>
9298

9399
<build>

src/test/java/com/google/genai/HttpApiClientTest.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
package com.google.genai;
22

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;
38
import static org.junit.jupiter.api.Assertions.assertEquals;
49
import static org.junit.jupiter.api.Assertions.assertFalse;
510
import static org.junit.jupiter.api.Assertions.assertNotNull;
611
import static org.junit.jupiter.api.Assertions.assertTrue;
712

13+
import com.github.tomakehurst.wiremock.WireMockServer;
14+
import com.github.tomakehurst.wiremock.client.WireMock;
815
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;
921
import com.google.genai.types.HttpOptions;
22+
import com.google.genai.types.Part;
1023
import java.lang.reflect.Field;
1124
import java.util.Optional;
1225
import org.apache.http.client.config.RequestConfig;
@@ -113,4 +126,42 @@ public void testHttpClientWithCustomCredentials() throws Exception {
113126
Optional.of(httpOptions));
114127
assertEquals(credentials, client.credentials.get());
115128
}
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+
}
116167
}

0 commit comments

Comments
 (0)