ephemeral_response 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -56,6 +56,34 @@ 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
+ ### Customize how requests get matched by the cache
60
+
61
+ For any given host a block of code can be run to determine the cache key for
62
+ the request. The request object is yielded to the block and can be used to
63
+ create the unique key for that request. An example may help clear this up.
64
+
65
+ EphemeralResponse.configure do |config|
66
+ config.register('example.com') do |request|
67
+ "#{request.method}{request.path}"
68
+ end
69
+ end
70
+
71
+ # This will be cached
72
+ Net::HTTP.start('example.com') do |http|
73
+ get = Net::HTTP::Get.new('/')
74
+ get['Date'] = Time.now.to_s
75
+ http.request(get)
76
+ end
77
+
78
+ # This will read from the cache even though the date is different
79
+ Net::HTTP.start('example.com') do |http|
80
+ get = Net::HTTP::Get.new('/')
81
+ get['Date'] = "Wed Dec 31 19:00:00 -0500 1969"
82
+ http.request(get)
83
+ end
84
+
85
+ Take a look in `examples/custom_cache_key.rb` to see this in action.
86
+
59
87
  ### Configuration
60
88
 
61
89
  Change the fixture directory; defaults to "spec/fixtures/ephemeral\_response"
@@ -73,11 +101,13 @@ method `one_day`
73
101
  one_day * 30 # Expire in thirty days: 60 * 60 * 24 * 30
74
102
  end
75
103
 
104
+ #### Selenium
105
+
76
106
  Always allow requests to be made to a host by adding it to the white list.
77
107
  Helpful when running ephemeral response with selenium which makes requests to
78
108
  the local server.
79
109
 
80
- EphemeralResponse::Configuration.white_list = "localhost", "smackaho.st"
110
+ EphemeralResponse::Configuration.white_list = "localhost", "127.0.0.1"
81
111
 
82
112
  Never let fixtures expire by setting skip\_expiration to true.
83
113
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.3.0
@@ -0,0 +1,28 @@
1
+ $LOAD_PATH.unshift("lib")
2
+ require 'rubygems'
3
+ require 'lib/ephemeral_response'
4
+ require 'benchmark'
5
+
6
+ EphemeralResponse::Configuration.expiration = 15
7
+ EphemeralResponse.activate
8
+
9
+ EphemeralResponse.configure do |config|
10
+ config.register('example.com') do |request|
11
+ "#{request.method}#{request.path}"
12
+ end
13
+ end
14
+
15
+ def benchmark_request(number=1)
16
+ uri = URI.parse('http://example.com/')
17
+ time = Benchmark.realtime do
18
+ Net::HTTP.start(uri.host) do |http|
19
+ get = Net::HTTP::Get.new('/')
20
+ get['Date'] = Time.now.to_s
21
+ http.request(get)
22
+ end
23
+ end
24
+ sleep 1
25
+ puts "Request #{number} took #{time} secs"
26
+ end
27
+
28
+ 5.times {|n| benchmark_request n + 1 }
@@ -7,7 +7,7 @@ require 'ephemeral_response/configuration'
7
7
  require 'ephemeral_response/fixture'
8
8
 
9
9
  module EphemeralResponse
10
- VERSION = "0.2.1".freeze
10
+ VERSION = "0.3.0".freeze
11
11
 
12
12
  def self.activate
13
13
  deactivate
@@ -19,11 +19,20 @@ module EphemeralResponse
19
19
  @expiration || one_day
20
20
  end
21
21
 
22
+ def host_registry
23
+ @host_registry ||= Hash.new(lambda {})
24
+ end
25
+
26
+ def register(host, &block)
27
+ host_registry[host] = block
28
+ end
29
+
22
30
  def reset
23
31
  @expiration = nil
24
32
  @fixture_directory = nil
25
33
  @white_list = nil
26
34
  @skip_expiration = nil
35
+ @host_registry = nil
27
36
  end
28
37
 
29
38
  def skip_expiration
@@ -53,12 +53,6 @@ module EphemeralResponse
53
53
  yield self if block_given?
54
54
  end
55
55
 
56
- def ==(other)
57
- %w(request_identifier uri_identifier created_at response).all? do |attribute|
58
- send(attribute) == other.send(attribute)
59
- end
60
- end
61
-
62
56
  def expired?
63
57
  !Configuration.skip_expiration && (created_at + Configuration.expiration) < Time.now
64
58
  end
@@ -68,7 +62,7 @@ module EphemeralResponse
68
62
  end
69
63
 
70
64
  def identifier
71
- Digest::SHA1.hexdigest("#{uri_identifier}#{request_identifier}")
65
+ Digest::SHA1.hexdigest(registered_identifier || default_identifier)
72
66
  end
73
67
 
74
68
  def method
@@ -94,10 +88,6 @@ module EphemeralResponse
94
88
  end
95
89
  end
96
90
 
