【发布时间】:2017-02-22 11:09:52
【问题描述】:
我需要自动化其余的 API。 API 受 Spring 安全性保护。
下面是验证代码:
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状态,表示重定向。我找到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。我无法理解我在哪里做错了。
假设:
- 由于 post 请求返回 302,我是否需要重定向到下一个 url,然后使用 cookie 执行 get 请求。
- 这是用请求设置 cookie 的正确方法吗?
- 我需要设置请求的标头吗?如果是,则以下是标题信息。我需要全部设置吗?
GET /example.com/abc.html HTTP/1.1 主机:example.com 连接: 保持活动缓存控制:max-age=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: en-US,en;q=0.8 Cookie:JSESSIONID=C70A69F1C60D93DC3F8AC564BDE3F4DE.lon2mcaqaapp002; __utma=185291189.2055499590.1460104969.1460104969.1460618428.2
【问题讨论】:
标签: cookies http-post httpclient http-get rest-assured