puffing-billy 0.2.3 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+
3
+ describe Billy::StubHandler do
4
+ let(:handler) { Billy::StubHandler.new }
5
+ let(:request) { {
6
+ method: 'GET',
7
+ url: 'http://example.test:8080/index?some=param',
8
+ headers: {'Accept-Encoding' => 'gzip',
9
+ 'Cache-Control' => 'no-cache' },
10
+ body: 'Some body'
11
+ } }
12
+
13
+ describe '#handles_request?' do
14
+ it 'handles the request if it is stubbed' do
15
+ expect(handler).to receive(:find_stub).and_return("a stub")
16
+ expect(handler.handles_request?(nil,nil,nil,nil)).to be true
17
+ end
18
+
19
+ it 'does not handle the request if it is not stubbed' do
20
+ expect(handler).to receive(:find_stub).and_return(nil)
21
+ expect(handler.handles_request?(nil,nil,nil,nil)).to be false
22
+ end
23
+ end
24
+
25
+ describe '#handle_request' do
26
+ it 'returns nil if the request is not stubbed' do
27
+ expect(handler).to receive(:handles_request?).and_return(false)
28
+ expect(handler.handle_request(nil,nil,nil,nil)).to be nil
29
+ end
30
+
31
+ it 'returns a response hash if the request is stubbed' do
32
+ stub = double("stub", :call => [200, {'Content-Type' => 'application/json'}, "Some content"])
33
+ expect(handler).to receive(:handles_request?).and_return(true)
34
+ expect(handler).to receive(:find_stub).and_return(stub)
35
+ expect(handler.handle_request('GET',
36
+ request[:url],
37
+ request[:headers],
38
+ request[:body])).to eql({:status => 200,
39
+ :headers => {'Content-Type' => 'application/json'},
40
+ :content => "Some content"})
41
+ end
42
+ end
43
+
44
+ describe '#reset' do
45
+ before do
46
+ # Can't use request params when creating the stub.
47
+ # See https://github.com/oesmith/puffing-billy/issues/21
48
+ handler.stub('http://example.test:8080/index')
49
+ end
50
+
51
+ it 'resets the stubs' do
52
+ expect(handler.handles_request?('GET',
53
+ request[:url],
54
+ request[:headers],
55
+ request[:body])).to be true
56
+ handler.reset
57
+ expect(handler.handles_request?('GET',
58
+ request[:url],
59
+ request[:headers],
60
+ request[:body])).to be false
61
+ end
62
+ end
63
+
64
+ it '#stubs requests' do
65
+ handler.stub('http://example.test:8080/index')
66
+ expect(handler.handles_request?('GET',
67
+ request[:url],
68
+ request[:headers],
69
+ request[:body])).to be true
70
+ end
71
+ end
@@ -46,10 +46,10 @@ describe Billy::JSONUtils do
46
46
  let(:non_json) { 'Not JSON.' }
47
47
 
48
48
  it 'identifies JSON' do
49
- expect(Billy::JSONUtils::json?(json)).to be_true
49
+ expect(Billy::JSONUtils::json?(json)).to be true
50
50
  end
51
51
  it 'identifies non-JSON' do
52
- expect(Billy::JSONUtils::json?(non_json)).to be_false
52
+ expect(Billy::JSONUtils::json?(non_json)).to be false
53
53
  end
54
54
  end
55
- end
55
+ end
@@ -129,13 +129,17 @@ shared_examples_for 'a cache' do
129
129
  end
130
130
 
131
131
  context "cache persistence" do
132
+ let(:cache_path) { Billy.config.cache_path }
132
133
  let(:cached_key) { proxy.cache.key('get',"#{url}/foo","") }
133
134
  let(:cached_file) do
134
135
  f = cached_key + ".yml"
135
- File.join(Billy.config.cache_path, f)
136
+ File.join(cache_path, f)
136
137
  end
137
138
 
138
- before { Billy.config.whitelist = [] }
139
+ before do
140
+ Billy.config.whitelist = []
141
+ Dir.mkdir(cache_path) unless Dir.exist?(cache_path)
142
+ end
139
143
 
140
144
  after do
141
145
  File.delete(cached_file) if File.exists?(cached_file)
