工具链接:
https://github.com/gstroup/apimocker
背景:
QA在内网部署服务器环境,但系统涉及服务太多,无法部署所有服务,并且ABTest(Maestro)服务使用Google OAuth做授权认证,测试环境没有域名,无法搭建实际的Maestro服务。在后端(one-eye)服务的代码中,每次对Maestro服务的请求均返回特定的json字符串,因此这种情况非常适合简单搭建一个mock server来模拟API,绕过真实的Maestro服务。
追踪one-eye代码,发现所有对maestro的请求都是根据call_maestro_placements_api调用placements接口,因此,我们只要修改配置并mock一个http://
# maestro_service.rb
def call_maestro_placements_api(query)
RestClient::Request.execute(method: :get, url: "#{OneEye::MAESTRO_API_URL}/placements#{URI.encode(query)}", headers: headers, timeout: self::TIMEOUT)
end
# 配置文件one-eye/config/env_config.rb
MAESTRO_API_URL = 'http://:7878'
MAESTRO_TESTS_ENABLED = true
使用:
1.安装apimocker服务
sudo npm install -g apimocker
2.启动apimocker
# 命令格式
# apimocker \[-c, --config \] \[-q, --quiet\] \[-p \] \[-f, --proxy \] \[-i, --intercept \]
#使用默认配置启动
cd /
find ./ -name apimocker # 根路径下查找apimocker所在安装路径
cd /usr/lib/node_modules/apimocker # 进入apimocker安装路径
apimocker
若报错,请检查安装包所在的配置文件json格式是否正确。通常apimocker安装在/usr/lib/node_modules/apimocker目录下。
3.配置目标API
配置apimocker/config.json文件,webServices节点增加以下内容
"placements":{
"mockFile": "placements.json",
"verbs": [
"get"
]
},
配置apimocker/samplemocks/placements.json文件为目标返回内容
{"admin-member-invite":"enabled",
"activity-studio":"enabled",
"enterprise-invite-members":"enabled",
"opengroup":"enabled",
"automatically-record-zoom-meeting":"ready_state",
"whats-new-announcements":"ready_state",
"print-quiz-submissions":"enabled",
"new-assignment-grading":"enabled",
"new-assignment-submission":"enabled"}
4.重新启动apimocker服务并验证API
执行apimocker后,浏览器访问http://
5.更新后端配置生效
修改前文中one-eye的配置文件one-eye/config/env_config.rb
MAESTRO_API_URL = 'http://
MAESTRO_TESTS_ENABLED = true
重启one-eye服务,使配置生效