rspec-cassette 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 67dfa93eac9cae1c07b731184e9011207a44ba31f961f14662aac20c84dca3b7
4
- data.tar.gz: ed094afc5cb2feccb559efb357999421faca1e6711eb5817ffde979d928b9230
3
+ metadata.gz: 19c996b45d630d32c304464a67035dbed686c5af31202511c7d2e6d75f32f2b9
4
+ data.tar.gz: 78d562bd3206c5766381b5ee2bc82d6f9cf378a024f42ed266c66fba5972a7a1
5
5
  SHA512:
6
- metadata.gz: 28419926d4f0d91d682d0edce2a745f6245dd167c3ea83ff3d8276ae34357b4dfeffd656875bc8ce34ba0d439dcd87cb78e55ca1b7cceb9d8b490248c78f56c3
7
- data.tar.gz: 32552c50d74803caf941e65bc4f31ef2584c9d7a2a973ef6f58437ce36862299e303718e691d71d7bf6223f6e831f92bdf5c7c225ab70852b10d40224dd2ff64
6
+ metadata.gz: e287877c9526172a2833fc232e59373eb4690384306a26964a21a57a0ed2bc5b81d5d21f06e76324f1385a6b0dbcc32703d494754431451a2dabcdb436da2f27
7
+ data.tar.gz: 7ca0839f025367842756ef94a832b1ac13b89d86706d72eace920a74956316c88a48c1ec3585b0fd9f8b95ab63cab83ccb1324a321fbde151070e47c15f593af
data/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.4.0 (2026-02-17)
4
+
5
+ - Add `ignore_request` block support for custom passthrough conditions.
6
+ - Extend `NetConnectManager` to translate `ignore_request` blocks into WebMock-compatible `allow` matchers.
7
+ - Add net connection controls compatible with VCR migration use cases:
8
+ - `allow_http_connections_when_no_cassette`
9
+ - `ignore_localhost`
10
+ - `ignore_hosts`
11
+ - Add `NetConnectManager` and wire it into the RSpec around hook to enforce and restore WebMock net-connect behavior per example.
12
+
3
13
  ## 0.3.0 (2026-02-17)
4
14
 
5
15
  - Improve sanitization to support non-ASCII characters in cassette names derived from VCR metadata.
data/README.md CHANGED
@@ -26,6 +26,10 @@ require "rspec/cassette/rspec_helper"
26
26
  RSpec::Cassette.configure do |config|
27
27
  config.cassettes_dir = "spec/fixtures/cassettes"
28
28
  config.default_match_on = %i[method uri]
29
+ config.allow_http_connections_when_no_cassette = false
30
+ config.ignore_localhost = true
31
+ config.ignore_hosts = ["selenium-hub", "chromedriver"]
32
+ config.ignore_request { |request| URI(request.uri).port == 9200 }
29
33
  end
30
34
  ```
31
35
 
@@ -68,6 +72,35 @@ it "matches body", use_cassette: "users/index", cassette_options: { match_on: %i
68
72
  end
69
73
  ```
70
74
 
75
+ ## Network Connection Controls
76
+
77
+ By default, `rspec-cassette` now blocks outgoing HTTP connections when no cassette is active:
78
+
79
+ ```ruby
80
+ RSpec::Cassette.configure do |config|
81
+ config.allow_http_connections_when_no_cassette = false
82
+ end
83
+ ```
84
+
85
+ Allow passthrough for localhost or specific hosts:
86
+
87
+ ```ruby
88
+ RSpec::Cassette.configure do |config|
89
+ config.ignore_localhost = true
90
+ config.ignore_hosts = ["selenium-hub", "chromedriver"]
91
+ end
92
+ ```
93
+
94
+ Use `ignore_request` for custom conditions:
95
+
96
+ ```ruby
97
+ RSpec::Cassette.configure do |config|
98
+ config.ignore_request do |request|
99
+ URI(request.uri).port == 9200
100
+ end
101
+ end
102
+ ```
103
+
71
104
  ## Migration Guide
72
105
 
73
106
  Before:
@@ -94,6 +127,10 @@ end
94
127
  | --- | --- | --- |
95
128
  | `cassettes_dir` | `spec/fixtures/cassettes` | Base directory for cassette files |
96
129
  | `default_match_on` | `[:method, :uri]` | WebMock matchers to apply |
130
+ | `allow_http_connections_when_no_cassette` | `false` | Allow real HTTP when no cassette is active |
131
+ | `ignore_localhost` | `false` | Allow localhost requests while other outgoing requests are blocked |
132
+ | `ignore_hosts` | `[]` | Allow specific hosts while other outgoing requests are blocked |
133
+ | `ignore_request` | none | Register predicate blocks to allow specific requests |
97
134
 
98
135
  ## Development
99
136
 
@@ -3,11 +3,24 @@
3
3
  module RSpec
4
4
  module Cassette
5
5
  class Configuration