@@ -146,7 +150,7 @@ shared_examples_for 'a cache' do
146
150
 
147
151
  it 'should persist' do
148
152
  r = http.get('/foo')
149
- expect(File.exists?(cached_file)).to be_true
153
+ expect(File.exists?(cached_file)).to be true
150
154
  end
151
155
 
152
156
  it 'should be read initially from persistent cache' do
@@ -211,7 +215,7 @@ shared_examples_for 'a cache' do
211
215
 
212
216
  it 'should not cache non-successful response when enabled' do
213
217
  http_error.get('/foo')
214
- expect(File.exists?(cached_file)).to be_false
218
+ expect(File.exists?(cached_file)).to be false
215
219
  end
216
220
 
217
221
  it 'should cache successful response when enabled' do
@@ -235,6 +239,9 @@ shared_examples_for 'a cache' do
235
239
  # 3) Change the test servers to start/stop for each test instead of before all
236
240
  # 4) Remove the test server completely and rely on the server instantiated by the app
237
241
  pending "Unable to test this without affecting the running test servers"
242
+ # If the 'pending' continues to execute the spec, 'skip' it to avoid EM errors.
243
+ # If 'pending' stops the test, 'skip' isn't defined but it won't hit this line.
244
+ skip "Unable to test this without affecting the running test servers"
238
245
  expect{http_error.get('/foo')}.to raise_error(Faraday::Error::ConnectionFailed)
239
246
  end
240
247
  end
@@ -246,7 +253,7 @@ shared_examples_for 'a cache' do
246
253
 
247
254
  it 'shouldnt persist' do
248
255
  r = http.get('/foo')
249
- expect(File.exists?(cached_file)).to be_false
256
+ expect(File.exists?(cached_file)).to be false
250
257
  end
251
258
  end
252
259
  end
@@ -279,7 +286,7 @@ describe Billy::Proxy do
279
286
  # Valid options: :request, :proxy, :ssl, :builder, :url, :parallel_manager, :params, :headers, :builder_class
280
287
  faraday_options = {
281
288
  :proxy => { :uri => proxy.url },
282
- :request => { :timeout => 0.5 }
289
+ :request => { :timeout => 1.0 }
283
290
  }
284
291
 
285
292
  @http = Faraday.new @http_url, faraday_options
@@ -320,7 +327,7 @@ describe Billy::Proxy do
320
327
  context 'caching' do
321
328
 
322
329
  it 'defaults to nil scope' do
323
- expect(proxy.cache.scope).to be_nil
330
+ expect(proxy.cache.scope).to be nil
324
331
  end
325
332
 
326
333
  context 'HTTP' do
@@ -358,7 +365,7 @@ describe Billy::Proxy do
358
365
 
359
366
  it 'can be reset to the default scope' do
360
367
  proxy.cache.use_default_scope
361
- expect(proxy.cache.scope).to be_nil
368
+ expect(proxy.cache.scope).to be nil
362
369
  end
363
370
 
364
371
  it 'can execute a block against a cache scope' do
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  Dir[File.expand_path("../support/**/*.rb", __FILE__)].each {|f| require f}
2
2
 
3
+ require 'pry'
3
4
  require 'billy/rspec'
4
5
  require 'rack'
5
6
  require 'logger'
@@ -13,7 +14,6 @@ end
13
14
 
14
15
  RSpec.configure do |config|
15
16
  include Billy::TestServer
16
- config.treat_symbols_as_metadata_keys_with_true_values = true
17
17
  config.run_all_when_everything_filtered = true
18
18
  config.filter_run :focus
19
19
  config.order = 'random'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puffing-billy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Olly Smith
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-07 00:00:00.000000000 Z
11
+ date: 2014-12-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -179,7 +179,7 @@ dependencies:
179
179
  - !ruby/object:Gem::Version
180
180
  version: '0'
181
181
  - !ruby/object:Gem::Dependency
182
- name: em-http-request
182
+ name: em-synchrony
183
183
  requirement: !ruby/object:Gem::Requirement
184
184
  requirements:
185
185
  - - ">="
@@ -193,7 +193,7 @@ dependencies:
193
193
  - !ruby/object:Gem::Version
194
194
  version: '0'
195
195
  - !ruby/object:Gem::Dependency
