1

我有一个工作的 spring-mvc 应用程序,带有休息服务和一些很好的放心测试:

@Test
public void createFoobarFromScratchReturns201(){
    expect().statusCode(201).given()    
    .queryParam("foo", generateFoo())
    .queryParam("bar", generateBar())
    .when().post("/foo/bar/");
}

=> OK

然后我实现了一个摘要认证。一切正常,现在我必须登录才能使用我的服务:

curl http://localhost:8089/foo/bar
=> HTTP ERROR 401,  Full authentication is required to access this resource

curl http://localhost:8089/foo/bar --digest -u user_test:password
=> HTTP 201, CREATED

但是当我尝试用最明显的功能升级我的测试时,我仍然有一个 401 错误:

@Test
public void createFoobarFromScratchReturns201(){
    expect().statusCode(201).given()    
    .auth().digest("user_test", "password") // Digest added here
    .queryParam("foo", generateFoo())
    .queryParam("bar", generateBar())
    .when().post("/foo/bar/");
}

=> Expected status code <201> doesn't match actual status code <401>

我发现了preemptive()函数的一些线索,但它似乎只针对 basic 实现:

// Returns an AuthenticatedScheme and stores it into the general configuration
RestAssured.authentication = preemptive().basic("user_test", "password");

// Try a similar thing, but it didn't work :
RestAssured.authentication = RestAssured.digest("user_test", "password");

目前,我正在努力实现两件事:

  • 我需要升级一些测试以支持摘要
  • 我需要修改我的其余测试套件(与身份验证问题无关)的@Before ,以便已经登录。

任何想法或文件?

4

1 回答 1

1

尝试在 Rest Assured 中嵌入的 HTTP 客户端中启用对 cookie 的支持:

 RestAssuredConfig config = new RestAssuredConfig().httpClient(new HttpClientConfig().setParam(ClientPNames.COOKIE_POLICY, CookiePolicy.BEST_MATCH));

 expect().statusCode(201).given()    
 .auth().digest("user_test", "password") // Digest added here
 .config(config)
 .queryParam("foo", generateFoo())
 .queryParam("bar", generateBar())
 .when().post("/foo/bar/");

HTTP 客户端(以及因此的 Rest Assured)支持摘要式身份验证,并且使用该digest方法配置 RestAssured 效果很好。

于 2014-07-21T13:05:11.037 回答