Skip to content

Commit

Permalink
Merge pull request #21 from janko-m/rewind-content-at-the-end-of-to_s
Browse files Browse the repository at this point in the history
Rewind content at the end of Readable#to_s
  • Loading branch information
ixti authored Feb 16, 2018
2 parents ff2ae95 + d60c1b0 commit 6779a01
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 21 deletions.
4 changes: 3 additions & 1 deletion lib/http/form_data/readable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ module Readable
# @return [String]
def to_s
rewind
read
content = read
rewind
content
end

# Reads and returns part of IO content.
Expand Down
64 changes: 44 additions & 20 deletions spec/lib/http/form_data/file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
# coding: utf-8

RSpec.describe HTTP::FormData::File do
let(:opts) { nil }
let(:opts) { nil }
let(:form_file) { described_class.new(file, opts) }

describe "#size" do
subject { described_class.new(file, opts).size }
subject { form_file.size }

context "when file given as a String" do
let(:file) { fixture("the-http-gem.info").to_s }
Expand All @@ -30,32 +31,56 @@
end

describe "#to_s" do
subject { described_class.new(file, opts).to_s }
subject { form_file.to_s }

context "when file given as a String" do
let(:file) { fixture("the-http-gem.info").to_s }
it { is_expected.to eq fixture("the-http-gem.info").read(:mode => "rb") }

it "rewinds content" do
content = form_file.read
expect(form_file.to_s).to eq content
expect(form_file.read).to eq content
end
end

context "when file given as a Pathname" do
let(:file) { fixture("the-http-gem.info") }
it { is_expected.to eq fixture("the-http-gem.info").read(:mode => "rb") }

it "rewinds content" do
content = form_file.read
expect(form_file.to_s).to eq content
expect(form_file.read).to eq content
end
end

context "when file given as File" do
let(:file) { fixture("the-http-gem.info").open("rb") }
after { file.close }
it { is_expected.to eq fixture("the-http-gem.info").read(:mode => "rb") }

it "rewinds content" do
content = form_file.read
expect(form_file.to_s).to eq content
expect(form_file.read).to eq content
end
end

context "when file given as IO" do
let(:file) { StringIO.new "привет мир!" }
it { is_expected.to eq "привет мир!" }

it "rewinds content" do
content = form_file.read
expect(form_file.to_s).to eq content
expect(form_file.read).to eq content
end
end
end

describe "#read" do
subject { described_class.new(file, opts).read }
subject { form_file.read }

context "when file given as a String" do
let(:file) { fixture("the-http-gem.info").to_s }
Expand All @@ -80,25 +105,23 @@
end

describe "#rewind" do
subject { described_class.new(file, opts) }

context "when file given as a String" do
let(:file) { fixture("the-http-gem.info").to_s }

it "rewinds the underlying IO object" do
content = subject.read
subject.rewind
expect(subject.read).to eq content
content = form_file.read
form_file.rewind
expect(form_file.read).to eq content
end
end

context "when file given as a Pathname" do
let(:file) { fixture("the-http-gem.info") }

it "rewinds the underlying IO object" do
content = subject.read
subject.rewind
expect(subject.read).to eq content
content = form_file.read
form_file.rewind
expect(form_file.read).to eq content
end
end

Expand All @@ -107,25 +130,25 @@
after { file.close }

it "rewinds the underlying IO object" do
content = subject.read
subject.rewind
expect(subject.read).to eq content
content = form_file.read
form_file.rewind
expect(form_file.read).to eq content
end
end

context "when file given as IO" do
let(:file) { StringIO.new "привет мир!" }

it "rewinds the underlying IO object" do
content = subject.read
subject.rewind
expect(subject.read).to eq content
content = form_file.read
form_file.rewind
expect(form_file.read).to eq content
end
end
end

describe "#filename" do
subject { described_class.new(file, opts).filename }
subject { form_file.filename }

context "when file given as a String" do
let(:file) { fixture("the-http-gem.info").to_s }
Expand Down Expand Up @@ -174,7 +197,8 @@
end

describe "#content_type" do
subject { described_class.new(StringIO.new, opts).content_type }
let(:file) { StringIO.new }
subject { form_file.content_type }

it { is_expected.to eq "application/octet-stream" }

Expand Down
6 changes: 6 additions & 0 deletions spec/lib/http/form_data/multipart_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ def disposition(params)
].join("")
end

it "rewinds content" do
content = form_data.read
expect(form_data.to_s).to eq content
expect(form_data.read).to eq content
end

context "with user-defined boundary" do
subject(:form_data) do
HTTP::FormData::Multipart.new params, :boundary => "my-boundary"
Expand Down
6 changes: 6 additions & 0 deletions spec/lib/http/form_data/part_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
context "when body given as String" do
let(:body) { "привет мир!" }
it { is_expected.to eq "привет мир!" }

it "rewinds content" do
content = part.read
expect(part.to_s).to eq content
expect(part.read).to eq content
end
end
end

Expand Down
6 changes: 6 additions & 0 deletions spec/lib/http/form_data/urlencoded_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
let(:data) { { "foo[bar]" => "тест" } }
it { is_expected.to eq "foo%5Bbar%5D=%D1%82%D0%B5%D1%81%D1%82" }
end

it "rewinds content" do
content = form_data.read
expect(form_data.to_s).to eq content
expect(form_data.read).to eq content
end
end

describe "#size" do
Expand Down

0 comments on commit 6779a01

Please sign in to comment.