当前位置: 首页 > news >正文

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 时代建立技术竞争力:
Alt文本


http://www.mrgr.cn/news/94160.html

相关文章:

  • 12 | 给应用添加优雅关停功能
  • 完整项目案例:基于Django的毕业设计选题管理系统(包含源码结构、核心代码及设计文档框架)
  • 【Linux】在VMWare中安装Ubuntu操作系统(2025最新_Ubuntu 24.04.2)#VMware安装Ubuntu实战分享#
  • Node-RED基础1
  • 【Godot4.3】RenderingServer总结
  • Quantum Computing:量子计算如何改变世界
  • 深入探索 Java Stream
  • 使用 OptiSLang 和 MotorCAD 构建一个强大的电机优化元模型
  • 感觉自己邮电部诗人
  • 【医院成本核算专题】8.大数据与医院成本核算的关联点:开启医疗成本管理新时代
  • 使用Dockerfile构建一个Docker镜像
  • 20 | 如何添加单元测试用例
  • 计算机:基于深度学习的Web应用安全漏洞检测与扫描
  • 工作记录 2017-01-06
  • 重要!!! 改进 梯度方差(Fisher 信息近似) 指数移动平均
  • 记录一下返修
  • 【操作系统】Linux基本命令2
  • JAVA SE 4.Java各版本特性
  • 【MapSet】哈希表
  • vue3,Element Plus中el-select默认显示0