ephemeral_response 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.markdown +18 -0
- data/README.markdown +53 -16
- data/Rakefile +2 -1
- data/VERSION +1 -1
- data/examples/simple_benchmark.rb +19 -0
- data/examples/white_list.rb +29 -0
- data/lib/ephemeral_response/configuration.rb +14 -0
- data/lib/ephemeral_response/fixture.rb +53 -22
- data/lib/ephemeral_response/net_http.rb +13 -3
- data/lib/ephemeral_response.rb +2 -1
- data/spec/ephemeral_response/configuration_spec.rb +48 -0
- data/spec/ephemeral_response/fixture_spec.rb +221 -21
- data/spec/ephemeral_response/net_http_spec.rb +15 -0
- data/spec/ephemeral_response_spec.rb +7 -1
- data/spec/integration/{net_spec.rb → normal_flow_spec.rb} +23 -11
- data/spec/integration/unique_fixtures_spec.rb +153 -0
- data/spec/integration/white_list_spec.rb +25 -0
- data/spec/spec_helper.rb +2 -1
- data/spec/support/clear_fixtures.rb +1 -0
- data/spec/support/rack_reflector.rb +63 -0
- data/spec/support/time.rb +3 -2
- metadata +34 -10
- data/README.html +0 -72
- /data/{LICENSE → MIT_LICENSE} +0 -0
@@ -1,13 +1,12 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe EphemeralResponse::Fixture do
|
4
|
-
|
5
|
-
let(:uri) { URI.parse("http://example.com") }
|
6
|
-
let(:fixture) { EphemeralResponse::Fixture.new(uri, 'GET') { |f| f.response = "hello world"} }
|
4
|
+
include FakeFS::SpecHelpers
|
7
5
|
|
8
|
-
|
9
|
-
|
10
|
-
|
6
|
+
let(:fixture_directory) { File.expand_path EphemeralResponse::Configuration.fixture_directory }
|
7
|
+
let(:request) { Net::HTTP::Get.new '/' }
|
8
|
+
let(:uri) { URI.parse("http://example.com/") }
|
9
|
+
let(:fixture) { EphemeralResponse::Fixture.new(uri, request) { |f| f.response = "hello world"} }
|
11
10
|
|
12
11
|
describe ".load_all" do
|
13
12
|
it "returns the empty fixtures hash when the fixture directory doesn't exist" do
|
@@ -24,72 +23,273 @@ describe EphemeralResponse::Fixture do
|
|
24
23
|
before do
|
25
24
|
FileUtils.mkdir_p fixture_directory
|
26
25
|
Dir.chdir(fixture_directory) do
|
27
|
-
FileUtils.touch %w(1.
|
26
|
+
FileUtils.touch %w(1.yml 2.yml)
|
28
27
|
end
|
29
28
|
end
|
30
29
|
|
31
30
|
it "calls #load_fixture for each fixture file" do
|
32
|
-
EphemeralResponse::Fixture.should_receive(:load_fixture).with("#{fixture_directory}/1.
|
33
|
-
EphemeralResponse::Fixture.should_receive(:load_fixture).with("#{fixture_directory}/2.
|
31
|
+
EphemeralResponse::Fixture.should_receive(:load_fixture).with("#{fixture_directory}/1.yml")
|
32
|
+
EphemeralResponse::Fixture.should_receive(:load_fixture).with("#{fixture_directory}/2.yml")
|
34
33
|
EphemeralResponse::Fixture.load_all
|
35
34
|
end
|
36
35
|
end
|
37
36
|
end
|
38
37
|
|
39
38
|
describe ".load_fixture" do
|
39
|
+
it "loads the yamlized fixture into the fixtures hash" do
|
40
|
+
fixture.save
|
41
|
+
EphemeralResponse::Fixture.load_fixture(fixture.path)
|
42
|
+
EphemeralResponse::Fixture.fixtures.should have_key(fixture.identifier)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe ".register" do
|
40
47
|
context "fixture expired" do
|
41
48
|
before do
|
42
49
|
fixture.instance_variable_set(:@created_at, Time.new - (EphemeralResponse::Configuration.expiration * 2))
|
43
|
-
fixture.
|
50
|
+
fixture.register
|
44
51
|
end
|
45
52
|
|
46
|
-
it "removes the fixture" do
|
47
|
-
EphemeralResponse::Fixture.load_all
|
53
|
+
it "removes the fixture file" do
|
48
54
|
File.exists?(fixture.path).should be_false
|
49
55
|
end
|
50
56
|
|
51
|
-
it "does not add the fixture
|
52
|
-
EphemeralResponse::Fixture.load_all
|
57
|
+
it "does not add the fixture to the fixtures hash" do
|
53
58
|
EphemeralResponse::Fixture.fixtures.should_not have_key(fixture.identifier)
|
54
59
|
end
|
55
60
|
end
|
56
61
|
|
57
62
|
context "fixture not expired" do
|
58
63
|
before do
|
59
|
-
fixture.
|
64
|
+
fixture.register
|
60
65
|
end
|
61
66
|
|
62
67
|
it "adds the the fixture to the fixtures hash" do
|
63
|
-
EphemeralResponse::Fixture.load_all
|
64
68
|
EphemeralResponse::Fixture.fixtures[fixture.identifier].should == fixture
|
65
69
|
end
|
66
70
|
end
|
67
71
|
end
|
68
72
|
|
73
|
+
describe ".find" do
|
74
|
+
context "when fixture registered" do
|
75
|
+
it "returns the fixture" do
|
76
|
+
fixture.register
|
77
|
+
EphemeralResponse::Fixture.find(uri, request).should == fixture
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
context "when fixture not registered" do
|
82
|
+
it "returns nil" do
|
83
|
+
EphemeralResponse::Fixture.find(uri, request).should be_nil
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
describe ".find_or_initialize" do
|
89
|
+
context "when the fixture is registered" do
|
90
|
+
it "returns the registered fixture" do
|
91
|
+
fixture.register
|
92
|
+
EphemeralResponse::Fixture.find_or_initialize(uri, request).should == fixture
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
context "when the fixture doesn't exist" do
|
97
|
+
it "processes the block" do
|
98
|
+
EphemeralResponse::Fixture.find_or_initialize(uri, request) do |fixture|
|
99
|
+
fixture.response = "bah"
|
100
|
+
end.response.should == "bah"
|
101
|
+
end
|
102
|
+
|
103
|
+
it "returns the new fixture" do
|
104
|
+
fixture = EphemeralResponse::Fixture.find_or_initialize(uri, request)
|
105
|
+
EphemeralResponse::Fixture.fixtures[fixture.identifier].should be_nil
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
69
110
|
describe ".respond_to" do
|
111
|
+
context "host included in white list" do
|
112
|
+
before do
|
113
|
+
EphemeralResponse::Configuration.white_list = uri.host
|
114
|
+
end
|
115
|
+
|
116
|
+
it "returns flow back to net/http" do
|
117
|
+
2.times do
|
118
|
+
EphemeralResponse::Fixture.respond_to(fixture.uri, request) {}
|
119
|
+
EphemeralResponse::Fixture.fixtures[fixture.identifier].should be_nil
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
context "uri not normalized" do
|
124
|
+
let(:uri) { URI.parse("HtTP://ExaMplE.Com/") }
|
125
|
+
let(:fixture) { EphemeralResponse::Fixture.new(uri, request) }
|
126
|
+
|
127
|
+
before do
|
128
|
+
EphemeralResponse::Configuration.white_list = "example.com"
|
129
|
+
end
|
130
|
+
|
131
|
+
it "returns flow to net/http when host is not normalized" do
|
132
|
+
EphemeralResponse::Fixture.respond_to(uri, request) {}
|
133
|
+
EphemeralResponse::Fixture.fixtures[fixture.identifier].should be_nil
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
end
|
138
|
+
|
70
139
|
context "fixture loaded" do
|
71
140
|
it "returns the fixture response" do
|
72
|
-
fixture.
|
73
|
-
EphemeralResponse::Fixture.
|
74
|
-
response = EphemeralResponse::Fixture.respond_to(fixture.uri, fixture.method)
|
141
|
+
fixture.register
|
142
|
+
response = EphemeralResponse::Fixture.respond_to(fixture.uri, request)
|
75
143
|
response.should == "hello world"
|
76
144
|
end
|
77
145
|
end
|
78
146
|
|
79
147
|
context "fixture not loaded" do
|
80
148
|
it "sets the response to the block" do
|
81
|
-
EphemeralResponse::Fixture.respond_to(fixture.uri,
|
149
|
+
EphemeralResponse::Fixture.respond_to(fixture.uri, request) do
|
82
150
|
"new response"
|
83
151
|
end
|
84
152
|
EphemeralResponse::Fixture.fixtures[fixture.identifier].response.should == "new response"
|
85
153
|
end
|
86
154
|
|
87
155
|
it "saves the fixture" do
|
88
|
-
EphemeralResponse::Fixture.respond_to(fixture.uri,
|
156
|
+
EphemeralResponse::Fixture.respond_to(fixture.uri, request) do
|
89
157
|
"new response"
|
90
158
|
end
|
91
159
|
File.exists?(fixture.path).should be_true
|
92
160
|
end
|
93
161
|
end
|
94
162
|
end
|
163
|
+
|
164
|
+
describe "#initialize" do
|
165
|
+
let(:uri) { URI.parse("HtTP://ExaMplE.Com/") }
|
166
|
+
subject { EphemeralResponse::Fixture }
|
167
|
+
|
168
|
+
it "normalizes the given uri" do
|
169
|
+
fixture = subject.new(uri, request)
|
170
|
+
fixture.uri.should == uri.normalize
|
171
|
+
end
|
172
|
+
|
173
|
+
it "duplicates the request" do
|
174
|
+
fixture = subject.new(uri, request)
|
175
|
+
request['something'] = "anything"
|
176
|
+
fixture.request['something'].should be_nil
|
177
|
+
request['something'].should == "anything"
|
178
|
+
end
|
179
|
+
|
180
|
+
it "sets created_at to the current time" do
|
181
|
+
Time.travel "2010-01-15 10:11:12" do
|
182
|
+
fixture = subject.new(uri, request)
|
183
|
+
fixture.created_at.should == Time.parse("2010-01-15 10:11:12")
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
it "yields itself" do
|
188
|
+
fixture = subject.new(uri, request) do |f|
|
189
|
+
f.response = "yielded self"
|
190
|
+
end
|
191
|
+
fixture.response.should == "yielded self"
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
describe "#identifier" do
|
196
|
+
let(:request) { Net::HTTP::Get.new '/?foo=bar' }
|
197
|
+
let(:uri) { URI.parse "http://example.com/" }
|
198
|
+
subject { EphemeralResponse::Fixture.new uri, request }
|
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
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
describe "#uri_identifier" do
|
207
|
+
|
208
|
+
it "returns an array containing the host when there is no query string" do
|
209
|
+
request = Net::HTTP::Get.new '/'
|
210
|
+
host = URI.parse("http://example.com/")
|
211
|
+
fixture = EphemeralResponse::Fixture.new(host, request)
|
212
|
+
fixture.uri_identifier.should == "http://example.com/"
|
213
|
+
end
|
214
|
+
|
215
|
+
it "does not incorrectly hash different hosts which sort identically" do
|
216
|
+
request = Net::HTTP::Get.new '/?foo=bar'
|
217
|
+
host1 = URI.parse("http://a.com/?f=b")
|
218
|
+
host2 = URI.parse("http://f.com/?b=a")
|
219
|
+
fixture1 = EphemeralResponse::Fixture.new(host1, request)
|
220
|
+
fixture2 = EphemeralResponse::Fixture.new(host2, request)
|
221
|
+
fixture1.uri_identifier.should_not == fixture2.uri_identifier
|
222
|
+
end
|
223
|
+
|
224
|
+
it "sorts the query strings" do
|
225
|
+
uri1 = URI.parse("http://example.com/?foo=bar&baz=qux&f")
|
226
|
+
uri2 = URI.parse("http://example.com/?baz=qux&foo=bar&f")
|
227
|
+
request1 = Net::HTTP::Get.new uri1.request_uri
|
228
|
+
request2 = Net::HTTP::Get.new uri2.request_uri
|
229
|
+
fixture1 = EphemeralResponse::Fixture.new(uri1, request1)
|
230
|
+
fixture2 = EphemeralResponse::Fixture.new(uri2, request2)
|
231
|
+
fixture1.uri_identifier.should == fixture2.uri_identifier
|
232
|
+
end
|
233
|
+
|
234
|
+
it "doesn't mix up the query string key pairs" do
|
235
|
+
uri1 = URI.parse("http://example.com/?foo=bar&baz=qux")
|
236
|
+
uri2 = URI.parse("http://example.com/?bar=foo&qux=baz")
|
237
|
+
request1 = Net::HTTP::Get.new uri1.request_uri
|
238
|
+
request2 = Net::HTTP::Get.new uri2.request_uri
|
239
|
+
fixture1 = EphemeralResponse::Fixture.new(uri1, request1)
|
240
|
+
fixture2 = EphemeralResponse::Fixture.new(uri2, request2)
|
241
|
+
fixture1.uri_identifier.should_not == fixture2.uri_identifier
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
describe "#register" do
|
246
|
+
context "uri not white listed" do
|
247
|
+
it "saves the fixture" do
|
248
|
+
fixture.register
|
249
|
+
File.exists?(fixture.path).should be_true
|
250
|
+
end
|
251
|
+
|
252
|
+
it "registers the fixture" do
|
253
|
+
fixture.register
|
254
|
+
EphemeralResponse::Fixture.fixtures.should have_key(fixture.identifier)
|
255
|
+
end
|
256
|
+
end
|
257
|
+
|
258
|
+
context "uri is white listed" do
|
259
|
+
before do
|
260
|
+
EphemeralResponse::Configuration.white_list << uri.host
|
261
|
+
end
|
262
|
+
|
263
|
+
it "doesn't save the fixture" do
|
264
|
+
fixture.register
|
265
|
+
File.exists?(fixture.path).should be_false
|
266
|
+
end
|
267
|
+
|
268
|
+
it "doesn't register the fixture" do
|
269
|
+
fixture.register
|
270
|
+
EphemeralResponse::Fixture.fixtures.should_not have_key(fixture.identifier)
|
271
|
+
end
|
272
|
+
end
|
273
|
+
end
|
274
|
+
|
275
|
+
describe "#file_name" do
|
276
|
+
it "chops off the starting slash when accessing '/'" do
|
277
|
+
uri = URI.parse("http://example.com/")
|
278
|
+
fixture = EphemeralResponse::Fixture.new(uri, request)
|
279
|
+
fixture.file_name.should =~ /example-com_GET_[\w]{7}.yml/
|
280
|
+
end
|
281
|
+
|
282
|
+
it "chops off the starting slash when accessing '/index.html'" do
|
283
|
+
uri = URI.parse("http://example.com/index.html")
|
284
|
+
fixture = EphemeralResponse::Fixture.new(uri, request)
|
285
|
+
fixture.file_name.should =~ /example-com_GET_index-html_[\w]{7}.yml/
|
286
|
+
end
|
287
|
+
|
288
|
+
it "looks like on longer paths" do
|
289
|
+
uri = URI.parse("http://example.com/users/1/photos/1.html")
|
290
|
+
fixture = EphemeralResponse::Fixture.new(uri, request)
|
291
|
+
fixture.file_name.should =~ /example-com_GET_users-1-photos-1-html_[\w]{7}.yml/
|
292
|
+
end
|
293
|
+
|
294
|
+
end
|
95
295
|
end
|
@@ -20,9 +20,15 @@ describe Net::HTTP do
|
|
20
20
|
describe "#generate_uri" do
|
21
21
|
context "when HTTP" do
|
22
22
|
subject { Net::HTTP.new('example.com') }
|
23
|
+
|
23
24
|
it "returns the proper http uri object" do
|
24
25
|
subject.generate_uri(request).should == URI.parse("http://example.com/foo?q=1")
|
25
26
|
end
|
27
|
+
|
28
|
+
it "sets the uri instance variable" do
|
29
|
+
subject.generate_uri(request)
|
30
|
+
subject.uri.should == URI.parse("http://example.com/foo?q=1")
|
31
|
+
end
|
26
32
|
end
|
27
33
|
|
28
34
|
context "when HTTPS" do
|
@@ -31,6 +37,7 @@ describe Net::HTTP do
|
|
31
37
|
https.use_ssl = true
|
32
38
|
https
|
33
39
|
end
|
40
|
+
|
34
41
|
it "returns the proper http uri object" do
|
35
42
|
subject.generate_uri(request).should == URI.parse("https://example.com/foo?q=1")
|
36
43
|
end
|
@@ -69,5 +76,13 @@ describe Net::HTTP do
|
|
69
76
|
subject.request(request)
|
70
77
|
end
|
71
78
|
end
|
79
|
+
|
80
|
+
context "connection not started" do
|
81
|
+
it "starts the connection" do
|
82
|
+
subject.should_not be_started
|
83
|
+
subject.request(request)
|
84
|
+
subject.should be_started
|
85
|
+
end
|
86
|
+
end
|
72
87
|
end
|
73
88
|
end
|
@@ -24,9 +24,15 @@ describe EphemeralResponse do
|
|
24
24
|
end
|
25
25
|
|
26
26
|
it "removes #generate_uri" do
|
27
|
-
Net::HTTP.instance_methods.
|
27
|
+
Net::HTTP.instance_methods.should include('generate_uri')
|
28
28
|
EphemeralResponse.deactivate
|
29
29
|
Net::HTTP.instance_methods.should_not include('generate_uri')
|
30
30
|
end
|
31
|
+
|
32
|
+
it "removes #uri" do
|
33
|
+
Net::HTTP.instance_methods.should include('uri')
|
34
|
+
EphemeralResponse.deactivate
|
35
|
+
Net::HTTP.instance_methods.should_not include('uri')
|
36
|
+
end
|
31
37
|
end
|
32
38
|
end
|
@@ -1,36 +1,48 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe "
|
4
|
-
|
3
|
+
describe "Normal flow" do
|
4
|
+
include FakeFS::SpecHelpers
|
5
|
+
|
6
|
+
after do
|
5
7
|
clear_fixtures
|
6
8
|
end
|
7
9
|
|
8
10
|
context "example.com" do
|
9
|
-
let(:uri) { URI.parse('http://example.com') }
|
11
|
+
let(:uri) { URI.parse('http://example.com/') }
|
12
|
+
let(:http) { Net::HTTP.new uri.host, uri.port }
|
13
|
+
|
14
|
+
def get
|
15
|
+
Net::HTTP::Get.new '/'
|
16
|
+
end
|
17
|
+
|
18
|
+
def send_request(req)
|
19
|
+
http.start {|h| h.request(req) }
|
20
|
+
end
|
21
|
+
|
10
22
|
it "generates a fixture, then uses the fixture" do
|
11
|
-
real_response =
|
12
|
-
fixture = EphemeralResponse::Fixture.new(uri,
|
23
|
+
real_response = send_request(get)
|
24
|
+
fixture = EphemeralResponse::Fixture.new(uri, get)
|
13
25
|
File.exists?(fixture.path).should be_true
|
14
26
|
Net::HTTP.should_not_receive(:connect_without_ephemeral_response)
|
15
27
|
Net::HTTP.should_not_receive(:request_without_ephemeral_response)
|
16
|
-
fixture_response =
|
28
|
+
fixture_response = send_request(get)
|
17
29
|
real_response.should == fixture_response
|
18
30
|
end
|
19
31
|
|
20
32
|
it "generates a new fixture when the initial fixture expires" do
|
21
|
-
|
22
|
-
old_fixture = EphemeralResponse::Fixture.find(uri,
|
33
|
+
send_request(get)
|
34
|
+
old_fixture = EphemeralResponse::Fixture.find(uri, get)
|
23
35
|
Time.travel((Time.now + EphemeralResponse::Configuration.expiration * 2).to_s) do
|
24
36
|
EphemeralResponse::Fixture.load_all
|
25
|
-
|
37
|
+
send_request(get)
|
26
38
|
end
|
27
|
-
new_fixture = EphemeralResponse::Fixture.find(uri,
|
39
|
+
new_fixture = EphemeralResponse::Fixture.find(uri, get)
|
28
40
|
old_fixture.created_at.should < new_fixture.created_at
|
29
41
|
|
30
42
|
# use the new fixture
|
31
43
|
Net::HTTP.should_not_receive(:connect_without_ephemeral_response)
|
32
44
|
Net::HTTP.should_not_receive(:request_without_ephemeral_response)
|
33
|
-
|
45
|
+
send_request(get)
|
34
46
|
end
|
35
47
|
|
36
48
|
context "Deactivation" do
|
@@ -0,0 +1,153 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module UniqueRequests
|
4
|
+
VARIATIONS = %w(
|
5
|
+
simple_get
|
6
|
+
get_with_query_string
|
7
|
+
get_with_query_string_and_basic_auth
|
8
|
+
get_with_multiple_headers
|
9
|
+
simple_post
|
10
|
+
post_with_data
|
11
|
+
post_with_body
|
12
|
+
post_with_data_and_query_string
|
13
|
+
post_with_data_and_query_string_and_basic_auth
|
14
|
+
)
|
15
|
+
|
16
|
+
def uri
|
17
|
+
@uri ||= URI.parse "http://localhost:#{EphemeralResponse::RackReflector.port}"
|
18
|
+
end
|
19
|
+
|
20
|
+
def responses
|
21
|
+
@responses ||= {}
|
22
|
+
end
|
23
|
+
|
24
|
+
def set_up_responses
|
25
|
+
VARIATIONS.each do |request|
|
26
|
+
responses[request] = send(request)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def perform(request)
|
31
|
+
http = Net::HTTP.new uri.host, uri.port
|
32
|
+
# http.set_debug_output $stdout
|
33
|
+
http.start do |http|
|
34
|
+
http.request(request)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def simple_get
|
39
|
+
perform Net::HTTP::Get.new('/foo')
|
40
|
+
end
|
41
|
+
|
42
|
+
def get_with_query_string
|
43
|
+
perform Net::HTTP::Get.new('/?foo=bar&baz=qux')
|
44
|
+
end
|
45
|
+
|
46
|
+
def get_with_query_string_and_basic_auth
|
47
|
+
request = Net::HTTP::Get.new('/?foo=bar')
|
48
|
+
request.basic_auth 'user', 'password'
|
49
|
+
perform request
|
50
|
+
end
|
51
|
+
|
52
|
+
def get_with_multiple_headers
|
53
|
+
request = Net::HTTP::Get.new('/')
|
54
|
+
request['Accept'] = "application/json, text/html"
|
55
|
+
request['Accept-Encoding'] = "deflate, gzip"
|
56
|
+
perform request
|
57
|
+
end
|
58
|
+
|
59
|
+
def simple_post
|
60
|
+
perform Net::HTTP::Post.new('/')
|
61
|
+
end
|
62
|
+
|
63
|
+
def post_with_data
|
64
|
+
request = Net::HTTP::Post.new('/')
|
65
|
+
request.set_form_data 'hi' => 'there'
|
66
|
+
perform request
|
67
|
+
end
|
68
|
+
|
69
|
+
def post_with_body
|
70
|
+
request = Net::HTTP::Post.new('/')
|
71
|
+
request.body = 'hi=there'
|
72
|
+
perform request
|
73
|
+
end
|
74
|
+
|
75
|
+
def post_with_data_and_query_string
|
76
|
+
request = Net::HTTP::Post.new('/?foo=bar')
|
77
|
+
request.set_form_data 'hi' => 'there'
|
78
|
+
perform request
|
79
|
+
end
|
80
|
+
|
81
|
+
def post_with_data_and_query_string_and_basic_auth
|
82
|
+
request = Net::HTTP::Post.new('/?foo=bar')
|
83
|
+
request.basic_auth 'user', 'password'
|
84
|
+
request.set_form_data 'hi' => 'there'
|
85
|
+
perform request
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
|
90
|
+
describe "Unique fixtures generated for the following requests" do
|
91
|
+
include UniqueRequests
|
92
|
+
|
93
|
+
before :all do
|
94
|
+
EphemeralResponse.activate
|
95
|
+
clear_fixtures
|
96
|
+
EphemeralResponse::RackReflector.while_running do
|
97
|
+
set_up_responses
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
after :all do
|
102
|
+
EphemeralResponse::RackReflector.stop
|
103
|
+
end
|
104
|
+
|
105
|
+
UniqueRequests::VARIATIONS.each do |request|
|
106
|
+
it "restores the correct response from the fixture" do
|
107
|
+
send(request).body.should == responses[request].body
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
context "when querystring has different order" do
|
112
|
+
it "restores the correct response" do
|
113
|
+
new_response = perform Net::HTTP::Get.new('/?baz=qux&foo=bar')
|
114
|
+
new_response.body.should == responses['get_with_query_string'].body
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
context "when headers have different order" do
|
119
|
+
it "restores the correct response when the headers are exactly reversed" do
|
120
|
+
request = Net::HTTP::Get.new('/')
|
121
|
+
request['Accept'] = "text/html, application/json"
|
122
|
+
request['Accept-Encoding'] = "gzip, deflate"
|
123
|
+
new_response = perform request
|
124
|
+
new_response.body.should == responses['get_with_multiple_headers'].body
|
125
|
+
end
|
126
|
+
|
127
|
+
it "restores the correct response when some headers are reversed" do
|
128
|
+
request = Net::HTTP::Get.new('/')
|
129
|
+
request['Accept'] = "text/html, application/json"
|
130
|
+
request['Accept-Encoding'] = "deflate, gzip"
|
131
|
+
new_response = perform request
|
132
|
+
new_response.body.should == responses['get_with_multiple_headers'].body
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
context "when the http service has not been started" do
|
137
|
+
def get
|
138
|
+
Net::HTTP::Get.new('/foo/bar/baz')
|
139
|
+
end
|
140
|
+
|
141
|
+
it "restores the correct fixture" do
|
142
|
+
clear_fixtures
|
143
|
+
http = Net::HTTP.new uri.host, uri.port
|
144
|
+
|
145
|
+
|
146
|
+
EphemeralResponse::RackReflector.while_running do
|
147
|
+
http.request(get)
|
148
|
+
end
|
149
|
+
|
150
|
+
http.request(get).body.should == EphemeralResponse::Fixture.find(http.uri, get).response.body
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "White Lists" do
|
4
|
+
before do
|
5
|
+
clear_fixtures
|
6
|
+
end
|
7
|
+
|
8
|
+
context "localhost added to white list" do
|
9
|
+
let(:uri) { URI.parse('http://localhost:9876/') }
|
10
|
+
let(:http) { Net::HTTP.new uri.host, uri.port }
|
11
|
+
let(:get) { Net::HTTP::Get.new '/' }
|
12
|
+
|
13
|
+
before do
|
14
|
+
EphemeralResponse::Configuration.white_list = "localhost"
|
15
|
+
end
|
16
|
+
|
17
|
+
it "doesn't save a fixture" do
|
18
|
+
EphemeralResponse::RackReflector.while_running do
|
19
|
+
http.start {|h| h.request(get) }
|
20
|
+
EphemeralResponse::Fixture.load_all.should be_empty
|
21
|
+
Dir.glob("#{EphemeralResponse::Configuration.fixture_directory}/*").should be_empty
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -7,12 +7,13 @@ require 'spec/autorun'
|
|
7
7
|
Dir.glob("spec/support/*.rb") {|f| require f}
|
8
8
|
|
9
9
|
Spec::Runner.configure do |config|
|
10
|
-
config.include FakeFS::SpecHelpers
|
11
10
|
config.include ClearFixtures
|
12
11
|
config.before(:each) do
|
12
|
+
EphemeralResponse::Configuration.reset
|
13
13
|
EphemeralResponse.activate
|
14
14
|
end
|
15
15
|
config.after(:suite) do
|
16
16
|
EphemeralResponse.deactivate
|
17
|
+
ClearFixtures.clear_fixtures
|
17
18
|
end
|
18
19
|
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'unicorn'
|
2
|
+
|
3
|
+
module EphemeralResponse::RackReflector
|
4
|
+
class Response
|
5
|
+
attr_reader :ordered_headers, :rack_input
|
6
|
+
def initialize(env={})
|
7
|
+
@ordered_headers = []
|
8
|
+
set_ordered_headers(env)
|
9
|
+
set_rack_input(env)
|
10
|
+
end
|
11
|
+
|
12
|
+
def set_ordered_headers(env)
|
13
|
+
env.each do |key, value|
|
14
|
+
@ordered_headers << [key, value] if key == key.upcase
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def headers
|
19
|
+
Hash[ordered_headers]
|
20
|
+
end
|
21
|
+
|
22
|
+
def set_rack_input(env)
|
23
|
+
@rack_input = env['rack.input'].read
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
extend self
|
28
|
+
|
29
|
+
def app
|
30
|
+
lambda do |env|
|
31
|
+
[ 200, {"Content-Type" => "application/x-yaml"}, [ Response.new(env).to_yaml ] ]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def new_server
|
36
|
+
http_server = Unicorn::HttpServer.new(app, :listeners => ["0.0.0.0:#{port}"])
|
37
|
+
http_server.logger.level = Logger::ERROR
|
38
|
+
http_server
|
39
|
+
end
|
40
|
+
|
41
|
+
def port
|
42
|
+
9876 || ENV['UNICORN_PORT']
|
43
|
+
end
|
44
|
+
|
45
|
+
def server
|
46
|
+
@server ||= new_server
|
47
|
+
end
|
48
|
+
|
49
|
+
def start
|
50
|
+
server.start
|
51
|
+
end
|
52
|
+
|
53
|
+
def stop
|
54
|
+
server.stop(false)
|
55
|
+
end
|
56
|
+
|
57
|
+
def while_running
|
58
|
+
start
|
59
|
+
yield
|
60
|
+
ensure
|
61
|
+
stop
|
62
|
+
end
|
63
|
+
end
|