21 | 全面测试项目功能
提示:
- 所有体系课见专栏:Go 项目开发极速入门实战课;
- 本节课最终源码位于 fastgo 项目的 feature/s17 分支。
编译并运行
执行以下命令编译并运行 fg-apiserver:
$ ./build.sh
$ _output/fg-apiserver -c configs/fg-apiserver.yaml
用户功能测试
这里,我们通过 curl 命令来测试用户相关的 API 接口是否可以正常工作。以下是待测试的接口:
- 创建用户;
- 列出所有用户。
创建 colin 用户
$ curl -XPOST -H'Content-Type: application/json' http://127.0.0.1:6666/v1/users -d '{"username":"fastgotest","password":"fastgotest1234","nickname":"colin404","email":"colin404@foxmail.com","phone":"18188881111"}'
{"userID":"user-eni0mm"}
登录
在调用其他接口时,需要先登录获取 Token,再使用 Token 访问 API 接口。为了方便后面的调用,通过以下命令将 Login 接口返回的 Token 保存在 token
变量中:
$ token=$(curl -s -XPOST -H"Content-Type: application/json" http://127.0.0.1:6666/login -d'{"username":"fastgotest","password":"fastgotest1234"}'|jq -r .token)
列出所有用户
使用 token
变量,方位用户列表接口:
$ curl -s -XGET -H"Authorization: Bearer ${token}" http://127.0.0.1:6666/v1/users|jq
{"totalCount": 1,"users": [{"userID": "user-eni0mm","username": "fastgotest","nickname": "colin404","email": "colin404@foxmail.com","phone": "18188881111","postCount": 0,"createdAt": "2025-03-09T15:54:07+08:00","updatedAt": "2025-03-09T15:54:07+08:00"}]
}
获取 colin 用户的详细信息
$ curl -s -XGET -H"Authorization: Bearer ${token}" http://127.0.0.1:6666/v1/users/user-eni0mm|jq
{"user": {"userID": "user-eni0mm","username": "fastgotest","nickname": "colin404","email": "colin404@foxmail.com","phone": "18188881111","postCount": 0,"createdAt": "2025-03-09T15:54:07+08:00","updatedAt": "2025-03-09T15:54:07+08:00"}
}
修改 colin 用户
$ curl -XPUT -H'Content-Type: application/json' -H"Authorization: Bearer ${token}" http://127.0.0.1:6666/v1/users/user-eni0mm -d '{"nickname":"colin505"}'
{}
删除 colin 用户
$ curl -XDELETE -H"Authorization: Bearer ${token}" http://127.0.0.1:6666/v1/users/user-eni0mm
{}
博客功能测试
创建测试用户 posttest 用户
$ curl -XPOST -H'Content-Type: application/json' http://127.0.0.1:6666/v1/users -d '{"username":"posttest","password":"posttest1234","nickname":"colin404","email":"colin404@foxmail.com","phone":"18188880000"}'
{"userID":"user-v1eok4"}
posttest 登录 fastgo
$ token=$(curl -s -XPOST -H"Content-Type: application/json" http://127.0.0.1:6666/login -d'{"username":"posttest","password":"posttest1234"}'|jq -r .token)
创建一个博客
$ curl -XPOST -H"Content-Type: application/json" -H"Authorization: Bearer ${token}" http://127.0.0.1:6666/v1/posts -d'{"title":"installation","content":"installation."}'
{"postID":"post-tq7ien"}
列出所有博客
$ curl -s -XGET -H"Authorization: Bearer ${token}" http://127.0.0.1:6666/v1/posts|jq
{"total_count": 1,"posts": [{"postID": "post-tq7ien","userID": "user-v1eok4","title": "installation","content": "installation.","createdAt": "2025-03-10T11:42:50+08:00","updatedAt": "2025-03-10T11:42:50+08:00"}]
}
获取所创建博客的信息
$ curl -s -XGET -H"Authorization: Bearer ${token}" http://127.0.0.1:6666/v1/posts/post-tq7ien|jq
{"post": {"postID": "post-tq7ien","userID": "user-v1eok4","title": "installation","content": "installation.","createdAt": "2025-03-10T11:42:50+08:00","updatedAt": "2025-03-10T11:42:50+08:00"}
}
修改所创建博客的信息
$ curl -XPUT -H"Content-Type: application/json" -H"Authorization: Bearer ${token}" http://127.0.0.1:6666/v1/posts/post-tq7ien -d'{"title":"installation(modified)"}'
{}
删除所创建的博客
$ curl -XDELETE -H"Authorization: Bearer ${token}" http://127.0.0.1:6666/v1/posts -d'{"postIDs":["post-tq7ien"]}'
{}
使用测试脚本测试整个项目
打开一个新的 Linux 终端,执行以下命令测试 fg-apiserver 中的 API 接口是否正常工作:
$ ./scripts/test.sh
执行结果如下图所示:
可以看到,fg-apiserver 中的接口,都测试通过。
欢迎加入「云原生AI 实战营」星球,12+ 高质量体系课、20+ 高质量实战项目助你在 AI 时代建立技术竞争力: