我想在获取 Json 响应之前测试需要身份验证的 Rest API。例如。如果我想访问rest API: http://192.168.xx.xx:9000/dashboards/all/list/m1/p1/sch1
然后
如果我还没有登录,那么这会将我重定向到登录 HTML 页面,登录后,这将显示 Json 输出。
现在我想用 java 写一个放心的代码:我不知道,这是否可以使用这个来登录。
所以我为此写了一个简单的代码::
public class TestNGSimpleTest1 {
@Test
public void testAdd() {
//expect().
//statusCode(400).
//body("Status", equalTo("Success")).
//when().
//get("http://localhost:9000/dashboards/all/list/m1/p1/sch1");
//System.out.println("Response received is ::" +res);
Response res = get("http://localhost:9000/dashboards/all/list/m1/p1/sch1");
assertEquals(200,res.getStatusCode());
String json = res.asString();
System.out.println("Response received is :: " +json);
}
所以这里我没有得到 Json 响应,而是得到 HTML 源页面响应。
所以,我的问题是如果可能的话,如何登录并获得 Json 响应。
提前致谢。