【问题标题】:Apache Wink Client - Test a REST service using form authApache Wink 客户端 - 使用表单身份验证测试 REST 服务
【发布时间】:2013-03-01 18:05:22
【问题描述】:

我正在尝试使用 Wink RestClient 对 Rest 服务端点进行功能测试。我使用模拟进行单元测试,但我想在功能上测试它作为端点消费者。

我知道有些人会反对我在使用基于表单的身份验证时将其称为 REST 端点,但这是我目前的架构。

我要测试的大部分资源都是受保护的资源,并且应用程序(在 Tomcat6 上运行)受表单身份验证的保护。 (如在下面的 web.xml sn-p 中)。

到目前为止,我尝试的是对未受保护的资源进行初始调用,以获取包含 JSESSIONID 的 set-cookie 标头,并在随后的标头中使用该 JSESSIONID(通过 Resource.cookie() )请求,但没有结果。

web.xml

<login-config>
    <auth-method>FORM</auth-method>
    <form-login-config>
        <form-login-page>/login.html</form-login-page>
        <form-error-page>/login.html?failure=true</form-error-page>
    </form-login-config>
</login-config>

我的 Wink RestClient 代码如下所示。所有响应都是 200,但我注意到两件事是对 /j_security_check/ 的调用的响应不包括 jsessionid cookie,对受保护资源的调用说我登录失败。调用 j_security_check 的负载是直接从之前拦截的成功浏览器请求中捕获的。

ClientConfig config = new ClientConfig();
config.setBypassHostnameVerification(true);
RestClient restClient = new RestClient(config);

Resource unprotectedResource = restClient.resource( BASE_URL + "/");
unprotectedResource.header( "Accept", "*/*" );
ClientResponse clientResponse = unprotectedResource.get();
String response = clientResponse.getEntity(String.class);

// get jSession ID
String jSessionId = clientResponse.getHeaders().get("set-cookie").get(0);
jSessionId = jSessionId.split(";")[0];
System.out.println(jSessionId);

// create a request to login via j_security_check
Resource loginResource = restClient.resource(BASE_URL + "/j_security_check/");
loginResource.accept("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
loginResource.header("referer", "http://localhost:8080/contextroot/");
loginResource.cookie( jSessionId );
loginResource.header("Connection", "keep-alive");
loginResource.header("Content-Type", "application/x-www-form-urlencoded");
loginResource.header("Content-Length", "41");

ClientResponse loginResponse = loginResource.post("j_username=*****&j_password=*************");

/*  the loginResponse, as this point, does not have the jsessionid cookie, my browser client does */

Resource protectedResource = restClient.resource(BASE_URL + "/protected/test/");
systemResource.accept("application/json");
systemResource.cookie( jSessionId );

ClientResponse systemResponse = systemResource.get();
response = clientResponse.getEntity(String.class);
System.out.println(response);

如果您有任何使用 Wink RestClient 来使用受表单身份验证保护的资源的想法或经验,我们将不胜感激。我想我会接受其他框架,我听说过 REST-Assured 和其他框架,但是由于应用程序使用 Wink 并且 RestClient 似乎为我提供了我需要的东西,所以我想我会坚持下去。

【问题讨论】:

  • 原来 j_security_check 以 [#302 临时移动] 响应 - 当我使用嗅探器拦截请求并将 JSESSIONID cookie 附加到该重定向请求时,一切都很好。问题是我似乎无法从 wink RestClient 对象(或资源或响应)对象中得到。重定向发生在幕后。任何人都知道如何指示 Resource.post() 方法停止重定向,或指示 ClientResponse 捕获重定向?我还看不到方法。

标签: rest jax-rs junit4 functional-testing apache-wink


【解决方案1】:

发现问题,解决办法

j_security_check 使用 #302/redirect 响应我的 POST 请求(进行身份验证)。紧随其后的是 wink RestClient,但我的 JSESSIONID cookie 没有附加到它上面。这导致响应(来自重定向的 URL)包含一个带有新标头的 set-cookie 标头。我随后的调用(我从第一次调用中插入了 JSESSIONID)失败了,因为该 cookie 已过期。我需要做的就是指示 RestClient 不要遵循重定向。如果重定向是必要的,我会自己构建它,包含适当的 cookie。

Chromium 和 Firefox 将 cookie 从原始请求传送到重定向请求,所以一切都很好。

这是一些对我有用的代码,使用来自 Apache Wink 项目的 JUnit4、RestClient(和 Jackson ObjectMapper)

@Test
public void testGenerateZipEntryName() throws JsonGenerationException, JsonMappingException, IOException
{
    final ObjectMapper mapper = new ObjectMapper();

    final String BASE_URL = "http://localhost:8080/rest";

    // Configure the Rest client
    ClientConfig config = new ClientConfig();
    config.proxyHost("localhost");    // helpful when sniffing traffic
    config.proxyPort(50080);          // helpful when sniffing traffic
    config.followRedirects(false);    // This is KEY for form auth
    RestClient restClient = new RestClient(config);


    // Get an unprotected resource -- to get a JSESSIONID
    Resource resource = restClient.resource( BASE_URL + "/");
    resource.header( "Accept", "*/*" );
    ClientResponse response = resource.get();
    // extract the jSession ID, in a brittle and ugly way
    String jSessId = response.getHeaders().get("set-cookie").get(0).split(";")[0].split("=")[1];


    // Get the login resource *j_security_check*
    resource = restClient.resource(BASE_URL + "/j_security_check");
    resource.cookie("j_username_tmp=admin; j_password_tmp=; JSESSIONID=" + jSessId);
    resource.header("Content-Type", "application/x-www-form-urlencoded");
    resource.header("Content-Length", "41");

    // Verify that login resource redirects us
    response = resource.post("j_username=admin&j_password=***********");
    assertTrue( response.getStatusCode() == 302 );


    // Grab a public resource
    resource = restClient.resource(BASE_URL + "/");
    resource.cookie("j_username_tmp=admin; j_password_tmp=; JSESSIONID=" + jSessId);
    response = resource.get();
    // verify status of response
    assertTrue( response.getStatusCode() == 200 );


    // Grab a protected resource
    resource = restClient.resource(BASE_URL + "/rest/system");
    resource.cookie("j_username_tmp=admin; j_password_tmp=; JSESSIONID=" + jSessId);

    // Verify resource returned OK
    response = resource.contentType("application/json").accept("*/*").get();
    assertTrue( response.getStatusCode() == 200 );

    // Deserialize body of protected response into domain object for further testing 
    MyObj myObj = mapper.readValue(response.getEntity(String.class), MyObj.class );
    assertTrue( myObj.customerArchived() == false );
}

【讨论】:

  • 鉴于缺乏好的 Apache Wink 客户端示例,您的帮助最大。
猜你喜欢
  • 1970-01-01
  • 2019-07-30
  • 1970-01-01
  • 2016-11-03
  • 2012-08-28
  • 2016-01-31
  • 1970-01-01
  • 2011-11-04
  • 2013-06-18
相关资源
最近更新 更多