SlideShare a Scribd company logo
Testing HTTP calls
           Kerry Buckley
 IPRUG lightning talk, 5 March 2013
Why?
Your tests


 Your app


Webby stuff
Webmock
Test::Unit          Minitest     RSpec        Cucumber


                          Your app


                                              EM-HTTP-
Net::HTTP       HTTPclient       Patron
                                               request

             Curb         Typhoeus        Excon


                          Webmock
Setup
group :test do
  gem "webmock"
end

require 'webmock/rspec'

require 'webmock/minitest'

require 'webmock/test_unit'

require '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) }
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} }
Raising exceptions
stub_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 Rack
class MyRackApp
  def self.call(env)
    [200, {}, ["Hello"]]
  end
end

stub_request(:get, "www.example.com").
  to_rack(MyRackApp)
Allow real requests
WebMock.allow_net_connect!

WebMock.disable_net_connect!(
  allow_localhost: true)

WebMock.disable_net_connect!(
  allow: "www.example.org:8080")
Expectations
require '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"]})
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 app




Webby stuff
Setup
group :test do
  gem "vcr"
end

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

VCR.use_cassette("example-dot-com-index") do
  Net::HTTP.get "www.example.com", "/"
end
Library support
Test::Unit   Minitest         RSpec     Cucumber


                        VCR


Webmock      Fakeweb          Faraday    Excon


                   Typhoeus
RSpec
  RSpec.configure do |c|
  c.extend VCR::RSpec::Macros
end

describe "something" do
  use_vcr_cassette

  it "does something" do
    ...
  end
end
Cucumber
  VCR.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
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 GMT
recorded_with: VCR 2.0.0
Request matching
VCR.configure do |c|
  c.default_cassette_options = {
    match_requests_on: [:method, :uri]}
end

Also 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 = true
end
Summary
Use Webmock (or Fakeweb)
• For fine-grained control
• If remote server isn’t available
Use VCR
• When record/replay is enough
• For an easy life

More Related Content

KEY
TDD of HTTP Clients With WebMock
PPTX
Solving anything in VCL
PDF
VCL template abstraction model and automated deployments to Fastly
PDF
Advanced VCL: how to use restart
PDF
Designing net-aws-glacier
KEY
Background Jobs with Resque
PDF
Background processing with Resque
KEY
Php resque
TDD of HTTP Clients With WebMock
Solving anything in VCL
VCL template abstraction model and automated deployments to Fastly
Advanced VCL: how to use restart
Designing net-aws-glacier
Background Jobs with Resque
Background processing with Resque
Php resque

What's hot (20)

KEY
GPerf Using Jesque
KEY
Railsconf2011 deployment tips_for_slideshare
PDF
Go Concurrency
PDF
Beyond Page Level Metrics
PDF
Ruby HTTP clients comparison
PDF
About Node.js
KEY
How I Learned to Stop Worrying and Love the Cloud - Wesley Beary, Engine Yard
ODP
Implementing Comet using PHP
PPTX
A complete guide to Node.js
PDF
Beyond Breakpoints: A Tour of Dynamic Analysis
PDF
Flask With Server-Sent Event
PDF
Puppet Performance Profiling
PDF
HTTPBuilder NG: Back From The Dead
PDF
Autoscaling with hashi_corp_nomad
PDF
Testing your infrastructure with litmus
PDF
Observability with Consul Connect
PDF
Http capturing
PDF
How we used ruby to build locaweb's cloud (http://presentations.pothix.com/ru...
PDF
HTTP caching with Varnish
PDF
Boxen: How to Manage an Army of Laptops
GPerf Using Jesque
Railsconf2011 deployment tips_for_slideshare
Go Concurrency
Beyond Page Level Metrics
Ruby HTTP clients comparison
About Node.js
How I Learned to Stop Worrying and Love the Cloud - Wesley Beary, Engine Yard
Implementing Comet using PHP
A complete guide to Node.js
Beyond Breakpoints: A Tour of Dynamic Analysis
Flask With Server-Sent Event
Puppet Performance Profiling
HTTPBuilder NG: Back From The Dead
Autoscaling with hashi_corp_nomad
Testing your infrastructure with litmus
Observability with Consul Connect
Http capturing
How we used ruby to build locaweb's cloud (http://presentations.pothix.com/ru...
HTTP caching with Varnish
Boxen: How to Manage an Army of Laptops
Ad

Similar to Testing http calls with Webmock and VCR (20)

