"""Offline Ruby 3.x semantic verification lab for Chapter 42 (web-services)."""
import json
from lxml import etree
VERSION = "CPython 3.12.14 offline Ruby 3.x semantic verifier (lxml SOAP XML model, MRI ruby absent)"
SOAP_ENV = "http://schemas.xmlsoap.org/soap/envelope/"
CALC_NS = "urn:ruby:calculation"
def case_soap_envelope_request_dispatch():
req_xml = f"""<env:Envelope xmlns:env="{SOAP_ENV}" xmlns:n1="{CALC_NS}">
<env:Body>
<n1:add><a>20</a><b>30</b></n1:add>
</env:Body>
</env:Envelope>"""
root = etree.fromstring(req_xml.encode("utf-8"))
ns = {"env": SOAP_ENV, "n1": CALC_NS}
a = int(root.xpath("//n1:add/a/text()", namespaces=ns)[0])
b = int(root.xpath("//n1:add/b/text()", namespaces=ns)[0])
methods = {"add": lambda x, y: x + y, "div": lambda x, y: x / y}
add_res = methods["add"](a, b)
div_res = methods["div"](60, 12)
expected = {"namespace": CALC_NS, "add_20_30": 50, "div_60_12": 5.0}
actual = {"namespace": CALC_NS, "add_20_30": add_res, "div_60_12": div_res}
return {
"name": "soap_envelope_request_dispatch",
"summary": "核验 SOAP Envelope/Body XML 请求报文解析以及 StandaloneServer 的 add/div 方法派发",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_soap_inout_out_retval_params():
# Models aMeth(inParam, inoutParam) returning [retVal, inoutParam, outParam]
# Note: source text had typo `inParam . inoutParam`; semantically subtraction `inParam - inoutParam`
def a_meth(in_param, inout_param):
ret_val = in_param + inout_param
out_param = in_param - inout_param
inout_param = in_param * inout_param
return ret_val, inout_param, out_param
param_spec = [("in", "inParam"), ("inout", "inoutParam"), ("out", "outParam"), ("retval", "return")]
ret_val, new_inout, out_val = a_meth(10, 4)
expected = {
"modes": ["in", "inout", "out", "retval"],
"retval": 14,
"inout_out": 40,
"out": 6,
}
actual = {
"modes": [m for m, _ in param_spec],
"retval": ret_val,
"inout_out": new_inout,
"out": out_val,
}
return {
"name": "soap_inout_out_retval_params",
"summary": "核验 SOAP4R add_method 的 in/inout/out/retval 四种参数模式与多返回值映射",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_soap_driver_and_action_headers():
def create_driver_config(endpoint, namespace, soap_action=""):
return {
"endpoint": endpoint,
"namespace": namespace,
"soap_action": None if soap_action == "" else soap_action,
"methods": {"add": ["a", "b"], "div": ["a", "b"]},
}
cfg_default = create_driver_config("http://localhost:8080/", CALC_NS, "")
cfg_explicit = create_driver_config("http://localhost:8080/", CALC_NS, "urn:ruby:calculation#add")
expected = {
"empty_action_becomes_nil": None,
"explicit_action": "urn:ruby:calculation#add",
"endpoint": "http://localhost:8080/",
"registered_methods": ["add", "div"],
}
actual = {
"empty_action_becomes_nil": cfg_default["soap_action"],
"explicit_action": cfg_explicit["soap_action"],
"endpoint": cfg_default["endpoint"],
"registered_methods": list(cfg_default["methods"].keys()),
}
return {
"name": "soap_driver_and_action_headers",
"summary": "核验 SOAP::RPC::Driver 端点、命名空间、空 SOAPAction 归一化为 nil 及远程方法登记",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_soap_envelope_request_dispatch(),
case_soap_inout_out_retval_params(),
case_soap_driver_and_action_headers(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14 离线 Ruby 3.x 语义验证器(本机未安装 MRI ruby 解释器)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14 离线 Ruby 3.x 语义验证器(本机未安装 MRI ruby 解释器)。
入口文件:projects/ruby-web-services-lab/explore-web-services.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-web-services.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 Ruby 3.x 语义验证器(本机未安装 MRI ruby 解释器)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联45章) · 含当前章节或实例 · mbskycom202612a2434.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2477.zip