java 基于URL编码方法的JUnit测试用例

1l5u6lss  于 2023-03-16  发布在  Java
关注(0)|答案(1)|浏览(168)

我做了一些更改来激活斜线编码的URL,for details它工作正常,我想在运行IT测试时激活这些更改。
如何在UserserviceApplication上激活这些更改。

@SpringBootApplication
    @EnableDiscoveryClient
    @EnableCaching
    public class UserserviceApplication implements WebMvcConfigurer {
    
    
        public static void main(String[] args) {
            System.setProperty("org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH", "true");
            SpringApplication.run(UserserviceApplication.class, args);
        }
    
        public void configurePathMatch(PathMatchConfigurer configurer) {
            UrlPathHelper urlPathHelper = new UrlPathHelper();
            urlPathHelper.setUrlDecode(false);
            configurer.setUrlPathHelper(urlPathHelper);
        }
    
    }
    
    
    @ActiveProfiles("it")
    @RunWith(SpringRunner.class)
    @TestPropertySource(locations = "classpath:application-it.properties")
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    
    public class TeamControllerIT {
    
        @LocalServerPort private int port;
    
        @Autowired private Environment environment;
zpjtge22

zpjtge221#

下面是我准备的工作代码。其中一个要点是setUrlEncodingEnabled()和urlEncodingEnabled()。您也可以检查以下链接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];
    }
}

相关问题