Browse files

Add cukes documenting new http request hooks.

Closes #91.
  • Loading branch information...
1 parent 4b274c3 commit 4f1f7136e0fb915cc354618b48b1d4fe68c0a331 @myronmarston committed Nov 24, 2011
View
17 cucumber.yml
@@ -1,14 +1,13 @@
<%
rerun = File.file?('rerun.txt') ? IO.read('rerun.txt') : ""
rerun_opts = rerun.to_s.strip.empty? ? "--format #{ENV['CUCUMBER_FORMAT'] || 'Cucumber::Formatter::Fuubar'} features" : "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} #{rerun}"
-std_opts = "--format #{ENV['CUCUMBER_FORMAT'] || 'Cucumber::Formatter::Fuubar'} --strict --tags ~@wip"
-interp_opts = if defined?(RUBY_ENGINE)
- " --tags ~@exclude-#{RUBY_ENGINE}"
-else
- ''
-end
+std_opts = "--format #{ENV['CUCUMBER_FORMAT'] || 'Cucumber::Formatter::Fuubar'} --strict"
+
+exclusions = ["--tags ~@exclude-#{RUBY_VERSION.split('.').first(2).join}"].tap do |a|
+ a << "--tags ~@exclude-#{RUBY_ENGINE}" if defined?(RUBY_ENGINE)
+end.join(' ')
%>
-default: <%= std_opts %><%= interp_opts %> features
-wip: --tags @wip features<%= interp_opts %>
-rerun: <%= rerun_opts %> --format rerun --out rerun.txt --strict --tags ~@wip<%= interp_opts %>
+default: <%= std_opts %> --tags ~@wip <%= exclusions %> features
+wip: --tags @wip <%= exclusions %> features
+rerun: <%= rerun_opts %> --format rerun --out rerun.txt --strict --tags ~@wip <%= exclusions %>
View
44 features/hooks/after_http_request.feature
@@ -0,0 +1,44 @@
+Feature: after_http_request hook
+
+ The `after_http_request` hook gets called with each request and response
+ just after a request has completed. It can be used for many things:
+
+ * globally logging requests and responses
+ * ejecting the current cassette (i.e. if you inserted it in a
+ `before_http_request` hook)
+
+ Scenario Outline: log all requests and responses using after_http_request hook
+ Given a file named "after_http_request.rb" with:
+ """ruby
+ include_http_adapter_for("<http_lib>")
+
+ start_sinatra_app(:port => 7777) do
+ get('/') { "Hello World" }
+ end
+
+ require 'vcr'
+
+ VCR.configure do |c|
+ <configuration>
+ c.cassette_library_dir = 'cassettes'
+ c.ignore_localhost = true
+ c.after_http_request do |request, response|
+ puts "Response for #{request.method} #{request.uri}: #{response.body}"
+ end
+ end
+
+ make_http_request(:get, "http://localhost:7777/")
+ """
+ When I run `ruby after_http_request.rb`
+ Then it should pass with "Response for get http://localhost:7777/: Hello World"
+
+ Examples:
+ | configuration | http_lib |
+ | c.hook_into :fakeweb | net/http |
+ | c.hook_into :webmock | net/http |
+ | c.hook_into :webmock | httpclient |
+ | c.hook_into :webmock | curb |
+ | c.hook_into :typhoeus | typhoeus |
+ | c.hook_into :excon | excon |
+ | c.hook_into :faraday | faraday (w/ net_http) |
+
View
56 features/hooks/around_http_request.feature
@@ -0,0 +1,56 @@
+@exclude-18
+Feature: around_http_request hook
+
+ The `around_http_request` hook wraps each HTTP request. It can be used
+ rather than separate `before_http_request` and `after_http_request` hooks
+ to simplify wrapping/transactional logic (such as using a VCR cassette).
+
+ In your block, call `#proceed` on the yielded request to cause it to continue.
+ Alternately, you can treat the request as a proc and pass it on to a method that
+ expects a block by prefixing it with an ampersand (`&request`).
+
+ Note that `around_http_request` will not work on Ruby 1.8. It uses a fiber
+ under the covers and thus is only available on interpreters that support fibers.
+ On 1.8, you can use separate `before_http_request` and `after_http_request` hooks.
+
+ Scenario Outline: globally handle requests using an around_http_request hook
+ Given a file named "globally_handle_requests.rb" with:
+ """ruby
+ include_http_adapter_for("<http_lib>")
+
+ request_count = 0
+ start_sinatra_app(:port => 7777) do
+ get('/') { "Response #{request_count += 1 }" }
+ end
+
+ require 'vcr'
+
+ VCR.configure do |c|
+ <configuration>
+ c.cassette_library_dir = 'cassettes'
+ c.around_http_request do |request|
+ VCR.use_cassette('global', :record => :new_episodes, &request)
+ end
+ end
+
+ puts "Response for request 1: " + response_body_for(:get, "http://localhost:7777/")
+ puts "Response for request 2: " + response_body_for(:get, "http://localhost:7777/")
+ """
+ When I run `ruby globally_handle_requests.rb`
+ Then it should pass with:
+ """
+ Response for request 1: Response 1
+ Response for request 2: Response 1
+ """
+ And the file "cassettes/global.yml" should contain "body: Response 1"
+
+ Examples:
+ | configuration | http_lib |
+ | c.hook_into :fakeweb | net/http |
+ | c.hook_into :webmock | net/http |
+ | c.hook_into :webmock | httpclient |
+ | c.hook_into :webmock | curb |
+ | c.hook_into :typhoeus | typhoeus |
+ | c.hook_into :excon | excon |
+ | c.hook_into :faraday | faraday (w/ net_http) |
+
View
44 features/hooks/before_http_request.feature
@@ -0,0 +1,44 @@
+Feature: before_http_request hook
+
+ The `before_http_request` hook gets called with each request
+ just before it proceeds. It can be used for many things:
+
+ * globally logging requests
+ * inserting a particular cassette based on the request URI host
+ * raising a timeout error
+
+ Scenario Outline: log all requests using a before_http_request hook
+ Given a file named "before_http_request.rb" with:
+ """ruby
+ include_http_adapter_for("<http_lib>")
+
+ start_sinatra_app(:port => 7777) do
+ get('/') { "Hello World" }
+ end
+
+ require 'vcr'
+
+ VCR.configure do |c|
+ <configuration>
+ c.cassette_library_dir = 'cassettes'
+ c.ignore_localhost = true
+ c.before_http_request do |request|
+ puts "before request: #{request.method} #{request.uri}"
+ end
+ end
+
+ make_http_request(:get, "http://localhost:7777/")
+ """
+ When I run `ruby before_http_request.rb`
+ Then it should pass with "before request: get http://localhost:7777/"
+
+ Examples:
+ | configuration | http_lib |
+ | c.hook_into :fakeweb | net/http |
+ | c.hook_into :webmock | net/http |
+ | c.hook_into :webmock | httpclient |
+ | c.hook_into :webmock | curb |
+ | c.hook_into :typhoeus | typhoeus |
+ | c.hook_into :excon | excon |
+ | c.hook_into :faraday | faraday (w/ net_http) |
+

0 comments on commit 4f1f713

Please sign in to comment.