ZIP
Palestra VCR
ZIP
Websockets at tossug
PDF
VUG5: Varnish at Opera Software
ODP
Otimizando seu projeto Rails
PPT
Assurer - a pluggable server testing/monitoring framework
PDF
How we use and deploy Varnish at Opera
PDF
Let's read code: python-requests library
PDF
PPTX
REST meets Semantic Web
PDF
Play 23x Documentation For Scala Developers 23x Unknown
PDF
Fargate 를 이용한 ECS with VPC 1부
PDF
HTTP Caching and PHP
PDF
Behavior Driven Development and Automation Testing Using Cucumber
PPTX
Full Stack Load Testing
PDF
Comet from JavaOne 2008
PPTX
How to ensure Presto scalability 
in multi use case
PDF
PDF
Istio Playground
ODP
RichFaces - Testing on Mobile Devices
Palestra VCR
Websockets at tossug
VUG5: Varnish at Opera Software
Otimizando seu projeto Rails
Assurer - a pluggable server testing/monitoring framework
How we use and deploy Varnish at Opera
Let's read code: python-requests library
REST meets Semantic Web
Play 23x Documentation For Scala Developers 23x Unknown
Fargate 를 이용한 ECS with VPC 1부
HTTP Caching and PHP
Behavior Driven Development and Automation Testing Using Cucumber
Full Stack Load Testing
Comet from JavaOne 2008
How to ensure Presto scalability 
in multi use case
Istio Playground
RichFaces - Testing on Mobile Devices
Ad

More from Kerry Buckley (20)

PDF
Jasmine
PDF
BDD with cucumber
KEY
Ruby nooks & crannies
KEY
TDD refresher
KEY
Javasccript MV* frameworks
KEY
Tdd for BT E2E test community
PDF
7li7w devcon5
PDF
What I learned from Seven Languages in Seven Weeks (IPRUG)
KEY
Functional ruby
KEY
Adastral Park code retreat introduction
KEY
MongoMapper lightning talk
KEY
KEY
PDF
The secret life of bees
PDF
Background processing
PDF
Katas, Contests and Coding Dojos
PDF
PDF
Doing REST Right
PPT
Kanban and Iterationless Working
PPT
Software Development Trends
Jasmine
BDD with cucumber
Ruby nooks & crannies
TDD refresher
Javasccript MV* frameworks
Tdd for BT E2E test community
7li7w devcon5
What I learned from Seven Languages in Seven Weeks (IPRUG)
Functional ruby
Adastral Park code retreat introduction
MongoMapper lightning talk
The secret life of bees
Background processing
Katas, Contests and Coding Dojos
Doing REST Right
Kanban and Iterationless Working
Software Development Trends

Recently uploaded (20)

PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Empathic Computing: Creating Shared Understanding
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Electronic commerce courselecture one. Pdf
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
A Presentation on Artificial Intelligence
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
A comparative analysis of optical character recognition models for extracting...
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Empathic Computing: Creating Shared Understanding
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Review of recent advances in non-invasive hemoglobin estimation
Mobile App Security Testing_ A Comprehensive Guide.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Electronic commerce courselecture one. Pdf
Assigned Numbers - 2025 - Bluetooth® Document
“AI and Expert System Decision Support & Business Intelligence Systems”
Spectral efficient network and resource selection model in 5G networks
Encapsulation_ Review paper, used for researhc scholars
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
A Presentation on Artificial Intelligence
MIND Revenue Release Quarter 2 2025 Press Release
A comparative analysis of optical character recognition models for extracting...

Testing http calls with Webmock and VCR

  • 1. Testing HTTP calls Kerry Buckley IPRUG lightning talk, 5 March 2013
  • 2. Why? Your tests Your app Webby stuff
  • 3. Webmock Test::Unit Minitest RSpec Cucumber Your app EM-HTTP- Net::HTTP HTTPclient Patron request Curb Typhoeus Excon Webmock
  • 4. Setup group :test do gem "webmock" end require 'webmock/rspec' require 'webmock/minitest' require 'webmock/test_unit' require '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. 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} }
  • 8. Raising exceptions stub_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 Rack class MyRackApp def self.call(env) [200, {}, ["Hello"]] end end stub_request(:get, "www.example.com"). to_rack(MyRackApp)
  • 10. Allow real requests WebMock.allow_net_connect! WebMock.disable_net_connect!( allow_localhost: true) WebMock.disable_net_connect!( allow: "www.example.org:8080")
  • 11. Expectations require '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"]})
  • 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 app Webby stuff
  • 14. Setup group :test do gem "vcr" end VCR.configure do |c| c.cassette_library_dir = "vcr_cassettes" c.hook_into :webmock end VCR.use_cassette("example-dot-com-index") do Net::HTTP.get "www.example.com", "/" end
  • 15. Library support Test::Unit Minitest RSpec Cucumber VCR Webmock Fakeweb Faraday Excon Typhoeus
  • 16. RSpec RSpec.configure do |c| c.extend VCR::RSpec::Macros end describe "something" do use_vcr_cassette it "does something" do ... end end
  • 17. Cucumber VCR.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
  • 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 GMT recorded_with: VCR 2.0.0
  • 19. Request matching VCR.configure do |c| c.default_cassette_options = { match_requests_on: [:method, :uri]} end Also 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 = true end
  • 21. Summary Use Webmock (or Fakeweb) • For fine-grained control • If remote server isn’t available Use VCR • When record/replay is enough • For an easy life