testing http calls with webmock and vcr

21
Testing HTTP calls Kerry Buckley IPRUG lightning talk, 5 March 2013

Upload: kerry-buckley

Post on 30-Oct-2014

5.162 views

Category:

Technology


3 download

DESCRIPTION

Lightning talk for IPRUG, 5 March 2013

TRANSCRIPT

Page 1: Testing http calls with Webmock and VCR

Testing HTTP callsKerry Buckley

IPRUG lightning talk, 5 March 2013

Page 2: Testing http calls with Webmock and VCR

Why?

Your app

Your tests

Webby stuff

Page 3: Testing http calls with Webmock and VCR

WebmockTest::Unit Minitest RSpec Cucumber

Your app

Net::HTTP HTTPclient Patron EM-HTTP-request

Curb Typhoeus Excon

Webmock

Page 4: Testing http calls with Webmock and VCR

Setupgroup :test do gem "webmock"end

require 'webmock/rspec'

require 'webmock/minitest'

require 'webmock/test_unit'

require 'webmock/cucumber'

Page 5: Testing http calls with Webmock and VCR

Basic stub

stub_request :any, "www.example.com"

Net::HTTP.get "www.example.com", "/"

# => 200 OK, body = ""

Page 6: Testing http calls with Webmock and VCR

Filtering requestsstub_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) }

Page 7: Testing http calls with Webmock and VCR

Response

stub_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} }

Page 8: Testing http calls with Webmock and VCR

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

Page 9: Testing http calls with Webmock and VCR

Stubbing with Rack

class MyRackApp def self.call(env) [200, {}, ["Hello"]] endend

stub_request(:get, "www.example.com"). to_rack(MyRackApp)

Page 10: Testing http calls with Webmock and VCR

Allow real requests

WebMock.allow_net_connect!

WebMock.disable_net_connect!( allow_localhost: true)

WebMock.disable_net_connect!( allow: "www.example.org:8080")

Page 11: Testing http calls with Webmock and VCR

Expectationsrequire 'webmock/rspec'

WebMock.should_not have_requested(:get, "www.something.com")

WebMock.should have_requested(:get, "www.example.com"). with(body: "abc", headers: {'Content-Length' => 3}).twice

WebMock.should have_requested(:get, "www.example.com"). with(query: {"a" => ["b", "c"]})

Page 12: Testing http calls with Webmock and VCR

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)

Page 13: Testing http calls with Webmock and VCR

VCR

Your app

Your tests

Webby stuff

Page 14: Testing http calls with Webmock and VCR

Setupgroup :test do gem "vcr"end

VCR.configure do |c| c.cassette_library_dir = "vcr_cassettes" c.hook_into :webmockend

VCR.use_cassette("example-dot-com-index") do Net::HTTP.get "www.example.com", "/"end

Page 15: Testing http calls with Webmock and VCR

Library support

Test::Unit Minitest RSpec Cucumber

VCR

Webmock Fakeweb Faraday Excon

Typhoeus

Page 16: Testing http calls with Webmock and VCR

RSpecRSpec.configure do |c|

c.extend VCR::RSpec::Macrosend

describe "something" do use_vcr_cassette

it "does something" do ... endend

Page 17: Testing http calls with Webmock and VCR

CucumberVCR.cucumber_tags do |t|

t.tag '@example-dot-com-index' t.tag '@example-dot-com-another-request'end

Feature: VCR stuff

@example-dot-com-index Scenario: Do something Given foo When bar Then baz

Page 18: Testing http calls with Webmock and VCR

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

Page 19: Testing http calls with Webmock and VCR

Request matchingVCR.configure do |c| c.default_cassette_options = { match_requests_on: [:method, :uri]}end

Also available:

•:host•:path•:query•:body•:headers

Page 20: Testing http calls with Webmock and VCR

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

Page 21: Testing http calls with Webmock and VCR

SummaryUse Webmock (or Fakeweb)

• For fine-grained control

• If remote server isn’t available

Use VCR

• When record/replay is enough

• For an easy life