2017-02-22 50 views
3

我需要自动化其余的API。该API使用Spring安全保护。如何使用放心的请求设置cookie?

下面是代码来验证:

Response response = given().auth() 
        .form(userName, password, FormAuthConfig.springSecurity().withLoggingEnabled(new LogConfig(captor, true))) 
        .post("/home/xyz.html"); 

      Assert.assertTrue("Error occurs", response.statusCode() == 302); 

      if (response.statusCode() == 302) { 
       Cookie cookie = response.getDetailedCookie("JSESSIONID"); 
       result.actualFieldValue = "User Authenticated: Session ID ->" + cookie.getValue(); 
       System.out.println("Cookie set : "+cookie.getValue()); 
       apiTestSessionID = cookie.getValue(); 
      } 

用户登录并返回302种状态,意味着redirection.I发现的cookie,并在一些全局变量设置。现在

,我设置与请求的cookie:

RequestSpecification reqSpecification = new RequestSpecBuilder().addCookie("JSESSIONID", AbstractBaseClass.apiTestSessionID).build(); 

      Map<String, String> parameters = new HashMap<String, String>(); 
      parameters.put("cstmrID", "000N0961"); 
      parameters.put("pageNumber", "1"); 
      parameters.put("pageSize", "10"); 
      parameters.put("sortColumnName", "FIELD_NM"); 
      parameters.put("sortDir", "asc"); 
      parameters.put("filterColumnName1", ""); 
      parameters.put("filterColumnName2", "USER_UPDT_EMAIL_ID"); 
      parameters.put("filterValue2", ""); 

      reqSpecification.queryParams(parameters); 

      Response response = given().spec(reqSpecification).when().get("/service/customerConfig").thenReturn(); 
      System.out.println(response.asString()); 

但在回答我得到登录页面HTML。我无法理解我做错了什么。

假设:

  1. 由于与POST请求,返回302,我需要重定向到下一个URL,之后执行获取与cookie请求。
  2. 这是用请求设置cookie的正确方法。
  3. 我是否需要为请求设置标题。如果是,那么以下是标题信息。我是否需要设置所有这些?

GET /example.com/abc.html HTTP/1.1主机:example.com连接: 保活缓存控制:最大年龄= 0升级不安全-要求:1 用户 - 代理:Mozilla/5.0(Windows NT 6.1; WOW64)AppleWebKit/537.36 (KHTML,如Gecko)Chrome/55.0.2883.87 Safari/537.36接受: text/html,application/xhtml + xml,application/xml; q = 0.9 ,image/webp,/; q = 0.8 Accept-Encoding:gzip,deflate,sdch Accept-Language:zh-CN,en; q = 0.8 Cookie:JSESSIONID = C70A69F1C60D93DC3F8AC564BDE3F4DE.lon2mcaqaapp002; __utma = 185291189.2055499590.1460104969.1460104969.1460618428.2

回答

2

我是新来的放心过,但我刚刚写了类似的试验。

我建议你写一个私人authenticate()方法:

private static String authenticate() { 
    given() 
     .auth() 
     .form(userName, password,FormAuthConfig.springSecurity().withLoggingEnabled(new LogConfig(captor, true))) 
    when() 
     .post("/home/xyz.html"). 
    thenReturn() 
     .getDetailedCookie("JSESSIONID"); 
} 

,然后用在请求中的cookie:

given() 
    .cookie(authenticate()) 
when() 
    .get("/service/customerConfig"). 
thenReturn(); 

但我不知道你是怎么检查statusCode这里。

使用.log().all()查看日志也是一个不错的主意。

0
import io.restassured.RestAssured; 
import io.restassured.http.ContentType; 
import io.restassured.http.Cookies; 

private Cookie cookie; 

@BeforeClass 
public void exampleOfLogin() { 
    String body = String.format("//json"); 
    cookies = RestAssured.given() 
      .contentType(ContentType.JSON) 
      .when() 
      .body(body) 
      .post("www.test_test.com") 
      .then() 
      .statusCode(200) 
      .extract() 
      .response() 
      .getDetailedCookies(); 
} 

@Test 
public void performActionsBasedOnCookies() { 
//set cookies before making a post request and check the returned status code 
    RestAssured.given() 
      .cookies(cookies) 
      .contentType(ContentType.JSON) 
      .when() 
      .post("www.test_url.com") 
      .then() 
      .statusCode(200); 
} 
相关问题