196
- name: eventmachine_httpserver
196
+ name: em-http-request
197
197
  requirement: !ruby/object:Gem::Requirement
198
198
  requirements:
199
199
  - - ">="
@@ -207,7 +207,7 @@ dependencies:
207
207
  - !ruby/object:Gem::Version
208
208
  version: '0'
209
209
  - !ruby/object:Gem::Dependency
210
- name: http_parser.rb
210
+ name: eventmachine_httpserver
211
211
  requirement: !ruby/object:Gem::Requirement
212
212
  requirements:
213
213
  - - ">="
@@ -220,6 +220,20 @@ dependencies:
220
220
  - - ">="
221
221
  - !ruby/object:Gem::Version
222
222
  version: '0'
223
+ - !ruby/object:Gem::Dependency
224
+ name: http_parser.rb
225
+ requirement: !ruby/object:Gem::Requirement
226
+ requirements:
227
+ - - "~>"
228
+ - !ruby/object:Gem::Version
229
+ version: 0.6.0
230
+ type: :runtime
231
+ prerelease: false
232
+ version_requirements: !ruby/object:Gem::Requirement
233
+ requirements:
234
+ - - "~>"
235
+ - !ruby/object:Gem::Version
236
+ version: 0.6.0
223
237
  - !ruby/object:Gem::Dependency
224
238
  name: multi_json
225
239
  requirement: !ruby/object:Gem::Requirement
@@ -259,6 +273,8 @@ extra_rdoc_files: []
259
273
  files:
260
274
  - ".gitignore"
261
275
  - ".rspec"
276
+ - ".travis.yml"
277
+ - CHANGELOG.md
262
278
  - Gemfile
263
279
  - Gemfile.lock
264
280
  - Guardfile
@@ -273,6 +289,11 @@ files:
273
289
  - lib/billy/cache.rb
274
290
  - lib/billy/config.rb
275
291
  - lib/billy/cucumber.rb
292
+ - lib/billy/handlers/cache_handler.rb
293
+ - lib/billy/handlers/handler.rb
294
+ - lib/billy/handlers/proxy_handler.rb
295
+ - lib/billy/handlers/request_handler.rb
296
+ - lib/billy/handlers/stub_handler.rb
276
297
  - lib/billy/json_utils.rb
277
298
  - lib/billy/mitm.crt
278
299
  - lib/billy/mitm.key
@@ -292,6 +313,11 @@ files:
292
313
  - spec/fixtures/test-server.crt
293
314
  - spec/fixtures/test-server.key
294
315
  - spec/lib/billy/cache_spec.rb
316
+ - spec/lib/billy/handlers/cache_handler_spec.rb
317
+ - spec/lib/billy/handlers/handler_spec.rb
318
+ - spec/lib/billy/handlers/proxy_handler_spec.rb
319
+ - spec/lib/billy/handlers/request_handler_spec.rb
320
+ - spec/lib/billy/handlers/stub_handler_spec.rb
295
321
  - spec/lib/billy/proxy_request_stub_spec.rb
296
322
  - spec/lib/billy/resource_utils_spec.rb
297
323
  - spec/lib/proxy_spec.rb
@@ -316,7 +342,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
316
342
  version: '0'
317
343
  requirements: []
318
344
  rubyforge_project:
319
- rubygems_version: 2.2.0
345
+ rubygems_version: 2.2.2
320
346
  signing_key:
321
347
  specification_version: 4
322
348
  summary: Easy request stubs for browser tests.
@@ -326,6 +352,11 @@ test_files:
326
352
  - spec/fixtures/test-server.crt
327
353
  - spec/fixtures/test-server.key
328
354
  - spec/lib/billy/cache_spec.rb
355
+ - spec/lib/billy/handlers/cache_handler_spec.rb
356
+ - spec/lib/billy/handlers/handler_spec.rb
357
+ - spec/lib/billy/handlers/proxy_handler_spec.rb
358
+ - spec/lib/billy/handlers/request_handler_spec.rb
359
+ - spec/lib/billy/handlers/stub_handler_spec.rb
329
360
  - spec/lib/billy/proxy_request_stub_spec.rb
330
361
  - spec/lib/billy/resource_utils_spec.rb
331
362
  - spec/lib/proxy_spec.rb