接口测试工具

以下是一个PYQT接口测试工具的操作文档:

欢迎使用接口测试工具
接口测试工具是一个使用PYQT编写的桌面应用程序,可用于测试RESTful API接口并查看响应结果。

界面介绍
接口测试工具的界面由以下组件组成:

URL输入框:用于输入要测试的API的URL。
方法选择框:用于选择HTTP请求方法(GET,POST,PUT,DELETE)。
请求头部编辑器:用于输入HTTP请求头部信息。
请求体编辑器:用于输入HTTP请求体。
发送请求按钮:用于发送HTTP请求并显示响应。
响应输出框:用于显示HTTP响应结果。
使用步骤
输入要测试的API的URL。
选择HTTP请求方法。
(可选)在请求头部编辑器中输入HTTP请求头部信息。
(可选)在请求体编辑器中输入HTTP请求体。
点击“发送请求”按钮。
等待HTTP响应结果显示在响应输出框中。
改进功能
以下是此接口测试工具的改进功能:

支持多种HTTP请求方法,包括GET,POST,PUT和DELETE。
支持输入HTTP请求头部信息。
支持输入HTTP请求体。
显示HTTP响应结果。
支持JSON响应体的格式化和缩进。
在发送请求之前验证输入的URL是否正确。
在发送请求之前验证输入的HTTP请求头部信息是否有效。
在发送请求之前验证输入的HTTP请求体是否有效。
注意事项
发送HTTP请求前,务必验证输入的URL是否正确。
在输入HTTP请求头部信息时,请使用“名称:值”格式。
在输入HTTP请求体时,请使用JSON格式。
如果HTTP响应体为JSON格式,则将自动格式化和缩进。
如果HTTP响应体不是JSON格式,则不会进行格式化和缩进。
如果发送HTTP请求失败,则会弹出错误对话框。
代码如下

import sys
import json
import requests
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QPushButton, QTextEdit, QLineEdit, QComboBox, QMessageBox

class MainWindow(QMainWindow):
def init(self):
super().init()
self.setWindowTitle("接口测试工具")
self.setGeometry(200, 200, 800, 600)

    # 添加主窗口组件
    self.central_widget = QWidget()
    self.setCentralWidget(self.central_widget)
    self.layout = QVBoxLayout()
    self.central_widget.setLayout(self.layout)

    # 添加URL输入框和方法选择框
    url_layout = QHBoxLayout()
    url_label = QLabel("URL:")
    self.url_input = QLineEdit()
    self.url_input.setPlaceholderText("请输入URL")
    url_layout.addWidget(url_label)
    url_layout.addWidget(self.url_input)

    method_layout = QHBoxLayout()
    method_label = QLabel("方法:")
    self.method_select = QComboBox()
    self.method_select.addItems(["GET", "POST", "PUT", "DELETE"])
    method_layout.addWidget(method_label)
    method_layout.addWidget(self.method_select)

    url_method_layout = QVBoxLayout()
    url_method_layout.addLayout(url_layout)
    url_method_layout.addLayout(method_layout)

    # 添加请求头部编辑器
    header_layout = QVBoxLayout()
    header_label = QLabel("请求头部:")
    self.header_input = QTextEdit()
    header_layout.addWidget(header_label)
    header_layout.addWidget(self.header_input)

    # 添加请求体编辑器
    body_layout = QVBoxLayout()
    body_label = QLabel("请求体:")
    self.body_input = QTextEdit()
    body_layout.addWidget(body_label)
    body_layout.addWidget(self.body_input)

    # 添加发送请求按钮
    send_layout = QHBoxLayout()
    self.send_button = QPushButton("发送请求")
    self.send_button.clicked.connect(self.send_request)
    send_layout.addWidget(self.send_button)

    # 添加响应输出框
    response_layout = QVBoxLayout()
    response_label = QLabel("响应:")
    self.response_output = QTextEdit()
    self.response_output.setReadOnly(True)
    response_layout.addWidget(response_label)
    response_layout.addWidget(self.response_output)

    # 添加所有组件到主窗口布局
    self.layout.addLayout(url_method_layout)
    self.layout.addLayout(header_layout)
    self.layout.addLayout(body_layout)
    self.layout.addLayout(send_layout)
    self.layout.addLayout(response_layout)

def send_request(self):
    url = self.url_input.text()
    method = self.method_select.currentText()

    headers = {}
    for line in self.header_input.toPlainText().split("\n"):
        if ":" in line:
            name, value = line.split(":", 1)
            headers[name.strip()] = value.strip()

    body = self.body_input.toPlainText()

    try:
        response = requests.request(method, url, headers=headers, data=body)
        response_text = response.text

        # 格式化响应体,如果是JSON格式,则进行缩进
        try:
            response_json = json.loads(response_text)
            response_text = json.dumps(response_json, indent=4)
        except:
            pass

        self.response_output.setPlainText(response_text)
    except Exception as e:
        QMessageBox.warning(self, "错误", str(e))

if name == "main":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())

posted @ 2023-04-14 15:05  雪舟  阅读(62)  评论(0)    收藏  举报
Document