首页 > 解决方案 > spring cloud gateway 谓词测试用例

问题描述

我已经实现了谓词工厂(spring cloud gateway)来验证标头,我想为此添加测试用例

public Predicate<ServerWebExchange> apply(Config config ) {
    return (ServerWebExchange t) -> {
        
        List<String> Header = t.getRequest().getHeaders().get("abcd");
        #business logic
        return true;
    };

}

我想包括上面谓词工厂的测试用例。

我尝试了如下测试用例

@Before
public void prepareStubs() {
    
    stubFor(any(urlPathEqualTo("/abcd")).willReturn(aResponse().withBody("ABCD")));
    
}

@Test
public void testGatewayRouting() throws JsonMappingException, JsonProcessingException {

    HttpHeaders headers = new HttpHeaders();
    
    headers.add("Authorization", "Bearer eyJraWQiOiIiLCJ0eXAiOiJKV1QiLCJhbGciOiJSUzUxMiJ9");
            
    HttpEntity<?> entity = new HttpEntity<>(headers);
    
    
    UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromHttpUrl(createURLWithPort("/abcd"));
    
    

    ResponseEntity<String> response = restTemplate.exchange(uriBuilder.toUriString(), HttpMethod.GET, entity,
            String.class);

    assertEquals(200, response.getStatusCodeValue());

    assertEquals("ABCD", response.getBody());
}

标签: javaspring-bootpredicatespring-cloud-gateway

解决方案


推荐阅读