我觉得我失去了一些东西返回错误的状态。我有一个过滤器打印出我的服务器返回的信息,并且报告我正在返回正确的响应(403)。我写了一个JUnit测试,以验证这种逻辑,很多时候我正在报告200,而不是403怪异的是,我的服务器日志仍然显示我发了403是否有在新泽西州1.17一些已知的错误,我不知道的我需要升级才能解决?我现在还没有真正升级,所以我希望我的逻辑中有一些错误。这是我的测试用例。的Java泽西ClientResponse从getClientResponseStatus
@Test
public void testIdsOwnedBySomeoneElse()
{
final Login user1Cred = Logins.getInstance().getLogin(Logins.LoginType.User1);
final Login user2Cred = Logins.getInstance().getLogin(Logins.LoginType.User2);
final ServiceEndpointAuthenticated authUser1 = LoginHelper.Login(user1Cred);
final ServiceEndpointAuthenticated authUser2 = LoginHelper.Login(user2Cred);
// Create generic entry owned by user 1
BigInteger user1Id = null;
{
final Object payload = endpoint.CreateEntity(authUser1.getUserId());
final ClientResponse response = endpoint.Post(authUser1, payload);
assertTrue(Status.OK == response.getClientResponseStatus());
final byte[] data = Utilities.getBytes(response.getEntityInputStream());
user1Id = endpoint.getEntityId(data);
}
// Using User2, try to use that id from user1!
{
// test 1
final MyEndpointWrapper endpoint = new MyEndpointWrapper(user1Id, validId);
final Object payload = endpoint.CreateEntity(authUser2.getUserId());
final ClientResponse response = endpoint.Post(authUser2, payload);
final Status status = response.getClientResponseStatus();
System.out.println("Returned status = " + status);
if (status != Status.FORBIDDEN)
{
byte[] data = Utilities.getBytes(response.getEntityInputStream());
String toString = null;
try
{
toString = new String(data, "UTF-8");
}
catch (UnsupportedEncodingException e)
{
}
System.out.println("data: " + toString);
}
assertEquals("Status " + status + " is not forbidden!", Status.FORBIDDEN, status);
}
{
// test 2
final MyEndpointWrapper endpoint = new MyEndpointWrapper(validId, user1Id);
final Object payload = endpoint.CreateEntity(authUser2.getUserId());
final ClientResponse response = endpoint.Post(authUser2, payload);
final Status status = response.getClientResponseStatus();
System.out.println("Returned status = " + status);
if (status != Status.FORBIDDEN)
{
int i = 9;
}
assertEquals("Status " + status + " is not forbidden!", Status.FORBIDDEN, status);
}
// Go ahead and delete this data for cleanup
assertTrue(Status.OK == endpoint.Delete(authUser1, user1Id).getClientResponseStatus());
}
我的通用代码首先登录到我们的服务器为creds。这些信誉被“附加”到WebResource,并在我建立我的请求时自动附加适当的标题。我首先创建一个实体,发布它,并存储返回的ID以供其他用户使用。我创建了另一个引用该违规ID的endpointwrapper,并尝试使用该ID进行发布。服务器日志:
INFO: RESPONSE: 403 http://myendpoint MediaType:(application/json) Payload: 232 MyErrorMessage
我甚至可以打印此消息(如上所示)!我不明白的部分是getClientResponseStatus返回给我。为什么?
我的邮编是这样的:
@Override
public ClientResponse Post(ServiceEndpointAuthenticated endpoint, Object entity)
{
MyUploadData uploadData = (MyUploadData)entity;
return endpoint.getResourceBuilder("/myendpoint")
.accept(MediaTypeExt.APPLICATION_JSON)
.type(MediaTypeExt.APPLICATION_JSON)
.post(ClientResponse.class, gson.toJson(uploadData));
}
[更新] 我跑线捕获,实际上确实看到200被送回!这似乎是Jersey Server内部的东西。这是我所看到的:
When working:
Request: 1099 17.021219000 127.0.0.1 127.0.0.1 HTTP 2214 POST /myEndpoint HTTP/1.1 (application/json)
Response: 1153 17.042535000 127.0.0.1 127.0.0.1 HTTP 628 HTTP/1.1 400 Bad Request (application/json)
When not working:
Request: 1161 17.044313000 127.0.0.1 127.0.0.1 HTTP 250 POST /myEndpoint HTTP/1.1 (application/json)
Response: 1217 17.066059000 127.0.0.1 127.0.0.1 HTTP 412 HTTP/1.1 200 OK
它工作时我看到我的正常头的响应(例如:接入控制 - *,附注没有缓存,等等)。当它不起作用时,我没有看到我的任何标题,但我看到“Transfer-Encoding:chunked”,我的回应是我的错误消息,但我的回复代码是200.我在服务器之前在发送我的回复,以确保我发送正确的状态,我是。
我没关系与允许分块传输,但我不失去我想要的HTTP响应真的好吗。
虽然我觉得你的问题有趣,我想帮助你,我不得不提到它不是[SSCCE(http://meta.stackexchange.com/questions/22754/sscce-how-to-provide -examples换编程-问题)。所以我不能将它复制并粘贴到我的IDE中尝试一些东西。我敢打赌,如果你以某种方式改变这一点,你会得到一个答案。 – cheffe 2014-10-09 09:43:32
不幸的是,这将是很多代码。我收到了更多信息,并会进行更新以查看是否有帮助。 – BabelFish 2014-10-10 03:17:04