Restlet客户端资源“未修改"状况 [英] Restlet client resource "not modified" condition

查看:54
本文介绍了Restlet客户端资源“未修改"状况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于 Restlet 的应用程序,我正在尝试使用 Restlet 客户端资源来测试它的某些部分.

I've got a Restlet based application, and I'm trying to use Restlet client resources to test certain parts of it.

自从从 Restlet 2.2.3 升级到 2.3.4 后,我的 ETag 验证测试开始失败.这是我在旧版本中添加标题的方式:

Since upgrading from Restlet 2.2.3 to 2.3.4, my ETag verification tests have started failing. Here's how I was adding the header in the old version:

Series<Header> headers = (Series<Header>) currentClientResource.getRequest().getAttributes().get("org.restlet.http.headers");
if (headers == null) {
    headers = new Series<Header>(Header.class);
}
headers.add("If-None-Match", "\"" + eTag + "\"");
currentClientResource.getRequestAttributes().put("org.restlet.http.headers", headers);

然后在包装好的 clientResource 上再次调用represent() 时,我得到了 304 Not Modified 响应(这正是我想要的).

Then when calling represent() again on the wrapped clientResource I was getting a 304 Not Modified response (which is what I want).

在 2.3.4 中,这开始返回 200 OK,我注意到一条关于未直接设置 If-None-Match 标头的日志消息.相反,我现在正在尝试:

In 2.3.4 this started returning a 200 OK instead, and I noticed a log message about not setting the If-None-Match header directly. Instead I'm now trying this:

currentClientResource.getRequest().getConditions().getNoneMatch().add(new Tag(eTag));

然而,这仍然给了我 200 OK.如果我通过 REST 客户端手动执行请求,我会得到 304 Not Modified,所以服务器仍然在做正确的行为.我需要在测试中做什么才能看到我想看到的内容?

However this is still giving me a 200 OK. If I do the request manually through a REST client I can get a 304 Not Modified, so the server is still doing the right behavior. What do I need to do in the tests to see what I want to see?

推荐答案

我试了一下,Restlet 2.3.4 版对我有用.

I made a try and it works for me with version 2.3.4 of Restlet.

这是我所做的:

  • 用于我的测试的 Maven 依赖项

<project>
  <modelVersion>4.0.0</modelVersion>
  (...)

  <properties>
    <java-version>1.7</java-version>
    <restlet-version>2.3.4</restlet-version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.restlet.jse</groupId>
      <artifactId>org.restlet</artifactId>
      <version>${restlet-version}</version>
    </dependency>

    <dependency>
      <groupId>org.restlet.jse</groupId>
      <artifactId>org.restlet.ext.jetty</artifactId>
      <version>${restlet-version}</version>
    </dependency>

    <dependency>
      <groupId>org.restlet.jse</groupId>
      <artifactId>org.restlet.ext.jackson</artifactId>
      <version>${restlet-version}</version>
    </dependency>

    <dependency>
      <groupId>org.restlet.jse</groupId>
      <artifactId>org.restlet.ext.crypto</artifactId>
      <version>${restlet-version}</version>
    </dependency>
  </dependencies>

  <repositories>
    <repository>
      <id>maven-restlet</id>
      <name>Public online Restlet repository</name>
      <url>http://maven.restlet.com</url>
    </repository>
  </repositories>
</project>

  • 一个服务器资源,用于在返回的表示上设置 etag:

  • A server resource that sets the etag on the returned representation:

    public class ETagServerResource extends ServerResource {
      @Get
      public Representation test() {
        String test = "test";
        String md5 = DigestUtils.toMd5(test);
    
        StringRepresentation repr = new StringRepresentation(test);
        repr.setTag(new Tag(md5));
        return repr;
      }
    }
    

  • 客户端进行两次调用:第一个没有 etag,第二个带有应该返回 304 状态代码的 etag.

  • The client that makes two calls: a first one without the etag and a second one with the etag that should return a 304 status code.

    // First call
    ClientResource cr
         = new ClientResource("http://localhost:8182/test");
    Representation repr = cr.get();
    Tag tag = repr.getTag();
    System.out.println(">> cr = "+cr); // Status code: 200
    
    // Second call
    cr.getRequest().getConditions().getNoneMatch().add(tag);
    cr.get();
    System.out.println(">> cr = "+cr); // Status code: 304
    

  • 我不知道你在服务器资源中使用了什么.随意告诉我.

    I don't know what you use within the server resource. Feel free to tell me.

    希望对你有帮助蒂埃里

    这篇关于Restlet客户端资源“未修改"状况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