lhc 12.0.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 (195) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +37 -0
  3. data/.rubocop.localch.yml +325 -0
  4. data/.rubocop.yml +61 -0
  5. data/.ruby-version +1 -0
  6. data/Gemfile +13 -0
  7. data/Gemfile.activesupport4 +4 -0
  8. data/Gemfile.activesupport5 +4 -0
  9. data/Gemfile.activesupport6 +4 -0
  10. data/LICENSE +674 -0
  11. data/README.md +984 -0
  12. data/Rakefile +25 -0
  13. data/cider-ci.yml +6 -0
  14. data/cider-ci/bin/bundle +51 -0
  15. data/cider-ci/bin/ruby_install +8 -0
  16. data/cider-ci/bin/ruby_version +25 -0
  17. data/cider-ci/jobs/rspec-activesupport-4.yml +28 -0
  18. data/cider-ci/jobs/rspec-activesupport-5.yml +27 -0
  19. data/cider-ci/jobs/rspec-activesupport-6.yml +28 -0
  20. data/cider-ci/jobs/rubocop.yml +18 -0
  21. data/cider-ci/task_components/bundle.yml +22 -0
  22. data/cider-ci/task_components/rspec.yml +36 -0
  23. data/cider-ci/task_components/rubocop.yml +29 -0
  24. data/cider-ci/task_components/ruby.yml +15 -0
  25. data/friday.yml +3 -0
  26. data/lhc.gemspec +39 -0
  27. data/lib/core_ext/hash/deep_transform_values.rb +48 -0
  28. data/lib/lhc.rb +136 -0
  29. data/lib/lhc/concerns/lhc/basic_methods_concern.rb +42 -0
  30. data/lib/lhc/concerns/lhc/configuration_concern.rb +20 -0
  31. data/lib/lhc/concerns/lhc/fix_invalid_encoding_concern.rb +42 -0
  32. data/lib/lhc/concerns/lhc/formats_concern.rb +25 -0
  33. data/lib/lhc/concerns/lhc/request/user_agent_concern.rb +25 -0
  34. data/lib/lhc/config.rb +47 -0
  35. data/lib/lhc/endpoint.rb +119 -0
  36. data/lib/lhc/error.rb +80 -0
  37. data/lib/lhc/errors/client_error.rb +73 -0
  38. data/lib/lhc/errors/parser_error.rb +4 -0
  39. data/lib/lhc/errors/server_error.rb +28 -0
  40. data/lib/lhc/errors/timeout.rb +4 -0
  41. data/lib/lhc/errors/unknown_error.rb +4 -0
  42. data/lib/lhc/format.rb +18 -0
  43. data/lib/lhc/formats.rb +8 -0
  44. data/lib/lhc/formats/form.rb +45 -0
  45. data/lib/lhc/formats/json.rb +55 -0
  46. data/lib/lhc/formats/multipart.rb +45 -0
  47. data/lib/lhc/formats/plain.rb +42 -0
  48. data/lib/lhc/interceptor.rb +32 -0
  49. data/lib/lhc/interceptors.rb +26 -0
  50. data/lib/lhc/interceptors/auth.rb +98 -0
  51. data/lib/lhc/interceptors/caching.rb +127 -0
  52. data/lib/lhc/interceptors/default_timeout.rb +16 -0
  53. data/lib/lhc/interceptors/logging.rb +37 -0
  54. data/lib/lhc/interceptors/monitoring.rb +63 -0
  55. data/lib/lhc/interceptors/prometheus.rb +51 -0
  56. data/lib/lhc/interceptors/retry.rb +41 -0
  57. data/lib/lhc/interceptors/rollbar.rb +36 -0
  58. data/lib/lhc/interceptors/throttle.rb +81 -0
  59. data/lib/lhc/interceptors/zipkin.rb +110 -0
  60. data/lib/lhc/railtie.rb +10 -0
  61. data/lib/lhc/request.rb +157 -0
  62. data/lib/lhc/response.rb +60 -0
  63. data/lib/lhc/response/data.rb +28 -0
  64. data/lib/lhc/response/data/base.rb +22 -0
  65. data/lib/lhc/response/data/collection.rb +16 -0
  66. data/lib/lhc/response/data/item.rb +29 -0
  67. data/lib/lhc/rspec.rb +12 -0
  68. data/lib/lhc/test/cache_helper.rb +3 -0
  69. data/lib/lhc/version.rb +5 -0
  70. data/script/ci/build.sh +19 -0
  71. data/spec/basic_methods/delete_spec.rb +34 -0
  72. data/spec/basic_methods/get_spec.rb +49 -0
  73. data/spec/basic_methods/post_spec.rb +42 -0
  74. data/spec/basic_methods/put_spec.rb +48 -0
  75. data/spec/basic_methods/request_spec.rb +19 -0
  76. data/spec/basic_methods/request_without_rails_spec.rb +29 -0
  77. data/spec/config/endpoints_spec.rb +63 -0
  78. data/spec/config/placeholders_spec.rb +32 -0
  79. data/spec/core_ext/hash/deep_transform_values_spec.rb +24 -0
  80. data/spec/dummy/README.rdoc +28 -0
  81. data/spec/dummy/Rakefile +8 -0
  82. data/spec/dummy/app/assets/config/manifest.js +3 -0
  83. data/spec/dummy/app/assets/images/.keep +0 -0
  84. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  85. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  86. data/spec/dummy/app/controllers/application_controller.rb +7 -0
  87. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  88. data/spec/dummy/app/helpers/application_helper.rb +4 -0
  89. data/spec/dummy/app/mailers/.keep +0 -0
  90. data/spec/dummy/app/models/.keep +0 -0
  91. data/spec/dummy/app/models/concerns/.keep +0 -0
  92. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  93. data/spec/dummy/bin/bundle +5 -0
  94. data/spec/dummy/bin/rails +6 -0
  95. data/spec/dummy/bin/rake +6 -0
  96. data/spec/dummy/config.ru +6 -0
  97. data/spec/dummy/config/application.rb +16 -0
  98. data/spec/dummy/config/boot.rb +7 -0
  99. data/spec/dummy/config/environment.rb +7 -0
  100. data/spec/dummy/config/environments/development.rb +36 -0
  101. data/spec/dummy/config/environments/production.rb +77 -0
  102. data/spec/dummy/config/environments/test.rb +41 -0
  103. data/spec/dummy/config/initializers/assets.rb +10 -0
  104. data/spec/dummy/config/initializers/backtrace_silencers.rb +9 -0
  105. data/spec/dummy/config/initializers/cookies_serializer.rb +5 -0
  106. data/spec/dummy/config/initializers/filter_parameter_logging.rb +6 -0
  107. data/spec/dummy/config/initializers/inflections.rb +18 -0
  108. data/spec/dummy/config/initializers/mime_types.rb +6 -0
  109. data/spec/dummy/config/initializers/session_store.rb +5 -0
  110. data/spec/dummy/config/initializers/wrap_parameters.rb +11 -0
  111. data/spec/dummy/config/locales/en.yml +23 -0
  112. data/spec/dummy/config/routes.rb +58 -0
  113. data/spec/dummy/config/secrets.yml +22 -0
  114. data/spec/dummy/lib/assets/.keep +0 -0
  115. data/spec/dummy/log/.keep +0 -0
  116. data/spec/dummy/public/404.html +67 -0
  117. data/spec/dummy/public/422.html +67 -0
  118. data/spec/dummy/public/500.html +66 -0
  119. data/spec/dummy/public/favicon.ico +0 -0
  120. data/spec/endpoint/compile_spec.rb +35 -0
  121. data/spec/endpoint/match_spec.rb +41 -0
  122. data/spec/endpoint/placeholders_spec.rb +30 -0
  123. data/spec/endpoint/remove_interpolated_params_spec.rb +17 -0
  124. data/spec/endpoint/values_as_params_spec.rb +31 -0
  125. data/spec/error/dup_spec.rb +12 -0
  126. data/spec/error/find_spec.rb +57 -0
  127. data/spec/error/response_spec.rb +17 -0
  128. data/spec/error/timeout_spec.rb +14 -0
  129. data/spec/error/to_s_spec.rb +80 -0
  130. data/spec/formats/form_spec.rb +27 -0
  131. data/spec/formats/json_spec.rb +66 -0
  132. data/spec/formats/multipart_spec.rb +26 -0
  133. data/spec/formats/plain_spec.rb +29 -0
  134. data/spec/interceptors/after_request_spec.rb +20 -0
  135. data/spec/interceptors/after_response_spec.rb +39 -0
  136. data/spec/interceptors/auth/basic_auth_spec.rb +17 -0
  137. data/spec/interceptors/auth/bearer_spec.rb +19 -0
  138. data/spec/interceptors/auth/reauthentication_configuration_spec.rb +61 -0
  139. data/spec/interceptors/auth/reauthentication_spec.rb +44 -0
  140. data/spec/interceptors/before_request_spec.rb +21 -0
  141. data/spec/interceptors/before_response_spec.rb +20 -0
  142. data/spec/interceptors/caching/hydra_spec.rb +26 -0
  143. data/spec/interceptors/caching/main_spec.rb +73 -0
  144. data/spec/interceptors/caching/methods_spec.rb +42 -0
  145. data/spec/interceptors/caching/options_spec.rb +89 -0
  146. data/spec/interceptors/caching/parameters_spec.rb +24 -0
  147. data/spec/interceptors/caching/response_status_spec.rb +29 -0
  148. data/spec/interceptors/caching/to_cache_spec.rb +16 -0
  149. data/spec/interceptors/default_interceptors_spec.rb +15 -0
  150. data/spec/interceptors/default_timeout/main_spec.rb +34 -0
  151. data/spec/interceptors/define_spec.rb +29 -0
  152. data/spec/interceptors/dup_spec.rb +19 -0
  153. data/spec/interceptors/logging/main_spec.rb +37 -0
  154. data/spec/interceptors/monitoring/main_spec.rb +97 -0
  155. data/spec/interceptors/prometheus_spec.rb +54 -0
  156. data/spec/interceptors/response_competition_spec.rb +41 -0
  157. data/spec/interceptors/retry/main_spec.rb +73 -0
  158. data/spec/interceptors/return_response_spec.rb +38 -0
  159. data/spec/interceptors/rollbar/invalid_encoding_spec.rb +43 -0
  160. data/spec/interceptors/rollbar/main_spec.rb +57 -0
  161. data/spec/interceptors/throttle/main_spec.rb +106 -0
  162. data/spec/interceptors/throttle/reset_track_spec.rb +53 -0
  163. data/spec/interceptors/zipkin/distributed_tracing_spec.rb +135 -0
  164. data/spec/rails_helper.rb +6 -0
  165. data/spec/request/body_spec.rb +39 -0
  166. data/spec/request/encoding_spec.rb +37 -0
  167. data/spec/request/error_handling_spec.rb +88 -0
  168. data/spec/request/headers_spec.rb +12 -0
  169. data/spec/request/ignore_errors_spec.rb +73 -0
  170. data/spec/request/option_dup_spec.rb +13 -0
  171. data/spec/request/parallel_requests_spec.rb +59 -0
  172. data/spec/request/params_encoding_spec.rb +26 -0
  173. data/spec/request/request_without_rails_spec.rb +15 -0
  174. data/spec/request/url_patterns_spec.rb +54 -0
  175. data/spec/request/user_agent_spec.rb +26 -0
  176. data/spec/request/user_agent_without_rails_spec.rb +27 -0
  177. data/spec/response/body_spec.rb +16 -0
  178. data/spec/response/code_spec.rb +16 -0
  179. data/spec/response/data_accessor_spec.rb +29 -0
  180. data/spec/response/data_spec.rb +61 -0
  181. data/spec/response/effective_url_spec.rb +16 -0
  182. data/spec/response/headers_spec.rb +18 -0
  183. data/spec/response/options_spec.rb +18 -0
  184. data/spec/response/success_spec.rb +13 -0
  185. data/spec/response/time_spec.rb +21 -0
  186. data/spec/spec_helper.rb +8 -0
  187. data/spec/support/fixtures/json/feedback.json +11 -0
  188. data/spec/support/fixtures/json/feedbacks.json +164 -0
  189. data/spec/support/fixtures/json/localina_content_ad.json +23 -0
  190. data/spec/support/load_json.rb +5 -0
  191. data/spec/support/reset_config.rb +7 -0
  192. data/spec/support/zipkin_mock.rb +113 -0
  193. data/spec/timeouts/no_signal_spec.rb +13 -0
  194. data/spec/timeouts/timings_spec.rb +55 -0
  195. metadata +534 -0
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ ENV["RAILS_ENV"] ||= 'test'
5
+
6
+ require File.expand_path("../dummy/config/environment", __FILE__)
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_helper'
4
+
5
+ describe LHC::Request do
6
+ context 'encode body' do
7
+ let(:encoded_data) { data.to_json }
8
+
9
+ before do
10
+ stub_request(:post, "http://datastore/q")
11
+ .with(body: encoded_data)
12
+ .to_return(status: 200)
13
+ end
14
+
15
+ context 'hash' do
16
+ let(:data) { { name: 'Steve' } }
17
+
18
+ it 'encodes the request body to the given format' do
19
+ LHC.post('http://datastore/q', body: data)
20
+ end
21
+
22
+ it 'does not encode the request body if it is already a string' do
23
+ LHC.post('http://datastore/q', body: encoded_data)
24
+ end
25
+ end
26
+
27
+ context 'array' do
28
+ let(:data) { [{ name: 'Steve' }] }
29
+
30
+ it 'encodes the request body to the given format' do
31
+ LHC.post('http://datastore/q', body: data)
32
+ end
33
+
34
+ it 'does not encode the request body if it is already a string' do
35
+ LHC.post('http://datastore/q', body: encoded_data)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_helper'
4
+
5
+ describe LHC::Request do
6
+ context 'encoding url' do
7
+ let(:url) { 'http://local.ch/something with spaces' }
8
+
9
+ it 'can request urls with spaces inside' do
10
+ stub_request(:get, URI.encode(url))
11
+ LHC.get(url)
12
+ end
13
+ end
14
+
15
+ context 'encoding params' do
16
+ let(:url) { 'http://local.ch/api/search?name=:name' }
17
+
18
+ it 'can do requests with params including spaces' do
19
+ stub_request(:get, 'http://local.ch/api/search?name=My%20name%20is%20rabbit')
20
+ LHC.get(url, params: { name: 'My name is rabbit' })
21
+ end
22
+ end
23
+
24
+ context 'skip encoding' do
25
+ let(:url) { 'http://local.ch/api/search?names[]=seba&names[]=david' }
26
+
27
+ it 'does not encode if encoding is skipped' do
28
+ stub_request(:get, 'http://local.ch/api/search?names%5B%5D%3Dseba%26names%5B%5D%3Ddavid')
29
+ LHC.get('http://local.ch/api/search?names%5B%5D%3Dseba%26names%5B%5D%3Ddavid', url_encoding: false)
30
+ end
31
+
32
+ it 'does double encoding, if you really want to' do
33
+ stub_request(:get, 'http://local.ch/api/search?names%255B%255D%253Dseba%2526names%255B%255D%253Ddavid')
34
+ LHC.get('http://local.ch/api/search?names%5B%5D%3Dseba%26names%5B%5D%3Ddavid')
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_helper'
4
+
5
+ describe LHC::Request do
6
+ context 'error handling' do
7
+ def to_fail_with(error)
8
+ raise_error(error)
9
+ end
10
+
11
+ def expect_status_code(status_code)
12
+ stub_request(:get, "http://something/#{status_code}").to_return(status: status_code)
13
+ expect(
14
+ -> { LHC::Request.new(url: "http://something/#{status_code}") }
15
+ ).to yield
16
+ end
17
+
18
+ it 'raises errors for anything but 2XX response codes' do
19
+ expect_status_code(0) { to_fail_with(LHC::UnknownError) }
20
+ expect_status_code(400) { to_fail_with(LHC::BadRequest) }
21
+ expect_status_code(401) { to_fail_with(LHC::Unauthorized) }
22
+ expect_status_code(402) { to_fail_with(LHC::PaymentRequired) }
23
+ expect_status_code(403) { to_fail_with(LHC::Forbidden) }
24
+ expect_status_code(403) { to_fail_with(LHC::Forbidden) }
25
+ expect_status_code(404) { to_fail_with(LHC::NotFound) }
26
+ expect_status_code(405) { to_fail_with(LHC::MethodNotAllowed) }
27
+ expect_status_code(406) { to_fail_with(LHC::NotAcceptable) }
28
+ expect_status_code(407) { to_fail_with(LHC::ProxyAuthenticationRequired) }
29
+ expect_status_code(408) { to_fail_with(LHC::RequestTimeout) }
30
+ expect_status_code(409) { to_fail_with(LHC::Conflict) }
31
+ expect_status_code(410) { to_fail_with(LHC::Gone) }
32
+ expect_status_code(411) { to_fail_with(LHC::LengthRequired) }
33
+ expect_status_code(412) { to_fail_with(LHC::PreconditionFailed) }
34
+ expect_status_code(413) { to_fail_with(LHC::RequestEntityTooLarge) }
35
+ expect_status_code(414) { to_fail_with(LHC::RequestUriToLong) }
36
+ expect_status_code(415) { to_fail_with(LHC::UnsupportedMediaType) }
37
+ expect_status_code(416) { to_fail_with(LHC::RequestedRangeNotSatisfiable) }
38
+ expect_status_code(417) { to_fail_with(LHC::ExpectationFailed) }
39
+ expect_status_code(422) { to_fail_with(LHC::UnprocessableEntity) }
40
+ expect_status_code(423) { to_fail_with(LHC::Locked) }
41
+ expect_status_code(424) { to_fail_with(LHC::FailedDependency) }
42
+ expect_status_code(426) { to_fail_with(LHC::UpgradeRequired) }
43
+ expect_status_code(500) { to_fail_with(LHC::InternalServerError) }
44
+ expect_status_code(501) { to_fail_with(LHC::NotImplemented) }
45
+ expect_status_code(502) { to_fail_with(LHC::BadGateway) }
46
+ expect_status_code(503) { to_fail_with(LHC::ServiceUnavailable) }
47
+ expect_status_code(504) { to_fail_with(LHC::GatewayTimeout) }
48
+ expect_status_code(505) { to_fail_with(LHC::HttpVersionNotSupported) }
49
+ expect_status_code(507) { to_fail_with(LHC::InsufficientStorage) }
50
+ expect_status_code(510) { to_fail_with(LHC::NotExtended) }
51
+ end
52
+ end
53
+
54
+ context 'parsing error' do
55
+ before(:each) do
56
+ stub_request(:get, 'http://datastore/v2/feedbacks').to_return(body: 'invalid json')
57
+ end
58
+
59
+ it 'requests json and parses response body' do
60
+ expect(-> {
61
+ LHC.json.get('http://datastore/v2/feedbacks').data
62
+ }).to raise_error(LHC::ParserError)
63
+ end
64
+ end
65
+
66
+ context 'custom error handler' do
67
+ it 'handles errors with the provided handler and does not raise them' do
68
+ stub_request(:get, "http://something").to_return(status: 400)
69
+ handler = double('handler', call: -> {})
70
+ LHC::Request.new(url: "http://something", rescue: handler)
71
+ expect(handler).to have_received(:call)
72
+ end
73
+
74
+ it 'exchanges body with handlers return if the handler returns something' do
75
+ stub_request(:get, "http://something").to_return(status: 400)
76
+ handler = ->(_response) { { name: 'unknown' }.to_json }
77
+ request = LHC::Request.new(url: "http://something", rescue: handler)
78
+ expect(request.response.data.name).to eq 'unknown'
79
+ end
80
+
81
+ it 'does not exchange body with handlers return if the handler returns nil' do
82
+ stub_request(:get, "http://something").to_return(status: 400, body: { message: 'an error occurred' }.to_json)
83
+ handler = ->(_response) { nil }
84
+ request = LHC::Request.new(url: "http://something", rescue: handler)
85
+ expect(request.response.data.message).to eq 'an error occurred'
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_helper'
4
+
5
+ describe LHC::Request do
6
+ it 'provides request headers' do
7
+ stub_request(:get, 'http://local.ch')
8
+ response = LHC.get('http://local.ch')
9
+ request = response.request
10
+ expect(request.headers.keys).to include 'User-Agent'
11
+ end
12
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_helper'
4
+
5
+ describe LHC::Request do
6
+ context 'ignoring LHC::NotFound' do
7
+ let(:response) { LHC.get('http://local.ch', ignore: [LHC::NotFound]) }
8
+
9
+ before { stub_request(:get, 'http://local.ch').to_return(status: 404) }
10
+
11
+ it 'does not raise an error' do
12
+ expect { response }.not_to raise_error
13
+ end
14
+
15
+ it 'body is nil' do
16
+ expect(response.body).to eq nil
17
+ end
18
+
19
+ it 'data is nil' do
20
+ expect(response.data).to eq nil
21
+ end
22
+
23
+ it 'does raise an error for 500' do
24
+ stub_request(:get, 'http://local.ch').to_return(status: 500)
25
+ expect { response }.to raise_error LHC::InternalServerError
26
+ end
27
+
28
+ it 'provides the information if the error was ignored' do
29
+ expect(response.error_ignored?).to eq true
30
+ expect(response.request.error_ignored?).to eq true
31
+ end
32
+ end
33
+
34
+ context 'inheritance when ignoring errors' do
35
+ before { stub_request(:get, 'http://local.ch').to_return(status: 404) }
36
+
37
+ it "does not raise an error when it's a subclass of the ignored error" do
38
+ expect {
39
+ LHC.get('http://local.ch', ignore: [LHC::Error])
40
+ }.not_to raise_error
41
+ end
42
+
43
+ it "does raise an error if it's not a subclass of the ignored error" do
44
+ expect {
45
+ LHC.get('http://local.ch', ignore: [ArgumentError])
46
+ }.to raise_error(LHC::NotFound)
47
+ end
48
+ end
49
+
50
+ context 'does not raise exception if ignored errors is set to nil' do
51
+ before { stub_request(:get, 'http://local.ch').to_return(status: 404) }
52
+
53
+ it "does not raise an error when ignored errors is set to array with nil" do
54
+ expect {
55
+ LHC.get('http://local.ch', ignore: [nil])
56
+ }.to raise_error(LHC::NotFound)
57
+ end
58
+
59
+ it "does not raise an error when ignored errors is set to nil" do
60
+ expect {
61
+ LHC.get('http://local.ch', ignore: nil)
62
+ }.to raise_error(LHC::NotFound)
63
+ end
64
+ end
65
+
66
+ context 'passing keys instead of arrays' do
67
+ before { stub_request(:get, 'http://local.ch').to_return(status: 404) }
68
+
69
+ it "does not raise an error when ignored errors is a key instead of an array" do
70
+ LHC.get('http://local.ch', ignore: LHC::NotFound)
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_helper'
4
+
5
+ describe LHC::Request do
6
+ it 'does not alter the options that where passed' do
7
+ LHC.configure { |c| c.endpoint(:kpi_tracker, 'http://analytics/track/{entity_id}/w', params: { env: 'PROD' }) }
8
+ options = { params: { entity_id: '123' } }
9
+ stub_request(:get, "http://analytics/track/123/w?env=PROD")
10
+ LHC.get(:kpi_tracker, options)
11
+ expect(options).to eq(params: { entity_id: '123' })
12
+ end
13
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_helper'
4
+
5
+ describe LHC::Request do
6
+ let(:request_options) do
7
+ [
8
+ { url: 'http://www.local.ch/restaurants' },
9
+ { url: 'http://www.local.ch' }
10
+ ]
11
+ end
12
+
13
+ let(:stub_parallel_requests) do
14
+ stub_request(:get, "http://www.local.ch/restaurants").to_return(status: 200, body: '1')
15
+ stub_request(:get, "http://www.local.ch").to_return(status: 200, body: '2')
16
+ end
17
+
18
+ it 'does parallel requests if you provide an array of requests' do
19
+ stub_parallel_requests
20
+ responses = LHC.request(request_options)
21
+ expect(responses[0].body).to eq '1'
22
+ expect(responses[1].body).to eq '2'
23
+ end
24
+
25
+ context 'interceptors' do
26
+ before(:each) do
27
+ class TestInterceptor < LHC::Interceptor; end
28
+ LHC.configure { |c| c.interceptors = [TestInterceptor] }
29
+ end
30
+
31
+ it 'calls interceptors also for parallel requests' do
32
+ stub_parallel_requests
33
+ @called = 0
34
+ allow_any_instance_of(TestInterceptor)
35
+ .to receive(:before_request) { @called += 1 }
36
+ LHC.request(request_options)
37
+ expect(@called).to eq 2
38
+ end
39
+ end
40
+
41
+ context 'webmock disabled' do
42
+ before do
43
+ WebMock.disable!
44
+ end
45
+
46
+ after do
47
+ WebMock.enable!
48
+ end
49
+
50
+ it 'does not memorize parallelization handlers in typhoeus (hydra) in case one request of the parallization fails' do
51
+ begin
52
+ LHC.request([{ url: 'https://www.google.com/' }, { url: 'https://nonexisting123' }, { url: 'https://www.google.com/' }, { url: 'https://nonexisting123' }])
53
+ rescue LHC::UnknownError
54
+ end
55
+
56
+ LHC.request([{ url: 'https://www.google.com' }])
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_helper'
4
+
5
+ describe LHC::Request do
6
+ context 'without an encoding setting' do
7
+ it 'encodes array params in default rack format' do
8
+ stub_request(:get, 'http://datastore/q?a%5B%5D=1&a%5B%5D=2&a%5B%5D=3')
9
+ LHC.get('http://datastore/q', params: { a: [1, 2, 3] })
10
+ end
11
+ end
12
+
13
+ context 'with encoding set to :rack' do
14
+ it 'encodes array params in rack format' do
15
+ stub_request(:get, 'http://datastore/q?a%5B%5D=1&a%5B%5D=2&a%5B%5D=3')
16
+ LHC.get('http://datastore/q', params: { a: [1, 2, 3] }, params_encoding: :rack)
17
+ end
18
+ end
19
+
20
+ context 'with encoding set to :multi' do
21
+ it 'encodes array params in multi format' do
22
+ stub_request(:get, 'http://datastore/q?a=1&a=2&a=3')
23
+ LHC.get('http://datastore/q', params: { a: [1, 2, 3] }, params_encoding: :multi)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe LHC::Request do
6
+ before do
7
+ allow_any_instance_of(LHC::Request).to receive(:use_configured_endpoint!)
8
+ allow_any_instance_of(LHC::Request).to receive(:generate_url_from_template!)
9
+ end
10
+ context 'request without rails' do
11
+ it 'does have deep_merge dependency met' do
12
+ expect { LHC::Request.new({}, false) }.not_to raise_error
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_helper'
4
+
5
+ describe LHC::Request do
6
+ it 'compiles url in case of configured endpoints' do
7
+ options = { params: {
8
+ has_reviews: true
9
+ } }
10
+ url = 'http://datastore/v2/campaign/{campaign_id}/feedbacks'
11
+ LHC.configure { |c| c.endpoint(:feedbacks, url, options) }
12
+ stub_request(:get, 'http://datastore/v2/campaign/123/feedbacks?has_reviews=true')
13
+ LHC.get(:feedbacks, params: { campaign_id: 123 })
14
+ end
15
+
16
+ it 'compiles url when doing a request' do
17
+ stub_request(:get, 'http://datastore:8080/v2/feedbacks/123')
18
+ LHC.get('http://datastore:8080/v2/feedbacks/{id}', params: { id: 123 })
19
+ end
20
+
21
+ it 'considers body when compiling urls' do
22
+ stub_request(:post, "http://datastore:8080/v2/places/123")
23
+ LHC.json.post('http://datastore:8080/v2/places/{id}', body: { id: 123 })
24
+ end
25
+
26
+ context 'custom data structures that respond to as_json (like LHS data or record)' do
27
+ before do
28
+ class CustomStructure
29
+
30
+ def initialize(data)
31
+ @data = data
32
+ end
33
+
34
+ def as_json
35
+ @data.as_json
36
+ end
37
+
38
+ def to_json
39
+ as_json.to_json
40
+ end
41
+ end
42
+ end
43
+
44
+ let(:data) do
45
+ CustomStructure.new(id: '12345')
46
+ end
47
+
48
+ it 'compiles url from body params when body object respond_to(:as_json)' do
49
+ stub_request(:post, "http://datastore/places/12345")
50
+ .to_return(status: 200)
51
+ LHC.post('http://datastore/places/{id}', body: data)
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_helper'
4
+
5
+ describe LHC::Request do
6
+ before do
7
+ LHC.send(:remove_const, :Request)
8
+ load('lhc/concerns/lhc/request/user_agent_concern.rb')
9
+ load('lhc/request.rb')
10
+ end
11
+
12
+ context 'default headers' do
13
+ context 'agent' do
14
+ it 'sets header agent information to be LHC' do
15
+ stub_request(:get, "http://local.ch/")
16
+ .with(
17
+ headers: {
18
+ 'User-Agent' => "LHC (#{LHC::VERSION}; Dummy) [https://github.com/local-ch/lhc]"
19
+ }
20
+ )
21
+ .to_return(status: 200)
22
+ LHC.get('http://local.ch')
23
+ end
24
+ end
25
+ end
26
+ end