faraday-encoding 0.0.2 → 0.0.3

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: 89fdc80352bd6ee60754f19ea6fbf6b213e43198
4
- data.tar.gz: 116f0cdfc9c4135a391ca9a2c955a7d5d20ecd07
3
+ metadata.gz: ce866285eb0008b4318248afe64b338b11754bf2
4
+ data.tar.gz: 08415d4be0469ad92d72cd863c4ca13a4fd768b5
5
5
  SHA512:
6
- metadata.gz: be550d9f77dd7c161b00b1debda2c369c8da41e708af5e4edc06f71341ae2825aa7094a4ada3e1988c6186a7566ced1849ab636972273d57a010f1f15f0bf160
7
- data.tar.gz: 6bc1bf936d5a0ac790c1d2da440168e07637b16ac71ea86df030859e5773798426c0d181e3dcf23663fa90319a1ef976aef6f55ff2cad3ddf98e2d6ec6998331
6
+ metadata.gz: 3f7a3707dd92a8ae3716c74a7d5fa89d0caffbc0cff0394445caec745b925acbf17ff629d09d3f49185c07b1d31adf856a59e04985827d45152aeabce41c06ad
7
+ data.tar.gz: d9df280956bf0d164d6d03f577667b7d405f3a4f588d472ad0c0fa0547a3fc54035c6c2766d4c2cf65b93605ce0a45e1d7c6f73bebdf4a409a7cfdb9f557ea8b
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "faraday-encoding"
7
- spec.version = "0.0.2"
7
+ spec.version = "0.0.3"
8
8
  spec.authors = ["Takayuki Matsubara"]
9
9
  spec.email = ["takayuki.1229@m3.com"]
10
10
  spec.summary = %q{A Faraday Middleware sets body encoding when specified by server.}
@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
20
20
  spec.add_development_dependency "bundler", "~> 1.7"
21
21
  spec.add_development_dependency "rake", "~> 10.0"
22
22
  spec.add_development_dependency "rspec"
23
+ spec.add_development_dependency 'faraday_middleware', '~> 0.10'
23
24
 
24
25
  spec.add_runtime_dependency "faraday"
25
26
  end
@@ -11,7 +11,9 @@ module Faraday
11
11
  def call(environment)
12
12
  @app.call(environment).on_complete do |env|
13
13
  @env = env
14
- env[:body].force_encoding(content_charset) if content_charset
14
+ if encoding = content_charset
15
+ env[:body].force_encoding(encoding)
16
+ end
15
17
  end
16
18
  end
17
19
 
@@ -19,15 +21,13 @@ module Faraday
19
21
 
20
22
  # @return [Encoding|NilClass] returns Encoding or nil
21
23
  def content_charset
22
- @content_charset ||= ::Encoding.find encoding_name rescue nil
24
+ ::Encoding.find encoding_name rescue nil
23
25
  end
24
26
 
25
27
  # @return [String] returns a string representing encoding name if it is find in the CONTENT TYPE header
26
28
  def encoding_name
27
- @encoding_name ||= begin
28
- if /charset=([^;|$]+)/.match(content_type)
29
- mapped_encoding(Regexp.last_match(1))
30
- end
29
+ if /charset=([^;|$]+)/.match(content_type)
30
+ mapped_encoding(Regexp.last_match(1))
31
31
  end
32
32
  end
33
33
 
@@ -3,6 +3,7 @@ require 'spec_helper'
3
3
  describe Faraday::Encoding do
4
4
  let(:client) do
5
5
  Faraday.new do |connection|
6
+ connection.use FaradayMiddleware::FollowRedirects, limit: 10
6
7
  connection.response :encoding
7
8
  connection.adapter :test, stubs
8
9
  end
@@ -13,6 +14,9 @@ describe Faraday::Encoding do
13
14
  stub.get('/') do
14
15
  [response_status, response_headers, response_body]
15
16
  end
17
+ stub.get('/redirected') do
18
+ [redirected_status, redirected_headers, redirected_body]
19
+ end
16
20
  end
17
21
  end
18
22
 
@@ -64,4 +68,31 @@ describe Faraday::Encoding do
64
68
  end
65
69
  end
66
70
  end
71
+
72
+ context 'deal correctly with redirects that specify an encoding' do
73
+ # First request response with a redirect + a charset in the content-type header
74
+ let(:response_status) { 301 }
75
+ let(:response_headers) do
76
+ {
77
+ 'content-type' => "text/plain; charset=utf-8",
78
+ 'location' => '/redirected'
79
+ }
80
+ end
81
+ let(:response_body) { 'Redirected to /redirected' }
82
+
83
+ # The second request does not specify a charset in the content-type
84
+ let(:redirected_status) { 200 }
85
+ let(:redirected_headers) do
86
+ {
87
+ 'content-type' => "text/html"
88
+ }
89
+ end
90
+ let(:redirected_body) do
91
+ 'abc'.force_encoding(Encoding::ASCII_8BIT)
92
+ end
93
+ it 'does not memoize redirect encoding' do
94
+ response = client.get('/')
95
+ expect(response.body.encoding).to eq(Encoding::ASCII_8BIT)
96
+ end
97
+ end
67
98
  end
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
2
2
  require "faraday-encoding"
3
+ require "faraday_middleware"
3
4
 
4
5
  RSpec.configure do |config|
5
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday-encoding
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takayuki Matsubara
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-18 00:00:00.000000000 Z
11
+ date: 2016-03-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: faraday_middleware
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.10'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.10'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: faraday
57
71
  requirement: !ruby/object:Gem::Requirement