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.
@@ -1,6 +1,13 @@
1
1
  History
2
2
  =======
3
3
 
4
+ 0.3.1 / 2010-06-29
5
+ --------------
6
+
7
+ #### Enhancements
8
+
9
+ * Allow custom matchers by host (leshill)
10
+
4
11
  0.2.1 / 2010-06-24
5
12
  --------------
6
13
 
@@ -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
- everytime something changes, your tests should fail. Mocking out web responses
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
- ### Customize how requests get matched by the cache
59
+ ## Customize how requests get matched by the cache
60
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.
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 be cached
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 will read from the cache even though the date is different
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
- ### Configuration
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
- #### Selenium
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.0
1
+ 0.3.1
@@ -8,7 +8,7 @@ EphemeralResponse.activate
8
8
 
9
9
  EphemeralResponse.configure do |config|
10
10
  config.register('example.com') do |request|
11
- "#{request.method}#{request.path}"
11
+ "#{request.uri.host}#{request.method}#{request.path}"
12
12
  end
13
13
  end
14
14
 
@@ -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.0".freeze
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
@@ -0,0 +1,12 @@
1
+ module EphemeralResponse
2
+ class Request < SimpleDelegator
3
+ attr_reader :uri
4
+
5
+ undef method
6
+
7
+ def initialize(uri, request)
8
+ @uri = uri
9
+ super request
10
+ end
11
+ end
12
+ 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
- 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
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
- EphemeralResponse::RackReflector.while_running do
18
- @post_response = http.start {|h| h.request(post) }
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
- 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
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
- - 0
9
- version: 0.3.0
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