Here is the working code I have prepared. one of the important points is setUrlEncodingEnabled() and urlEncodingEnabled().
you can check following links as well
CustomAppContext
RestAssure URL Encoding
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
public class CustomApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext>
{
@Override
public void initialize(ConfigurableApplicationContext applicationContext)
{
System.setProperty("org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH", "true");
}
}
import static io.restassured.RestAssured.given;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;
@ActiveProfiles("it")
@RunWith(SpringRunner.class)
@TestPropertySource(locations = "classpath:application-it.properties")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration(initializers = CustomApplicationContextInitializer.class)
public class TeamControllerIT {
@LocalServerPort
private int port;
@Autowired
private Environment environment;
private RequestSpecification correctCredentialsPortSpecUrlEncodedForService;
@MockBean
private TeamService teamService;
@Before
public void setUp() throws Exception {
JSONArray rolesArray = new JSONArray();
rolesArray.put("user");
String token = JWTUtil.createSelfSignedTestToken(environment.getProperty("publickey"),
environment.getProperty("privatekey"), Date.valueOf(LocalDate.parse("2999-01-01")), "integrationTest",
environment.getProperty("testclient"), rolesArray);
correctCredentialsPortSpecUrlEncodedForService = new RequestSpecBuilder()
.addHeader("Authorization", "Bearer " + token)
.setPort(port)
.setUrlEncodingEnabled(false)
.build();
}
@Test
public void shouldGetUsersInTeam() throws UnsupportedEncodingException {
String teamID = "slash/slash";
when(teamService.getTeamUsers(teamID)).thenReturn(List.of(
User.builder().userId("user1").firstName("user").lastName("1").email("user1@test.com").build(),
User.builder().userId("user2").firstName("user").lastName("2").email("user2@test.com").build()
));
List<KeycloakUserDTO> list2 =
given()
.spec(correctCredentialsPortSpecUrlEncodedForService)
.log().ifValidationFails()
.contentType("application/json")
.urlEncodingEnabled(false)
.pathParam("teamIDEncoded", urlEncodeString(teamID))
.when()
.get("/userservice/teams/{teamIDEncoded}/users")
.then()
.log().ifValidationFails()
.statusCode(200)
.and().extract().jsonPath().getList("", KeycloakUserDTO.class);
assertThat(list2.size()==2);
}
private static String urlEncodeString(String value) throws UnsupportedEncodingException {
return URLEncoder.encode(value, StandardCharsets.UTF_8.name());
}
private String activeProfile() {
return environment.getActiveProfiles()[0];
}
}