[英]How to get header from InvocationBuilder object from request in JUnit 5 test
I want to test a method which is adding a header in a request to outside public API. 我想测试一种在向外部公共API的请求中添加标头的方法。
The method looks like: 该方法如下所示:
@Service
@AllArgsConstructor
class ProductsFacadeImpl implements ProductsFacade {
private NutritionixHeader nutritionixHeader;
@Override
public Invocation.Builder prepareHeaderForApiCall(String query) {
Client client = ClientBuilder.newClient();
WebTarget webTarget =
client.target("https://trackapi.nutritionix.com/v2/search/instant?query=" + query);
Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON_VALUE);
invocationBuilder.header("x-app-id", nutritionixHeader.getNutritionixAppId());
invocationBuilder.header("x-app-key", nutritionixHeader.getNutritionixAppKey());
return invocationBuilder;
}
}
new version method for EDIT purpose: 用于编辑目的的新版本方法:
@Override
public Invocation prepareHeaderForApiCall(String query) {
Client client = ClientBuilder.newClient();
WebTarget webTarget =
client.target("https://trackapi.nutritionix.com/v2/search/instant?query=" + query);
Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON_VALUE);
invocationBuilder.header("x-app-id", nutritionixHeader.getNutritionixAppId());
invocationBuilder.header("x-app-key", nutritionixHeader.getNutritionixAppKey());
return invocationBuilder.buildGet();
}
In my JUnit 5 test, I want to check if my header contains two objects which are required by public API. 在我的JUnit 5测试中,我想检查我的标头是否包含公共API所需的两个对象。 It's: 它的:
"x-app-id"
and 和
"x-app-key"
My test looks like: 我的测试看起来像:
@Test
void prepareHeaderForApiCall() {
var query = "query";
given(nutritionixHeader.getNutritionixAppId()).willReturn(appId);
given(nutritionixHeader.getNutritionixAppKey()).willReturn(appKey);
Invocation.Builder builder = productsFacade.prepareHeaderForApiCall(query);
builder.get();
assertEquals(appId, builder.get().getHeaders().entrySet().stream().filter(entry -> entry.getKey().equals("x-app-id")).map(Map.Entry::getValue).findFirst().orElse(null));
}
The problem is that the method 问题是方法
getHeaders()
in my stream does not contain headers which I added and it looks like: 在我的流中不包含我添加的标头,它看起来像:
I want to know how I can get a header params objects which are placed in a requestContext object as a header objects like below: 我想知道我如何获取放置在requestContext对象中的标头params对象,作为标头对象,如下所示:
EDIT: 编辑:
I found one interesting thing with debugger and evaluate expression in IntelliJ, namely: 我在调试器中发现了一件有趣的事情,并在IntelliJ中评估了表达式:
with this above expression, I can find out my two added headers but I cannot use the same expression in a code. 使用上面的表达式,我可以找出我添加的两个标头,但不能在代码中使用相同的表达式。
I will be grateful for a suggestion about how to reach a goal by getting headers object from the requestContext object. 我将对如何通过从requestContext对象获取标头对象来达到目标的建议表示感谢。
You could try following: 您可以尝试以下操作:
1) builder.get().getMetadata()
1) builder.get().getMetadata()
2) builder.get().getStringHeaders()
2) builder.get().getStringHeaders()
3) if previous two fail ((ClientResponse)ReflectionTestUtils.getField(builder.get(), "context")).getHeaders()
3)如果前两个失败((ClientResponse)ReflectionTestUtils.getField(builder.get(), "context")).getHeaders()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.