Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Change http stubbing adapters to be modules rather than classes.

They should never be instantiated and don't really hold state.  A module is more appropriate here.
  • Loading branch information...
commit 5f5da7a1192c13c554a8b8d4ef33fb9a78019b1b 1 parent a2a9f54
Myron Marston authored
2  CHANGELOG.md
Source Rendered
@@ -8,6 +8,8 @@
8 8 * Use new `FakeWeb.allow_net_connect = /url regex/` feature to power the `ignore_localhost` VCR option rather
9 9 then toggling `FakeWeb.allow_net_connect` in our Net::HTTP extension.
10 10 * Optimized `VCR.http_stubbing_adapter.stub_requests` a bit.
  11 +* Changed the http stubbing adapters to be modules rather than classes. They should never be instantiated and
  12 + don't really hold state, so a module is more appropriate.
11 13
12 14 ## 1.1.0 (August 22, 2010)
13 15
4 lib/vcr.rb
@@ -8,7 +8,7 @@
8 8
9 9 require 'vcr/extensions/net_http_response'
10 10
11   -require 'vcr/http_stubbing_adapters/base'
  11 +require 'vcr/http_stubbing_adapters/common'
12 12
13 13 module VCR
14 14 extend self
@@ -77,4 +77,4 @@ def record_http_interaction(interaction)
77 77 def cassettes
78 78 @cassettes ||= []
79 79 end
80   -end
  80 +end
25 lib/vcr/http_stubbing_adapters/base.rb
... ... @@ -1,25 +0,0 @@
1   -module VCR
2   - module HttpStubbingAdapters
3   - autoload :FakeWeb, 'vcr/http_stubbing_adapters/fakeweb'
4   - autoload :WebMock, 'vcr/http_stubbing_adapters/webmock'
5   -
6   - class UnsupportedRequestMatchAttributeError < ArgumentError; end
7   -
8   - class Base
9   - class << self
10   - def meets_version_requirement?(version, required_version)
11   - major, minor, patch = *version.split('.').map { |v| v.to_i }
12   - req_major, req_minor, req_patch = *required_version.split('.').map { |v| v.to_i }
13   -
14   - return false if major < req_major
15   - return true if major > req_major
16   -
17   - return false if minor < req_minor
18   - return true if minor > req_minor
19   -
20   - patch >= req_patch
21   - end
22   - end
23   - end
24   - end
25   -end
23 lib/vcr/http_stubbing_adapters/common.rb
... ... @@ -0,0 +1,23 @@
  1 +module VCR
  2 + module HttpStubbingAdapters
  3 + autoload :FakeWeb, 'vcr/http_stubbing_adapters/fakeweb'
  4 + autoload :WebMock, 'vcr/http_stubbing_adapters/webmock'
  5 +
  6 + class UnsupportedRequestMatchAttributeError < ArgumentError; end
  7 +
  8 + module Common
  9 + def meets_version_requirement?(version, required_version)
  10 + major, minor, patch = *version.split('.').map { |v| v.to_i }
  11 + req_major, req_minor, req_patch = *required_version.split('.').map { |v| v.to_i }
  12 +
  13 + return false if major < req_major
  14 + return true if major > req_major
  15 +
  16 + return false if minor < req_minor
  17 + return true if minor > req_minor
  18 +
  19 + patch >= req_patch
  20 + end
  21 + end
  22 + end
  23 +end
147 lib/vcr/http_stubbing_adapters/fakeweb.rb
@@ -3,97 +3,98 @@
3 3
4 4 module VCR
5 5 module HttpStubbingAdapters
6   - class FakeWeb < Base
7   - class << self
8   - UNSUPPORTED_REQUEST_MATCH_ATTRIBUTES = [:body, :headers].freeze
9   - LOCALHOST_REGEX = %r|\Ahttps?://((\w+:)?\w+@)?(#{VCR::LOCALHOST_ALIASES.map { |a| Regexp.escape(a) }.join('|')})(:\d+)?/|i
10   -
11   - def check_version!
12   - unless meets_version_requirement?(::FakeWeb::VERSION, '1.3.0')
13   - raise "You are using FakeWeb #{::FakeWeb::VERSION}. VCR requires version 1.3.0 or greater."
14   - end
15   - end
  6 + module FakeWeb
  7 + include VCR::HttpStubbingAdapters::Common
  8 + extend self
