lhc 3.4.0 → 3.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.localch.yml +190 -0
  3. data/.rubocop.yml +10 -0
  4. data/cider-ci.yml +2 -1
  5. data/cider-ci/jobs/rspec.yml +48 -0
  6. data/cider-ci/jobs/rubocop.yml +55 -0
  7. data/cider-ci/scripts/bundle.yml +2 -0
  8. data/cider-ci/scripts/github_comment.yml +6 -0
  9. data/cider-ci/scripts/rspec.yml +4 -0
  10. data/cider-ci/scripts/rubocop.yml +5 -0
  11. data/cider-ci/scripts/ruby-version.yml +2 -0
  12. data/cider-ci/scripts/tmp-cache.yml +2 -0
  13. data/lhc.gemspec +2 -1
  14. data/lib/lhc.rb +1 -1
  15. data/lib/lhc/concerns/lhc/basic_methods.rb +4 -6
  16. data/lib/lhc/concerns/lhc/formats.rb +0 -2
  17. data/lib/lhc/endpoint.rb +8 -6
  18. data/lib/lhc/error.rb +3 -1
  19. data/lib/lhc/errors/parser_error.rb +4 -0
  20. data/lib/lhc/formats/json.rb +18 -4
  21. data/lib/lhc/interceptor.rb +2 -0
  22. data/lib/lhc/interceptor_processor.rb +1 -1
  23. data/lib/lhc/request.rb +12 -10
  24. data/lib/lhc/response.rb +3 -9
  25. data/lib/lhc/version.rb +1 -1
  26. data/non_rails_spec/request/request_spec.rb +2 -2
  27. data/spec/basic_methods/delete_spec.rb +4 -6
  28. data/spec/basic_methods/get_spec.rb +3 -6
  29. data/spec/basic_methods/post_spec.rb +7 -9
  30. data/spec/basic_methods/put_spec.rb +7 -9
  31. data/spec/basic_methods/request_spec.rb +2 -4
  32. data/spec/config/endpoints_spec.rb +13 -16
  33. data/spec/config/placeholders_spec.rb +9 -11
  34. data/spec/dummy/bin/rails +1 -1
  35. data/spec/dummy/config.ru +1 -1
  36. data/spec/dummy/config/initializers/cookies_serializer.rb +1 -1
  37. data/spec/endpoint/compile_spec.rb +3 -5
  38. data/spec/endpoint/match_spec.rb +10 -12
  39. data/spec/endpoint/placeholders_spec.rb +1 -3
  40. data/spec/endpoint/remove_interpolated_params_spec.rb +2 -3
  41. data/spec/endpoint/values_as_params_spec.rb +2 -4
  42. data/spec/error/find_spec.rb +37 -39
  43. data/spec/error/response_spec.rb +0 -2
  44. data/spec/error/timeout_spec.rb +1 -3
  45. data/spec/interceptors/after_request_spec.rb +2 -4
  46. data/spec/interceptors/after_response_spec.rb +3 -5
  47. data/spec/interceptors/before_request_spec.rb +2 -4
  48. data/spec/interceptors/before_response_spec.rb +2 -4
  49. data/spec/interceptors/default_interceptors_spec.rb +3 -5
  50. data/spec/interceptors/define_spec.rb +4 -6
  51. data/spec/interceptors/response_competition_spec.rb +6 -7
  52. data/spec/interceptors/return_response_spec.rb +8 -11
  53. data/spec/request/encoding_spec.rb +1 -4
  54. data/spec/request/error_handling_spec.rb +13 -2
  55. data/spec/request/headers_spec.rb +1 -2
  56. data/spec/request/option_dup_spec.rb +2 -3
  57. data/spec/request/parallel_requests_spec.rb +2 -2
  58. data/spec/request/url_patterns_spec.rb +4 -5
  59. data/spec/response/body_spec.rb +2 -4
  60. data/spec/response/code_spec.rb +2 -4
  61. data/spec/response/data_spec.rb +2 -4
  62. data/spec/response/effective_url_spec.rb +1 -3
  63. data/spec/response/headers_spec.rb +2 -4
  64. data/spec/response/options_spec.rb +2 -4
  65. data/spec/response/success_spec.rb +2 -3
  66. data/spec/response/time_spec.rb +3 -5
  67. data/spec/spec_helper.rb +1 -1
  68. data/spec/support/reset_config.rb +0 -2
  69. data/spec/timeouts/no_signal_spec.rb +1 -3
  70. data/spec/timeouts/timings_spec.rb +27 -33
  71. metadata +27 -4
  72. data/cider-ci/contexts/rspec.yml +0 -19
  73. data/cider-ci/jobs/tests.yml +0 -27
