forked from httprb/form_data
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for non-file parts with Content-Type
- Loading branch information
Andrei Botalov
committed
Jan 21, 2017
1 parent
1d15f41
commit b3fac90
Showing
6 changed files
with
107 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# frozen_string_literal: true | ||
|
||
module HTTP | ||
module FormData | ||
class Part | ||
attr_reader :mime_type, :filename | ||
|
||
# @param [#to_s] body | ||
# @param [String] :mime_type | ||
# @param [String] :filename | ||
def initialize(body, mime_type: nil, filename: nil) | ||
@body = body.to_s | ||
@mime_type = mime_type | ||
@filename = filename | ||
end | ||
|
||
# Returns content size. | ||
# | ||
# @return [Integer] | ||
def size | ||
@body.bytesize | ||
end | ||
|
||
# Returns content of a file of IO. | ||
# | ||
# @return [String] | ||
def to_s | ||
@body | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe HTTP::FormData::Part do | ||
let(:body) { "" } | ||
let(:opts) { {} } | ||
|
||
describe "#size" do | ||
subject { described_class.new(body, opts).size } | ||
|
||
context "when body given as a String" do | ||
let(:body) { "привет мир!" } | ||
it { is_expected.to eq 20 } | ||
end | ||
end | ||
|
||
describe "#to_s" do | ||
subject { described_class.new(body, opts).to_s } | ||
|
||
context "when body given as String" do | ||
let(:body) { "привет мир!" } | ||
it { is_expected.to eq "привет мир!" } | ||
end | ||
end | ||
|
||
describe "#filename" do | ||
subject { described_class.new(body, opts).filename } | ||
|
||
it { is_expected.to eq nil } | ||
|
||
context "when it was given with options" do | ||
let(:opts) { { :filename => "foobar.txt" } } | ||
it { is_expected.to eq "foobar.txt" } | ||
end | ||
end | ||
|
||
describe "#mime_type" do | ||
subject { described_class.new(body, opts).mime_type } | ||
|
||
it { is_expected.to eq nil } | ||
|
||
context "when it was given with options" do | ||
let(:opts) { { :mime_type => "application/json" } } | ||
it { is_expected.to eq "application/json" } | ||
end | ||
end | ||
end |