java - 如何测试从服务器拦截器设置的上下文键值
问题描述
我正在尝试为我的身份验证服务器拦截器编写单元测试。拦截器从附加的元数据头中提取用户 ID 信息,并将其存储为 ContextKey,稍后在服务调用中使用。我是根据这个线程的说明完成的。
基于HeaderServerInterceptorTest示例,我设置了自己的测试,它确实调用了拦截器。但是,当尝试断言用户 ID 值时,它始终为空。
我的拦截器类是:
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers,
ServerCallHandler<ReqT, RespT> next) {
// authentication and extracting user ID info
Context context = Context.current();
return Contexts.interceptCall(context.withValue(USER_ID, id), call, headers, next);
}
断言总是失败,因为 USER_ID 值为空。
assertEquals("john_doe_ID", MyServerInterceptor.getUserID());
我猜我在测试中缺少 Context 的一些额外配置,但我找不到类似的东西......
解决方案
推荐阅读
- python - 检查角度是否落入圆形置信区间的优雅(最有效)方法
- xslt - Apache Xalan 的替代 XSLT 处理器
- jenkins - Groovy:数组的最后 5 个元素(如果存在)
- sql-server - 使用任何连接函数或其他函数从 2 个表中选择数据
- javascript - 如何在另一个函数中使用一个函数的变量?
- hyperledger-fabric - 如何使用cli命令检查用户是否在fabric-ca服务器中注册和注册?
- c# - SOAP 消息错误的 C# 安全凭证 - WS-Security (WSS)
- maven - 将 cxf 肥皂战争文件部署到 weblogic 12.2.1 时实体 bean 未增强错误
- scala - 检查特定标识符是否存在于其他数据框中
- sql - 如何正确传递字符串作为参数