【问题标题】:How to add HTTP header to MicroShedTest and SharedContainerConfig JUnit tests如何将 HTTP 标头添加到 MicroShedTest 和 SharedContainerConfig JUnit 测试
【发布时间】:2020-05-06 13:01:24
【问题描述】:
在我为我的应用程序添加安全性之前,我的集成测试一直运行顺利。安全性使用自定义生成的 api 密钥,并在标头“X-API-Key”的自定义 HttpAuthenticationMechanism 中完成验证。
我需要确定是否可以在测试套件进行的调用中添加标头。我检查了互联网,发现只有@BasicAuthConfig 和@JwtConfig 没有任何用处。
我需要在对容器进行的 http 调用中添加一个标头“X-API-Key”。
【问题讨论】:
标签:
integration-testing
junit5
payara
payara-micro
【解决方案1】:
我没有发现任何有用的东西,所以我创建了自己的解决方案。我没有依赖 @RESTClient 给我创建自己的资源代理,而是像这样:
public static <T> T getResourceProxy(Class<T> t) {
Map<String, String> headerMap = new HashMap<>();
headerMap.put("X-API-Key", "abcdefg.abcdefg1234567hij890");
headerMap.put("Content-Type", "application/json");
headerMap.put("Accept", "application/json, text/plain");
JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
bean.setHeaders(headerMap);
bean.setResourceClass(t);
bean.setAddress("http://localhost:8080/myApp");
List<Object> providers = new ArrayList<>();
providers.add(new JacksonJaxbJsonProvider());
providers.add(new JacksonJsonProvider());
bean.setProviders(providers);
return bean.create(t);
}