lhc 3.8.0 → 3.8.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b11d4708f7d94c14f063078c8c7d2d19ef0b17b3
4
- data.tar.gz: 6af6b618ffbf68766bcbe1fa0aa1a90320794d1f
3
+ metadata.gz: e327e8cf911398de6860f5e328bfee3d4ec4a117
4
+ data.tar.gz: eee4da06a4953d23eaef467a935a0a6ab7198454
5
5
  SHA512:
6
- metadata.gz: 9d592f45c4492bf54a9a91be316bee2023612cb21680e996812db68d9a47b3cf11c37933c4a17ef40f7c0cee919947d6f0a30fc1a27877aa6fd0c99a7e382a2f
7
- data.tar.gz: 00b53d02f8a57a918873f110b733d7084989e2c92057d0ca66ca0a693163cbe8d8351173f3b84c703f0bd931006dc2046d0c3dc7b56cc83cbbc4bf5025e1898c
6
+ metadata.gz: 63b5203325d62df8aec412bb04e0e4bd6cffdafa29527641a8e0319a91e7fd39300135d31a0a9e3e169ad287c5c678f248994a71eb786a558957017ff6c8bed5
7
+ data.tar.gz: b13e872cab4a98c6e06a49d1c5e59ce86fdb1c1ae0ddc2b1bfb11366b5157e2444953b0d6cb52db4d0192656d64c413a24f83295b040afec96cfb795378db954
@@ -55,12 +55,43 @@ class LHC::Error < StandardError
55
55
  def to_s
56
56
  request = response.request
57
57
  debug = []
58
- debug << "#{request.method} #{request.url}"
59
- debug << "Params: #{request.options}"
58
+ debug << [request.method, request.url].map { |str| fix_invalid_encoding(str) }.join(' ')
59
+ debug << "Options: #{request.options}"
60
60
  debug << "Headers: #{request.headers}"
61
61
  debug << "Response Code: #{response.code}"
62
62
  debug << response.body
63
63
  debug << _message
64
- debug.join("\n")
64
+ debug.map { |str| fix_invalid_encoding(str) }.join("\n")
65
+ end
66
+
67
+ private
68
+
69
+ # fix strings that contain non-UTF8 encoding in a forceful way
70
+ # should none of the fix-attempts be successful,
71
+ # an empty string is returned instead
72
+ def fix_invalid_encoding(string)
73
+ return string unless string.is_a?(String)
74
+ result = string.dup
75
+
76
+ # we assume it's ISO-8859-1 first
77
+ if !result.valid_encoding? || !utf8?(result)
78
+ result.encode!('UTF-8', 'ISO-8859-1', invalid: :replace, undef: :replace, replace: '')
79
+ end
80
+
81
+ # if it's still an issue, try with BINARY
82
+ if !result.valid_encoding? || !utf8?(result)
83
+ result.encode!('UTF-8', 'BINARY', invalid: :replace, undef: :replace, replace: '')
84
+ end
85
+
86
+ # if its STILL an issue, return an empty string :(
87
+ if !result.valid_encoding? || !utf8?(result)
88
+ result = ""
89
+ end
90
+
91
+ result
92
+ end
93
+
94
+ def utf8?(string)
95
+ string.encoding == Encoding::UTF_8
65
96
  end
66
97
  end
@@ -1,3 +1,3 @@
1
1
  module LHC
2
- VERSION ||= "3.8.0"
2
+ VERSION ||= "3.8.1"
3
3
  end
@@ -0,0 +1,44 @@
1
+ require 'rails_helper'
2
+
3
+ describe LHC::Error do
4
+ context 'to_s' do
5
+ let(:invalid) { "in\xc3lid".force_encoding('ASCII-8BIT') }
6
+ let(:valid) { "vælid" }
7
+
8
+ context 'check assumptions' do
9
+ it 'joining raises an error' do
10
+ expect { [valid, invalid].join }.to raise_error Encoding::CompatibilityError
11
+ end
12
+ it 'interpolation raises an error' do
13
+ expect { "#{valid} #{invalid}" }.to raise_error Encoding::CompatibilityError
14
+ end
15
+ it 'to_json on an array raises an error' do
16
+ expect { [valid, invalid].to_json }.to raise_error Encoding::UndefinedConversionError
17
+ end
18
+
19
+ it 'to_s on a hash does not raise an error' do
20
+ expect { { valid: valid, invalid: invalid }.to_s }.not_to raise_error
21
+ end
22
+
23
+ it 'to_json on a hash does raise an error' do
24
+ expect { { valid: valid, invalid: invalid }.to_json }.to raise_error Encoding::UndefinedConversionError
25
+ end
26
+ end
27
+
28
+ it 'invalid body, valid message' do
29
+ stub_request(:get, 'http://local.ch')
30
+ .to_return(status: 200, body: "{ text : '#{invalid}' }")
31
+ response = LHC.get('http://local.ch')
32
+ expect { LHC::Error.new(valid, response).to_s }.not_to raise_error # Encoding::CompatibilityError
33
+ end
34
+
35
+ it 'valid body, invalid message' do
36
+ stub_request(:get, 'http://local.ch')
37
+ .to_return(status: 200, body: "{ text : '#{valid}' }")
38
+ response = LHC.get('http://local.ch')
39
+ expect { LHC::Error.new(invalid, response).to_s }.not_to raise_error # Encoding::CompatibilityError
40
+ end
41
+ # the other cases cannot be tested (for example what happens if the headers contain invalid data)
42
+ # because the mocking framework triggers the encoding error already
43
+ end
44
+ end
@@ -4,8 +4,7 @@ describe LHC do
4
4
  context 'interceptor' do
5
5
  before(:each) do
6
6
  class SomeInterceptor < LHC::Interceptor
7
- def after_request(request)
8
- end
7
+ def after_request(request); end
9
8
  end
10
9
  LHC.configure { |c| c.interceptors = [SomeInterceptor] }
11
10
  end
@@ -4,8 +4,7 @@ describe LHC do
4
4
  context 'interceptor' do
5
5
  before(:each) do
6
6
  class SomeInterceptor < LHC::Interceptor
7
- def before_response(request)
8
- end
7
+ def before_response(request); end
9
8
  end
10
9
  LHC.configure { |c| c.interceptors = [SomeInterceptor] }
11
10
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lhc
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.8.0
4
+ version: 3.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - local.ch
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-07 00:00:00.000000000 Z
11
+ date: 2016-12-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: typhoeus
@@ -241,6 +241,7 @@ files:
241
241
  - spec/error/find_spec.rb
242
242
  - spec/error/response_spec.rb
243
243
  - spec/error/timeout_spec.rb
244
+ - spec/error/to_s_spec.rb
244
245
  - spec/formats/json_spec.rb
245
246
  - spec/interceptors/after_request_spec.rb
246
247
  - spec/interceptors/after_response_spec.rb
@@ -295,7 +296,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
295
296
  requirements:
296
297
  - Ruby >= 1.9.2
297
298
  rubyforge_project:
298
- rubygems_version: 2.4.6
299
+ rubygems_version: 2.6.8
299
300
  signing_key:
300
301
  specification_version: 4
301
302
  summary: LocalHttpClient
@@ -354,6 +355,7 @@ test_files:
354
355
  - spec/error/find_spec.rb
355
356
  - spec/error/response_spec.rb
356
357
  - spec/error/timeout_spec.rb
358
+ - spec/error/to_s_spec.rb
357
359
  - spec/formats/json_spec.rb
358
360
  - spec/interceptors/after_request_spec.rb
359
361
  - spec/interceptors/after_response_spec.rb