RestAssured 使用通用代码
RestAssured Using Common Code
所以我是初学者放心
在 JAVA 中,为了便于说明和简化,我缩短了代码。我有一个 class,其中包含以下代码:
public class ApiEndpointHelper {
public static String postIdRequest(String requestBody) {
String jsonBody = FileReader.getFile(requestBody);
Response response = RestAssured.given()
.auth()
.basic("userbob", "bobspassword")
.contentType(ContentType.JSON)
.body(jsonBody)
.when()
.post("http://localhost:8080/report/v1/");
response
.then()
.log().ifError().and()
.statusCode(200);
return response.asString();
}
public static String getId() {
Response response = RestAssured.given()
.auth()
.basic("NEWuserjames", "jamesspassword")
.contentType(ContentType.JSON)
.when()
.get("http://localhost:3099/v1/");
response
.then()
.log().ifError().and()
.statusCode(200);
return response.asString();
}
}
然后另一个 class 有:
public class BasicTest extends ApiEndpointHelper {
@Test
public void Query_endpoint() {
String ActualResponse = ApiEndpointHelper.getId();
assertJsonEquals(resource("responseExpected.json"),
ActualResponse, when(IGNORING_ARRAY_ORDER));
}
}
我的问题是:
我如何使用代码,以便可以在某处声明常见的 body 项目,例如 auth headers、内容类型、post URL 等然后所有请求都能得到它们?两者都使用相同的身份验证 header 但密码不同。有什么聪明的方法可以做到这一点?!
请参阅方法:'postIdRequest' 和 'getId'。我想我可以使用 RequestSpecification 但不确定如何使用!
有人可以举例说明,最好使用当前上下文。
将常用代码提取到带参数的方法中:
public class ApiEndpointHelper {
private RequestSpecification givenAuthJson() {
return RestAssured.given()
.auth()
.basic("userbob", "bobspassword")
.contentType(ContentType.JSON)
}
public static String getId() {
Response response = givenAuthJson()
.when()
.get("http://localhost:3099/v1/");
}
}
如有需要可传参进行鉴权
同样,您可以将 URL 构造提取到方法中。这都是基本的编程,所以除非你有具体的问题,否则这个问题对于 Whosebug 来说可能太宽泛了。
所以我是初学者放心
在 JAVA 中,为了便于说明和简化,我缩短了代码。我有一个 class,其中包含以下代码:
public class ApiEndpointHelper {
public static String postIdRequest(String requestBody) {
String jsonBody = FileReader.getFile(requestBody);
Response response = RestAssured.given()
.auth()
.basic("userbob", "bobspassword")
.contentType(ContentType.JSON)
.body(jsonBody)
.when()
.post("http://localhost:8080/report/v1/");
response
.then()
.log().ifError().and()
.statusCode(200);
return response.asString();
}
public static String getId() {
Response response = RestAssured.given()
.auth()
.basic("NEWuserjames", "jamesspassword")
.contentType(ContentType.JSON)
.when()
.get("http://localhost:3099/v1/");
response
.then()
.log().ifError().and()
.statusCode(200);
return response.asString();
}
}
然后另一个 class 有:
public class BasicTest extends ApiEndpointHelper {
@Test
public void Query_endpoint() {
String ActualResponse = ApiEndpointHelper.getId();
assertJsonEquals(resource("responseExpected.json"),
ActualResponse, when(IGNORING_ARRAY_ORDER));
}
}
我的问题是:
我如何使用代码,以便可以在某处声明常见的 body 项目,例如 auth headers、内容类型、post URL 等然后所有请求都能得到它们?两者都使用相同的身份验证 header 但密码不同。有什么聪明的方法可以做到这一点?! 请参阅方法:'postIdRequest' 和 'getId'。我想我可以使用 RequestSpecification 但不确定如何使用! 有人可以举例说明,最好使用当前上下文。
将常用代码提取到带参数的方法中:
public class ApiEndpointHelper {
private RequestSpecification givenAuthJson() {
return RestAssured.given()
.auth()
.basic("userbob", "bobspassword")
.contentType(ContentType.JSON)
}
public static String getId() {
Response response = givenAuthJson()
.when()
.get("http://localhost:3099/v1/");
}
}
如有需要可传参进行鉴权
同样,您可以将 URL 构造提取到方法中。这都是基本的编程,所以除非你有具体的问题,否则这个问题对于 Whosebug 来说可能太宽泛了。