6
- attr_accessor :cassettes_dir, :default_match_on
6
+ attr_accessor :cassettes_dir,
7
+ :default_match_on,
8
+ :allow_http_connections_when_no_cassette,
9
+ :ignore_localhost,
10
+ :ignore_hosts
11
+ attr_reader :ignore_request_blocks
7
12
 
8
13
  def initialize
9
14
  @cassettes_dir = "spec/fixtures/cassettes"
10
15
  @default_match_on = %i[method uri]
16
+ @allow_http_connections_when_no_cassette = false
17
+ @ignore_localhost = false
18
+ @ignore_hosts = []
19
+ @ignore_request_blocks = []
20
+ end
21
+
22
+ def ignore_request(&block)
23
+ @ignore_request_blocks << block if block
11
24
  end
12
25
  end
13
26
  end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "webmock"
4
+
5
+ module RSpec
6
+ module Cassette
7
+ class NetConnectManager
8
+ IgnoredRequest = Struct.new(:uri)
9
+
10
+ def initialize(configuration)
11
+ @configuration = configuration
12
+ end
13
+
14
+ def disable!
15
+ return if @configuration.allow_http_connections_when_no_cassette
16
+
17
+ options = {}
18
+ options[:allow_localhost] = true if @configuration.ignore_localhost
19
+
20
+ allow_list = build_allow_list
21
+ options[:allow] = allow_list unless allow_list.empty?
22
+
23
+ WebMock.disable_net_connect!(**options)
24
+ end
25
+
26
+ def restore!
27
+ return if @configuration.allow_http_connections_when_no_cassette
28
+
29
+ WebMock.allow_net_connect!
30
+ end
31
+
32
+ private
33
+
34
+ def build_allow_list
35
+ allow_list = @configuration.ignore_hosts.dup
36
+ allow_list.concat(ignore_request_matchers)
37
+ allow_list
38
+ end
39
+
40
+ def ignore_request_matchers
41
+ @configuration.ignore_request_blocks.map do |block|
42
+ lambda do |uri|
43
+ block.call(IgnoredRequest.new(uri.to_s))
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -24,8 +24,15 @@ RSpec.configure do |config|
24
24
  config.include RSpec::Cassette::RSpecHelper
25
25
 
26
26
  config.around(:each) do |example|
27
- resolved = RSpec::Cassette::MetadataResolver.new(example).resolve
28
- use_cassette(resolved[:cassette_name], **resolved[:cassette_options]) if resolved
29
- example.run
27
+ manager = RSpec::Cassette::NetConnectManager.new(RSpec::Cassette.configuration)
28
+ manager.disable!
29
+
30
+ begin
31
+ resolved = RSpec::Cassette::MetadataResolver.new(example).resolve
32
+ use_cassette(resolved[:cassette_name], **resolved[:cassette_options]) if resolved
33
+ example.run
34
+ ensure
35
+ manager.restore!
36
+ end
30
37
  end
31
38
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RSpec
4
4
  module Cassette
5
- VERSION = "0.3.0"
5
+ VERSION = "0.4.0"
6
6
  end
7
7
  end
@@ -6,6 +6,7 @@ require_relative "cassette/configuration"
6
6
  require_relative "cassette/cassette"
7
7
  require_relative "cassette/interaction"
8
8
  require_relative "cassette/metadata_resolver"
9
+ require_relative "cassette/net_connect_manager"
9
10
  require_relative "cassette/stub_registrar"
10
11
 
11
12
  module RSpec
@@ -13,6 +13,11 @@ module RSpec
13
13
  class Configuration
14
14
  attr_accessor cassettes_dir: String
15
15
  attr_accessor default_match_on: Array[Symbol]
16
+ attr_accessor allow_http_connections_when_no_cassette: bool
17
+ attr_accessor ignore_localhost: bool
18
+ attr_accessor ignore_hosts: Array[String]
19
+ attr_reader ignore_request_blocks: Array[Proc]
20
+ def ignore_request: () { (untyped request) -> untyped } -> void
16
21
  end
17
22
 
18
23
  def self.configuration: () -> Configuration
@@ -46,5 +51,11 @@ module RSpec
46
51
  def initialize: (untyped example) -> void
47
52
  def resolve: () -> Hash[Symbol, untyped]?
48
53
  end
54
+
55
+ class NetConnectManager
56
+ def initialize: (Configuration configuration) -> void
57
+ def disable!: () -> void
58
+ def restore!: () -> void
59
+ end
49
60
  end
50
61
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-cassette
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yudai Takada
@@ -54,6 +54,7 @@ files:
54
54
  - lib/rspec/cassette/errors.rb
55
55
  - lib/rspec/cassette/interaction.rb
56
56
  - lib/rspec/cassette/metadata_resolver.rb
57
+ - lib/rspec/cassette/net_connect_manager.rb
57
58
  - lib/rspec/cassette/rspec_helper.rb
58
59
  - lib/rspec/cassette/stub_registrar.rb
59
60
  - lib/rspec/cassette/version.rb