faraday-hot_mock 0.5.0 → 0.6.1

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: d42875d59eb57c21511d1c8edb287415cfae818f0a5af8b3be38f06086c8920f
4
- data.tar.gz: 22551de3ae9e35709702c762880abbf797407f4e95e4f84a53e6bc3f930c4337
3
+ metadata.gz: c7d36fd4d278626550e98a108fb9878190fa829ff10216806897bbcf8601ef20
4
+ data.tar.gz: 88f1cc037062e2581de849d0043a75f17ef23e5cdbfdb6eabd5311ce90ce0136
5
5
  SHA512:
6
- metadata.gz: 8bf83f23c923e2459e5cc1d50005959df3f9113cb029c2cdf28f6a28967f67f2c00003d1ee2f4fcd25a00e2787665ea73056759137266f3e22de604c8cda606e
7
- data.tar.gz: 30dbdcccbb2bad3aa706614b4b1b759ec18e7e995e422cf8fa6beb568034940732cce587a345e2874fb2afd68d60e1f308817ce815defdcadb95bed016e6ff5a
6
+ metadata.gz: 2757eff042477296cfc296c8a4c0c2bcab0dd9d411d8406b6147252510ed2cf1a8063d97f2fe754c3195f610ee6e24678766f96a40b0f49f6fc3bcf5555362bb
7
+ data.tar.gz: c2e8063cd973a938240a2b2835955c80137f50e2f3e4874cebcb9f65a9aa3afacf537a4ef42e19cddb096b3aedf5fceb180458ced84fc3e901a3cb98595da186
data/README.md CHANGED
@@ -88,9 +88,11 @@ In most cases, it makes sense to not check in the mocks for any environment, so
88
88
  lib/faraday/mocks/**
89
89
  ```
90
90
 
91
+ If you're using scenarios, however, it's probably useful to check in any environment directory that has scenarios since they're only activated when a scenario is directly selected.
92
+
91
93
  ### Scenarios
92
94
 
93
- You can use directories to conditionally group mocks. For example, you might want a "success" scenario and an "failure" scenario.
95
+ You can use directories to conditionally group mocks. For example, you might want a "success" scenario and a "failure" scenario.
94
96
 
95
97
  To do that, simply create the `/scenarios/success` and `/scenarios/failure` subdirectories within `lib/faraday/mocks/#{Rails.env}/`, and place the appropriate mock files in each, probably with the same endpoints but with different responses.
96
98
 
@@ -100,6 +102,24 @@ When a scenario is active, only mocks in that scenario directory will be conside
100
102
 
101
103
  To use the mocks not in the `scenarios` directory again, simply set `Faraday::HotMock.scenario = nil`.
102
104
 
105
+ ### VCR Mode
106
+
107
+ VCR mode basically calls `Faraday::HotMock.record` on all requests made until it's turned off. You can start it with:
108
+
109
+ ```ruby
110
+ Faraday::HotMock.vcr = true
111
+ ```
112
+
113
+ Just like using `Faraday::HotMock.record` individually, these mocks are recorded to the main default file (`Faraday::HotMock.hot_mock_file`)
114
+
115
+ You can also record directly into a scenario, which will create the scenario directory structure, switch the current scenario and begin recording responses. This will continue until you set `Faraday::HotMock.vcr` to `false` or another scenario name.
116
+
117
+ ```ruby
118
+ Faraday::HotMock.vcr = :new_scenario
119
+ ```
120
+
121
+ > NOTE: VCR Mode does not overwrite previously recorded mocks
122
+
103
123
  ### Testing
104
124
 
105
125
  In tests, you can certainly use a mocking library of choice. In many cases, that might be easier. This is because Faraday::HotMock is built for quick iteration using runtime-loaded YAML files, which isn't needed in tests.
@@ -18,19 +18,23 @@ module Faraday
18
18
  def call(env)
19
19
  super
20
20
 
21
- if Rails.env.production?
21
+ if Rails.env.production? || Faraday::HotMock.disabled?
22
22
  return @fallback_adapter.new(@app, @options).call(env)
23
23
  end
24
24
 
25
- if Faraday::HotMock.enabled? && (mock = Faraday::HotMock.mocked?(method: env.method, url: env.url))
25
+ if Faraday::HotMock.vcr && !Faraday::HotMock.mocked?(method: env.method, url: env.url)
26
+ case Faraday::HotMock.vcr
27
+ when Symbol, String
28
+ Faraday::HotMock.record(method: env.method, url: env.url, into_scenario: Faraday::HotMock.vcr)
29
+ else
30
+ Faraday::HotMock.record(method: env.method, url: env.url)
31
+ end
32
+ end
33
+
34
+ if (mock = Faraday::HotMock.mocked?(method: env.method, url: env.url))
26
35
  interpolate(mock, env) if mock_interpolated?(mock)
27
36
 
28
- save_response(
29
- env,
30
- mock["status"] || 200,
31
- mock["body"] || "",
32
- (mock["headers"] || {}).merge("x-hot-mocked" => "true")
33
- )
37
+ mock_response!(env, mock)
34
38
  else
35
39
  @fallback_adapter.new(@app, @options).call(env)
36
40
  end
@@ -51,6 +55,15 @@ module Faraday
51
55
 
52
56
  mock["body"].merge!(interpolated_hash || {})
53
57
  end
58
+
59
+ def mock_response!(env, mock)
60
+ save_response(
61
+ env,
62
+ mock["status"] || 200,
63
+ mock["body"] || "",
64
+ (mock["headers"] || {}).merge(Faraday::HotMock::HEADERS[:mocked] => "true")
65
+ )
66
+ end
54
67
  end
55
68
  end
56
69
  end
@@ -1,5 +1,5 @@
1
1
  module Faraday
2
2
  module HotMock
3
- VERSION = "0.5.0"
3
+ VERSION = "0.6.1"
4
4
  end
5
5
  end
@@ -7,7 +7,13 @@ module Faraday
7
7
  module HotMock
8
8
  extend self
9
9
 
10
- attr_accessor :scenario
10
+ attr_accessor :scenario, :vcr
11
+
12
+ HEADERS = {
13
+ recorded: "x-hot-mock-recorded-at",
14
+ mocked: "x-hot-mocked"
15
+ }
16
+ FILE_NAME = "hot_mocks.yml"
11
17
 
12
18
  def disable!
13
19
  FileUtils.rm_f(hot_mocking_file)
@@ -66,22 +72,25 @@ module Faraday
66
72
  mocks.find { |entry| entry["method"].to_s.upcase == method.to_s.upcase && Regexp.new(entry["url_pattern"]).match?(url.to_s) } || false
67
73
  end
68
74
 
69
- def record(method:, url:)
75
+ def record(method:, url:, into_scenario: nil)
76
+ self.scenario = into_scenario if into_scenario.present?
77
+
70
78
  return false if mocked?(method:, url:)
71
79
 
72
80
  faraday = Faraday.new
73
81
 
74
82
  response = faraday.send(method.downcase.to_sym, url)
75
83
 
84
+ FileUtils.mkdir_p(hot_mock_dir)
76
85
  FileUtils.touch(hot_mock_file)
77
86
 
78
87
  hot_mocks = YAML.load_file(hot_mock_file) || []
79
88
 
80
89
  hot_mocks << {
81
90
  "method" => method.to_s.upcase,
82
- "url_pattern" => url,
91
+ "url_pattern" => url.to_s,
83
92
  "status" => response.status,
84
- "headers" => response.headers.to_h.merge("x-hot-mock-recorded-at" => Time.now.utc.iso8601),
93
+ "headers" => response.headers.to_h.merge(HEADERS[:recorded] => Time.now.utc.iso8601),
85
94
  "body" => response.body
86
95
  }
87
96
 
@@ -103,9 +112,9 @@ module Faraday
103
112
 
104
113
  hot_mocks << {
105
114
  "method" => method.to_s.upcase,
106
- "url_pattern" => url,
115
+ "url_pattern" => url.to_s,
107
116
  "status" => response.status,
108
- "headers" => response.headers.to_h.merge("x-hot-mock-recorded-at" => Time.now.utc.iso8601, "x-hot-mock" => "true"),
117
+ "headers" => response.headers.to_h.merge(HEADERS[:recorded] => Time.now.utc.iso8601, "x-hot-mock" => "true"),
109
118
  "body" => response.body
110
119
  }
111
120
 
@@ -115,7 +124,11 @@ module Faraday
115
124
  end
116
125
 
117
126
  def hot_mock_dir
118
- Rails.root.join "lib/faraday/mocks/#{Rails.env}"
127
+ if self.scenario
128
+ Rails.root.join "lib/faraday/mocks/#{Rails.env}/scenarios/#{self.scenario}"
129
+ else
130
+ Rails.root.join "lib/faraday/mocks/#{Rails.env}"
131
+ end
119
132
  end
120
133
 
121
134
  def scenario_dir
@@ -127,7 +140,7 @@ module Faraday
127
140
  end
128
141
 
129
142
  def hot_mock_file
130
- Rails.root.join(hot_mock_dir, "hot_mocks.yml")
143
+ Rails.root.join(hot_mock_dir, FILE_NAME)
131
144
  end
132
145
 
133
146
  def mocks
@@ -144,12 +157,38 @@ module Faraday
144
157
  end
145
158
 
146
159
  def all_hot_mock_files
147
- if scenario
148
- Dir.glob(File.join(hot_mock_dir, "scenarios", scenario.to_s, "**", "*.{yml,yaml}"))
160
+ if scenario.present?
161
+ Dir.glob(File.join(hot_mock_dir, "**", "*.{yml,yaml}"))
149
162
  else
150
163
  Dir.glob(File.join(hot_mock_dir, "**", "*.{yml,yaml}")).reject { |path| path.include?("/scenarios/") }
151
164
  end
152
165
  end
166
+
167
+ def scenario
168
+ YAML.load_file(settings_file)["scenario"] rescue nil
169
+ end
170
+
171
+ def scenario=(name)
172
+ settings = YAML.load_file(settings_file) rescue {}
173
+
174
+ settings["scenario"] = name
175
+ File.write(settings_file, settings.to_yaml)
176
+ end
177
+
178
+ def settings_file
179
+ Rails.root.join("tmp/hot_mock_settings.yml")
180
+ end
181
+
182
+ def vcr
183
+ YAML.load_file(settings_file)["vcr"] rescue nil
184
+ end
185
+
186
+ def vcr=(value)
187
+ settings = YAML.load_file(settings_file) rescue {}
188
+
189
+ settings["vcr"] = value
190
+ File.write(settings_file, settings.to_yaml)
191
+ end
153
192
  end
154
193
  end
155
194
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday-hot_mock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Hogge