问题描述
我正在针对针对Web服务编写集成测试,该Web服务使用JAX-RS注释进行注释,并使用Spring Security进行保护。我使用Resteasy Client代理框架生成代理,从中可以调用方法以测试Web服务。例如,
ResteasyClient client = new ResteasyClientBuilder()。connectionPoolSize(10).connectionTTL(10,TimeUnit.SECONDS).build();
I am working on writing integration tests against web services that are annotated with JAX-RS annotations and secured with Spring Security. I use Resteasy Client proxy framework to generate proxies, from which I can invoke methods on to test the web services. For example,ResteasyClient client = new ResteasyClientBuilder().connectionPoolSize(10).connectionTTL(10, TimeUnit.SECONDS).build();
ResteasyWebTarget target = client.target(properties.getRestWebBaseUrl());
ProxyBuilder<UserClient> builder = target.proxyBuilder(UserClient.class);
UserClient userClient = builder
.defaultConsumes(MediaType.APPLICATION_JSON).build();
如何向Resteasy正在构造的请求中添加Cookie?我尝试注册ClientRequestFilter,但是它不起作用。
How do I add a cookie to the request being constructed by Resteasy? I have tried registering a ClientRequestFilter but it does not work.
谢谢
推荐答案
注册ClientRequestFilter确实可以,如下所示:
Registering a ClientRequestFilter does work, as follows :
ResteasyWebTarget target = client.target(properties.getRestWebBaseUrl());
Cookie cookie = new Cookie("foo", "bar");
client.register(new CookieClientRequestFilter(cookie));
ProxyBuilder<UserClient> builder = target.proxyBuilder(UserClient.class);
UserClient userClient = builder
.defaultConsumes(MediaType.APPLICATION_JSON).build();
with:
import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientRequestFilter;
import javax.ws.rs.core.Cookie;
import javax.ws.rs.ext.Provider;
import java.io.IOException;
@Provider
public class CookieClientRequestFilter implements ClientRequestFilter {
private Cookie cookie;
public CookieClientRequestFilter(Cookie cookie) {
super();
this.cookie = cookie;
}
@Override
public void filter(ClientRequestContext clientRequestContext) throws IOException {
List<Object> cookies = new ArrayList<>();
cookies.add(this.cookie);
clientRequestContext.getHeaders().put("Cookie", cookies);
}
}
这篇关于如何为通过Resteasy Client API构建的请求设置cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!