java - 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());
}
解决方案
推荐阅读
- python - RuntimeError 字典在字典迭代期间更改大小
- c# - 作为 Windows 服务部署的 ASP .Net Core RESTful 应用程序不工作/启动
- reactjs - 如何使用 React DatePicker 选择时间和日期范围
- mongodb - 如何为 mongodb 动态添加的集合创建索引?
- javascript - 你好!如何从 Vue.js 中的 localStorage 中提取数据并呈现在页面上?
- c++ - WaitNamedPipe() 在 Windows XP 中返回 0 与 2 个以上的客户端
- python - 无法在我的 Windows 10 机器上使用 cx_Freeze
- sql - 我可以将 table1 的每一行加入到 table 2 的唯一行吗?
- c - 如何解决遍历森林中抛出的异常
- android - 如何在flutter中使用Dio将flutter_sound包录制的音频上传到服务器?