16 9
17   - def http_connections_allowed?
18   - !!::FakeWeb.allow_net_connect?("http://some.url/besides/localhost")
19   - end
  10 + UNSUPPORTED_REQUEST_MATCH_ATTRIBUTES = [:body, :headers].freeze
  11 + LOCALHOST_REGEX = %r|\Ahttps?://((\w+:)?\w+@)?(#{VCR::LOCALHOST_ALIASES.map { |a| Regexp.escape(a) }.join('|')})(:\d+)?/|i
20 12
21   - def http_connections_allowed=(value)
22   - @http_connections_allowed = value
23   - update_fakeweb_allow_net_connect
  13 + def check_version!
  14 + unless meets_version_requirement?(::FakeWeb::VERSION, '1.3.0')
  15 + raise "You are using FakeWeb #{::FakeWeb::VERSION}. VCR requires version 1.3.0 or greater."
24 16 end
  17 + end
25 18
26   - def stub_requests(http_interactions, match_attributes)
27   - validate_match_attributes(match_attributes)
28   - requests = Hash.new { |h,k| h[k] = [] }
29   -
30   - http_interactions.each do |i|
31   - requests[i.request.matcher(match_attributes)] << i.response
32   - end
33   -
34   - requests.each do |request_matcher, responses|
35   - ::FakeWeb.register_uri(
36   - request_matcher.method || :any,
37   - request_matcher.uri,
38   - responses.map{ |r| response_hash(r) }
39   - )
40   - end
41   - end
  19 + def http_connections_allowed?
  20 + !!::FakeWeb.allow_net_connect?("http://some.url/besides/localhost")
  21 + end
42 22
43   - def create_stubs_checkpoint(checkpoint_name)
44   - checkpoints[checkpoint_name] = ::FakeWeb::Registry.instance.uri_map.dup
45   - end
  23 + def http_connections_allowed=(value)
  24 + @http_connections_allowed = value
  25 + update_fakeweb_allow_net_connect
  26 + end
46 27
47   - def restore_stubs_checkpoint(checkpoint_name)
48   - ::FakeWeb::Registry.instance.uri_map = checkpoints.delete(checkpoint_name)
49   - end
  28 + def stub_requests(http_interactions, match_attributes)
  29 + validate_match_attributes(match_attributes)
  30 + requests = Hash.new { |h,k| h[k] = [] }
50 31
51   - def request_stubbed?(request, match_attributes)
52   - validate_match_attributes(match_attributes)
53   - ::FakeWeb.registered_uri?(request.method, request.uri)
  32 + http_interactions.each do |i|
  33 + requests[i.request.matcher(match_attributes)] << i.response
54 34 end
55 35
56   - def request_uri(net_http, request)
57   - ::FakeWeb::Utility.request_uri_as_string(net_http, request)
  36 + requests.each do |request_matcher, responses|
  37 + ::FakeWeb.register_uri(
  38 + request_matcher.method || :any,
  39 + request_matcher.uri,
  40 + responses.map{ |r| response_hash(r) }
  41 + )
58 42 end
  43 + end
59 44
60   - def ignore_localhost=(value)
61   - @ignore_localhost = value
62   - update_fakeweb_allow_net_connect
63   - end
  45 + def create_stubs_checkpoint(checkpoint_name)
  46 + checkpoints[checkpoint_name] = ::FakeWeb::Registry.instance.uri_map.dup
  47 + end
64 48
65   - def ignore_localhost?
66   - @ignore_localhost
67   - end
  49 + def restore_stubs_checkpoint(checkpoint_name)
  50 + ::FakeWeb::Registry.instance.uri_map = checkpoints.delete(checkpoint_name)
  51 + end
68 52
69   - private
  53 + def request_stubbed?(request, match_attributes)
  54 + validate_match_attributes(match_attributes)
  55 + ::FakeWeb.registered_uri?(request.method, request.uri)
  56 + end
