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 +4 -4
- data/README.md +21 -1
- data/lib/faraday/hot_mock/adapter.rb +21 -8
- data/lib/faraday/hot_mock/version.rb +1 -1
- data/lib/faraday/hot_mock.rb +49 -10
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c7d36fd4d278626550e98a108fb9878190fa829ff10216806897bbcf8601ef20
|
|
4
|
+
data.tar.gz: 88f1cc037062e2581de849d0043a75f17ef23e5cdbfdb6eabd5311ce90ce0136
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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
|
|
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.
|
|
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
|
-
|
|
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
|
data/lib/faraday/hot_mock.rb
CHANGED
|
@@ -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(
|
|
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(
|
|
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
|
-
|
|
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,
|
|
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, "
|
|
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
|
|