puffing-billy 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,31 +4,31 @@ describe Billy::JSONUtils do
4
4
  describe 'sorting' do
5
5
  describe '#sort_hash_keys' do
6
6
  it 'sorts simple Hashes' do
7
- data = {c: 'three',a: 'one',b: 'two'}
8
- expected = {a: 'one',b: 'two',c: 'three'}
9
- expect(Billy::JSONUtils::sort_hash_keys(data)).to eq expected
7
+ data = { c: 'three', a: 'one', b: 'two' }
8
+ expected = { a: 'one', b: 'two', c: 'three' }
9
+ expect(Billy::JSONUtils.sort_hash_keys(data)).to eq expected
10
10
  end
11
11
 
12
12
  it 'does not sort simple Arrays' do
13
- data = [3,1,2,'two','three','one']
14
- expect(Billy::JSONUtils::sort_hash_keys(data)).to eq data
13
+ data = [3, 1, 2, 'two', 'three', 'one']
14
+ expect(Billy::JSONUtils.sort_hash_keys(data)).to eq data
15
15
  end
16
16
 
17
17
  it 'does not sort multi-dimensional Arrays' do
18
- data = [[3,2,1],[5,4,6],['b','c','a']]
19
- expect(Billy::JSONUtils::sort_hash_keys(data)).to eq data
18
+ data = [[3, 2, 1], [5, 4, 6], %w(b c a)]
19
+ expect(Billy::JSONUtils.sort_hash_keys(data)).to eq data
20
20
  end
21
21
 
22
22
  it 'sorts multi-dimensional Hashes' do
23
- data = {c: {l: 2,m: 3,k: 1},a: {f: 3,e: 2,d: 1},b: {i: 2,h: 1,j: 3}}
24
- expected = {a: {d: 1,e: 2,f: 3},b: {h: 1,i: 2,j: 3},c: {k: 1,l: 2,m: 3}}
25
- expect(Billy::JSONUtils::sort_hash_keys(data)).to eq expected
23
+ data = { c: { l: 2, m: 3, k: 1 }, a: { f: 3, e: 2, d: 1 }, b: { i: 2, h: 1, j: 3 } }
24
+ expected = { a: { d: 1, e: 2, f: 3 }, b: { h: 1, i: 2, j: 3 }, c: { k: 1, l: 2, m: 3 } }
25
+ expect(Billy::JSONUtils.sort_hash_keys(data)).to eq expected
26
26
  end
27
27
 
28
28
  it 'sorts abnormal data structures' do
29
- data = {b: [['b','c','a'],{ab: 5,aa: 4, ac: 6},[3,2,1],{ba: true,bc: false, bb: nil}],a: {f: 3,e: 2,d: 1}}
30
- expected = {a: {d: 1,e: 2,f: 3},b: [['b','c','a'],{aa: 4,ab: 5,ac: 6},[3,2,1],{ba: true, bb: nil,bc: false}]}
31
- expect(Billy::JSONUtils::sort_hash_keys(data)).to eq expected
29
+ data = { b: [%w(b c a), { ab: 5, aa: 4, ac: 6 }, [3, 2, 1], { ba: true, bc: false, bb: nil }], a: { f: 3, e: 2, d: 1 } }
30
+ expected = { a: { d: 1, e: 2, f: 3 }, b: [%w(b c a), { aa: 4, ab: 5, ac: 6 }, [3, 2, 1], { ba: true, bb: nil, bc: false }] }
31
+ expect(Billy::JSONUtils.sort_hash_keys(data)).to eq expected
32
32
  end
33
33
  end
34
34
 
@@ -36,20 +36,20 @@ describe Billy::JSONUtils do
36
36
  it 'sorts JSON' do
37
37
  data = '{"c":"three","a":"one","b":"two"}'
38
38
  expected = '{"a":"one","b":"two","c":"three"}'
39
- expect(Billy::JSONUtils::sort_json(data)).to eq expected
39
+ expect(Billy::JSONUtils.sort_json(data)).to eq expected
40
40
  end
41
41
  end
42
42
  end
43
43
 
44
44
  describe 'json?' do
45
- let(:json) { {a: '1'}.to_json }
45
+ let(:json) { { a: '1' }.to_json }
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
55
  end
@@ -8,11 +8,11 @@ shared_examples_for 'a proxy server' do
8
8
  end
9
9
 
10
10
  it 'should proxy POST requests' do