70 57
71   - def update_fakeweb_allow_net_connect
72   - ::FakeWeb.allow_net_connect = if @http_connections_allowed
73   - true
74   - elsif @ignore_localhost
75   - LOCALHOST_REGEX
76   - else
77   - false
78   - end
79   - end
  58 + def request_uri(net_http, request)
  59 + ::FakeWeb::Utility.request_uri_as_string(net_http, request)
  60 + end
80 61
81   - def checkpoints
82   - @checkpoints ||= {}
83   - end
  62 + def ignore_localhost=(value)
  63 + @ignore_localhost = value
  64 + update_fakeweb_allow_net_connect
  65 + end
84 66
85   - def response_hash(response)
86   - response.headers.merge(
87   - :body => response.body,
88   - :status => [response.status.code.to_s, response.status.message]
89   - )
  67 + def ignore_localhost?
  68 + @ignore_localhost
  69 + end
  70 +
  71 + private
  72 +
  73 + def update_fakeweb_allow_net_connect
  74 + ::FakeWeb.allow_net_connect = if @http_connections_allowed
  75 + true
  76 + elsif @ignore_localhost
  77 + LOCALHOST_REGEX
  78 + else
  79 + false
90 80 end
  81 + end
  82 +
  83 + def checkpoints
  84 + @checkpoints ||= {}
  85 + end
  86 +
  87 + def response_hash(response)
  88 + response.headers.merge(
  89 + :body => response.body,
  90 + :status => [response.status.code.to_s, response.status.message]
  91 + )
  92 + end
91 93
92   - def validate_match_attributes(match_attributes)
93   - invalid_attributes = match_attributes & UNSUPPORTED_REQUEST_MATCH_ATTRIBUTES
94   - if invalid_attributes.size > 0
95   - raise UnsupportedRequestMatchAttributeError.new("FakeWeb does not support matching requests on #{invalid_attributes.join(' or ')}")
96   - end
  94 + def validate_match_attributes(match_attributes)
  95 + invalid_attributes = match_attributes & UNSUPPORTED_REQUEST_MATCH_ATTRIBUTES
  96 + if invalid_attributes.size > 0
  97 + raise UnsupportedRequestMatchAttributeError.new("FakeWeb does not support matching requests on #{invalid_attributes.join(' or ')}")
97 98 end
98 99 end
99 100 end
123 lib/vcr/http_stubbing_adapters/webmock.rb
@@ -3,86 +3,87 @@
3 3
4 4 module VCR
5 5 module HttpStubbingAdapters
6   - class WebMock < Base
7   - class << self
8   - VERSION_REQUIREMENT = '1.3.3'
9   -
10   - def check_version!
11   - unless meets_version_requirement?(::WebMock.version, VERSION_REQUIREMENT)
12   - raise "You are using WebMock #{::WebMock.version}. VCR requires version #{VERSION_REQUIREMENT} or greater."
13   - end
14   - end
  6 + module WebMock
  7 + include VCR::HttpStubbingAdapters::Common
  8 + extend self
15 9
16   - def http_connections_allowed?
17   - ::WebMock::Config.instance.allow_net_connect
18   - end
  10 + VERSION_REQUIREMENT = '1.3.3'
19 11
20   - def http_connections_allowed=(value)
21   - ::WebMock::Config.instance.allow_net_connect = value
  12 + def check_version!
  13 + unless meets_version_requirement?(::WebMock.version, VERSION_REQUIREMENT)
  14 + raise "You are using WebMock #{::WebMock.version}. VCR requires version #{VERSION_REQUIREMENT} or greater."
22 15 end
  16 + end
23 17
24   - def stub_requests(http_interactions, match_attributes)
25   - requests = Hash.new { |h,k| h[k] = [] }
26   -
27   - http_interactions.each do |i|
28   - requests[i.request.matcher(match_attributes)] << i.response
29   - end
  18 + def http_connections_allowed?
  19 + ::WebMock::Config.instance.allow_net_connect
  20 + end
30 21
31   - requests.each do |request_matcher, responses|
32   - stub = ::WebMock.stub_request(request_matcher.method || :any, request_matcher.uri)
  22 + def http_connections_allowed=(value)
  23 + ::WebMock::Config.instance.allow_net_connect = value
  24 + end
33 25
34   - with_hash = request_signature_hash(request_matcher)
35   - stub = stub.with(with_hash) if with_hash.size > 0
  26 + def stub_requests(http_interactions, match_attributes)
  27 + requests = Hash.new { |h,k| h[k] = [] }
