faraday_json 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/spec/helper.rb ADDED
@@ -0,0 +1,74 @@
1
+ if ENV['COVERAGE']
2
+ require 'simplecov'
3
+
4
+ SimpleCov.formatter = Class.new do
5
+ def format(result)
6
+ SimpleCov::Formatter::HTMLFormatter.new.format(result) unless ENV['CI']
7
+ File.open('coverage/covered_percent', 'w') do |f|
8
+ f.printf "%.2f", result.source_files.covered_percent
9
+ end
10
+ end
11
+ end
12
+
13
+ SimpleCov.start do
14
+ # add_filter 'faraday_middleware.rb'
15
+ add_filter 'backwards_compatibility.rb'
16
+ end
17
+ end
18
+
19
+ require 'rspec'
20
+ require 'faraday'
21
+
22
+ module EnvCompatibility
23
+ def faraday_env(env)
24
+ if defined?(Faraday::Env)
25
+ Faraday::Env.from(env)
26
+ else
27
+ env
28
+ end
29
+ end
30
+ end
31
+
32
+ module ResponseMiddlewareExampleGroup
33
+ def self.included(base)
34
+ base.let(:options) { Hash.new }
35
+ base.let(:headers) { Hash.new }
36
+ base.let(:middleware) {
37
+ described_class.new(lambda {|env|
38
+ Faraday::Response.new(env)
39
+ }, options)
40
+ }
41
+ end
42
+
43
+ def process(body, content_type = nil, options = {})
44
+ env = {
45
+ :body => body, :request => options,
46
+ :request_headers => Faraday::Utils::Headers.new,
47
+ :response_headers => Faraday::Utils::Headers.new(headers)
48
+ }
49
+ env[:response_headers]['content-type'] = content_type if content_type
50
+ yield(env) if block_given?
51
+ middleware.call(faraday_env(env))
52
+ end
53
+ end
54
+
55
+ RSpec.configure do |config|
56
+ config.include EnvCompatibility
57
+ config.include ResponseMiddlewareExampleGroup, :type => :response
58
+ config.expect_with :rspec do |c|
59
+ c.syntax = :expect
60
+ end
61
+ end
62
+
63
+
64
+ def test_encode(str, encoding)
65
+ if RUBY_VERSION.start_with?("1.8")
66
+ # Ruby 1.8 stores strings as bytes. Since this file is in UTF-8, we'll
67
+ # fix the FROM encoding to UTF-8.
68
+ require 'iconv'
69
+ return ::Iconv.conv(encoding, 'UTF-8', str)
70
+ else
71
+ # Yay, sanity!
72
+ return str.encode(encoding)
73
+ end
74
+ end
@@ -0,0 +1,217 @@
1
+ # encoding: utf-8
2
+
3
+ require 'helper'
4
+ require 'faraday_json/parse_json'
5
+
6
+ describe FaradayJSON::ParseJson, :type => :response do
7
+ context "no type matching" do
8
+ it "doesn't change nil body" do
9
+ expect(process(nil).body).to be_nil
10
+ end
11
+
12
+ it "nullifies empty body" do
13
+ expect(process('').body).to be_nil
14
+ end
15
+
16
+ it "parses json body" do
17
+ response = process('{"a":1}')
18
+ expect(response.body).to eq('a' => 1)
19
+ expect(response.env[:raw_body]).to be_nil
20
+ end
21
+ end
22
+
23
+ context "with preserving raw" do
24
+ let(:options) { {:preserve_raw => true} }
25
+
26
+ it "parses json body" do
27
+ response = process('{"a":1}')
28
+ expect(response.body).to eq('a' => 1)
29
+ expect(response.env[:raw_body]).to eq('{"a":1}')
30
+ end
31
+
32
+ it "can opt out of preserving raw" do
33
+ response = process('{"a":1}', nil, :preserve_raw => false)
34
+ expect(response.env[:raw_body]).to be_nil
35
+ end
36
+ end
37
+
38
+ context "with regexp type matching" do
39
+ let(:options) { {:content_type => /\bjson$/} }
40
+
41
+ it "parses json body of correct type" do
42
+ response = process('{"a":1}', 'application/x-json')
43
+ expect(response.body).to eq('a' => 1)
44
+ end
45
+
46
+ it "ignores json body of incorrect type" do
47
+ response = process('{"a":1}', 'text/json-xml')
48
+ expect(response.body).to eq('{"a":1}')
49
+ end
50
+ end
51
+
52
+ context "with array type matching" do
53
+ let(:options) { {:content_type => %w[a/b c/d]} }
54
+
55
+ it "parses json body of correct type" do
56
+ expect(process('{"a":1}', 'a/b').body).to be_a(Hash)
57
+ expect(process('{"a":1}', 'c/d').body).to be_a(Hash)
58
+ end
59
+
60
+ it "ignores json body of incorrect type" do
61
+ expect(process('{"a":1}', 'a/d').body).not_to be_a(Hash)
62
+ end
63
+ end
64
+
65
+ it "chokes on invalid json" do
66
+ ['{!', '"a"', 'true', 'null', '1'].each do |data|
67
+ expect{ process(data) }.to raise_error(Faraday::Error::ParsingError)
68
+ end
69
+ end
70
+
71
+ context "with mime type fix" do
72
+ let(:middleware) {
73
+ app = FaradayJSON::ParseJsonMimeTypeFix.new(lambda {|env|
74
+ Faraday::Response.new(env)
75
+ }, :content_type => /^text\//)
76
+ described_class.new(app, :content_type => 'application/json')
77
+ }
78
+
79
+ it "ignores completely incompatible type" do
80
+ response = process('{"a":1}', 'application/xml')
81
+ expect(response.body).to eq('{"a":1}')
82
+ end
83
+
84
+ it "ignores compatible type with bad data" do
85
+ response = process('var a = 1', 'text/javascript')
86
+ expect(response.body).to eq('var a = 1')
87
+ expect(response['content-type']).to eq('text/javascript')
88
+ end
89
+
90
+ it "corrects compatible type and data" do
91
+ response = process('{"a":1}', 'text/javascript')
92
+ expect(response.body).to be_a(Hash)
93
+ expect(response['content-type']).to eq('application/json')
94
+ end
95
+
96
+ it "corrects compatible type even when data starts with whitespace" do
97
+ response = process(%( \r\n\t{"a":1}), 'text/javascript')
98
+ expect(response.body).to be_a(Hash)
99
+ expect(response['content-type']).to eq('application/json')
100
+ end
101
+ end
102
+
103
+ context "HEAD responses" do
104
+ it "nullifies the body if it's only one space" do
105
+ response = process(' ')
106
+ expect(response.body).to be_nil
107
+ end
108
+
109
+ it "nullifies the body if it's two spaces" do
110
+ response = process(' ')
111
+ expect(response.body).to be_nil
112
+ end
113
+ end
114
+
115
+ ### Unicode test cases
116
+ # Ruby 1.8 will almost certainly fail if there is no charset given in a header.
117
+ # In Ruby >1.8, we have some more methods for guessing well.
118
+
119
+ ### All Ruby versions should work with a charset given.
120
+ context "with utf-8 encoding" do
121
+ it "parses json body" do
122
+ response = process(test_encode('{"a":"ü"}', 'utf-8'), 'application/json; charset=utf-8')
123
+ expect(response.body).to eq('a' => 'ü')
124
+ end
125
+ end
126
+
127
+ context "with utf-16be encoding" do
128
+ it "parses json body" do
129
+ response = process(test_encode('{"a":"ü"}', 'utf-16be'), 'application/json; charset=utf-16be')
130
+ expect(response.body).to eq('a' => 'ü')
131
+ end
132
+ end
133
+
134
+ context "with utf-16le encoding" do
135
+ it "parses json body" do
136
+ response = process(test_encode('{"a":"ü"}', 'utf-16le'), 'application/json; charset=utf-16le')
137
+ expect(response.body).to eq('a' => 'ü')
138
+ end
139
+ end
140
+
141
+ context "with iso-8859-15 encoding" do
142
+ it "parses json body" do
143
+ response = process(test_encode('{"a":"ü"}', 'iso-8859-15'), 'application/json; charset=iso-8859-15')
144
+ expect(response.body).to eq('a' => 'ü')
145
+ end
146
+ end
147
+
148
+ ### Ruby versions > 1.8 should be able to guess missing charsets at times.
149
+ if not RUBY_VERSION.start_with?("1.8")
150
+ context "with utf-8 encoding without content type" do
151
+ it "parses json body" do
152
+ response = process(test_encode('{"a":"ü"}', 'utf-8'))
153
+ expect(response.body).to eq('a' => 'ü')
154
+ end
155
+ end
156
+
157
+ context "with utf-16be encoding without content type" do
158
+ it "parses json body" do
159
+ response = process(test_encode('{"a":"ü"}', 'utf-16be'))
160
+ expect(response.body).to eq('a' => 'ü')
161
+ end
162
+ end
163
+
164
+ context "with utf-16le encoding without content type" do
165
+ it "parses json body" do
166
+ response = process(test_encode('{"a":"ü"}', 'utf-16le'))
167
+ expect(response.body).to eq('a' => 'ü')
168
+ end
169
+ end
170
+
171
+ context "with iso-8859-15 encoding without content type" do
172
+ it "parses json body" do
173
+ response = process(test_encode('{"a":"ü"}', 'iso-8859-15'))
174
+ expect(response.body).to eq('a' => 'ü')
175
+ end
176
+ end
177
+ end
178
+
179
+ ### Dealing with files in various encoding should ideally be easy
180
+ FILES = {
181
+ 'spec/data/iso8859-15_file.json' => 'iso-8859-15',
182
+ 'spec/data/utf16be_file.json' => 'utf-16be',
183
+ 'spec/data/utf16le_file.json' => 'utf-16le',
184
+ 'spec/data/utf8_file.json' => 'utf-8',
185
+ }
186
+
187
+ if not RUBY_VERSION.start_with?("1.8")
188
+ FILES.each do |fname, enc|
189
+ context "reading #{enc} encoded file '#{fname}'" do
190
+ # Read the string from file; read binary/with encoding. Ruby 1.8 will
191
+ # ignore this, but must still work.
192
+ data = File.new(fname, "rb:#{enc}").read
193
+
194
+ # Passing that data with a charset should do the right thing.
195
+ it "decodes body" do
196
+ response = process(data)
197
+ expect(response.body).to eq('a' => "Hellö, Wörld!")
198
+ end
199
+ end
200
+ end
201
+ end
202
+
203
+ FILES.each do |fname, enc|
204
+ context "reading #{enc} encoded file '#{fname}' as binary" do
205
+ # Read the string from file; read binary/with encoding. Ruby 1.8 will
206
+ # ignore this, but must still work.
207
+ data = File.new(fname, "rb").read
208
+
209
+ # Passing that data with a charset should do the right thing.
210
+ it "decodes body" do
211
+ response = process(data, "application/json; charset=#{enc}")
212
+ expect(response.body).to eq('a' => "Hellö, Wörld!")
213
+ end
214
+ end
215
+ end
216
+
217
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: faraday_json
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jens Finkhaeuser
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 0.7.4
48
+ - - "<"
49
+ - !ruby/object:Gem::Version
50
+ version: '0.10'
51
+ type: :runtime
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: 0.7.4
58
+ - - "<"
59
+ - !ruby/object:Gem::Version
60
+ version: '0.10'
61
+ description: This is a character encoding-aware JSON middleware for Faraday.
62
+ email:
63
+ - foss@spritecloud.com
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files: []
67
+ files:
68
+ - ".gitignore"
69
+ - ".travis.yml"
70
+ - Gemfile
71
+ - Gemfile-0.7.rb
72
+ - Gemfile-0.7.rb.lock
73
+ - Gemfile-0.8.rb
74
+ - Gemfile-0.8.rb.lock
75
+ - Gemfile.lock
76
+ - LICENSE
77
+ - README.md
78
+ - Rakefile
79
+ - faraday_json.gemspec
80
+ - lib/faraday_json.rb
81
+ - lib/faraday_json/encode_json.rb
82
+ - lib/faraday_json/encoding.rb
83
+ - lib/faraday_json/parse_json.rb
84
+ - lib/faraday_json/version.rb
85
+ - spec/data/iso8859-15_file.json
86
+ - spec/data/utf16be_file.json
87
+ - spec/data/utf16le_file.json
88
+ - spec/data/utf8_file.json
89
+ - spec/encode_json_spec.rb
90
+ - spec/helper.rb
91
+ - spec/parse_json_spec.rb
92
+ homepage: https://github.com/spriteCloud/faraday_json
93
+ licenses:
94
+ - MITNFA
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.4.5
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: This is a character encoding-aware JSON middleware for Faraday.
116
+ test_files:
117
+ - spec/data/iso8859-15_file.json
118
+ - spec/data/utf16be_file.json
119
+ - spec/data/utf16le_file.json
120
+ - spec/data/utf8_file.json
121
+ - spec/encode_json_spec.rb
122
+ - spec/helper.rb
123
+ - spec/parse_json_spec.rb