idea测试rest api方法(HTTP client in IntelliJ IDEA code editor)

最近更新了IDEA之后,发现了个新功能,可以用来测试rest api接口。之前写好的接口测试一直使用的Postman,发现在这个功能之后就把Postman完全抛弃了。

这是IDEA新版本的功能介绍HTTP client in IntelliJ IDEA code editor,链接先附上。

简单介绍

当你想测试你的rest api接口的时候,可以在你的IDEA当中直接创建、编辑、执行你的HTTP请求。这是IDEA官方帮助文档上找下的GIF。

 

 IDEA使用快捷键Ctrl+Shift+Alt+Insert键可以选择建立一个HTTP请求文件,IDEA会自动把文件建立在Scratch文件夹下,当HTTP请求执行完成后,返回信息会以文件的形式保存在IDEA的顶部的历史文件中。

语法

Method Request-URI HTTP-Version
Header-field: Header-value

Request-Body

书写格式

  • 请求文件中可以使用注释,使用// 或者 #
// A basic request
GET http://example.com/a/
  • 请求方法可以不写,默认使用为get
  • 一个请求文件中可以多个请求,使用 ### 隔开
// A basic request
http://example.com/a/

###

// A second request using the GET method
http://example.com:8080/api/html/get?id=123&value=content
  • 请求过长可以断行,但要注意使用换行符
// Using line breaks with indent
GET http://example.com:8080
    /api
    /html
    /get
    ?id=123
    &value=content
  • 请求时可以使用身份认证,携带用户名和密码
// Basic authentication
GET http://example.com
Authorization: Basic username password

###

// Digest authentication
GET http://example.com
Authorization: Digest username password
  • POST请求时可以构建request的body信息
POST http://example.com:8080/api/html/post HTTP/1.1
Content-Type: application/json Cookie: key=first-value

{ "key" : "value", "list": [1, 2, 3] }
  • 可以实现文件的上传
POST http://example.com/api/upload HTTP/1.1
Content-Type: multipart/form-data; boundary=boundary

--boundary
Content-Disposition: form-data; name="first"; filename="input.txt"

// The 'input.txt' file will be uploaded
< ./input.txt

--boundary
Content-Disposition: form-data; name="second"; filename="input-second.txt"

// A temporary 'input-second.txt' file with the 'Text' content will be created and uploaded
Text
--boundary
Content-Disposition: form-data; name="third";

// The 'input.txt' file contents will be sent as plain text.
< ./input.txt --boundary--

IDEA给出的例子

get requests

### 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

### Get request with disabled redirects
# @no-redirect
GET http://httpbin.org/status/301

###

auth resquests

### 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}}

###

post requests

### 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--

###

test responses

### 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");
});
%}

###

 

你可能感兴趣的:(测试工具)