@@ -1,37 +1,34 @@
1
1
  require 'rails_helper'
2
2
 
3
3
  describe LHC do
4
-
5
4
  context 'interceptor' do
6
-
7
5
  before(:each) do
8
6
  class CacheInterceptor < LHC::Interceptor
9
7
 
10
- def before_request(request)
11
- return LHC::Response.new(Typhoeus::Response.new(response_body: 'Im served from cache'), nil)
8
+ def before_request(_request)
9
+ LHC::Response.new(Typhoeus::Response.new(response_body: 'Im served from cache'), nil)
12
10
  end
13
11
  end
14
- LHC.configure { |c| c.interceptors = [CacheInterceptor] }
12
+ described_class.configure { |c| c.interceptors = [CacheInterceptor] }
15
13
  end
16
14
 
17
15
  it 'can return a response rather then doing a real request' do
18
- response = LHC.get('http://local.ch')
16
+ response = described_class.get('http://local.ch')
19
17
  expect(response.body).to eq 'Im served from cache'
20
18
  end
21
19
 
22
20
  context 'misusage' do
23
-
24
21
  before(:each) do
25
22
  class AnotherInterceptor < LHC::Interceptor
26
- def before_request(request)
27
- return LHC::Response.new(Typhoeus::Response.new({}), nil)
23
+ def before_request(_request)
24
+ LHC::Response.new(Typhoeus::Response.new({}), nil)
28
25
  end
29
26
  end
30
27
  end
31
28
 
32
29
  it 'raises an exception when two interceptors try to return a response' do