11
- expect(http.post('/echo', :foo => 'bar').body).to eql "POST /echo\nfoo=bar"
11
+ expect(http.post('/echo', foo: 'bar').body).to eql "POST /echo\nfoo=bar"
12
12
  end
13
13
 
14
14
  it 'should proxy PUT requests' do
15
- expect(http.post('/echo', :foo => 'bar').body).to eql "POST /echo\nfoo=bar"
15
+ expect(http.post('/echo', foo: 'bar').body).to eql "POST /echo\nfoo=bar"
16
16
  end
17
17
 
18
18
  it 'should proxy HEAD requests' do
@@ -26,44 +26,43 @@ end
26
26
 
27
27
  shared_examples_for 'a request stub' do
28
28
  it 'should stub GET requests' do
29
- proxy.stub("#{url}/foo").
30
- and_return(:text => 'hello, GET!')
29
+ proxy.stub("#{url}/foo")
30
+ .and_return(text: 'hello, GET!')
31
31
  expect(http.get('/foo').body).to eql 'hello, GET!'
32
32
  end
33
33
 
34
34
  it 'should stub GET response statuses' do
35
- proxy.stub("#{url}/foo").
36
- and_return(:code => 200)
35
+ proxy.stub("#{url}/foo")
36
+ .and_return(code: 200)
37
37
  expect(http.get('/foo').status).to eql 200
38
38
  end
39
39
 
40
40
  it 'should stub POST requests' do
41
- proxy.stub("#{url}/bar", :method => :post).
42
- and_return(:text => 'hello, POST!')
43
- expect(http.post('/bar', :foo => :bar).body).to eql 'hello, POST!'
41
+ proxy.stub("#{url}/bar", method: :post)
42
+ .and_return(text: 'hello, POST!')
43
+ expect(http.post('/bar', foo: :bar).body).to eql 'hello, POST!'
44
44
  end
45
45
 
46
46
  it 'should stub PUT requests' do
47
- proxy.stub("#{url}/baz", :method => :put).
48
- and_return(:text => 'hello, PUT!')
49
- expect(http.put('/baz', :foo => :bar).body).to eql 'hello, PUT!'
47
+ proxy.stub("#{url}/baz", method: :put)
48
+ .and_return(text: 'hello, PUT!')
49
+ expect(http.put('/baz', foo: :bar).body).to eql 'hello, PUT!'
50
50
  end
51
51
 
52
52
  it 'should stub HEAD requests' do
53
- proxy.stub("#{url}/bap", :method => :head).
54
- and_return(:headers => {'HTTP-X-Hello' => 'hello, HEAD!'})
53
+ proxy.stub("#{url}/bap", method: :head)
54
+ .and_return(headers: { 'HTTP-X-Hello' => 'hello, HEAD!' })
55
55
  expect(http.head('/bap').headers['http-x-hello']).to eql 'hello, HEAD!'
56
56
  end
57
57
 
58
58
  it 'should stub DELETE requests' do
59
- proxy.stub("#{url}/bam", :method => :delete).
60
- and_return(:text => 'hello, DELETE!')
59
+ proxy.stub("#{url}/bam", method: :delete)
60
+ .and_return(text: 'hello, DELETE!')
61
61
  expect(http.delete('/bam').body).to eql 'hello, DELETE!'
62
62
  end
63
63
  end
64
64
 
65
65
  shared_examples_for 'a cache' do
66
-
67
66
  context 'whitelisted GET requests' do
68
67
  it 'should not be cached' do
69
68
  assert_noncached_url
@@ -93,7 +92,7 @@ shared_examples_for 'a cache' do
93
92
  context 'with ports' do
94
93
  before do
95
94
  rack_app_url = URI(http.url_prefix)
96
- Billy.config.whitelist = ["#{rack_app_url.host}:#{rack_app_url.port+1}"]
95
+ Billy.config.whitelist = ["#{rack_app_url.host}:#{rack_app_url.port + 1}"]
97
96
  end
98
97
 
99
98
  it 'should be cached' do
@@ -110,11 +109,11 @@ shared_examples_for 'a cache' do
110
109
  it 'should be cached' do
111
110
  r = http.get('/analytics?some_param=5')
112
111
  expect(r.body).to eql 'GET /analytics'
