REST Assured 系列汇总 之 REST Assured 19 - Default Host And Port
我们本地或服务器上安装Jenkins,访问Jenkins是通过某个特殊的端口号。所以,有一些service运行在特殊的端口上,我们需要在访问这些端口,URL就得拼接上对应的端口号,规则就是BaseURL上追加:port
例如: http://localhost:8080/
让我们先看看下面的request的输出:
import io.restassured.RestAssured;
public class DefaultHostAndPortExample {
public static void main(String[] args){
RestAssured.given()
.log()
.all()
.when()
.get();
}
}
输出:
Request method: GET
Request URI: http://localhost:8080/
Proxy: <none>
Request params: <none>
Query params: <none>
Form params: <none>
Path params: <none>
Headers: Accept=*/*
Cookies: <none>
Multiparts: <none>
Body: <none>
Process finished with exit code 0
可以看出REST Assured发起一个request时如果不提供任何host和port,默认的host是localhost, 默认的端口号是8080.
我们总是能覆盖默认的配置,传入想要的URL。前面我们有设置一个BaseURI ,设置port也是相似的。
RequestSpecification 接口有一个 port() 方法,我们可以通过这个方法设置希望的端口号。
// Using BDD
RestAssured
.given()
.baseUri("https://restful-booker.herokuapp.com")
.basePath("/ping")
.port(8181)
.when()
.get();
// None BDD
// Creating request specification using given()
RequestSpecification request1= RestAssured.given();
// Setting Base URI
request1.baseUri("https://restful-booker.herokuapp.com");
// Setting Base Path
request1.basePath("/ping");
request1.port(8181);
// Using RequestSpecBuilder
RequestSpecification req2= new RequestSpecBuilder()
.setBaseUri("https://restful-booker.herokuapp.com")
.setBasePath("/ping")
.setPort(8181)
.build();
还可以使用RestAssured 类的静态属性port,这种方式用来设置所有request的port。
// Using RestAssured static property
RestAssured.port = 9191;