spring 云网关谓词测试用例

问题描述

我已经实现了谓词工厂(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());
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)