diff restful api result
还没有人认领这个 Issue。
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 28/100
- Issue 类型
- 功能
- 描述清晰度
- 基本清楚
- 活跃度
- 停滞
- 技术栈
- python
- 领域
- api, testing-qa
调研方向
Start with the Python example in the issue, especially diff_api_service_resullt and the main entry point. Review how it submits requests to the test and expected hosts, writes JSON responses, and generates an HTML diff report; done means the two API versions can be compared and differences are reported reliably.
由索引模型根据 Issue 内容生成。
描述
Case:
Need to compare different version of restful api to check if the result are identical.
-*- coding: utf-8
import os
import json
import requests
import traceback
try:
from urlparse import urlparse
except:
# Python 3x:
from urllib.parse import urlparse
test_api_host="xxx-test.com"
expected_api_host="xxx.com"
def generat_output_json_path(imageUrl, apiName, fileNamePostfix):
# it should not occur
if not imageUrl:
return "null_image_url.json"
path = urlparse(imageUrl).path
relative_url = path.lstrip("/").replace("/","_") + fileNamePostfix + ".json"
output_folder = "output/" + apiName.lower()
if not os.path.exists(output_folder):
os.makedirs(output_folder)
return output_folder + "/" + relative_url
def request_url_with_json(http_path, json):
headers = {"Content-Type": "application/json", "x-sdk-invoke-type":"common", "Accept":"application/json", "PK":"123456789"}
response = requests.post(http_path, data=json, headers=headers)
return response.json()
def submit_request_to_url(serviceUrl, serviceUrlPostfix, imageUrl, jsonTemplateFile):
if serviceUrl and not serviceUrl.startswith("http"):
serviceUrl = "http://" + serviceUrl
request_image_url = imageUrl
request_json_template = open(jsonTemplateFile).read()
request_json = request_json_template.replace("$IMAGE_URL", request_image_url)
response = None
httpCode = None
status = ""
message = ""
try:
response = request_url_with_json(serviceUrl + serviceUrlPostfix, request_json)
if not response:
print("Error request url: %s" % serviceUrl + serviceUrlPostfix)
return None
## ignore fields:
if "request_id" in response:
del response["request_id"]
except:
# printing stack trace
traceback.print_exc()
return response
def diff_api_service_resullt(serviceUrlPostfix, imageUrl, jsonTemplateFile):
test_api_response = submit_request_to_url(test_api_host, serviceUrlPostfix, imageUrl, jsonTemplateFile)
expected_api_response = submit_request_to_url(expected_api_host, serviceUrlPostfix, imageUrl, jsonTemplateFile)
test_json_file = generat_output_json_path(url, "detect", "_test")
expected_json_file = generat_output_json_path(url, "detect", "_expected")
diff_output_file = generat_output_json_path(url, "detect", "_diff")
outfile = open(test_json_file,"w")
output_json = json.dumps(test_api_response, indent=4, sort_keys=True, ensure_ascii=False)
outfile.write(output_json)
outfile.close()
outfile = open(expected_json_file,"w")
expected_json = json.dumps(expected_api_response, indent=4, sort_keys=True, ensure_ascii=False)
outfile.write(expected_json)
outfile.close()
if not expected_json == output_json:
# 保存差异到 diff 文件
cmd = "python3 ./diff2html %s %s" % (test_json_file, expected_json_file)
print("executing: %s" % cmd)
diff_stdout = os.popen(cmd, "r")
diff_output = diff_stdout.readlines()
diff_outputfile_path = "./report/" + diff_output_file.replace("/","_") + ".html"
dirname = os.path.dirname(diff_outputfile_path)
if not os.path.exists(dirname):
os.makedirs(dirname)
diff_outfile = open(diff_outputfile_path,"w")
diff_outfile.write("".join(diff_output))
diff_outfile.close()
diff_stdout.close()
if __name__ == "__main__":
url = "https://www.demo.com/a.jpeg"
diff_api_service_resullt("/api/face/detect", url, "./input/detect.json")
- 主要语言
- Python
- 星标
- 5
- 派生
- 2
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
davideuler/programming-tips 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 35/100
-
难度 2/5 1-3 小时 新手友好度 35/100
-
难度 1/5 1 小时以内 新手友好度 35/100
-
难度 2/5 1-3 小时 新手友好度 45/100
-
难度 3/5 1-2 天 新手友好度 35/100
查看 davideuler/programming-tips 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
conda-forge/conda-build-feedstock#289 · 1 条评论 · 1 个 reaction ·
-
`pulptest` no longer works in 4.0.0: `ImportError: Start directory is not importable: 'pulp/tests'`未关闭
难度 2/5 1-3 小时 新手友好度 75/100
-
难度 2/5 1-3 小时 新手友好度 72/100
-
难度 2/5 1-3 小时 新手友好度 78/100
-
难度 2/5 1-3 小时 新手友好度 72/100
TomCasavant/ohio-sites#224 ·