REST Assured 19 - Default Host And Port

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;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值