1.HTTP请求构造简介
Requests和Rest-Assured提供了很多HTTP请求构造方法。请求构建方法通过传入参数的方式,对发送请求进行定会化的配置,可以用不同的请求参数来应对不同的请求场景。常见的HTTP请求构建分别为GET、POST、PUT、Delete、head、options等。
2.实战演示
(1)发送GET请求
实现的演示代码如下(Python版和Java版)。
Python演示代码
import requests r = requests.get('http://api.GitHub网站/events')
Java演示代码
import static io.restassured.RestAssured.*; public class Requests{ public static void main(String[] args){ given().when(). Get("https://httpbin.ceshiren.com/get"). then().log().all(); } }
(2)发送POST请求
实现的演示代码如下(Python版和Java版)。
Python演示代码
import request r = requests.post('https://httpbin.ceshiren.com/post')
Java演示代码
import static io.restassured.RestAssured.*; public class Requests{ public static void main(String[] args) { given().when(). post("https://httpbin.ceshiren.com/post"). then().log().all(); } }
(3)发送PUT请求
实现的演示代码如下(Python版和Java版)。
Python演示代码
import request r = request.put('http://httpbin.ceshiren.com/put')
Java演示代码
import static io.restassured.RestAssured.*; public class Request{ public static void main (String [] args){ given().when(). put("https://httpbin.ceshiren.com/put"). then().log().all(); } }
(4)发送Delete请求
实现的演示代码如下(Python版和)
import request r = requests.delete('https://httpbin.ceshiren.com/delete')
Java演示代码
import static io.restassured.RestAssured.*; public class Requests{ public static void main (String[] args){ given().when(). Delete("https://httpbin.ceshiren.com/delete"). then().log().all() } }
(5)发送head请求
实现的演示代码如下(Python版和Java版)。
Python演示代码
import static io.restassured.RestAssured.*; public class Requests{ public static viod main(String[] args){ given().when(). head("https://httpbin.ceshiren.com/get"). then().log().all(); } }
(6)发送options请求
实现的演示代码如下(Python版和Java版)。
Python演示代码
import request r = request.optiona('https://httpbin.ceshiren.com/get')
Java演示代码
import static io.restassured.RestAssured.*; public class Requests{ public static void main(String[] args){ given().when(). Options("https://httpbin.ceshiren.com/get"). then().log().all(); } }
Python版本其他请求方式
例如,通过request这个函数发送GET请求,实现代码如下。
import request # 使用 request 函数 request.requet("get","http://www.baidu.com")
3.其他重要请求信息
如果需要对请求做额外的定制化的配置信息,如添加请求头,则需要在请求体添加请求头的配置信息。
(1)定制请求URL参数
实现的演示代码如下(Python版和Java版)。
Python演示代码
通过params参数传入URL参数信息。
import request param = {"school":"hogwarts"} r = request.get("https://httpbin.ceshiren.com/get",params=param)
Java演示代码
通过param方法传入URL参数信息。
import static io.restassured.RestAssured.*; public class Requests{ public static void main (Sting[] args){ given().params("school","hogwarts"). When().get("https://httpbin.ceshiren.com/get"). Then().log().all(); } }
(2)定制请求头信息
实现的演示代码如下(Python版和Java版)。
Python演示代码
通过headers参数传入请求头信息。
url = 'https://api.GitHub网站/some/endpoint' headers = {'user-agent':'hogwarts'} r = request.get(url,headers=headers)
Java演示代码
通过headers方法指定请求头信息。
import static io.restassured.RestAssured.*; public class Requests{ public static void main (String[] args){ given().headers("user-agent","hogwarts"). When().get("https://httbin.ceshiren.com/get"). then().log().all(); } }
(3)重定向配置
在接口自动化测试过程中,被测接口会在某些场景中触发重定向,我们若不想让其触发重定向,需要获取此接口重定向前的内容,实现的演示代码如下(Python版和Java版)。
Python演示代码
通过allow_redirescts参数获取接口的重定向前的内容。被测接口是否触发重定向,由allow_redirects参数决定,参数默认值为True,True为触发,False为不触发。
>>> import requests >>> r = requests.get('http://GitHub网站',allow_redirects-False) >>> r.status_code 301
Java演示代码
Java编码中使用的是config方法提供的redirect方法,实现就是,通过传入配置信息来控制接口是否触发重定向:redirectConfig().floolwRedirects(ture)是触发,redirectConfig().followRedirects(false)是不触发。
import io.restassured.RestAssured; import static io.restassured.RestAssured.*; import static io.restassured.config.RedirectConfig.redirectConfig; public class Requests{ public static void main(String[] args){ given().config(RestAssured.config(). //不触发 redirect(redirectConfig().followRedirects(false))). when().get("http://GitHub网站").then().log().all(); } }