问题背景
这段时间使用开发一些rest api相关的功能,准备做一些接口的简单测试,快速的验证一下api功能是否正常,正好觉得intellij idea中的http client功能非常方便,它允许我们直接在编辑器中操作,正好记录一下。
解决方案
1、创建http请求文件
在idea工具的tools
菜单中,选择http client
,在里面选择创建一个测试请求,或者你创建一个.http
或.rest
文件,通常在项目的src
目录中,例如src/test/http/
。你可以右键点击该目录,选择new
-> file
,然后输入文件名如api_requests.http
。
2、编写http请求
在创建的文件中,可以编写http请求。以下是几个基本请求的例子:
get请求
向http://localhost:8080/api/users
发送一个get请求,并期望json格式的响应。
get http://localhost:8080/api/users accept: application/json
post请求
向http://localhost:8080/api/users
发送一个post请求,并期望json格式的响应。
post http://localhost:8080/api/users content-type: application/json accept: application/json { "name": "john doe", "email": "johndoe@example.com" }
put请求
向http://localhost:8080/api/users/1
发送一个put请求,用来更新id为1的用户信息。
put http://localhost:8080/api/users/1 content-type: application/json accept: application/json { "name": "jane doe", "email": "janedoe@example.com" }
delete请求
向http://localhost:8080/api/users/1
发送一个put请求,用来更新id为1的用户信息。
delete http://localhost:8080/api/users/1
请求写好了之后,就是验证结果对不对问题,我们可以在控制台查看结果是否正确,只是几个接口,我们可以自己看一看,但是如果是几十个接口做测试,这再一个一个的去看,这就要了老命了,那么是不是还可以通过代码自动校验结果呢?
3、执行和验证请求
编写好请求后,你可以通过点击请求行旁边的运行图标(绿色的三角形)来执行它。执行后,idea会在下方的run窗口中显示http响应。
为了验证返回结果是否正确,你可以在http请求下方写上一些验证条件:
get http://localhost:8080/api/users accept: application/json > {% client.test("request executed successfully", function() { client.assert(response.status === 200, "response status is not 200"); }); client.test("response contains users", function() { var jsondata = json.parse(response.body); client.assert(jsondata.length > 0, "response does not contain a list of users"); }); %}
在上面的例子中,我们不仅发送了get请求,还定义了一些测试来验证请求是否成功执行,以及响应体是否包含用户数据。
接下来我们列举一些常见的方法和示例:
1、校验响应状态码:使用response.status
来验证http响应的状态码是否符合预期值。
get http://localhost:8080/api/users accept: application/json > {% client.test("status code is 200", function() { client.assert(response.status === 200, "expected status code 200, but got " + response.status); }); %}
2、校验响应正文内容:使用json.parse(response.body)
来解析响应正文中的json,然后对其进行各种校验。
get http://localhost:8080/api/users/1 accept: application/json > {% client.test("user name is john", function() { var responsebody = json.parse(response.body); client.assert(responsebody.name === "john", "expected name to be john"); }); %}
3、检查响应头:使用response.headers
来验证响应头中的特定值。
get http://localhost:8080/api/users accept: application/json > {% client.test("content-type is set to application/json", function() { var contenttype = response.headers['content-type'] || response.headers['content-type']; client.assert(contenttype.includes('application/json'), "expected 'content-type' header to be 'application/json'"); }); %}
4、校验响应时间:使用response.timings
来测量请求的响应时间,并确保响应足够快。
get http://localhost:8080/api/users accept: application/json > {% client.test("response time is under 500ms", function() { var timetaken = response.timings.response; client.assert(timetaken < 500, "expected response time to be under 500ms"); }); %}
5、检查响应是否包含某字符串:验证响应正文中是否包含某个特定的字符串。
get http://localhost:8080/api/users accept: application/json > {% client.test("body contains 'john'", function() { client.assert(response.body.indexof('john') !== -1, "response body does not contain 'john'"); }); %}
6、检查数组或对象长度:验证jsonobject或jsonarray的长度与预期是否一致。
get http://localhost:8080/api/users accept: application/json > {% client.test("user list is not empty", function() { var responsebody = json.parse(response.body); client.assert(responsebody.length > 0, "user list should not be empty"); }); %}
最后我们使用一个完整的示例,说明一下在使用intellij idea的http client时,如何验证响应正文中的json数据?
这时候通常涉及以下几个步骤:
- 发送http请求。
- 接收响应正文。
- 将响应正文中的json字符串转换为javascript对象。
- 对转换后的对象进行断言测试以验证数据。
get http://localhost:8080/api/users/1 accept: application/json > {% client.test("validate user data", function() { var responsejson = json.parse(response.body); // 验证状态码是200 client.assert(response.status === 200, "expected 200 ok response"); // 验证某个具体的属性值 client.assert(responsejson.id === 1, "expected user id to be 1"); // 验证返回的json对象包含必要的属性 client.assert("name" in responsejson, "response json should include 'name' property"); client.assert("email" in responsejson, "response json should include 'email' property"); // 验证属性值满足某种条件 client.assert(responsejson.name.length > 0, "user name should not be empty"); // 验证邮箱格式(这里采用简单的正则表达式,实际情况可能需要更严格的验证) client.assert(/^[^@]+@[^@]+.[^@]+$/i.test(responsejson.email), "email format is invalid"); // 验证数字类型的属性 client.assert(typeof responsejson.age === 'number', "age should be a number"); // 验证布尔类型的属性 client.assert(typeof responsejson.isactive === 'boolean', "isactive should be a boolean"); // 验证返回的json数组 client.assert(array.isarray(responsejson.tags), "tags should be an array"); client.assert(responsejson.tags.includes("developer"), "tags should include 'developer'"); // 验证嵌套的json对象 client.assert(responsejson.address.city === "new york", "user should be from new york"); }); %}
在这个例子中,这段http client脚本在intellij idea中执行以下操作:
- 发送一个
get
请求到urlhttp://localhost:8080/api/users/1
,期望获取application/json
格式的响应。 - 使用
client.test()
定义了一个测试用例,名称为“validate user data”。 - 将响应正文的内容解析成json对象,赋值给变量
responsejson
。 - 验证http响应状态码是否是200。验证解析出的json对象中
id
属性值是否为1。 - 检查json对象是否包含
name
和email
属性。验证name
属性的值是否非空。 - 使用正则表达式检测
email
属性的格式是否符合简单的电子邮件格式。 - 确保
age
属性的类型是一个数字。确保isactive
属性的类型是布尔值。 - 验证
tags
是一个数组,并且包含字符串"developer"。 - 验证嵌套的json对象中
address
对象的city
属性的值是否是"new york"。
到此这篇关于使用intellij idea的http client进行接口验证的文章就介绍到这了,更多相关idea http client接口验证内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论