Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: throw exception when user attempt to override api-version header. #2745

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,13 @@ private static Map<String, String> getHeadersFromSettings(StubSettings settings)
Map<String, String> internalHeaders = settings.getInternalHeaderProvider().getHeaders();
Map<String, String> conflictResolution = new HashMap<>();

// api version header user override is not allowed, even when not set by internal headers
if (userHeaders.keySet().contains(ApiClientHeaderProvider.API_VERSION_HEADER_KEY)) {
throw new IllegalArgumentException(
"Header provider can't override the header: "
+ ApiClientHeaderProvider.API_VERSION_HEADER_KEY);
}

Set<String> conflicts = Sets.intersection(userHeaders.keySet(), internalHeaders.keySet());
for (String key : conflicts) {
if ("user-agent".equals(key)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mockito;
Expand Down Expand Up @@ -681,6 +683,72 @@ public void testUserAgentConcat() throws Exception {
.containsEntry("user-agent", "user-supplied-agent internal-agent");
}

@Rule public ExpectedException apiVersionExceptionRule = ExpectedException.none();

private void createClientContextAndSetApiVersionHeaders(
String internalVersion, String userVersion) throws IOException {
TransportChannelProvider transportChannelProvider =
new FakeTransportProvider(
FakeTransportChannel.create(new FakeChannel()),
null,
true,
null,
null,
DEFAULT_ENDPOINT);

ClientSettings.Builder builder =
new FakeClientSettings.Builder()
.setExecutorProvider(
FixedExecutorProvider.create(Mockito.mock(ScheduledExecutorService.class)))
.setTransportChannelProvider(transportChannelProvider)
.setCredentialsProvider(
FixedCredentialsProvider.create(Mockito.mock(GoogleCredentials.class)));

builder.setHeaderProvider(
FixedHeaderProvider.create(
ApiClientHeaderProvider.API_VERSION_HEADER_KEY, "user-supplied-version"));
if (internalVersion != null) {
builder.setInternalHeaderProvider(
FixedHeaderProvider.create(
ApiClientHeaderProvider.API_VERSION_HEADER_KEY, "internal-version"));
}

ClientContext.create(builder.build());
}

@Test
public void testApiVersionHeaderConflictShouldThrowException() throws Exception {

apiVersionExceptionRule.expect(IllegalArgumentException.class);
apiVersionExceptionRule.expectMessage(
"Header provider can't override the header: "
+ ApiClientHeaderProvider.API_VERSION_HEADER_KEY);

createClientContextAndSetApiVersionHeaders("internal-version", "user-supplied-version");
}

@Test
public void testApiVersionHeaderEmptyConflictShouldThrowException() throws Exception {

apiVersionExceptionRule.expect(IllegalArgumentException.class);
apiVersionExceptionRule.expectMessage(
"Header provider can't override the header: "
+ ApiClientHeaderProvider.API_VERSION_HEADER_KEY);

createClientContextAndSetApiVersionHeaders("", "user-supplied-version");
}

@Test
public void testUserApiVersionHeaderOnlyShouldThrowException() throws Exception {

apiVersionExceptionRule.expect(IllegalArgumentException.class);
apiVersionExceptionRule.expectMessage(
"Header provider can't override the header: "
+ ApiClientHeaderProvider.API_VERSION_HEADER_KEY);

createClientContextAndSetApiVersionHeaders(null, "user-supplied-version");
}

private static String endpoint = "https://foo.googleapis.com";
private static String mtlsEndpoint = "https://foo.mtls.googleapis.com";

Expand Down
Loading