36 28
37   - stub.to_return(responses.map{ |r| response_hash(r) })
38   - end
  29 + http_interactions.each do |i|
  30 + requests[i.request.matcher(match_attributes)] << i.response
39 31 end
40 32
41   - def create_stubs_checkpoint(checkpoint_name)
42   - checkpoints[checkpoint_name] = ::WebMock::RequestRegistry.instance.request_stubs.dup
43   - end
  33 + requests.each do |request_matcher, responses|
  34 + stub = ::WebMock.stub_request(request_matcher.method || :any, request_matcher.uri)
44 35
45   - def restore_stubs_checkpoint(checkpoint_name)
46   - ::WebMock::RequestRegistry.instance.request_stubs = checkpoints.delete(checkpoint_name)
47   - end
  36 + with_hash = request_signature_hash(request_matcher)
  37 + stub = stub.with(with_hash) if with_hash.size > 0
48 38
49   - def request_stubbed?(request, match_attributes)
50   - matcher = request.matcher(match_attributes)
51   - !!::WebMock.registered_request?(::WebMock::RequestSignature.new(matcher.method || :any, request.uri, request_signature_hash(matcher)))
  39 + stub.to_return(responses.map{ |r| response_hash(r) })
52 40 end
  41 + end
53 42
54   - def request_uri(net_http, request)
55   - ::WebMock::NetHTTPUtility.request_signature_from_request(net_http, request).uri.to_s
56   - end
  43 + def create_stubs_checkpoint(checkpoint_name)
  44 + checkpoints[checkpoint_name] = ::WebMock::RequestRegistry.instance.request_stubs.dup
  45 + end
57 46
58   - def ignore_localhost=(value)
59   - ::WebMock::Config.instance.allow_localhost = value
60   - end
  47 + def restore_stubs_checkpoint(checkpoint_name)
  48 + ::WebMock::RequestRegistry.instance.request_stubs = checkpoints.delete(checkpoint_name)
  49 + end
61 50
62   - def ignore_localhost?
63   - ::WebMock::Config.instance.allow_localhost
64   - end
  51 + def request_stubbed?(request, match_attributes)
  52 + matcher = request.matcher(match_attributes)
  53 + !!::WebMock.registered_request?(::WebMock::RequestSignature.new(matcher.method || :any, request.uri, request_signature_hash(matcher)))
  54 + end
65 55
66   - private
  56 + def request_uri(net_http, request)
  57 + ::WebMock::NetHTTPUtility.request_signature_from_request(net_http, request).uri.to_s
  58 + end
67 59
68   - def request_signature_hash(request_matcher)
69   - signature = {}
70   - signature[:body] = request_matcher.body if request_matcher.match_requests_on?(:body)
71   - signature[:headers] = request_matcher.headers if request_matcher.match_requests_on?(:headers)
72   - signature
73   - end
  60 + def ignore_localhost=(value)
  61 + ::WebMock::Config.instance.allow_localhost = value
  62 + end
74 63
75   - def response_hash(response)
76   - {
77   - :body => response.body,
78   - :status => [response.status.code.to_i, response.status.message],
79   - :headers => response.headers
80   - }
81   - end
  64 + def ignore_localhost?
  65 + ::WebMock::Config.instance.allow_localhost
  66 + end
82 67
83   - def checkpoints
84   - @checkpoints ||= {}
85   - end
  68 + private
  69 +
  70 + def request_signature_hash(request_matcher)
  71 + signature = {}
  72 + signature[:body] = request_matcher.body if request_matcher.match_requests_on?(:body)
  73 + signature[:headers] = request_matcher.headers if request_matcher.match_requests_on?(:headers)
  74 + signature
  75 + end
  76 +
  77 + def response_hash(response)
  78 + {
  79 + :body => response.body,
  80 + :status => [response.status.code.to_i, response.status.message],
  81 + :headers => response.headers
  82 + }
  83 + end
  84 +
  85 + def checkpoints
  86 + @checkpoints ||= {}
86 87 end
87 88 end
88 89 end

0 comments on commit 5f5da7a

Please sign in to comment.
Something went wrong with that request. Please try again.