Skip to content

Commit

Permalink
Set the proper string encoding on responses
Browse files Browse the repository at this point in the history
Fixes #1258
  • Loading branch information
jcoyne committed Jun 15, 2017
1 parent 8126242 commit 9526ffd
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
2 changes: 2 additions & 0 deletions active-fedora.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Gem::Specification.new do |s|
s.add_dependency "deprecation"
s.add_dependency "ldp", '~> 0.7.0'
s.add_dependency "ruby-progressbar", '~> 1.0'
s.add_dependency 'faraday', '~> 0.12.1'
s.add_dependency 'faraday-encoding', '0.0.4'

s.add_development_dependency "rails"
s.add_development_dependency "rdoc"
Expand Down
11 changes: 8 additions & 3 deletions lib/active_fedora/fedora.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
require 'faraday'
require 'faraday-encoding'

module ActiveFedora
class Fedora
class << self
Expand Down Expand Up @@ -83,9 +86,11 @@ def build_connection
def authorized_connection
options = {}
options[:ssl] = ssl_options if ssl_options
connection = Faraday.new(host, options)
connection.basic_auth(user, password)
connection
Faraday.new(host, options) do |conn|
conn.response :encoding # use Faraday::Encoding middleware
conn.adapter Faraday.default_adapter # net/http
conn.basic_auth(user, password)
end
end

def validate_options
Expand Down
45 changes: 39 additions & 6 deletions spec/integration/file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,44 @@ def content
end
end

describe "#content" do
let(:file) { described_class.new { |ds| ds.content = content } }

before do
file.save
end

describe "#content" do
subject(:resource) { described_class.new(file.uri).content }

before { content.rewind }

context "when encoding is not set" do
let(:content) { fixture('dino.jpg') }

it "is read from fedora" do
expect(resource).to eq content.read
expect(resource.encoding).to eq Encoding::ASCII_8BIT
end
end

context "when encoding is set" do
let(:file) do
described_class.new do |f|
f.content = content
f.mime_type = 'text/plain;charset=UTF-8'
end
end
let(:content) { StringIO.new "I'm a little teåpot" }

it "is read from fedora" do
expect(resource).to eq content.read
expect(resource.encoding).to eq Encoding::UTF_8
end
end
end
end

context "with a sub-resource" do
before do
class MockAFBase < ActiveFedora::Base
Expand All @@ -114,7 +152,7 @@ class SampleResource < ActiveFedora::File
context "a binary file" do
let(:path) { "ds#{Time.now.to_i}" }
let(:content) { fixture('dino.jpg') }
let(:file) { described_class.new.tap { |ds| ds.content = content } }
let(:file) { described_class.new { |ds| ds.content = content } }

before do
test_object.attach_file(file, path)
Expand All @@ -125,11 +163,6 @@ class SampleResource < ActiveFedora::File
expect(test_object.attached_files[path]).to_not be_content_changed
end

it "is able to read the content from fedora" do
content.rewind
expect(test_object.attached_files[path].content).to eq content.read
end

describe "streaming the response" do
let(:stream_reader) { instance_double(IO) }
it "streams the response" do
Expand Down

0 comments on commit 9526ffd

Please sign in to comment.