und*_*ned 7 java rest testng json rest-assured
我想在获取Json响应之前测试需要身份验证的Rest API.对于exa.如果我想访问rest API: http://192.168.xx.xx:9000/dashboards/all/list/m1/p1/sch1
然后
如果我还没有登录,那么这会将我重定向到Login 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);
}
Run Code Online (Sandbox Code Playgroud)
所以这里得到的是HTML源页面响应,而不是获得Json响应.
所以,我的问题是如果可能的话,如何登录并获得Json响应.
提前致谢.
如果您使用的是JIRA的默认身份验证,则需要先行身份验证。下面是示例代码。
RestAssured.baseURI = System.getProperty("baseurl");
PreemptiveBasicAuthScheme authScheme = new PreemptiveBasicAuthScheme();
authScheme.setUserName("admin");
authScheme.setPassword("admin");
RestAssured.authentication = authScheme;
Run Code Online (Sandbox Code Playgroud)
该示例代码将帮助您在发送每个请求之前进行身份验证。
小智 6
这个方法效果很好。除了提交登录凭证,还同时验证200状态码
given().auth().basic(username, password).when().get("/uri/").then().statusCode(200);
Run Code Online (Sandbox Code Playgroud)
您可能正在寻找表单身份验证:
given().auth().form("username", "password"). ..
Run Code Online (Sandbox Code Playgroud)
(可选)您需要提供 FormAuthConfig 的实例作为第三个参数来描述 HTML 登录页面的外观。
归档时间: |
|
查看次数: |
35474 次 |
最近记录: |