113
- expect {
114
- expect {
112
+ expect do
113
+ expect do
115
114
  r = http.get('/analytics?some_param=20')
116
- }.to change { r.headers['HTTP-X-EchoCount'].to_i }.by(1)
117
- }.to_not change { r.body }
115
+ end.to change { r.headers['HTTP-X-EchoCount'].to_i }.by(1)
116
+ end.to_not change { r.body }
118
117
  end
119
118
  end
120
119
 
@@ -128,11 +127,11 @@ shared_examples_for 'a cache' do
128
127
  end
129
128
  end
130
129
 
131
- context "cache persistence" do
130
+ context 'cache persistence' do
132
131
  let(:cache_path) { Billy.config.cache_path }
133
- let(:cached_key) { proxy.cache.key('get',"#{url}/foo","") }
132
+ let(:cached_key) { proxy.cache.key('get', "#{url}/foo", '') }
134
133
  let(:cached_file) do
135
- f = cached_key + ".yml"
134
+ f = cached_key + '.yml'
136
135
  File.join(cache_path, f)
137
136
  end
138
137
 
@@ -142,24 +141,24 @@ shared_examples_for 'a cache' do
142
141
  end
143
142
 
144
143
  after do
145
- File.delete(cached_file) if File.exists?(cached_file)
144
+ File.delete(cached_file) if File.exist?(cached_file)
146
145
  end
147
146
 
148
- context "enabled" do
147
+ context 'enabled' do
149
148
  before { Billy.config.persist_cache = true }
150
149
 
151
150
  it 'should persist' do
152
- r = http.get('/foo')
153
- expect(File.exists?(cached_file)).to be true
151
+ http.get('/foo')
152
+ expect(File.exist?(cached_file)).to be true
154
153
  end
155
154
 
156
155
  it 'should be read initially from persistent cache' do
157
156
  File.open(cached_file, 'w') do |f|
158
157
  cached = {
159
- :headers => {},
160
- :content => "GET /foo cached"
158
+ headers: {},
159
+ content: 'GET /foo cached'
161
160
  }
162
- f.write(cached.to_yaml(:Encoding => :Utf8))
161
+ f.write(cached.to_yaml(Encoding: :Utf8))
163
162
  end
164
163
 
165
164
  r = http.get('/foo')
@@ -168,7 +167,7 @@ shared_examples_for 'a cache' do
168
167
 
169
168
  context 'cache_request_headers requests' do
170
169
  it 'should not be cached by default' do
171
- r = http.get('/foo')
170
+ http.get('/foo')
172
171
  saved_cache = Billy.proxy.cache.fetch_from_persistence(cached_key)
173
172
  expect(saved_cache.keys).not_to include :request_headers
174
173
  end
@@ -179,7 +178,7 @@ shared_examples_for 'a cache' do
179
178
  end
180
179
 
181
180
  it 'should be cached' do
182
- r = http.get('/foo')
181
+ http.get('/foo')
183
182
  saved_cache = Billy.proxy.cache.fetch_from_persistence(cached_key)
184
183
  expect(saved_cache.keys).to include :request_headers
185
184
  end
@@ -201,8 +200,8 @@ shared_examples_for 'a cache' do
201
200
  before { Billy.config.non_whitelisted_requests_disabled = true }
202
201
 
203
202
  it 'should raise error when disabled' do
204
- #TODO: Suppress stderr output: https://gist.github.com/adamstegman/926858
205
- expect{http.get('/foo')}.to raise_error(Faraday::Error::ConnectionFailed, "end of file reached")
203
+ # TODO: Suppress stderr output: https://gist.github.com/adamstegman/926858
204
+ expect { http.get('/foo') }.to raise_error(Faraday::Error::ConnectionFailed, 'end of file reached')
206
205
  end
207
206
  end
208
207
 
@@ -215,7 +214,7 @@ shared_examples_for 'a cache' do
215
214
 
216
215
  it 'should not cache non-successful response when enabled' do
217
216
  http_error.get('/foo')
218
- expect(File.exists?(cached_file)).to be false
217
+ expect(File.exist?(cached_file)).to be false
219
218
  end
220
219
 
221
220
  it 'should cache successful response when enabled' do
@@ -238,22 +237,21 @@ shared_examples_for 'a cache' do
238
237
  # 2) Restart the test servers if they aren't running
239
238
  # 3) Change the test servers to start/stop for each test instead of before all
240
239
  # 4) Remove the test server completely and rely on the server instantiated by the app
