Pet*_*own 28 ruby xml soap savon httpi
我正在使用Savon gem使用类似于下面的代码来发出SOAP请求.它工作正常,但我想查看/捕获请求XML而不实际调用他们的服务器.通过在请求之后粘贴调试器行并检查客户端变量,我可以在发出请求后立即查看它.
有没有人知道在不实际发出请求的情况下查看请求XML的方法?我希望能够使用Cucumber或Rspec对模式验证XML.
client = Savon::Client.new do |wsdl, http|
wsdl.document = "http://fakesite.org/fake.asmx?wsdl"
end
client.request(:testpostdata, :xmlns => "http://fakesite.org/") do
soap.header = { :cAuthentication => {"UserName" => "MyName", "Password" => "MyPassword" } }
soap.body = { :xml_data => to_xml }
end
Run Code Online (Sandbox Code Playgroud)
car*_*onr 44
使用Savon 2我这样做,编写一个从客户端返回请求体的方法.
client = Savon::Client.new(....)
Run Code Online (Sandbox Code Playgroud)
这在文档中没有提到
def get_request
# list of operations can be found using client.operations
ops = client.operation(:action_name_here)
# build the body of the xml inside the message here
ops.build(message: { id: 42, name: "Test User", age: 20 }).to_s
end
Run Code Online (Sandbox Code Playgroud)
小智 32
你可以直接通过这个Savon::Client#build_request
方法.
例:
request = client.build_request(:some_operation, some_payload)
request.body # Get the request body
request.headers # Get the request headers
Run Code Online (Sandbox Code Playgroud)
在@ https://github.com/savonrb/savon/blob/master/lib/savon/request.rb上获取完整文档的高峰.
eco*_*gic 13
我正在使用Savon 2.11,我可以在客户端使用全局变量完成它:
def client
@client ||= Savon.client(soap_version: 2,
wsdl: config.wsdl,
logger: Rails.logger,
log: true)
end
Run Code Online (Sandbox Code Playgroud)
然后记录器为请求和响应吐出主机,http动词和完整的xml("标题"和正文).
小智 11
虽然我确信有更好的方法可以做到这一点,但我只是过度回应.
class Savon::SOAP::Request
def response
pp self.request.headers
puts
puts self.request.body
exit
end
end
Run Code Online (Sandbox Code Playgroud)
小智 9
自上一篇文章以来,他们已经更新了API.在Savon.client中设置此设置:: pretty_print_xml => true.通话结束后,在日志中搜索SOAP请求:输出被放到stdout.如果您正在从控制台测试连接,请检查控制台控制台历史记录.
Savon使用HTTPI来执行SOAP请求.HTTPI是各种Ruby HTTP客户端之上的通用接口.您可以通过以下方式模拟/存储Savon执行的HTTP请求:
HTTPI.expects(:post).with do |http|
SchemaValidation.validate(:get_user, http.body)
end
Run Code Online (Sandbox Code Playgroud)
请注意,我使用Mocha来模拟SOAP请求,获取HTTP正文并根据某些验证方法(伪代码)验证它.
目前,Savon不支持在不执行请求的情况下构建请求.因此,验证请求的唯一方法是拦截它.
如果您需要Savon支持此功能,请告诉我并在GitHub上打开一张票.
编辑:还有savon_spec,这是使用Savon进行基本夹具测试的小帮手.
小智 6
我有同样的问题,修补了Savon如下:
module Savon
class Client
def get_request_xml operation_name, locals
Savon::Builder.new(operation_name, @wsdl, @globals, locals).pretty
end
end
end
Run Code Online (Sandbox Code Playgroud)
这将构建XML并将其作为字符串返回,而不将其发送到API端点.它不会像client.call那样接受块参数,因此它无法重现您正在制作的每种类型的请求,但它现在满足了我的需求.