Skip to content

Commit

Permalink
MockMetadataServerTransport uses s2aContentMap.
Browse files Browse the repository at this point in the history
  • Loading branch information
rmehta19 committed Oct 25, 2024
1 parent 20825f7 commit 594df7b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,7 @@ public class MockMetadataServerTransport extends MockHttpTransport {

private byte[] signature;

private String plaintextS2AAddressJsonKey;

private String plaintextS2AAddress;

private String mtlsS2AAddressJsonKey;

private String mtlsS2AAddress;
private Map<String, String> s2aContentMap = new HashMap<>();

private boolean emptyContent;
private MockLowLevelHttpRequest request;
Expand Down Expand Up @@ -112,20 +106,8 @@ public void setIdToken(String idToken) {
this.idToken = idToken;
}

public void setPlaintextS2AAddressJsonKey(String key) {
this.plaintextS2AAddressJsonKey = key;
}

public void setPlaintextS2AAddress(String address) {
this.plaintextS2AAddress = address;
}

public void setMtlsS2AAddressJsonKey(String key) {
this.mtlsS2AAddressJsonKey = key;
}

public void setMtlsS2AAddress(String address) {
this.mtlsS2AAddress = address;
public void setS2AContentMap(String key, String value) {
s2aContentMap.put(key, value);
}

public void setEmptyContent(boolean emptyContent) {
Expand Down Expand Up @@ -317,8 +299,8 @@ public LowLevelHttpResponse execute() throws IOException {
GenericJson content = new GenericJson();
content.setFactory(OAuth2Utils.JSON_FACTORY);
if (requestStatusCode == 200) {
content.put(plaintextS2AAddressJsonKey, plaintextS2AAddress);
content.put(mtlsS2AAddressJsonKey, mtlsS2AAddress);
content.put(s2aContentMap.get("plaintextS2AAddressJsonKey"), s2aContentMap.get("plaintextS2AAddress"));
content.put(s2aContentMap.get("mtlsS2AAddressJsonKey"), s2aContentMap.get("mtlsS2AAddress"));
}
String contentText = content.toPrettyString();

Expand Down Expand Up @@ -351,10 +333,10 @@ protected boolean isIdentityDocumentUrl(String url) {
}

protected boolean isMtlsConfigRequestUrl(String url) {
return plaintextS2AAddressJsonKey != null
&& plaintextS2AAddress != null
&& mtlsS2AAddress != null
&& mtlsS2AAddressJsonKey != null
return s2aContentMap.containsKey("plaintextS2AAddressJsonKey")
&& s2aContentMap.containsKey("plaintextS2AAddress")
&& s2aContentMap.containsKey("mtlsS2AAddressJsonKey")
&& s2aContentMap.containsKey("mtlsS2AAddress")
&& url.equals(
String.format(
ComputeEngineCredentials.getMetadataServerUrl() + S2A.S2A_CONFIG_ENDPOINT_POSTFIX));
Expand Down
40 changes: 20 additions & 20 deletions oauth2_http/javatests/com/google/auth/oauth2/S2ATest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ public class S2ATest {
@Test
public void getS2AAddress_validAddress() {
MockMetadataServerTransportFactory transportFactory = new MockMetadataServerTransportFactory();
transportFactory.transport.setPlaintextS2AAddressJsonKey(S2A.S2A_PLAINTEXT_ADDRESS_JSON_KEY);
transportFactory.transport.setPlaintextS2AAddress(S2A_PLAINTEXT_ADDRESS);
transportFactory.transport.setMtlsS2AAddressJsonKey(S2A.S2A_MTLS_ADDRESS_JSON_KEY);
transportFactory.transport.setMtlsS2AAddress(S2A_MTLS_ADDRESS);
transportFactory.transport.setS2AContentMap("plaintextS2AAddressJsonKey", S2A.S2A_PLAINTEXT_ADDRESS_JSON_KEY);
transportFactory.transport.setS2AContentMap("plaintextS2AAddress", S2A_PLAINTEXT_ADDRESS);
transportFactory.transport.setS2AContentMap("mtlsS2AAddressJsonKey", S2A.S2A_MTLS_ADDRESS_JSON_KEY);
transportFactory.transport.setS2AContentMap("mtlsS2AAddress", S2A_MTLS_ADDRESS);
transportFactory.transport.setRequestStatusCode(HttpStatusCodes.STATUS_CODE_OK);

S2A s2aUtils = S2A.newBuilder().setHttpTransportFactory(transportFactory).build();
Expand All @@ -66,10 +66,10 @@ public void getS2AAddress_validAddress() {
@Test
public void getS2AAddress_queryEndpointResponseErrorCode_emptyAddress() {
MockMetadataServerTransportFactory transportFactory = new MockMetadataServerTransportFactory();
transportFactory.transport.setPlaintextS2AAddressJsonKey(S2A.S2A_PLAINTEXT_ADDRESS_JSON_KEY);
transportFactory.transport.setPlaintextS2AAddress(S2A_PLAINTEXT_ADDRESS);
transportFactory.transport.setMtlsS2AAddressJsonKey(S2A.S2A_MTLS_ADDRESS_JSON_KEY);
transportFactory.transport.setMtlsS2AAddress(S2A_MTLS_ADDRESS);
transportFactory.transport.setS2AContentMap("plaintextS2AAddressJsonKey", S2A.S2A_PLAINTEXT_ADDRESS_JSON_KEY);
transportFactory.transport.setS2AContentMap("plaintextS2AAddress", S2A_PLAINTEXT_ADDRESS);
transportFactory.transport.setS2AContentMap("mtlsS2AAddressJsonKey", S2A.S2A_MTLS_ADDRESS_JSON_KEY);
transportFactory.transport.setS2AContentMap("mtlsS2AAddress", S2A_MTLS_ADDRESS);
transportFactory.transport.setRequestStatusCode(
HttpStatusCodes.STATUS_CODE_SERVICE_UNAVAILABLE);

Expand All @@ -83,10 +83,10 @@ public void getS2AAddress_queryEndpointResponseErrorCode_emptyAddress() {
@Test
public void getS2AAddress_queryEndpointResponseEmpty_emptyAddress() {
MockMetadataServerTransportFactory transportFactory = new MockMetadataServerTransportFactory();
transportFactory.transport.setPlaintextS2AAddressJsonKey(S2A.S2A_PLAINTEXT_ADDRESS_JSON_KEY);
transportFactory.transport.setPlaintextS2AAddress(S2A_PLAINTEXT_ADDRESS);
transportFactory.transport.setMtlsS2AAddressJsonKey(S2A.S2A_MTLS_ADDRESS_JSON_KEY);
transportFactory.transport.setMtlsS2AAddress(S2A_MTLS_ADDRESS);
transportFactory.transport.setS2AContentMap("plaintextS2AAddressJsonKey", S2A.S2A_PLAINTEXT_ADDRESS_JSON_KEY);
transportFactory.transport.setS2AContentMap("plaintextS2AAddress", S2A_PLAINTEXT_ADDRESS);
transportFactory.transport.setS2AContentMap("mtlsS2AAddressJsonKey", S2A.S2A_MTLS_ADDRESS_JSON_KEY);
transportFactory.transport.setS2AContentMap("mtlsS2AAddress", S2A_MTLS_ADDRESS);
transportFactory.transport.setRequestStatusCode(HttpStatusCodes.STATUS_CODE_OK);
transportFactory.transport.setEmptyContent(true);

Expand All @@ -100,10 +100,10 @@ public void getS2AAddress_queryEndpointResponseEmpty_emptyAddress() {
@Test
public void getS2AAddress_queryEndpointResponseInvalidPlaintextJsonKey_plaintextEmptyAddress() {
MockMetadataServerTransportFactory transportFactory = new MockMetadataServerTransportFactory();
transportFactory.transport.setPlaintextS2AAddressJsonKey(INVALID_JSON_KEY);
transportFactory.transport.setPlaintextS2AAddress(S2A_PLAINTEXT_ADDRESS);
transportFactory.transport.setMtlsS2AAddressJsonKey(S2A.S2A_MTLS_ADDRESS_JSON_KEY);
transportFactory.transport.setMtlsS2AAddress(S2A_MTLS_ADDRESS);
transportFactory.transport.setS2AContentMap("plaintextS2AAddressJsonKey", INVALID_JSON_KEY);
transportFactory.transport.setS2AContentMap("plaintextS2AAddress", S2A_PLAINTEXT_ADDRESS);
transportFactory.transport.setS2AContentMap("mtlsS2AAddressJsonKey", S2A.S2A_MTLS_ADDRESS_JSON_KEY);
transportFactory.transport.setS2AContentMap("mtlsS2AAddress", S2A_MTLS_ADDRESS);
transportFactory.transport.setRequestStatusCode(HttpStatusCodes.STATUS_CODE_OK);

S2A s2aUtils = S2A.newBuilder().setHttpTransportFactory(transportFactory).build();
Expand All @@ -116,10 +116,10 @@ public void getS2AAddress_queryEndpointResponseInvalidPlaintextJsonKey_plaintext
@Test
public void getS2AAddress_queryEndpointResponseInvalidMtlsJsonKey_mtlsEmptyAddress() {
MockMetadataServerTransportFactory transportFactory = new MockMetadataServerTransportFactory();
transportFactory.transport.setPlaintextS2AAddressJsonKey(S2A.S2A_PLAINTEXT_ADDRESS_JSON_KEY);
transportFactory.transport.setPlaintextS2AAddress(S2A_PLAINTEXT_ADDRESS);
transportFactory.transport.setMtlsS2AAddressJsonKey(INVALID_JSON_KEY);
transportFactory.transport.setMtlsS2AAddress(S2A_MTLS_ADDRESS);
transportFactory.transport.setS2AContentMap("plaintextS2AAddressJsonKey", S2A.S2A_PLAINTEXT_ADDRESS_JSON_KEY);
transportFactory.transport.setS2AContentMap("plaintextS2AAddress", S2A_PLAINTEXT_ADDRESS);
transportFactory.transport.setS2AContentMap("mtlsS2AAddressJsonKey", INVALID_JSON_KEY);
transportFactory.transport.setS2AContentMap("mtlsS2AAddress", S2A_MTLS_ADDRESS);
transportFactory.transport.setRequestStatusCode(HttpStatusCodes.STATUS_CODE_OK);

S2A s2aUtils = S2A.newBuilder().setHttpTransportFactory(transportFactory).build();
Expand Down

0 comments on commit 594df7b

Please sign in to comment.