# restApi-test-tool **Repository Path**: clint315/rest-api-test-tool ## Basic Information - **Project Name**: restApi-test-tool - **Description**: 本地的restApi测试工具 - **Primary Language**: Unknown - **License**: GPL-3.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-11-11 - **Last Updated**: 2025-11-11 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # REST API 测试站 (Go + Docker) 一个用于测试 REST API 的轻量网站,内置 httpbin 风格端点,并提供简单 Web 前端和代理功能,方便在浏览器里构造请求并查看响应。 ## 功能 - /echo 回显请求方法、URL、头、Body - /headers 返回请求头 - /json 返回示例 JSON - /uuid 生成 UUID - /cookies 和 /cookies/set?key=value 查看/设置 Cookie - /status/{code} 返回指定 HTTP 状态码 - /delay/{sec} 延迟响应(秒) - /redirect/{n} 多次 302 重定向 - /auth/basic 需要 Basic Auth - /gzip 返回 gzip 压缩内容 - /proxy 作为浏览器代理转发任意 URL 请求(仅用于测试) - / 静态页面,提供简易 Postman 风格界面,默认走 /proxy ## 本地运行(需 Go 1.22+) ```bash # 初始化与运行 go mod init restapitest go get github.com/gorilla/mux github.com/google/uuid go run ./cmd/server ``` 浏览器访问 http://localhost:8080/ 打开前端页面。 ### 为什么用“go run ./cmd/server”这样启动? - 目录结构遵循 Go 社区通用约定:可执行程序入口放在 `cmd//main.go`,库代码放在 `internal/...`。 - 因此用 `go run ./cmd/server` 明确指定入口目录,Go 会编译并运行其中的 `main.go`。 - 服务器默认监听 `:8080`,也可通过环境变量 `PORT` 覆盖,例如: ```bash PORT=9090 go run ./cmd/server ``` ### 关于工作目录与静态文件 - 首页通过 `http.ServeFile` 以相对路径加载 `web/static/index.html`。 - 请在项目根目录执行启动命令,这样相对路径才能正确定位到静态文件。 ### 用 curl 验证与 405 说明 - 访问首页(GET): ```bash curl -s http://localhost:8080/ | head -n 3 ``` - 访问示例 JSON: ```bash curl -s http://localhost:8080/json | jq . ``` - 以前首页路由只允许 GET,请求 `curl -I /`(HEAD)会返回 405;现已支持 HEAD(返回 200)。 ### 前台/后台运行与停止 - 前台运行(按 Ctrl+C 停止): ```bash go run ./cmd/server ``` - 后台运行(Linux): ```bash nohup bash -lc 'go run ./cmd/server' >/tmp/restapitest.log 2>&1 & tail -f /tmp/restapitest.log ``` - 停止占用 8080 的进程: ```bash kill $(lsof -t -iTCP:8080 -sTCP:LISTEN) ``` ## Docker 运行 ```bash # 构建镜像 docker build -t restapitest:latest . # 运行容器 docker run --rm -p 8080:8080 restapitest:latest ``` 容器内入口即上述同一个二进制,监听 8080;若要变更端口,传入 `-e PORT=9090 -p 9090:9090`。 ## 配置 - PORT:服务监听端口,默认 8080。 ## 安全说明 - /proxy 会按请求转发到目标 URL,仅用于调试和测试环境,请勿对外开放到不受控网络。 ## 目录结构 - cmd/server/main.go:程序入口 - internal/server:路由与服务启动 - internal/handlers:各测试端点与 /proxy - web/static:前端静态页面 - Dockerfile:多阶段构建,distroless 运行时