97
- def request_identifier
98
- request.to_yaml.split(//).sort
99
- end
100
-
101
91
  def save
102
92
  FileUtils.mkdir_p Configuration.fixture_directory
103
93
  File.open(path, 'w') do |f|
@@ -121,8 +111,16 @@ module EphemeralResponse
121
111
  Marshal.load(Marshal.dump(object))
122
112
  end
123
113
 
114
+ def default_identifier
115
+ "#{uri_identifier}#{request.method}#{request.body}"
116
+ end
117
+
124
118
  def generate_file_name
125
119
  "#{normalized_name}_#{identifier[0..6]}.yml"
126
120
  end
121
+
122
+ def registered_identifier
123
+ identity = Configuration.host_registry[uri.host].call(request) and identity.to_s
124
+ end
127
125
  end
128
126
  end
@@ -71,14 +71,17 @@ describe EphemeralResponse::Configuration do
71
71
  subject.skip_expiration.should == false
72
72
  end
73
73
 
74
- it "resets expiration, fixture directory, expiration, and white list to the defaults" do
75
- end
76
-
77
74
  it "resets white list after the default has been modified" do
78
75
  subject.white_list << "localhost"
79
76
  subject.reset
80
77
  subject.white_list.should be_empty
81
78
  end
79
+
80
+ it "resets the host_registry" do
81
+ subject.register('example.com') {}
82
+ subject.reset
83
+ subject.host_registry.should be_empty
84
+ end
82
85
  end
83
86
 
84
87
  describe "#white_list" do
@@ -116,4 +119,12 @@ describe EphemeralResponse::Configuration do
116
119
  subject.skip_expiration.should == false
117
120
  end
118
121
  end
122
+
123
+ describe "#register" do
124
+ it "registers the block for the host" do
125
+ block = Proc.new {}
126
+ subject.register('example.com', &block)
127
+ subject.host_registry['example.com'].should == block
128
+ end
129
+ end
119
130
  end
@@ -197,10 +197,26 @@ describe EphemeralResponse::Fixture do
197
197
  let(:uri) { URI.parse "http://example.com/" }
198
198
  subject { EphemeralResponse::Fixture.new uri, request }
199
199
 
200
- it "hashes the uri_identifier with request_identifier" do
201
- Digest::SHA1.should_receive(:hexdigest).with("#{subject.uri_identifier}#{subject.request_identifier}")
202
- subject.identifier
200
+ context "without a registration for the host" do
201
+ it "hashes the uri_identifier with method and the post body" do
202
+ Digest::SHA1.should_receive(:hexdigest).with("#{subject.uri_identifier}#{request.method}#{request.body}")
203
+ subject.identifier
204
+ end
203
205
  end
206
+
207
+ context "with a registration for the host" do
208
+
209
+ it "returns the hash of the block's return value as a string" do
210
+ EphemeralResponse.configure {|c| c.register('example.com') {|request| :identifier } }
211
+ subject.identifier.should == Digest::SHA1.hexdigest(:identifier.to_s)
212
+ end
213
+
214
+ it "returns the hash of the default identifier when the block returns nil" do
215
+ EphemeralResponse.configure {|c| c.register('example.com') {|request| } }
216
+ subject.identifier.should == Digest::SHA1.hexdigest(subject.send(:default_identifier))
217
+ end
218
+ end
219
+
204
220
  end
205
221
 
206
222
  describe "#uri_identifier" do
@@ -322,6 +338,5 @@ describe EphemeralResponse::Fixture do
322
338
  end
323
339
  end
324
340
  end
325
-
326
341
  end
327
342
  end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Custom Identifiers' do
4
+ let(:uri) { URI.parse("http://localhost:9876/") }
5
+ let(:http) { Net::HTTP.new uri.host, uri.port }
6
+ let(:post) { Net::HTTP::Post.new '/' }
7
+
8
+ before do
9
+ clear_fixtures
10
+ post.set_form_data :same => :new
11
+ EphemeralResponse.configure do |config|
12
+ config.register(uri.host) do |request|
13
+ request.body.split("=").first
14
+ end
15
+ end
16
+
17
+ EphemeralResponse::RackReflector.while_running do
18
+ @post_response = http.start {|h| h.request(post) }
19
+ end
20
+ end
21
+
22
+ it "returns the same fixture when the post data is slightly different" do
23
+ post.set_form_data :same => :different
24
+ http.start {|h| h.request(post) }.body.should == @post_response.body
25
+ end
26
+
27
+ end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
8
- - 1
9
- version: 0.2.1
7
+ - 3
8
+ - 0
9
+ version: 0.3.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Sandro Turriate
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-06-24 00:00:00 -04:00
17
+ date: 2010-06-29 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -89,6 +89,7 @@ files:
89
89
  - README.markdown
90
90
  - Rakefile
91
91
  - VERSION
92
+ - examples/custom_cache_key.rb
92
93
  - examples/simple_benchmark.rb
93
94
  - examples/white_list.rb
94
95
  - lib/ephemeral_response.rb
@@ -99,6 +100,7 @@ files:
99
100
  - spec/ephemeral_response/fixture_spec.rb
100
101
  - spec/ephemeral_response/net_http_spec.rb
101
102
  - spec/ephemeral_response_spec.rb
103
+ - spec/integration/custom_identifier_spec.rb
102
104
  - spec/integration/normal_flow_spec.rb
103
105
  - spec/integration/unique_fixtures_spec.rb
104
106
  - spec/integration/white_list_spec.rb
@@ -143,6 +145,7 @@ test_files:
143
145
  - spec/ephemeral_response/fixture_spec.rb
144
146
  - spec/ephemeral_response/net_http_spec.rb
145
147
  - spec/ephemeral_response_spec.rb
148
+ - spec/integration/custom_identifier_spec.rb
146
149
  - spec/integration/normal_flow_spec.rb
147
150
  - spec/integration/unique_fixtures_spec.rb
148
151
  - spec/integration/white_list_spec.rb
@@ -151,5 +154,6 @@ test_files:
151
154
  - spec/support/fakefs_ext.rb
152
155
  - spec/support/rack_reflector.rb
153
156
  - spec/support/time.rb
157
+ - examples/custom_cache_key.rb
154
158
  - examples/simple_benchmark.rb
155
159
  - examples/white_list.rb