Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewind content at the end of Readable#to_s #21

Merged
merged 1 commit into from
Feb 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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