使用 Savon 测试本地 Web 服务

发布于 2024-12-24 19:13:17 字数 291 浏览 0 评论 0原文

我正在编写一个 gem,以向 Ruby 添加对 SOAP 服务的支持(我讨厌自己这样做,但是,你知道,遗留系统感到孤独,必须与某人交谈),我想知道是否有一种方法可以编写使用 Savon 作为客户端库的一些测试。

我的问题是:如何告诉 Savon 使用 Rack::Test 调用 WebService?

宝石来源托管在这里:https://github.com/elementar/shapewear

I'm writing a gem to add support for SOAP services to Ruby (I hate myself for doing this but, you know, legacy systems are feeling lonely and gotta talk to someone), and I'm wondering if there's a way I can write some tests using Savon as a client library.

My question is: how can I tell Savon to call the WebService using Rack::Test?

The gem sources are hosted here: https://github.com/elementar/shapewear

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若水微香 2024-12-31 19:13:17

我最终使用了 WebMock gem。结果如下:

https://github.com/elementar/shapewear /blob/master/spec/shapewear/savon_usage_spec.rb

describe Shapewear do
  describe "usage with SOAP clients" do
    before do
      stub_request(:get, "http://services.example.com/complete/soap/wsdl") \
        .to_return :body => CompleteService.to_wsdl, 
                   :headers => {'Content-Type' => 'application/xml'}

      stub_request(:post, "http://services.example.com/complete/soap") \
        .to_return :body => lambda { |r| CompleteService.serve(r) }, 
                   :headers => {'Content-Type' => 'application/xml'}
    end

    it "should work with Savon" do
      client = Savon::Client.new 'http://services.example.com/complete/soap/wsdl'
      response = client.request :echo_in_uppercase, :xmlns => 'http://services.example.com/v1' do
        soap.body = {:text => 'uppercase text'}
      end

      response.body[:echo_in_uppercase_response][:body].should == 'UPPERCASE TEXT'
    end
  end
end

I ended up using the WebMock gem. Here's the result:

https://github.com/elementar/shapewear/blob/master/spec/shapewear/savon_usage_spec.rb

describe Shapewear do
  describe "usage with SOAP clients" do
    before do
      stub_request(:get, "http://services.example.com/complete/soap/wsdl") \
        .to_return :body => CompleteService.to_wsdl, 
                   :headers => {'Content-Type' => 'application/xml'}

      stub_request(:post, "http://services.example.com/complete/soap") \
        .to_return :body => lambda { |r| CompleteService.serve(r) }, 
                   :headers => {'Content-Type' => 'application/xml'}
    end

    it "should work with Savon" do
      client = Savon::Client.new 'http://services.example.com/complete/soap/wsdl'
      response = client.request :echo_in_uppercase, :xmlns => 'http://services.example.com/v1' do
        soap.body = {:text => 'uppercase text'}
      end

      response.body[:echo_in_uppercase_response][:body].should == 'UPPERCASE TEXT'
    end
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文