241
- pending "Unable to test this without affecting the running test servers"
240
+ pending 'Unable to test this without affecting the running test servers'
242
241
  # If the 'pending' continues to execute the spec, 'skip' it to avoid EM errors.
243
242
  # 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"
245
- expect{http_error.get('/foo')}.to raise_error(Faraday::Error::ConnectionFailed)
243
+ skip 'Unable to test this without affecting the running test servers'
244
+ expect { http_error.get('/foo') }.to raise_error(Faraday::Error::ConnectionFailed)
246
245
  end
247
246
  end
248
-
249
247
  end
250
248
 
251
- context "disabled" do
249
+ context 'disabled' do
252
250
  before { Billy.config.persist_cache = false }
253
251
 
254
252
  it 'shouldnt persist' do
255
- r = http.get('/foo')
256
- expect(File.exists?(cached_file)).to be false
253
+ http.get('/foo')
254
+ expect(File.exist?(cached_file)).to be false
257
255
  end
258
256
  end
259
257
  end
@@ -261,41 +259,39 @@ shared_examples_for 'a cache' do
261
259
  def assert_noncached_url(url = '/foo')
262
260
  r = http.get(url)
263
261
  expect(r.body).to eql "GET #{url}"
264
- expect {
265
- expect {
262
+ expect do
263
+ expect do
266
264
  r = http.get(url)
267
- }.to change { r.headers['HTTP-X-EchoCount'].to_i }.by(1)
268
- }.to_not change { r.body }
265
+ end.to change { r.headers['HTTP-X-EchoCount'].to_i }.by(1)
266
+ end.to_not change { r.body }
269
267
  end
270
268
 
271
269
  def assert_cached_url(url = '/foo')
272
270
  r = http.get(url)
273
271
  expect(r.body).to eql "GET #{url}"
274
- expect {
275
- expect {
272
+ expect do
273
+ expect do
276
274
  r = http.get(url)
277
- }.to_not change { r.headers['HTTP-X-EchoCount'] }
278
- }.to_not change { r.body }
275
+ end.to_not change { r.headers['HTTP-X-EchoCount'] }
276
+ end.to_not change { r.body }
279
277
  end
280
278
  end
281
279
 
282
280
  describe Billy::Proxy do
283
-
284
281
  before do
285
282
  # Adding non-valid Faraday options throw an error: https://github.com/arsduo/koala/pull/311
286
283
  # Valid options: :request, :proxy, :ssl, :builder, :url, :parallel_manager, :params, :headers, :builder_class
287
284
  faraday_options = {
288
- :proxy => { :uri => proxy.url },
289
- :request => { :timeout => 1.0 }
285
+ proxy: { uri: proxy.url },
286
+ request: { timeout: 1.0 }
290
287
  }
291
288
 
292
289
  @http = Faraday.new @http_url, faraday_options
293
- @https = Faraday.new @https_url, faraday_options.merge(:ssl => { :verify => false })
290
+ @https = Faraday.new @https_url, faraday_options.merge(ssl: { verify: false })
294
291
  @http_error = Faraday.new @error_url, faraday_options
295
292
  end
296
293
 
297
294
  context 'proxying' do
298
-
299
295
  context 'HTTP' do
300
296
  let!(:http) { @http }
301
297
  it_should_behave_like 'a proxy server'
@@ -305,11 +301,9 @@ describe Billy::Proxy do
305
301
  let!(:http) { @https }
306
302
  it_should_behave_like 'a proxy server'
307
303
  end
308
-
309
304
  end
310
305
 
311
306
  context 'stubbing' do
312
-
313
307
  context 'HTTP' do
314
308
  let!(:url) { @http_url }
315
309
  let!(:http) { @http }
@@ -321,11 +315,9 @@ describe Billy::Proxy do
321
315
  let!(:http) { @https }
322
316
  it_should_behave_like 'a request stub'
323
317
  end
324
-
325
318
  end
326
319
 
327
320
  context 'caching' do
328
-
329
321
  it 'defaults to nil scope' do
330
322
  expect(proxy.cache.scope).to be nil
331
323
  end
@@ -350,7 +342,7 @@ describe Billy::Proxy do
350
342
  let!(:http_error) { @http_error }
351
343
 
352
344
  before do
353
- proxy.cache.scope_to "my_cache"
345
+ proxy.cache.scope_to 'my_cache'
354
346
  end
355
347
 
356
348
  after do
