Skip to content

Commit 80c069b

Browse files
committed
refactor so that we can reuse the ZoneResolutionMode in other MockMvc tests
1 parent 6c951c3 commit 80c069b

File tree

2 files changed

+51
-22
lines changed

2 files changed

+51
-22
lines changed

uaa/src/test/java/org/cloudfoundry/identity/uaa/mock/token/ZonePathTokenMockMvcTests.java

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,26 @@
55
import org.cloudfoundry.identity.uaa.oauth.common.util.OAuth2Utils;
66
import org.cloudfoundry.identity.uaa.oauth.jwt.Jwt;
77
import org.cloudfoundry.identity.uaa.oauth.jwt.JwtHelper;
8+
import org.cloudfoundry.identity.uaa.mock.util.MockMvcUtils.ZoneResolutionMode;
89
import org.cloudfoundry.identity.uaa.util.JsonUtils;
9-
import org.cloudfoundry.identity.uaa.util.SetServerNameRequestPostProcessor;
1010
import org.cloudfoundry.identity.uaa.zone.IdentityZone;
1111
import org.cloudfoundry.identity.uaa.zone.IdentityZoneHolder;
1212
import org.junit.jupiter.params.ParameterizedTest;
1313
import org.junit.jupiter.params.provider.EnumSource;
14-
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
14+
import org.springframework.http.HttpMethod;
1515

1616
import java.util.Map;
17-
import java.util.function.Function;
1817

1918
import static org.cloudfoundry.identity.uaa.authentication.AbstractClientParametersAuthenticationFilter.CLIENT_SECRET;
2019
import static org.cloudfoundry.identity.uaa.oauth.token.ClaimConstants.ISS;
2120
import static org.cloudfoundry.identity.uaa.oauth.token.TokenConstants.GRANT_TYPE_CLIENT_CREDENTIALS;
2221
import static org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED;
2322
import static org.springframework.http.MediaType.APPLICATION_JSON;
24-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
2523
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
2624
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
2725

2826
public class ZonePathTokenMockMvcTests extends AbstractTokenMockMvcTests {
2927

30-
private enum ZoneResolutionMode {
31-
SUBDOMAIN(subdomain -> get("/oauth/token")
32-
.with(new SetServerNameRequestPostProcessor(subdomain + ".localhost"))
33-
),
34-
ZONE_PATH(subdomain -> get("/z/{subdomain}/oauth/token", subdomain));
35-
36-
private final Function<String, MockHttpServletRequestBuilder> requestBuilder;
37-
38-
ZoneResolutionMode(Function<String, MockHttpServletRequestBuilder> requestBuilder) {
39-
this.requestBuilder = requestBuilder;
40-
}
41-
42-
MockHttpServletRequestBuilder createRequestBuilder(String subdomain) {
43-
return requestBuilder.apply(subdomain);
44-
}
45-
}
46-
4728
@ParameterizedTest
4829
@EnumSource(ZoneResolutionMode.class)
4930
void clientCredentialsGrant(ZoneResolutionMode mode) throws Exception {
@@ -59,7 +40,7 @@ void clientCredentialsGrant(ZoneResolutionMode mode) throws Exception {
5940

6041
IdentityZoneHolder.clear();
6142

62-
String tokenResult = mockMvc.perform(mode.createRequestBuilder(subdomain)
43+
String tokenResult = mockMvc.perform(mode.createRequestBuilder(subdomain, HttpMethod.GET, "/oauth/token")
6344
.accept(APPLICATION_JSON)
6445
.contentType(APPLICATION_FORM_URLENCODED)
6546
.param(OAuth2Utils.GRANT_TYPE, GRANT_TYPE_CLIENT_CREDENTIALS)

uaa/src/test/java/org/cloudfoundry/identity/uaa/mock/util/MockMvcUtils.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
import org.springframework.context.ApplicationListener;
7070
import org.springframework.context.ConfigurableApplicationContext;
7171
import org.springframework.context.event.ApplicationEventMulticaster;
72+
import org.springframework.http.HttpMethod;
7273
import org.springframework.http.MediaType;
7374
import org.springframework.jdbc.core.JdbcTemplate;
7475
import org.springframework.mock.web.MockHttpServletRequest;
@@ -134,6 +135,53 @@ private MockMvcUtils() {
134135
throw new java.lang.UnsupportedOperationException("This is a utility class and cannot be instantiated");
135136
}
136137

138+
/**
139+
* Mode for resolving identity zone in tests: by host subdomain or by path prefix {@code /z/{subdomain}/}.
140+
* The test passes the path suffix (e.g. {@code "/oauth/token"});
141+
* the mode constructs the full path.
142+
*/
143+
public enum ZoneResolutionMode {
144+
SUBDOMAIN {
145+
@Override
146+
public MockHttpServletRequestBuilder createRequestBuilder(String subdomain, HttpMethod method, String pathSuffix) {
147+
return requestBuilderForMethod(method, pathSuffix)
148+
.with(new SetServerNameRequestPostProcessor(subdomain + ".localhost"));
149+
}
150+
},
151+
ZONE_PATH {
152+
@Override
153+
public MockHttpServletRequestBuilder createRequestBuilder(String subdomain, HttpMethod method, String pathSuffix) {
154+
return requestBuilderForMethod(method, "/z/{subdomain}" + pathSuffix, subdomain);
155+
}
156+
};
157+
158+
public abstract MockHttpServletRequestBuilder createRequestBuilder(String subdomain, HttpMethod method, String pathSuffix);
159+
}
160+
161+
/**
162+
* Builds a MockHttpServletRequestBuilder for the given HTTP method and path.
163+
*
164+
* @param method the HTTP method (GET, POST, PUT, DELETE)
165+
* @param path the path (may contain path variables like {@code /z/{subdomain}/oauth/token})
166+
* @param pathVars optional path variable values
167+
* @return the request builder
168+
*/
169+
public static MockHttpServletRequestBuilder requestBuilderForMethod(HttpMethod method, String path, Object... pathVars) {
170+
if (method == HttpMethod.GET) {
171+
return get(path, pathVars);
172+
}
173+
if (method == HttpMethod.POST) {
174+
return post(path, pathVars);
175+
}
176+
if (method == HttpMethod.PUT) {
177+
return put(path, pathVars);
178+
}
179+
if (method == HttpMethod.DELETE) {
180+
return delete(path, pathVars);
181+
}
182+
throw new IllegalArgumentException("Unsupported method: " + method);
183+
}
184+
137185
private static final String SIMPLESAMLPHP_UAA_ACCEPTANCE = "http://simplesamlphp.uaa-acceptance.cf-app.com";
138186

139187
public static final String IDP_META_DATA =

0 commit comments

Comments
 (0)