REST Assured 62 - How To Use Path Or URL Parameters In Rest Assured

REST Assured 系列汇总 之 REST Assured 62 - How To Use Path Or URL Parameters In Rest Assured

介绍

我们可以参数化 URL,使得它可变,可读,重复使用。
例如:
https://restful-booker.herokuapp.com/auth
https://restful-booker.herokuapp.com/booking
https://restful-booker.herokuapp.com/ping

上面三个 URLs,“https://restful-booker.herokuapp.com/” 是共同的,只是 base path 不同。我们就可以创建一个参数化的 URL,这样就可以根据需求重用它。

参数化 URL

一个参数就是一个键值对。一个 URL 可以有多个参数,通过多种方式提供其值。例如参数化上面的 URLs:

https://restful-booker.herokuapp.com/{resourcePath}
https://restful-booker.herokuapp.com/{resourcePath}/{bookingID}

我们可以用 { } 参数化一个 path 名,也可以参数化多个。

URL参数化赋值

如果我们参数化了一个 URL,接下来就要负责给参数赋值了。否则就会认为 URL就是原本的那个值。这有多种方式给参数传值。
pathParam() 方法
这个方法接收两个 string 参数。第一个是参数的名字,第二个是参数的值,例如:

@Test
	public void pathVariable1()
	{
    
    
		RestAssured
			.given()
				.log()
				.all()
				.baseUri("https://restful-booker.herokuapp.com/")
				.basePath("{resourcePath}")
			    .pathParam("resourcePath", "booking")
			.when()
				.get()
			.then()
				.log()
				.all();
	}

输出:

Request method:	GET
Request URI:	https://restful-booker.herokuapp.com/booking
Proxy:			<none>
Request params:	<none>
Query params:	<none>
Form params:	<none>
Path params:	resourcePath=booking
Headers:		Accept=*/*
Cookies:		<none>
Multiparts:		<none>
Body:			<none>
HTTP/1.1 200 OK
Server: Cowboy
Connection: keep-alive
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 162
Etag: W/"a2-gzy3QBBwVx4LfnY0IjxFXn1y8jQ"
Date: Sat, 09 Jan 2021 07:24:05 GMT
Via: 1.1 vegur
 
[
    {
        "bookingid": 2
    },
    {
        "bookingid": 10
    },
    {
        "bookingid": 1
    },
    {
        "bookingid": 5
    },
    {
        "bookingid": 7
    },
    {
        "bookingid": 3
    },
    {
        "bookingid": 4
    },
    {
        "bookingid": 8
    },
    {
        "bookingid": 9
    },
    {
        "bookingid": 6
    }
]

上面的参数 “resourcePath” 在运行时被 “booking” 替换了。
当然也 call HTTP 方法时传被参数化的URL,取代用 baseURL 和 basePath,例如:

@Test
	public void pathVariable2()
	{
    
    
		RestAssured
			.given()
				.log()
				.all()
			    .pathParam("resourcePath", "booking")
			.when()
				.get("https://restful-booker.herokuapp.com/{resourcePath}")
			.then()
				.log()
				.all();
	}

inline path parameter 内嵌路径参数

@Test
	public void pathVariable3()
	{
    
    
		RestAssured
			.given()
				.log()
				.all()
			.when()
				.get("https://restful-booker.herokuapp.com/{resourcePath}", "booking")
			.then()
				.log()
				.all();
	}

观察上面的 get() 方法有两个参数。第二个参数 “booking” 是 path 参数 “resourcePath” 的值。

@Test
	public void pathVariable4()
	{
    
    
		RestAssured
			.given()
				.log()
				.all()
			.when()
				.get("https://restful-booker.herokuapp.com/{resourcePath}/{bookingId}", "booking",10)
			.then()
				.log()
				.all();
	}

上面的例子,“booking” 是第一个参数 “resourcePath” 的值,“10” 是第二个参数 “bookingId” 的值。

你也可以混合指定参数名字和不指定参数名字的方式,只是要慎重一点。

@Test
	public void pathVariable4()
	{
    
    
		RestAssured
			.given()
				.log()
				.all()
				.pathParam("resourcePath", "booking")
			.when()
				.get("https://restful-booker.herokuapp.com/{resourcePath}/{bookingId}",10)
			.then()
				.log()
				.all();
	}

注意:参数化 baseURI() 或 RestAssured.baseURI 是不起作用的。

@Test
	public void pathVariable5()
	{
    
    
		RestAssured
			.given()
				.log()
				.all()
				.baseUri("https://restful-booker.herokuapp.com/{resourcePath}")
			    .pathParam("resourcePath", "booking")
			.when()
				.get()
			.then()
				.log()
				.all();
	}

会得到异常
java.net.URISyntaxException: Illegal character in path at index 37: https://restful-booker.herokuapp.com/{resourcePath}

猜你喜欢

转载自blog.csdn.net/wumingxiaoyao/article/details/120601285