@@ -360,7 +352,7 @@ describe Billy::Proxy do
360
352
  it_should_behave_like 'a cache'
361
353
 
362
354
  it 'uses the cache scope' do
363
- expect(proxy.cache.scope).to eq("my_cache")
355
+ expect(proxy.cache.scope).to eq('my_cache')
364
356
  end
365
357
 
366
358
  it 'can be reset to the default scope' do
@@ -369,21 +361,21 @@ describe Billy::Proxy do
369
361
  end
370
362
 
371
363
  it 'can execute a block against a cache scope' do
372
- expect(proxy.cache.scope).to eq "my_cache"
373
- proxy.cache.with_scope "another_cache" do
374
- expect(proxy.cache.scope).to eq "another_cache"
364
+ expect(proxy.cache.scope).to eq 'my_cache'
365
+ proxy.cache.with_scope 'another_cache' do
366
+ expect(proxy.cache.scope).to eq 'another_cache'
375
367
  end
376
- expect(proxy.cache.scope).to eq "my_cache"
368
+ expect(proxy.cache.scope).to eq 'my_cache'
377
369
  end
378
370
 
379
371
  it 'requires a block to be passed to with_scope' do
380
- expect {proxy.cache.with_scope "some_scope"}.to raise_error ArgumentError
372
+ expect { proxy.cache.with_scope 'some_scope' }.to raise_error ArgumentError
381
373
  end
382
374
 
383
375
  it 'should have different keys for the same request under a different scope' do
384
- args = ['get',"#{url}/foo",""]
376
+ args = ['get', "#{url}/foo", '']
385
377
  key = proxy.cache.key(*args)
386
- proxy.cache.with_scope "another_cache" do
378
+ proxy.cache.with_scope 'another_cache' do
387
379
  expect(proxy.cache.key(*args)).to_not eq key
388
380
  end
389
381
  end
@@ -1,15 +1,15 @@
1
- Dir[File.expand_path("../support/**/*.rb", __FILE__)].each {|f| require f}
1
+ Dir[File.expand_path('../support/**/*.rb', __FILE__)].each { |f| require f }
2
2
 
3
3
  require 'pry'
4
4
  require 'billy/rspec'
5
5
  require 'rack'
6
6
  require 'logger'
7
7
 
8
- Capybara.app = Rack::Directory.new(File.expand_path("../../examples", __FILE__))
8
+ Capybara.app = Rack::Directory.new(File.expand_path('../../examples', __FILE__))
9
9
  Capybara.javascript_driver = :poltergeist_billy
10
10
 
11
11
  Billy.configure do |config|
12
- config.logger = Logger.new(File.expand_path("../../log/test.log", __FILE__))
12
+ config.logger = Logger.new(File.expand_path('../../log/test.log', __FILE__))
13
13
  end
14
14
 
15
15
  RSpec.configure do |config|
@@ -62,8 +62,8 @@ module Billy
62
62
  if ssl
63
63
  http_server.ssl = true
64
64
  http_server.ssl_options = {
65
- :private_key_file => File.expand_path('../../fixtures/test-server.key', __FILE__),
66
- :cert_chain_file => File.expand_path('../../fixtures/test-server.crt', __FILE__)
65
+ private_key_file: File.expand_path('../../fixtures/test-server.key', __FILE__),
66
+ cert_chain_file: File.expand_path('../../fixtures/test-server.crt', __FILE__)
67
67
  }
68
68
  end
69
69
  http_server.start
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.4.1
4
+ version: 0.5.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: 2015-01-03 00:00:00.000000000 Z
11
+ date: 2015-02-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -224,16 +224,16 @@ dependencies:
224
224
  name: em-http-request
225
225
  requirement: !ruby/object:Gem::Requirement
226
226
  requirements:
227
- - - ">="
227
+ - - "~>"
228
228
  - !ruby/object:Gem::Version
229
- version: '0'
229
+ version: 1.1.0
230
230
  type: :runtime
231
231
  prerelease: false
232
232
  version_requirements: !ruby/object:Gem::Requirement
233
233
  requirements:
234
- - - ">="
234
+ - - "~>"
235
235
  - !ruby/object:Gem::Version
236
- version: '0'
236
+ version: 1.1.0
237
237
  - !ruby/object:Gem::Dependency
238
238
  name: eventmachine_httpserver
239
239
  requirement: !ruby/object:Gem::Requirement