33
- expect(->{
34
- LHC.get('http://local.ch', interceptors: [CacheInterceptor, AnotherInterceptor])
30
+ expect(lambda {
31
+ described_class.get('http://local.ch', interceptors: [CacheInterceptor, AnotherInterceptor])
35
32
  }).to raise_error 'Response already set from another interceptor'
36
33
  end
37
34
  end
@@ -1,19 +1,16 @@
1
1
  require 'rails_helper'
2
2
 
3
3
  describe LHC::Request do
4
-
5
4
  context 'encoding url' do
6
-
7
5
  let(:url) { 'http://local.ch/something with spaces' }
8
6
 
9
7
  it 'can request urls with spaces inside' do
10
- stub_request(:get, URI::encode(url))
8
+ stub_request(:get, URI.encode(url))
11
9
  LHC.get(url)
12
10
  end
13
11
  end
14
12
 
15
13
  context 'encoding params' do
16
-
17
14
  let(:url) { 'http://local.ch/api/search?name=:name' }
18
15
 
19
16
  it 'can do requests with params including spaces' do
@@ -1,9 +1,7 @@
1
1
  require 'rails_helper'
2
2
 
3
3
  describe LHC::Request do
4
-
5
4
  context 'error handling' do
6
-
7
5
  def to_fail_with(error)
8
6
  raise_error(error)
9
7
  end
@@ -49,4 +47,17 @@ describe LHC::Request do
49
47
  expect_status_code(510) { to_fail_with(LHC::NotExtended) }
50
48
  end
51
49
  end
50
+
51
+ context 'parsing error' do
52
+
53
+ before(:each) do
54
+ stub_request(:get, 'http://datastore/v2/feedbacks').to_return(body: 'invalid json')
55
+ end
56
+
57
+ it 'requests json and parses response body' do
58
+ expect(->{
59
+ LHC.json.get('http://datastore/v2/feedbacks').data
60
+ }).to raise_error(LHC::ParserError)
61
+ end
62
+ end
52
63
  end
@@ -1,11 +1,10 @@
1
1
  require 'rails_helper'
2
2
 
3
3
  describe LHC::Request do
4
-
5
4
  it 'provides request headers' do
6
5
  stub_request(:get, 'http://local.ch')
7
6
  response = LHC.get('http://local.ch')
8
7
  request = response.request
9
- expect(request.headers).to eq({"User-Agent"=>"Typhoeus - https://github.com/typhoeus/typhoeus"})
8
+ expect(request.headers).to eq("User-Agent" => "Typhoeus - https://github.com/typhoeus/typhoeus")
10
9
  end
11
10
  end
@@ -1,12 +1,11 @@
1
1
  require 'rails_helper'
2
2
 
3
3
  describe LHC::Request do
4
-
5
4
  it 'does not alter the options that where passed' do
6
- LHC.configure { |c| c.endpoint(:kpi_tracker, 'http://analytics/track/:entity_id/w', { params: { env: 'PROD' } }) }
5
+ LHC.configure { |c| c.endpoint(:kpi_tracker, 'http://analytics/track/:entity_id/w', params: { env: 'PROD' }) }
7
6
  options = { params: { entity_id: '123' } }
8
7
  stub_request(:get, "http://analytics/track/123/w?env=PROD")
9
8
  LHC.get(:kpi_tracker, options)
10
- expect(options).to eq({ params: { entity_id: '123' } })
9
+ expect(options).to eq(params: { entity_id: '123' })
11
10
  end
12
11
  end
@@ -1,7 +1,6 @@
1
1
  require 'rails_helper'
2
2
 
3
3
  describe LHC::Request do
4
-
5
4
  let(:request_options) do
6
5
  [
7
6
  { url: 'http://www.local.ch/restaurants' },
@@ -22,12 +21,12 @@ describe LHC::Request do
22
21
  end
23
22
 
24
23
  context 'interceptors' do
25
-
26
24
  before(:each) do
27
25
  class TestInterceptor < LHC::Interceptor; end
28
26
  LHC.configure { |c| c.interceptors = [TestInterceptor] }
29
27
  end
30
28
 
29
+ # rubocop:disable RSpec/InstanceVariable
31
30
  it 'calls interceptors also for parallel requests' do
32
31
  stub_parallel_requests
33
32
  @called = 0
@@ -36,5 +35,6 @@ describe LHC::Request do
36
35
  LHC.request(request_options)
37
36
  expect(@called).to eq 2
38
37
  end
38
+ # rubocop:enable RSpec/InstanceVariable
39
39
  end
40
40
  end
@@ -1,24 +1,23 @@
1
1
  require 'rails_helper'
2
2
 
3
3
  describe LHC::Request do
4
-
5
4
  it 'compiles url in case of configured endpoints' do
6
5
  options = { params: {
7
6
  has_reviews: true
8
- }}
7
+ } }
9
8
  url = 'http://datastore/v2/campaign/:campaign_id/feedbacks'
10
9
  LHC.configure { |c| c.endpoint(:feedbacks, url, options) }
11
10
  stub_request(:get, 'http://datastore/v2/campaign/123/feedbacks?has_reviews=true')
12
- LHC.get(:feedbacks, params:{campaign_id: 123})
11
+ LHC.get(:feedbacks, params: { campaign_id: 123 })
13
12
  end
14
13
 
15
14
  it 'compiles url when doing a request' do
16
15
  stub_request(:get, 'http://datastore:8080/v2/feedbacks/123')
17
- LHC.get('http://datastore:8080/v2/feedbacks/:id', params:{id: 123})
16
+ LHC.get('http://datastore:8080/v2/feedbacks/:id', params: { id: 123 })
18
17
  end
19
18
 
20
19
  it 'considers body when compiling urls' do
21
20
  stub_request(:post, "http://datastore:8080/v2/places/123")
22
- LHC.json.post('http://datastore:8080/v2/places/:id', body: {id: 123}.to_json)
21
+ LHC.json.post('http://datastore:8080/v2/places/:id', body: { id: 123 }.to_json)
23
22
  end
24
23
  end
@@ -1,15 +1,13 @@
1
1
  require 'rails_helper'
2
2
 
3
3
  describe LHC::Response do
4
-
5
4
  context 'body' do
6
-
7
5
  let(:body) { 'this is a body' }
8
6
 
9
- let(:raw_response) { OpenStruct.new({ body: body }) }
7
+ let(:raw_response) { OpenStruct.new(body: body) }
10
8
 
11
9
  it 'provides response body' do
12
- response = LHC::Response.new(raw_response, nil)
10
+ response = described_class.new(raw_response, nil)
13
11
  expect(response.body).to eq body
14
12
  end
15
13
  end
@@ -1,15 +1,13 @@
1
1
  require 'rails_helper'
2
2
 
3
3
  describe LHC::Response do
4
-
5
4
  context 'code' do
6
-
7
5
  let(:code) { 200 }
8
6
 
9
- let(:raw_response) { OpenStruct.new({ code: code }) }
7
+ let(:raw_response) { OpenStruct.new(code: code) }
10
8
 
11
9
  it 'provides response code' do
12
- response = LHC::Response.new(raw_response, nil)
10
+ response = described_class.new(raw_response, nil)
13
11
  expect(response.code).to eq code
14
12
  end
15
13
  end
@@ -1,14 +1,12 @@
1
1
  require 'rails_helper'
2
2
 
3
3
  describe LHC::Response do
4
-
5
4
  context 'data' do
6
-
7
5
  let(:value) { 'some_value' }
8
6
 
9
- let(:body) {{ some_key: {nested: value} }}
7
+ let(:body) { { some_key: { nested: value } } }
10
8
 
11
- let(:raw_response) { OpenStruct.new({ body: body.to_json }) }
9
+ let(:raw_response) { OpenStruct.new(body: body.to_json) }
12
10
 
13
11
  it 'makes data from response body available' do
14
12
  response = LHC::Response.new(raw_response, nil)
@@ -1,15 +1,13 @@
1
1
  require 'rails_helper'
2
2
 
3
3
  describe LHC::Response do
4
-
5
4
  context 'effective_url' do
6
-
7
5
  let(:effective_url) { 'https://www.local.ch' }
8
6
 
9
7
  let(:raw_response) { OpenStruct.new(effective_url: effective_url) }
10
8
 
11
9
  it 'provides effective_url' do
12
- response = LHC::Response.new(raw_response, nil)
10
+ response = described_class.new(raw_response, nil)
13
11
  expect(response.effective_url).to eq effective_url
14
12
  end
15
13
  end
@@ -1,17 +1,15 @@
1
1
  require 'rails_helper'
2
2
 
3
3
  describe LHC::Response do
4
-
5
4
  context 'headers' do
6
-
7
5
  let(:headers) do
8
6
  { 'Content-Encoding' => 'UTF-8' }
9
7
  end
10
8
 
11
- let(:raw_response) { OpenStruct.new({ headers: headers }) }
9
+ let(:raw_response) { OpenStruct.new(headers: headers) }
12
10
 
13
11
  it 'provides headers' do
14
- response = LHC::Response.new(raw_response, nil)
12
+ response = described_class.new(raw_response, nil)
15
13
  expect(response.headers).to eq headers
16
14
  end
17
15
  end
@@ -1,17 +1,15 @@
1
1
  require 'rails_helper'
2
2
 
3
3
  describe LHC::Response do
4
-
5
4
  context 'headers' do
6
-
7
5
  let(:options) do
8
6
  { 'return_code' => :ok }
9
7
  end
10
8
 
11
- let(:raw_response) { OpenStruct.new({ options: options }) }
9
+ let(:raw_response) { OpenStruct.new(options: options) }
12
10
 
13
11
  it 'provides headers' do
14
- response = LHC::Response.new(raw_response, nil)
12
+ response = described_class.new(raw_response, nil)
15
13
  expect(response.options).to eq options
16
14
  end
17
15
  end
@@ -1,10 +1,9 @@
1
1
  require 'rails_helper'
2
2
 
3
3
  describe LHC::Response do
4
-
5
4
  context 'success?' do
6
- let(:response_success) { LHC::Response.new(Typhoeus::Response.new(response_code: 200, mock: true), nil) }
7
- let(:response_error) { LHC::Response.new(Typhoeus::Response.new(response_code: 404, mock: true), nil) }
5
+ let(:response_success) { described_class.new(Typhoeus::Response.new(response_code: 200, mock: true), nil) }
6
+ let(:response_error) { described_class.new(Typhoeus::Response.new(response_code: 404, mock: true), nil) }
8
7
 
9
8
  it { expect(response_success.success?).to be_truthy }
10
9
  it { expect(response_error.success?).to be_falsy }
@@ -1,16 +1,14 @@
1
1
  require 'rails_helper'
2
2
 
3
3
  describe LHC::Response do
4
-
5
4
  context 'time' do
6
-
7
5
  let(:time) { 1.3 }
8
6
 
9
- let(:raw_response) { OpenStruct.new({time: time}) }
7
+ let(:raw_response) { OpenStruct.new(time: time) }
10
8
 
11
9
  it 'provides response time in milliseconds' do
12
- response = LHC::Response.new(raw_response, nil)
13
- expect(response.time).to eq time*1000
10
+ response = described_class.new(raw_response, nil)
11
+ expect(response.time).to eq time * 1000
14
12
  end
15
13
  end
16
14
  end
@@ -2,4 +2,4 @@ require 'pry'
2
2
  require 'webmock/rspec'
3
3
  require 'lhc'
4
4
 
5
- Dir[File.join(__dir__, "support/**/*.rb")].each {|f| require f}
5
+ Dir[File.join(__dir__, "support/**/*.rb")].each { |f| require f }
@@ -1,7 +1,5 @@
1
1
  RSpec.configure do |config|
2
-
3
2
  config.before(:each) do
4
3
  LHC::Config.instance.reset
5
4
  end
6
-
7
5
  end
@@ -1,11 +1,9 @@
1
1
  require 'rails_helper'
2
2
 
3
3
  describe LHC::Request do
4
-
5
4
  context 'timeouts' do
6
-
7
5
  it 'has no_signal options set to true by default' do
8
- expect_any_instance_of(Ethon::Easy).to receive(:http_request).with(anything(), anything(), hash_including(:nosignal => true)).and_call_original
6
+ expect_any_instance_of(Ethon::Easy).to receive(:http_request).with(anything, anything, hash_including(nosignal: true)).and_call_original
9
7
  stub_request(:get, "http://local.ch/")
10
8
  LHC.get('http://local.ch')
11
9
  end
@@ -1,59 +1,53 @@
1
1
  require 'rails_helper'
2
2
 
3
3
  describe LHC::Request do
4
-
5
4
  context 'timeouts' do
6
-
7
5
  context "when timeout is not a whole number and timeout_ms is not set" do
8
-
9
- let(:options) { {:timeout => 0.1} }
6
+ let(:options) { { timeout: 0.1 } }
10
7
 
11
8
  it "ceils timeout and sets timeout_ms" do
12
9
  expect_any_instance_of(Ethon::Easy).to receive(:http_request)
13
- .with(anything(), anything(), hash_including(:timeout_ms => 100, :timeout => 1))
14
- .and_call_original
10
+ .with(anything, anything, hash_including(timeout_ms: 100, timeout: 1))
11
+ .and_call_original
15
12
  stub_request(:get, "http://local.ch/")
16
13
  LHC.get('http://local.ch', options)
17
14
  end
18
15
  end
19
16
 
20
- context "when timeout is not a whole number and timeout_ms is set" do
21
-
22
- let(:options) { {:timeout => 0.1, :timeout_ms => 123} }
17
+ context "when timeout is not a whole number and timeout_ms is set" do
18
+ let(:options) { { timeout: 0.1, timeout_ms: 123 } }
23
19
 
24
- it "ceils timeout and does not change timeout_ms" do
25
- expect_any_instance_of(Ethon::Easy).to receive(:http_request)
26
- .with(anything(), anything(), hash_including(:timeout_ms => 123, :timeout => 1))
20
+ it "ceils timeout and does not change timeout_ms" do
21
+ expect_any_instance_of(Ethon::Easy).to receive(:http_request)
22
+ .with(anything, anything, hash_including(timeout_ms: 123, timeout: 1))
27
23
  .and_call_original
28
- stub_request(:get, "http://local.ch/")
29
- LHC.get('http://local.ch', options)
30
- end
24
+ stub_request(:get, "http://local.ch/")
25
+ LHC.get('http://local.ch', options)
31
26
  end
27
+ end
32
28
 
33
- context "when connecttimeout is not a whole number and connecttimeout_ms is not set" do
34
-
35
- let(:options) { {:connecttimeout => 0.1} }
29
+ context "when connecttimeout is not a whole number and connecttimeout_ms is not set" do
30
+ let(:options) { { connecttimeout: 0.1 } }
36
31
 
37
- it "ceils connecttimeout and sets connecttimeout_ms" do
38
- expect_any_instance_of(Ethon::Easy).to receive(:http_request)
39
- .with(anything(), anything(), hash_including(:connecttimeout_ms => 100, :connecttimeout => 1))
32
+ it "ceils connecttimeout and sets connecttimeout_ms" do
33
+ expect_any_instance_of(Ethon::Easy).to receive(:http_request)
34
+ .with(anything, anything, hash_including(connecttimeout_ms: 100, connecttimeout: 1))
40
35
  .and_call_original
41
- stub_request(:get, "http://local.ch/")
42
- LHC.get('http://local.ch', options)
43
- end
36
+ stub_request(:get, "http://local.ch/")
37
+ LHC.get('http://local.ch', options)
44
38
  end
39
+ end
45
40
 
46
- context "when connecttimeout is not a whole number and connecttimeout_ms is set" do
47
-
48
- let(:options) { {:connecttimeout => 0.1, :connecttimeout_ms => 123} }
41
+ context "when connecttimeout is not a whole number and connecttimeout_ms is set" do
42
+ let(:options) { { connecttimeout: 0.1, connecttimeout_ms: 123 } }
49
43
 
50
- it "ceils connecttimeout and does not change connecttimeout_ms" do
51
- expect_any_instance_of(Ethon::Easy).to receive(:http_request)
52
- .with(anything(), anything(), hash_including(:connecttimeout_ms => 123, :connecttimeout => 1))
44
+ it "ceils connecttimeout and does not change connecttimeout_ms" do
45
+ expect_any_instance_of(Ethon::Easy).to receive(:http_request)
46
+ .with(anything, anything, hash_including(connecttimeout_ms: 123, connecttimeout: 1))
53
47
  .and_call_original
54
- stub_request(:get, "http://local.ch/")
55
- LHC.get('http://local.ch', options)
56
- end
48
+ stub_request(:get, "http://local.ch/")
49
+ LHC.get('http://local.ch', options)
57
50
  end
51
+ end
58
52
  end
59
53
  end