Testing http calls with Webmock and VCR

  • 4,802 views
Uploaded on

Lightning talk for IPRUG, 5 March 2013

Lightning talk for IPRUG, 5 March 2013

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
4,802
On Slideshare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
23
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

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