• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Testing http calls with Webmock and VCR
 

Testing http calls with Webmock and VCR

on

  • 4,142 views

Lightning talk for IPRUG, 5 March 2013

Lightning talk for IPRUG, 5 March 2013

Statistics

Views

Total Views
4,142
Views on SlideShare
4,129
Embed Views
13

Actions

Likes
1
Downloads
18
Comments
0

2 Embeds 13

http://localhost 10
https://duckduckgo.com 3

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Testing http calls with Webmock and VCR Testing http calls with Webmock and VCR Presentation Transcript

    • Testing HTTP calls Kerry Buckley IPRUG lightning talk, 5 March 2013
    • Why?Your tests Your appWebby stuff
    • WebmockTest::Unit Minitest RSpec Cucumber Your app EM-HTTP-Net::HTTP HTTPclient Patron request Curb Typhoeus Excon Webmock
    • Setupgroup :test do gem "webmock"endrequire webmock/rspecrequire webmock/minitestrequire webmock/test_unitrequire webmock/cucumber
    • Basic stub stub_request :any, "www.example.com"Net::HTTP.get "www.example.com", "/" # => 200 OK, body = ""
    • 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) }
    • 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} }
    • 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
    • Stubbing with Rackclass MyRackApp def self.call(env) [200, {}, ["Hello"]] endendstub_request(:get, "www.example.com"). to_rack(MyRackApp)
    • Allow real requestsWebMock.allow_net_connect!WebMock.disable_net_connect!( allow_localhost: true)WebMock.disable_net_connect!( allow: "www.example.org:8080")
    • 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"]})
    • 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)
    • VCR Your tests Your appWebby stuff
    • 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
    • Library supportTest::Unit Minitest RSpec Cucumber VCRWebmock Fakeweb Faraday Excon Typhoeus
    • RSpec RSpec.configure do |c| c.extend VCR::RSpec::Macrosenddescribe "something" do use_vcr_cassette it "does something" do ... endend
    • 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
    • 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
    • Request matchingVCR.configure do |c| c.default_cassette_options = { match_requests_on: [:method, :uri]}endAlso available:• :host• :path• :query• :body• :headers
    • 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
    • SummaryUse Webmock (or Fakeweb)• For fine-grained control• If remote server isn’t availableUse VCR• When record/replay is enough• For an easy life