Polishing of CreatedResponseUtil.getCreatedId

closes #36557

Signed-off-by: mposolda <mposolda@gmail.com>
(cherry picked from commit fd1e82f05dac312d95431fe9293926cf41d88aaa)
This commit is contained in:
mposolda 2025-01-17 11:59:50 +01:00 committed by Marek Posolda
parent 0426e91e7b
commit 24bf490916
3 changed files with 38 additions and 9 deletions

View File

@ -46,17 +46,22 @@ public class CreatedResponseUtil {
String errorMessage = "Create method returned status " +
statusInfo.getReasonPhrase() + " (Code: " + statusInfo.getStatusCode() + "); " +
"expected status: Created (201).";
if (MediaType.APPLICATION_JSON.equals(contentType)) {
// try to add actual server error message to the exception message
try {
try {
if (matches(MediaType.APPLICATION_JSON_TYPE, MediaType.valueOf(contentType))) {
// try to add actual server error message to the exception message
@SuppressWarnings("raw")
Map responseBody = response.readEntity(Map.class);
if (responseBody != null && responseBody.containsKey("errorMessage")) {
errorMessage += " ErrorMessage: " + responseBody.get("errorMessage");
if (responseBody != null) {
if (responseBody.containsKey("errorMessage")) {
errorMessage += " ErrorMessage: " + responseBody.get("errorMessage");
}
if (responseBody.containsKey("error")) {
errorMessage += " Error: " + responseBody.get("error");
}
}
} catch(Exception ignored) {
// ignore if we couldn't parse the response
}
} catch(Exception ignored){
// ignore if we couldn't parse the response
}
throw new WebApplicationException(errorMessage, response);
@ -67,4 +72,12 @@ public class CreatedResponseUtil {
String path = location.getPath();
return path.substring(path.lastIndexOf('/') + 1);
}
private static boolean matches(MediaType a, MediaType b) {
if (a == null) {
return b == null;
} else if (b == null) return false;
return a.getType().equalsIgnoreCase(b.getType()) && a.getSubtype().equalsIgnoreCase(b.getSubtype());
}
}

View File

@ -27,6 +27,7 @@ import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.keycloak.TokenVerifier;
import org.keycloak.admin.client.CreatedResponseUtil;
import org.keycloak.admin.client.Keycloak;
import org.keycloak.admin.client.resource.GroupResource;
import org.keycloak.admin.client.resource.IdentityProviderResource;
@ -131,6 +132,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.hasSize;
@ -324,8 +326,13 @@ public class UserTest extends AbstractAdminTest {
assertEquals(409, response.getStatus());
assertAdminEvents.assertEmpty();
ErrorRepresentation error = response.readEntity(ErrorRepresentation.class);
Assert.assertEquals("User exists with same email", error.getErrorMessage());
// Alternative way of showing underlying error message
try {
CreatedResponseUtil.getCreatedId(response);
Assert.fail("Not expected getCreatedId to success");
} catch (WebApplicationException wae) {
Assert.assertThat(wae.getMessage(), endsWith("ErrorMessage: User exists with same email"));
}
}
}

View File

@ -19,6 +19,7 @@ package org.keycloak.testsuite.admin.authentication;
import org.junit.Assert;
import org.junit.Test;
import org.keycloak.admin.client.CreatedResponseUtil;
import org.keycloak.admin.client.resource.ClientResource;
import org.keycloak.admin.client.resource.IdentityProviderResource;
import org.keycloak.authentication.authenticators.browser.IdentityProviderAuthenticatorFactory;
@ -61,6 +62,7 @@ import java.util.stream.Collectors;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.endsWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
@ -128,6 +130,13 @@ public class FlowTest extends AbstractAuthenticationTest {
public void testAddFlowWithRestrictedCharInAlias() {
Response resp = authMgmtResource.createFlow(newFlow("fo]o", "Browser flow", "basic-flow", true, false));
Assert.assertEquals(400, resp.getStatus());
try {
CreatedResponseUtil.getCreatedId(resp);
Assert.fail("Not expected getCreatedId to success");
} catch (WebApplicationException wae) {
Assert.assertThat(wae.getMessage(), endsWith("Error: Character ']' not allowed."));
}
}
@Test