0

I am trying to test my app using rest assured, I am using spring security This is sthe code

RestAssured.baseURI = "http://myapp-breakid.rhcloud.com/";
        RestAssured.port = 80;
        Response response  = expect().given().auth().form("admin", "sababa1.",springSecurity().withLoggingEnabled(new LogConfig())).
        when().get("login");

but this is the response:

HTTP/1.1 302 Found
Date: Mon, 01 Dec 2014 20:38:57 GMT
Server: Apache-Coyote/1.1
Location: http://myapp-breakid.rhcloud.com/index
Content-Length: 0
Set-Cookie: JSESSIONID=32E5F577A6885826DF17ACEE4B3386AF; Path=/; HttpOnly
Accept-Ranges: none
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: text/plain

What am I missing ?

4

1 回答 1

2

您无需进入登录页面即可登录,REST Assured 会自动执行此操作。这有效:

RestAssured.baseURI = "http://myapp-breakid.rhcloud.com";
RestAssured.authentication = form("admin", "sababa1.", springSecurity());

get("/index").prettyPrint();

这里发生的是表单身份验证在每个请求之前自动执行。如果在多个请求中使用相同的会话很重要,您应该使用会话过滤器。例如:

RestAssured.baseURI = "http://myapp-breakid.rhcloud.com";

SessionFilter sessionFilter = new SessionFilter();

given().
        auth().form("admin", "sababa1.", springSecurity()).
        filter(sessionFilter).
when().
        get("/index");

// Session filter will automatically capture the session id and you can reuse it in subsequent requests.

given().
        filter(sessionFilter).
when().
        get("/another_protected_resource").
then().
        statusCode(200);

您还可以从 SessionFilter 获取 sessionId 并像这样使用它:

given().
        sessionId(sessionFilter.getSessionId()).
when().
        get("/another_protected_resource").
then().
        statusCode(200);
于 2014-12-16T11:45:52.160 回答