如何获得Jersey测试/客户不填写默认的Accept标头? [英] How do I get Jersey Test/Client to not fill in a default Accept header?
问题描述
我正在尝试以特定方式处理不带Accept
头的请求,但是无论我做什么,Jersey似乎都难以满足要求,因此,看起来请求总是带有
I'm trying to handle a request with no Accept
header in a particular way, but Jersey seems hell-bent on filling one in, no matter what I do, so it always looks like the request has an Accept
header, even if it doesn't.
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Test;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import static org.junit.Assert.assertEquals;
public class JerseyTestTest extends JerseyTest {
@Path("hello")
public static class HelloResource {
@GET
public String getHello(@Context HttpHeaders httpHeaders) {
String acceptHeader = httpHeaders.getHeaderString(HttpHeaders.ACCEPT);
return acceptHeader != null ? acceptHeader : "No Accept Header";
}
}
@Override
protected Application configure() {
return new ResourceConfig(HelloResource.class);
}
@Test
public void test() {
final String hello = target("hello").request()
.header(HttpHeaders.ACCEPT, null) // null means remove header
.get(String.class);
assertEquals("No Accept Header", hello);
}
}
此测试的结果是:
org.junit.ComparisonFailure:
Expected :No Accept Header
Actual :text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
不知何故,默认的Accept
标头text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
被设置在某处.它没有记录,我很想弄清楚如何禁用它.我查看了泽西岛的消息来源,但似乎找不到发生这种情况的地方或原因.
Somehow there is a default Accept
header of text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
that gets set somewhere. It's not documented, and I would love to figure out how to disable it. I've looked through the Jersey source, but can't seem to locate where this is happening or why.
更新:当我使用curl来打一个没有Accept标头的端点时,没有生成的Accept标头,因此问题出在某种程度上是Jersey Client或Jersey Test环境中.
Update: when I use curl to hit an endpoint without an Accept header, there is no generated Accept header, so the problem lies in Jersey Client or the Jersey Test environment, somehow.
更新2:使用默认的Grizzly2测试容器或JDK测试容器,而不是内存中测试容器时,会出现此错误.
Update 2: This bug exhibits when using the default Grizzly2 test container or the JDK test container, but NOT with the In Memory test container.
推荐答案
不确定是否对其他人有帮助,但是我们看到了类似的行为,即使用客户代码在生产环境中调用服务.我们正在使用Jersey 2.21.1.
Not sure if this helps anyone else but we were seeing similar behavior calling a service in production with our client code. We are using Jersey 2.21.1.
展开原始帖子中的代码,并发现以下内容是正确的:
Expanded the code from the original post and found the following to be true:
- 如果
Accept
标头为空,则Jersey添加默认值 - 如果
Accept
标头为空的String
,则使用空的Accept
标头 - 如果
Accept
标头具有使用的值
- if
Accept
header is null then Jersey adds the default - if
Accept
header is an emptyString
then an emptyAccept
header is used - if
Accept
header has a value it is used
我不确定在使用null时是否有办法告诉Jersey不要添加默认值.
I'm not sure if there is a way to tell Jersey not to add the default value when a null is used.
public class JerseyAcceptHeaderTest extends JerseyTest {
@Path("hello")
public static class HelloResource {
@GET
public String getHello(@Context HttpHeaders httpHeaders) {
String acceptHeader = httpHeaders.getHeaderString(HttpHeaders.ACCEPT);
System.out.println("SERVER RECEIVED:" + acceptHeader);
if (acceptHeader == null) {
return "Null Accept Header";
} else if (acceptHeader.equals("")) {
return "No Accept Header";
} else {
return acceptHeader;
}
}
}
@Override
protected Application configure() {
return new ResourceConfig(HelloResource.class);
}
/**
* this seems to be a bug in Jersey
* it overrides a null Accept header
*/
@Test
public void test_accept_header_with_null() {
final String acceptHeader = target("hello").request()
.header(HttpHeaders.ACCEPT, null)
.get(String.class);
assertEquals("Null Accept Header", acceptHeader);
}
@Test
public void test_accept_header_with_empty_string() {
final String acceptHeader = target("hello").request()
.header(HttpHeaders.ACCEPT, "")
.get(String.class);
assertEquals("No Accept Header", acceptHeader);
}
@Test
public void test_accept_header_with_spaced_string() {
final String acceptHeader = target("hello").request()
.header(HttpHeaders.ACCEPT, " ")
.get(String.class);
assertEquals("No Accept Header", acceptHeader);
}
@Test
public void test_accept_header_with_value() {
final String acceptHeader = target("hello").request()
.header(HttpHeaders.ACCEPT, "application/json")
.get(String.class);
assertEquals("application/json", acceptHeader);
}
}
这篇关于如何获得Jersey测试/客户不填写默认的Accept标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!