Testing http calls with Webmock and VCR

8,339 views
7,690 views

Published on

Lightning talk for IPRUG, 5 March 2013

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
8,339
On SlideShare
0
From Embeds
0
Number of Embeds
21
Actions
Shares
0
Downloads
27
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Testing http calls with Webmock and VCR

  1. 1. Testing HTTP calls Kerry Buckley IPRUG lightning talk, 5 March 2013
  2. 2. Why?Your tests Your appWebby stuff
  3. 3. WebmockTest::Unit Minitest RSpec Cucumber Your app EM-HTTP-Net::HTTP HTTPclient Patron request Curb Typhoeus Excon Webmock
  4. 4. Setupgroup :test do gem "webmock"endrequire webmock/rspecrequire webmock/minitestrequire webmock/test_unitrequire webmock/cucumber
  5. 5. Basic stub stub_request :any, "www.example.com"Net::HTTP.get "www.example.com", "/" # => 200 OK, body = ""
  6. 6. Filtering requests stub_request(:post, "www.example.com"). with(body: "abc", headers: {Content-Length => 3})stub_http_request(:post, "www.example.com"). with(body: {data: {a: "1", b: "five"}})stub_request(:post, "www.example.com"). with {|request| some_checks(request) }
  7. 7. Responsestub_request(:any, "www.example.com"). to_return(body: "abc", status: 200, headers: {Content-Length => 3} )stub_request(:any, www.example.net). to_return {|request| {body: request.body} }
  8. 8. Raising exceptionsstub_request(:any, www.example.net). to_raise("some error")stub_request(:any, www.example.net). to_raise(Errno::ECONNRESET.new("some error"))stub_request(:any, www.example.net). to_raise(Errno::ETIMEDOUT)stub_request(:any, www.example.net).to_timeout
  9. 9. Stubbing with Rackclass MyRackApp def self.call(env) [200, {}, ["Hello"]] endendstub_request(:get, "www.example.com"). to_rack(MyRackApp)
  10. 10. Allow real requestsWebMock.allow_net_connect!WebMock.disable_net_connect!( allow_localhost: true)WebMock.disable_net_connect!( allow: "www.example.org:8080")
  11. 11. Expectationsrequire webmock/rspecWebMock.should_not have_requested(:get, "www.something.com")WebMock.should have_requested(:get, "www.example.com"). with(body: "abc", headers: {Content-Length => 3}).twiceWebMock.should have_requested(:get, "www.example.com"). with(query: {"a" => ["b", "c"]})
  12. 12. Replaying curl`curl -is www.example.com > /tmp/example.txt`raw_response_file = File.new("/tmp/example.txt")stub_request(:get, "www.example.com"). to_return(raw_response_file)stub_request(:get, "www.example.com"). to_return(raw_response_file.read)
  13. 13. VCR Your tests Your appWebby stuff
  14. 14. Setupgroup :test do gem "vcr"endVCR.configure do |c| c.cassette_library_dir = "vcr_cassettes" c.hook_into :webmockendVCR.use_cassette("example-dot-com-index") do Net::HTTP.get "www.example.com", "/"end
  15. 15. Library supportTest::Unit Minitest RSpec Cucumber VCRWebmock Fakeweb Faraday Excon Typhoeus
  16. 16. RSpec RSpec.configure do |c| c.extend VCR::RSpec::Macrosenddescribe "something" do use_vcr_cassette it "does something" do ... endend
  17. 17. Cucumber VCR.cucumber_tags do |t| t.tag @example-dot-com-index t.tag @example-dot-com-another-requestendFeature: VCR stuff @example-dot-com-index Scenario: Do something Given foo When bar Then baz
  18. 18. Cassettes---http_interactions:- request: method: get uri: http://example.com/ body: headers: {} response: status: code: 200 message: OK headers: Content-Type: - text/html;charset=utf-8 Content-Length: - 26 body: This is the response body http_version: 1.1 recorded_at: Tue, 01 Nov 2011 04:58:44 GMTrecorded_with: VCR 2.0.0
  19. 19. Request matchingVCR.configure do |c| c.default_cassette_options = { match_requests_on: [:method, :uri]}endAlso available:• :host• :path• :query• :body• :headers
  20. 20. Ignoring requests VCR.configure do |c| c.ignore_request do |request| URI(request.uri).port == 7777 end c.ignore_hosts "foo.com", "bar.com" c.ignore_localhost = trueend
  21. 21. SummaryUse Webmock (or Fakeweb)• For fine-grained control• If remote server isn’t availableUse VCR• When record/replay is enough• For an easy life

×