前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >IDEA中的HTTP Client Editor测试API

IDEA中的HTTP Client Editor测试API

作者头像
wuweixiang
发布2018-08-14 10:22:34
1.5K0
发布2018-08-14 10:22:34
举报
文章被收录于专栏:吴伟祥吴伟祥

在前后端分离项目,前后端通过api进行通信。如果用postman免费版进行api测试的话,由于无法保存测试脚本到文件,不方便前端查看。

你可以选择付费版。也可以利用IDEA自带的HTTP Client Editor编写测试脚本。这里写个demo主要是方便查询语法。

HTTP请求

代码语言:javascript
复制
### Get request with a header
GET https://httpbin.org/ip
Accept: application/json

### Get request with parameter
GET https://httpbin.org/get?show_env=1
Accept: application/json

### Get request with environment variables
GET {{host}}/get?show_env={{show_env}}
Accept: application/json

###

POST请求

代码语言:javascript
复制
### Send POST request with json body
POST https://httpbin.org/post
Content-Type: application/json

{
  "id": 999,
  "value": "content"
}

### Send POST request with body as parameters
POST https://httpbin.org/post
Content-Type: application/x-www-form-urlencoded

id=999&value=content

### Send a form with the text and file fields
POST https://httpbin.org/post
Content-Type: multipart/form-data; boundary=WebAppBoundary

--WebAppBoundary
Content-Disposition: form-data; name="element-name"
Content-Type: text/plain

Name
--WebAppBoundary
Content-Disposition: form-data; name="data"; filename="data.json"
Content-Type: application/json

< ./request-form-data.json
--WebAppBoundary--

###

认证请求

代码语言:javascript
复制
### Basic authorization.
GET https://httpbin.org/basic-auth/user/passwd
Authorization: Basic user passwd

### Basic authorization with variables.
GET https://httpbin.org/basic-auth/user/passwd
Authorization: Basic {{username}} {{password}}

### Digest authorization.
GET https://httpbin.org/digest-auth/realm/user/passwd
Authorization: Digest user passwd

### Digest authorization with variables.
GET https://httpbin.org/digest-auth/realm/user/passwd
Authorization: Digest {{username}} {{password}}

### Authorization by token, part 1. Retrieve and save token.
POST https://httpbin.org/post
Content-Type: application/json

{
  "token": "my-secret-token"
}

> {% client.global.set("auth_token", response.body.json.token); %}

### Authorization by token, part 2. Use token to authorize.
GET https://httpbin.org/headers
Authorization: Bearer {{auth_token}}

###

测试响应

代码语言:javascript
复制
### Successful test: check response status is 200
GET https://httpbin.org/status/200

> {%
client.test("Request executed successfully", function() {
  client.assert(response.status === 200, "Response status is not 200");
});
%}

### Failed test: check response status is 200
GET https://httpbin.org/status/404

> {%
client.test("Request executed successfully", function() {
  client.assert(response.status === 200, "Response status is not 200");
});
%}

### Check response status and content-type
GET https://httpbin.org/get

> {%
client.test("Request executed successfully", function() {
  client.assert(response.status === 200, "Response status is not 200");
});

client.test("Response content-type is json", function() {
  var type = response.contentType.mimeType;
  client.assert(type === "application/json", "Expected 'application/json' but received '" + type + "'");
});
%}

### Check response body
GET https://httpbin.org/get

> {%
client.test("Headers option exists", function() {
  client.assert(response.body.hasOwnProperty("headers"), "Cannot find 'headers' option in response");
});
%}

###

上面这些脚本是很好的demo,涵盖了语法,并且可以直接点击运行。

本文转载自:https://my.oschina.net/hutaishi/blog/1859073

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • HTTP请求
  • POST请求
  • 认证请求
  • 测试响应
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档