Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -457,7 +457,8 @@ private static String getAppEngineProjectIdFromMetadataServer() throws IOExcepti

@InternalApi("Visible for testing")
static boolean headerContainsMetadataFlavor(HttpResponse response) {
return "Google".equals(response.getHeaders().getFirstHeaderStringValue("Metadata-Flavor"));
String metadataFlavorValue = response.getHeaders().getFirstHeaderStringValue("Metadata-Flavor");
return "Google".equals(metadataFlavorValue);
}

protected static String getServiceAccountProjectId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import com.google.common.io.Files;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -328,25 +329,29 @@ public void testGetServiceAccountProjectId_nonExistentFile() throws Exception {
}

@Test

This comment was marked as spam.

public void testResponseHeaderContainsMetaDataFlavor() throws Exception {
Map<String, String> headers = new HashMap<String, String>();
HttpResponse httpResponse = createHttpResponseWithHeader(headers);
assertThat(ServiceOptions.headerContainsMetadataFlavor(httpResponse)).isFalse();

public void testResponseHeaderContainsMetaDataFlavor() throws Exception {
Multimap<String, String> headers = ArrayListMultimap.create();
headers.put("Metadata-Flavor", "Google");
httpResponse = createHttpResponseWithHeader(headers);
HttpResponse httpResponse = createHttpResponseWithHeader(headers);
assertThat(ServiceOptions.headerContainsMetadataFlavor(httpResponse)).isTrue();

This comment was marked as spam.

}

@Test
public void testResponseHeaderDoesNotContainMetaDataFlavor() throws Exception {

This comment was marked as spam.

Multimap<String, String> headers = ArrayListMultimap.create();
HttpResponse httpResponse = createHttpResponseWithHeader(headers);
assertThat(ServiceOptions.headerContainsMetadataFlavor(httpResponse)).isFalse();
}

private HttpResponse createHttpResponseWithHeader(final Map<String, String> headers) throws Exception {
private HttpResponse createHttpResponseWithHeader(final Multimap<String, String> headers) throws Exception {
HttpTransport mockHttpTransport = new MockHttpTransport() {
@Override
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
return new MockLowLevelHttpRequest() {
@Override
public LowLevelHttpResponse execute() throws IOException {
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
for (Map.Entry<String, String> entry : headers.entrySet()) {
for (Map.Entry<String, String> entry : headers.entries()) {

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

response.addHeader(entry.getKey(), entry.getValue());
}
return response;
Expand Down