ephemeral_response 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.markdown +7 -0
- data/README.markdown +19 -10
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/examples/custom_cache_key.rb +1 -1
- data/lib/ephemeral_response.rb +3 -1
- data/lib/ephemeral_response/fixture.rb +1 -1
- data/lib/ephemeral_response/request.rb +12 -0
- data/spec/ephemeral_response/fixture_spec.rb +5 -0
- data/spec/integration/custom_identifier_spec.rb +35 -11
- metadata +4 -2
data/History.markdown
CHANGED
data/README.markdown
CHANGED
@@ -6,7 +6,7 @@ _Save HTTP responses to give your tests a hint of reality._
|
|
6
6
|
## Premise
|
7
7
|
|
8
8
|
Web responses are volatile. Servers go down, API's change, responses change and
|
9
|
-
|
9
|
+
every time something changes, your tests should fail. Mocking out web responses
|
10
10
|
may speed up your test suite but the tests essentially become lies. Ephemeral
|
11
11
|
Response encourages you to run your tests against real web services while
|
12
12
|
keeping your test suite snappy by caching the responses and reusing them until
|
@@ -56,26 +56,35 @@ I'd recommend git ignoring this directory to ensure your tests always hit the
|
|
56
56
|
remote service at least once and to prevent credentials (like API keys) from
|
57
57
|
being stored in your repo.
|
58
58
|
|
59
|
-
|
59
|
+
## Customize how requests get matched by the cache
|
60
60
|
|
61
|
-
|
62
|
-
|
63
|
-
|
61
|
+
Every request gets a unique key that gets added to the cache. Additional
|
62
|
+
requests attempt to generate this same key so that their responses can be
|
63
|
+
fetched from the cache.
|
64
|
+
|
65
|
+
The default key is a combination of the URI, request method, and request body.
|
66
|
+
Occasionally, these properties contain variations which cannot be consistently
|
67
|
+
reproduced. Time is a good example. If your query string or post data
|
68
|
+
references the current time then every request will generate a different key
|
69
|
+
therefore no fixtures will be loaded. You can overcome this issue by
|
70
|
+
registering a custom key generation block per host.
|
71
|
+
|
72
|
+
An example may help clear this up.
|
64
73
|
|
65
74
|
EphemeralResponse.configure do |config|
|
66
75
|
config.register('example.com') do |request|
|
67
|
-
"#{request.method}{request.path}"
|
76
|
+
"#{request.uri.host}#{request.method}#{request.path}"
|
68
77
|
end
|
69
78
|
end
|
70
79
|
|
71
|
-
# This will
|
80
|
+
# This will get cached
|
72
81
|
Net::HTTP.start('example.com') do |http|
|
73
82
|
get = Net::HTTP::Get.new('/')
|
74
83
|
get['Date'] = Time.now.to_s
|
75
84
|
http.request(get)
|
76
85
|
end
|
77
86
|
|
78
|
-
# This
|
87
|
+
# This is read from the cache even though the date is different
|
79
88
|
Net::HTTP.start('example.com') do |http|
|
80
89
|
get = Net::HTTP::Get.new('/')
|
81
90
|
get['Date'] = "Wed Dec 31 19:00:00 -0500 1969"
|
@@ -84,7 +93,7 @@ create the unique key for that request. An example may help clear this up.
|
|
84
93
|
|
85
94
|
Take a look in `examples/custom_cache_key.rb` to see this in action.
|
86
95
|
|
87
|
-
|
96
|
+
## Configuration
|
88
97
|
|
89
98
|
Change the fixture directory; defaults to "spec/fixtures/ephemeral\_response"
|
90
99
|
|
@@ -101,7 +110,7 @@ method `one_day`
|
|
101
110
|
one_day * 30 # Expire in thirty days: 60 * 60 * 24 * 30
|
102
111
|
end
|
103
112
|
|
104
|
-
|
113
|
+
### Selenium Tip
|
105
114
|
|
106
115
|
Always allow requests to be made to a host by adding it to the white list.
|
107
116
|
Helpful when running ephemeral response with selenium which makes requests to
|
data/Rakefile
CHANGED
@@ -12,7 +12,7 @@ begin
|
|
12
12
|
}
|
13
13
|
gem.email = "sandro.turriate@gmail.com"
|
14
14
|
gem.homepage = "http://github.com/sandro/ephemeral_response"
|
15
|
-
gem.authors = ["Sandro Turriate"]
|
15
|
+
gem.authors = ["Sandro Turriate", "Les Hill"]
|
16
16
|
gem.add_development_dependency "rspec", ">= 1.2.9"
|
17
17
|
gem.add_development_dependency "yard", ">= 0.5.0"
|
18
18
|
gem.add_development_dependency "fakefs", ">= 0.2.1"
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.1
|
data/lib/ephemeral_response.rb
CHANGED
@@ -1,13 +1,15 @@
|
|
1
1
|
require 'net/http'
|
2
2
|
require 'fileutils'
|
3
3
|
require 'time'
|
4
|
+
require 'delegate'
|
4
5
|
require 'digest/sha1'
|
5
6
|
require 'yaml'
|
6
7
|
require 'ephemeral_response/configuration'
|
8
|
+
require 'ephemeral_response/request'
|
7
9
|
require 'ephemeral_response/fixture'
|
8
10
|
|
9
11
|
module EphemeralResponse
|
10
|
-
VERSION = "0.3.
|
12
|
+
VERSION = "0.3.1".freeze
|
11
13
|
|
12
14
|
def self.activate
|
13
15
|
deactivate
|
@@ -120,7 +120,7 @@ module EphemeralResponse
|
|
120
120
|
end
|
121
121
|
|
122
122
|
def registered_identifier
|
123
|
-
identity = Configuration.host_registry[uri.host].call(request) and identity.to_s
|
123
|
+
identity = Configuration.host_registry[uri.host].call(Request.new(uri, request)) and identity.to_s
|
124
124
|
end
|
125
125
|
end
|
126
126
|
end
|
@@ -206,6 +206,11 @@ describe EphemeralResponse::Fixture do
|
|
206
206
|
|
207
207
|
context "with a registration for the host" do
|
208
208
|
|
209
|
+
it "returns a request object with the uri" do
|
210
|
+
EphemeralResponse.configure {|c| c.register('example.com') {|request| request.uri } }
|
211
|
+
subject.identifier.should == Digest::SHA1.hexdigest(uri.to_s)
|
212
|
+
end
|
213
|
+
|
209
214
|
it "returns the hash of the block's return value as a string" do
|
210
215
|
EphemeralResponse.configure {|c| c.register('example.com') {|request| :identifier } }
|
211
216
|
subject.identifier.should == Digest::SHA1.hexdigest(:identifier.to_s)
|
@@ -4,24 +4,48 @@ describe 'Custom Identifiers' do
|
|
4
4
|
let(:uri) { URI.parse("http://localhost:9876/") }
|
5
5
|
let(:http) { Net::HTTP.new uri.host, uri.port }
|
6
6
|
let(:post) { Net::HTTP::Post.new '/' }
|
7
|
+
let(:get) { Net::HTTP::Get.new '/' }
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
9
|
+
context "with customization based on request body" do
|
10
|
+
before do
|
11
|
+
clear_fixtures
|
12
|
+
post.set_form_data :name => :joe
|
13
|
+
EphemeralResponse.configure do |config|
|
14
|
+
config.register(uri.host) do |request|
|
15
|
+
request.body.split("=").first
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
EphemeralResponse::RackReflector.while_running do
|
20
|
+
@post_response = http.start {|h| h.request(post) }
|
14
21
|
end
|
15
22
|
end
|
16
23
|
|
17
|
-
|
18
|
-
|
24
|
+
it "returns the same fixture when the post data is slightly different" do
|
25
|
+
post.set_form_data :name => :jane
|
26
|
+
http.start {|h| h.request(post) }.body.should == @post_response.body
|
19
27
|
end
|
20
28
|
end
|
21
29
|
|
22
|
-
|
23
|
-
|
24
|
-
|
30
|
+
context "when the customization doesn't match" do
|
31
|
+
before do
|
32
|
+
clear_fixtures
|
33
|
+
EphemeralResponse.configure do |config|
|
34
|
+
config.register(uri.host) do |request|
|
35
|
+
if Net::HTTP::Post === request
|
36
|
+
request.body.split("=").first
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
EphemeralResponse::RackReflector.while_running do
|
42
|
+
@post_response = http.start {|h| h.request(get) }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
it "falls back to the default identifier and returns the correct fixture" do
|
47
|
+
http.start {|h| h.request(get) }.body.should == @post_response.body
|
48
|
+
end
|
25
49
|
end
|
26
50
|
|
27
51
|
end
|
metadata
CHANGED
@@ -5,11 +5,12 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 3
|
8
|
-
-
|
9
|
-
version: 0.3.
|
8
|
+
- 1
|
9
|
+
version: 0.3.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Sandro Turriate
|
13
|
+
- Les Hill
|
13
14
|
autorequire:
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
@@ -96,6 +97,7 @@ files:
|
|
96
97
|
- lib/ephemeral_response/configuration.rb
|
97
98
|
- lib/ephemeral_response/fixture.rb
|
98
99
|
- lib/ephemeral_response/net_http.rb
|
100
|
+
- lib/ephemeral_response/request.rb
|
99
101
|
- spec/ephemeral_response/configuration_spec.rb
|
100
102
|
- spec/ephemeral_response/fixture_spec.rb
|
101
103
|
- spec/ephemeral_response/net_http_spec.rb
|