http-form_data 2.3.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +161 -0
- data/LICENSE.txt +1 -1
- data/README.md +11 -10
- data/http-form_data.gemspec +28 -14
- data/lib/http/form_data/composite_io.rb +46 -28
- data/lib/http/form_data/file.rb +44 -19
- data/lib/http/form_data/multipart/param.rb +18 -48
- data/lib/http/form_data/multipart.rb +59 -12
- data/lib/http/form_data/part.rb +24 -2
- data/lib/http/form_data/readable.rb +24 -6
- data/lib/http/form_data/urlencoded.rb +100 -10
- data/lib/http/form_data/version.rb +1 -1
- data/lib/http/form_data.rb +38 -18
- data/sig/http/form_data/composite_io.rbs +32 -0
- data/sig/http/form_data/file.rbs +23 -0
- data/sig/http/form_data/multipart/param.rbs +23 -0
- data/sig/http/form_data/multipart.rbs +40 -0
- data/sig/http/form_data/part.rbs +16 -0
- data/sig/http/form_data/readable.rbs +19 -0
- data/sig/http/form_data/urlencoded.rbs +46 -0
- data/sig/http/form_data/version.rbs +5 -0
- data/sig/http/form_data.rbs +30 -0
- metadata +24 -43
- data/.editorconfig +0 -9
- data/.gitignore +0 -15
- data/.rspec +0 -2
- data/.rubocop.yml +0 -64
- data/.travis.yml +0 -34
- data/.yardopts +0 -2
- data/CHANGES.md +0 -96
- data/Gemfile +0 -26
- data/Guardfile +0 -16
- data/Rakefile +0 -24
- data/appveyor.yml +0 -8
- data/spec/fixtures/expected-multipart-body.tpl +0 -0
- data/spec/fixtures/the-http-gem.info +0 -1
- data/spec/lib/http/form_data/composite_io_spec.rb +0 -109
- data/spec/lib/http/form_data/file_spec.rb +0 -217
- data/spec/lib/http/form_data/multipart_spec.rb +0 -157
- data/spec/lib/http/form_data/part_spec.rb +0 -74
- data/spec/lib/http/form_data/urlencoded_spec.rb +0 -78
- data/spec/lib/http/form_data_spec.rb +0 -50
- data/spec/spec_helper.rb +0 -83
- data/spec/support/fixtures_helper.rb +0 -13
|
@@ -1,217 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
# coding: utf-8
|
|
3
|
-
|
|
4
|
-
RSpec.describe HTTP::FormData::File do
|
|
5
|
-
let(:opts) { nil }
|
|
6
|
-
let(:form_file) { described_class.new(file, opts) }
|
|
7
|
-
|
|
8
|
-
describe "#size" do
|
|
9
|
-
subject { form_file.size }
|
|
10
|
-
|
|
11
|
-
context "when file given as a String" do
|
|
12
|
-
let(:file) { fixture("the-http-gem.info").to_s }
|
|
13
|
-
it { is_expected.to eq fixture("the-http-gem.info").size }
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
context "when file given as a Pathname" do
|
|
17
|
-
let(:file) { fixture("the-http-gem.info") }
|
|
18
|
-
it { is_expected.to eq fixture("the-http-gem.info").size }
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
context "when file given as File" do
|
|
22
|
-
let(:file) { fixture("the-http-gem.info").open }
|
|
23
|
-
after { file.close }
|
|
24
|
-
it { is_expected.to eq fixture("the-http-gem.info").size }
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
context "when file given as IO" do
|
|
28
|
-
let(:file) { StringIO.new "привет мир!" }
|
|
29
|
-
it { is_expected.to eq 20 }
|
|
30
|
-
end
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
describe "#to_s" do
|
|
34
|
-
subject { form_file.to_s }
|
|
35
|
-
|
|
36
|
-
context "when file given as a String" do
|
|
37
|
-
let(:file) { fixture("the-http-gem.info").to_s }
|
|
38
|
-
it { is_expected.to eq fixture("the-http-gem.info").read(:mode => "rb") }
|
|
39
|
-
|
|
40
|
-
it "rewinds content" do
|
|
41
|
-
content = form_file.read
|
|
42
|
-
expect(form_file.to_s).to eq content
|
|
43
|
-
expect(form_file.read).to eq content
|
|
44
|
-
end
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
context "when file given as a Pathname" do
|
|
48
|
-
let(:file) { fixture("the-http-gem.info") }
|
|
49
|
-
it { is_expected.to eq fixture("the-http-gem.info").read(:mode => "rb") }
|
|
50
|
-
|
|
51
|
-
it "rewinds content" do
|
|
52
|
-
content = form_file.read
|
|
53
|
-
expect(form_file.to_s).to eq content
|
|
54
|
-
expect(form_file.read).to eq content
|
|
55
|
-
end
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
context "when file given as File" do
|
|
59
|
-
let(:file) { fixture("the-http-gem.info").open("rb") }
|
|
60
|
-
after { file.close }
|
|
61
|
-
it { is_expected.to eq fixture("the-http-gem.info").read(:mode => "rb") }
|
|
62
|
-
|
|
63
|
-
it "rewinds content" do
|
|
64
|
-
content = form_file.read
|
|
65
|
-
expect(form_file.to_s).to eq content
|
|
66
|
-
expect(form_file.read).to eq content
|
|
67
|
-
end
|
|
68
|
-
end
|
|
69
|
-
|
|
70
|
-
context "when file given as IO" do
|
|
71
|
-
let(:file) { StringIO.new "привет мир!" }
|
|
72
|
-
it { is_expected.to eq "привет мир!" }
|
|
73
|
-
|
|
74
|
-
it "rewinds content" do
|
|
75
|
-
content = form_file.read
|
|
76
|
-
expect(form_file.to_s).to eq content
|
|
77
|
-
expect(form_file.read).to eq content
|
|
78
|
-
end
|
|
79
|
-
end
|
|
80
|
-
end
|
|
81
|
-
|
|
82
|
-
describe "#read" do
|
|
83
|
-
subject { form_file.read }
|
|
84
|
-
|
|
85
|
-
context "when file given as a String" do
|
|
86
|
-
let(:file) { fixture("the-http-gem.info").to_s }
|
|
87
|
-
it { is_expected.to eq fixture("the-http-gem.info").read(:mode => "rb") }
|
|
88
|
-
end
|
|
89
|
-
|
|
90
|
-
context "when file given as a Pathname" do
|
|
91
|
-
let(:file) { fixture("the-http-gem.info") }
|
|
92
|
-
it { is_expected.to eq fixture("the-http-gem.info").read(:mode => "rb") }
|
|
93
|
-
end
|
|
94
|
-
|
|
95
|
-
context "when file given as File" do
|
|
96
|
-
let(:file) { fixture("the-http-gem.info").open("rb") }
|
|
97
|
-
after { file.close }
|
|
98
|
-
it { is_expected.to eq fixture("the-http-gem.info").read(:mode => "rb") }
|
|
99
|
-
end
|
|
100
|
-
|
|
101
|
-
context "when file given as IO" do
|
|
102
|
-
let(:file) { StringIO.new "привет мир!" }
|
|
103
|
-
it { is_expected.to eq "привет мир!" }
|
|
104
|
-
end
|
|
105
|
-
end
|
|
106
|
-
|
|
107
|
-
describe "#rewind" do
|
|
108
|
-
context "when file given as a String" do
|
|
109
|
-
let(:file) { fixture("the-http-gem.info").to_s }
|
|
110
|
-
|
|
111
|
-
it "rewinds the underlying IO object" do
|
|
112
|
-
content = form_file.read
|
|
113
|
-
form_file.rewind
|
|
114
|
-
expect(form_file.read).to eq content
|
|
115
|
-
end
|
|
116
|
-
end
|
|
117
|
-
|
|
118
|
-
context "when file given as a Pathname" do
|
|
119
|
-
let(:file) { fixture("the-http-gem.info") }
|
|
120
|
-
|
|
121
|
-
it "rewinds the underlying IO object" do
|
|
122
|
-
content = form_file.read
|
|
123
|
-
form_file.rewind
|
|
124
|
-
expect(form_file.read).to eq content
|
|
125
|
-
end
|
|
126
|
-
end
|
|
127
|
-
|
|
128
|
-
context "when file given as File" do
|
|
129
|
-
let(:file) { fixture("the-http-gem.info").open("rb") }
|
|
130
|
-
after { file.close }
|
|
131
|
-
|
|
132
|
-
it "rewinds the underlying IO object" do
|
|
133
|
-
content = form_file.read
|
|
134
|
-
form_file.rewind
|
|
135
|
-
expect(form_file.read).to eq content
|
|
136
|
-
end
|
|
137
|
-
end
|
|
138
|
-
|
|
139
|
-
context "when file given as IO" do
|
|
140
|
-
let(:file) { StringIO.new "привет мир!" }
|
|
141
|
-
|
|
142
|
-
it "rewinds the underlying IO object" do
|
|
143
|
-
content = form_file.read
|
|
144
|
-
form_file.rewind
|
|
145
|
-
expect(form_file.read).to eq content
|
|
146
|
-
end
|
|
147
|
-
end
|
|
148
|
-
end
|
|
149
|
-
|
|
150
|
-
describe "#filename" do
|
|
151
|
-
subject { form_file.filename }
|
|
152
|
-
|
|
153
|
-
context "when file given as a String" do
|
|
154
|
-
let(:file) { fixture("the-http-gem.info").to_s }
|
|
155
|
-
|
|
156
|
-
it { is_expected.to eq ::File.basename file }
|
|
157
|
-
|
|
158
|
-
context "and filename given with options" do
|
|
159
|
-
let(:opts) { { :filename => "foobar.txt" } }
|
|
160
|
-
it { is_expected.to eq "foobar.txt" }
|
|
161
|
-
end
|
|
162
|
-
end
|
|
163
|
-
|
|
164
|
-
context "when file given as a Pathname" do
|
|
165
|
-
let(:file) { fixture("the-http-gem.info") }
|
|
166
|
-
|
|
167
|
-
it { is_expected.to eq ::File.basename file }
|
|
168
|
-
|
|
169
|
-
context "and filename given with options" do
|
|
170
|
-
let(:opts) { { :filename => "foobar.txt" } }
|
|
171
|
-
it { is_expected.to eq "foobar.txt" }
|
|
172
|
-
end
|
|
173
|
-
end
|
|
174
|
-
|
|
175
|
-
context "when file given as File" do
|
|
176
|
-
let(:file) { fixture("the-http-gem.info").open }
|
|
177
|
-
after { file.close }
|
|
178
|
-
|
|
179
|
-
it { is_expected.to eq "the-http-gem.info" }
|
|
180
|
-
|
|
181
|
-
context "and filename given with options" do
|
|
182
|
-
let(:opts) { { :filename => "foobar.txt" } }
|
|
183
|
-
it { is_expected.to eq "foobar.txt" }
|
|
184
|
-
end
|
|
185
|
-
end
|
|
186
|
-
|
|
187
|
-
context "when file given as IO" do
|
|
188
|
-
let(:file) { StringIO.new }
|
|
189
|
-
|
|
190
|
-
it { is_expected.to eq "stream-#{file.object_id}" }
|
|
191
|
-
|
|
192
|
-
context "and filename given with options" do
|
|
193
|
-
let(:opts) { { :filename => "foobar.txt" } }
|
|
194
|
-
it { is_expected.to eq "foobar.txt" }
|
|
195
|
-
end
|
|
196
|
-
end
|
|
197
|
-
end
|
|
198
|
-
|
|
199
|
-
describe "#content_type" do
|
|
200
|
-
let(:file) { StringIO.new }
|
|
201
|
-
subject { form_file.content_type }
|
|
202
|
-
|
|
203
|
-
it { is_expected.to eq "application/octet-stream" }
|
|
204
|
-
|
|
205
|
-
context "when it was given with options" do
|
|
206
|
-
let(:opts) { { :content_type => "application/json" } }
|
|
207
|
-
it { is_expected.to eq "application/json" }
|
|
208
|
-
end
|
|
209
|
-
end
|
|
210
|
-
|
|
211
|
-
describe "#mime_type" do
|
|
212
|
-
it "should be an alias of #content_type" do
|
|
213
|
-
expect(described_class.instance_method(:mime_type))
|
|
214
|
-
.to eq(described_class.instance_method(:content_type))
|
|
215
|
-
end
|
|
216
|
-
end
|
|
217
|
-
end
|
|
@@ -1,157 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
RSpec.describe HTTP::FormData::Multipart do
|
|
4
|
-
subject(:form_data) { HTTP::FormData::Multipart.new params }
|
|
5
|
-
|
|
6
|
-
let(:file) { HTTP::FormData::File.new fixture "the-http-gem.info" }
|
|
7
|
-
let(:params) { { :foo => :bar, :baz => file } }
|
|
8
|
-
let(:boundary) { /-{21}[a-f0-9]{42}/ }
|
|
9
|
-
|
|
10
|
-
describe "#to_s" do
|
|
11
|
-
def disposition(params)
|
|
12
|
-
params = params.map { |k, v| "#{k}=#{v.inspect}" }.join("; ")
|
|
13
|
-
"Content-Disposition: form-data; #{params}"
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
let(:crlf) { "\r\n" }
|
|
17
|
-
|
|
18
|
-
it "properly generates multipart data" do
|
|
19
|
-
boundary_value = form_data.boundary
|
|
20
|
-
|
|
21
|
-
expect(form_data.to_s).to eq [
|
|
22
|
-
"--#{boundary_value}#{crlf}",
|
|
23
|
-
"#{disposition 'name' => 'foo'}#{crlf}",
|
|
24
|
-
"#{crlf}bar#{crlf}",
|
|
25
|
-
"--#{boundary_value}#{crlf}",
|
|
26
|
-
"#{disposition 'name' => 'baz', 'filename' => file.filename}#{crlf}",
|
|
27
|
-
"Content-Type: #{file.content_type}#{crlf}",
|
|
28
|
-
"#{crlf}#{file}#{crlf}",
|
|
29
|
-
"--#{boundary_value}--#{crlf}"
|
|
30
|
-
].join("")
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
it "rewinds content" do
|
|
34
|
-
content = form_data.read
|
|
35
|
-
expect(form_data.to_s).to eq content
|
|
36
|
-
expect(form_data.read).to eq content
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
context "with user-defined boundary" do
|
|
40
|
-
subject(:form_data) do
|
|
41
|
-
HTTP::FormData::Multipart.new params, :boundary => "my-boundary"
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
it "uses the given boundary" do
|
|
45
|
-
expect(form_data.to_s).to eq [
|
|
46
|
-
"--my-boundary#{crlf}",
|
|
47
|
-
"#{disposition 'name' => 'foo'}#{crlf}",
|
|
48
|
-
"#{crlf}bar#{crlf}",
|
|
49
|
-
"--my-boundary#{crlf}",
|
|
50
|
-
"#{disposition 'name' => 'baz', 'filename' => file.filename}#{crlf}",
|
|
51
|
-
"Content-Type: #{file.content_type}#{crlf}",
|
|
52
|
-
"#{crlf}#{file}#{crlf}",
|
|
53
|
-
"--my-boundary--#{crlf}"
|
|
54
|
-
].join("")
|
|
55
|
-
end
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
context "with filename set to nil" do
|
|
59
|
-
let(:part) { HTTP::FormData::Part.new("s", :content_type => "mime/type") }
|
|
60
|
-
let(:form_data) { HTTP::FormData::Multipart.new({ :foo => part }) }
|
|
61
|
-
|
|
62
|
-
it "doesn't include a filename" do
|
|
63
|
-
boundary_value = form_data.content_type[/(#{boundary})$/, 1]
|
|
64
|
-
|
|
65
|
-
expect(form_data.to_s).to eq [
|
|
66
|
-
"--#{boundary_value}#{crlf}",
|
|
67
|
-
"#{disposition 'name' => 'foo'}#{crlf}",
|
|
68
|
-
"Content-Type: #{part.content_type}#{crlf}",
|
|
69
|
-
"#{crlf}s#{crlf}",
|
|
70
|
-
"--#{boundary_value}--#{crlf}"
|
|
71
|
-
].join("")
|
|
72
|
-
end
|
|
73
|
-
end
|
|
74
|
-
|
|
75
|
-
context "with content type set to nil" do
|
|
76
|
-
let(:part) { HTTP::FormData::Part.new("s") }
|
|
77
|
-
let(:form_data) { HTTP::FormData::Multipart.new({ :foo => part }) }
|
|
78
|
-
|
|
79
|
-
it "doesn't include a filename" do
|
|
80
|
-
boundary_value = form_data.content_type[/(#{boundary})$/, 1]
|
|
81
|
-
|
|
82
|
-
expect(form_data.to_s).to eq [
|
|
83
|
-
"--#{boundary_value}#{crlf}",
|
|
84
|
-
"#{disposition 'name' => 'foo'}#{crlf}",
|
|
85
|
-
"#{crlf}s#{crlf}",
|
|
86
|
-
"--#{boundary_value}--#{crlf}"
|
|
87
|
-
].join("")
|
|
88
|
-
end
|
|
89
|
-
end
|
|
90
|
-
end
|
|
91
|
-
|
|
92
|
-
describe "#size" do
|
|
93
|
-
it "returns bytesize of multipart data" do
|
|
94
|
-
expect(form_data.size).to eq form_data.to_s.bytesize
|
|
95
|
-
end
|
|
96
|
-
end
|
|
97
|
-
|
|
98
|
-
describe "#read" do
|
|
99
|
-
it "returns multipart data" do
|
|
100
|
-
expect(form_data.read).to eq form_data.to_s
|
|
101
|
-
end
|
|
102
|
-
end
|
|
103
|
-
|
|
104
|
-
describe "#rewind" do
|
|
105
|
-
it "rewinds the multipart data IO" do
|
|
106
|
-
form_data.read
|
|
107
|
-
form_data.rewind
|
|
108
|
-
expect(form_data.read).to eq form_data.to_s
|
|
109
|
-
end
|
|
110
|
-
end
|
|
111
|
-
|
|
112
|
-
describe "#content_type" do
|
|
113
|
-
subject { form_data.content_type }
|
|
114
|
-
|
|
115
|
-
let(:content_type) { %r{^multipart\/form-data; boundary=#{boundary}$} }
|
|
116
|
-
|
|
117
|
-
it { is_expected.to match(content_type) }
|
|
118
|
-
|
|
119
|
-
context "with user-defined boundary" do
|
|
120
|
-
let(:form_data) do
|
|
121
|
-
HTTP::FormData::Multipart.new params, :boundary => "my-boundary"
|
|
122
|
-
end
|
|
123
|
-
|
|
124
|
-
it "includes the given boundary" do
|
|
125
|
-
expect(form_data.content_type)
|
|
126
|
-
.to eq "multipart/form-data; boundary=my-boundary"
|
|
127
|
-
end
|
|
128
|
-
end
|
|
129
|
-
end
|
|
130
|
-
|
|
131
|
-
describe "#content_length" do
|
|
132
|
-
subject { form_data.content_length }
|
|
133
|
-
it { is_expected.to eq form_data.to_s.bytesize }
|
|
134
|
-
end
|
|
135
|
-
|
|
136
|
-
describe "#boundary" do
|
|
137
|
-
it "returns a new boundary" do
|
|
138
|
-
expect(form_data.boundary).to match(boundary)
|
|
139
|
-
end
|
|
140
|
-
|
|
141
|
-
context "with user-defined boundary" do
|
|
142
|
-
let(:form_data) do
|
|
143
|
-
HTTP::FormData::Multipart.new params, :boundary => "my-boundary"
|
|
144
|
-
end
|
|
145
|
-
|
|
146
|
-
it "returns the given boundary" do
|
|
147
|
-
expect(form_data.boundary).to eq "my-boundary"
|
|
148
|
-
end
|
|
149
|
-
end
|
|
150
|
-
end
|
|
151
|
-
|
|
152
|
-
describe ".generate_boundary" do
|
|
153
|
-
it "returns a string suitable as a multipart boundary" do
|
|
154
|
-
expect(form_data.class.generate_boundary).to match(boundary)
|
|
155
|
-
end
|
|
156
|
-
end
|
|
157
|
-
end
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
RSpec.describe HTTP::FormData::Part do
|
|
4
|
-
let(:body) { "" }
|
|
5
|
-
let(:opts) { {} }
|
|
6
|
-
subject(:part) { HTTP::FormData::Part.new(body, **opts) }
|
|
7
|
-
|
|
8
|
-
describe "#size" do
|
|
9
|
-
subject { part.size }
|
|
10
|
-
|
|
11
|
-
context "when body given as a String" do
|
|
12
|
-
let(:body) { "привет мир!" }
|
|
13
|
-
it { is_expected.to eq 20 }
|
|
14
|
-
end
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
describe "#to_s" do
|
|
18
|
-
subject! { part.to_s }
|
|
19
|
-
|
|
20
|
-
context "when body given as String" do
|
|
21
|
-
let(:body) { "привет мир!" }
|
|
22
|
-
it { is_expected.to eq "привет мир!" }
|
|
23
|
-
|
|
24
|
-
it "rewinds content" do
|
|
25
|
-
content = part.read
|
|
26
|
-
expect(part.to_s).to eq content
|
|
27
|
-
expect(part.read).to eq content
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
describe "#read" do
|
|
33
|
-
subject { part.read }
|
|
34
|
-
|
|
35
|
-
context "when body given as String" do
|
|
36
|
-
let(:body) { "привет мир!" }
|
|
37
|
-
it { is_expected.to eq "привет мир!" }
|
|
38
|
-
end
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
describe "#rewind" do
|
|
42
|
-
context "when body given as String" do
|
|
43
|
-
let(:body) { "привет мир!" }
|
|
44
|
-
|
|
45
|
-
it "rewinds the underlying IO object" do
|
|
46
|
-
part.read
|
|
47
|
-
part.rewind
|
|
48
|
-
expect(part.read).to eq "привет мир!"
|
|
49
|
-
end
|
|
50
|
-
end
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
describe "#filename" do
|
|
54
|
-
subject { part.filename }
|
|
55
|
-
|
|
56
|
-
it { is_expected.to eq nil }
|
|
57
|
-
|
|
58
|
-
context "when it was given with options" do
|
|
59
|
-
let(:opts) { { :filename => "foobar.txt" } }
|
|
60
|
-
it { is_expected.to eq "foobar.txt" }
|
|
61
|
-
end
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
describe "#content_type" do
|
|
65
|
-
subject { part.content_type }
|
|
66
|
-
|
|
67
|
-
it { is_expected.to eq nil }
|
|
68
|
-
|
|
69
|
-
context "when it was given with options" do
|
|
70
|
-
let(:opts) { { :content_type => "application/json" } }
|
|
71
|
-
it { is_expected.to eq "application/json" }
|
|
72
|
-
end
|
|
73
|
-
end
|
|
74
|
-
end
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
# coding: utf-8
|
|
3
|
-
|
|
4
|
-
RSpec.describe HTTP::FormData::Urlencoded do
|
|
5
|
-
let(:data) { { "foo[bar]" => "test" } }
|
|
6
|
-
subject(:form_data) { HTTP::FormData::Urlencoded.new data }
|
|
7
|
-
|
|
8
|
-
describe "#content_type" do
|
|
9
|
-
subject { form_data.content_type }
|
|
10
|
-
it { is_expected.to eq "application/x-www-form-urlencoded" }
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
describe "#content_length" do
|
|
14
|
-
subject { form_data.content_length }
|
|
15
|
-
it { is_expected.to eq form_data.to_s.bytesize }
|
|
16
|
-
|
|
17
|
-
context "with unicode chars" do
|
|
18
|
-
let(:data) { { "foo[bar]" => "тест" } }
|
|
19
|
-
it { is_expected.to eq form_data.to_s.bytesize }
|
|
20
|
-
end
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
describe "#to_s" do
|
|
24
|
-
subject { form_data.to_s }
|
|
25
|
-
it { is_expected.to eq "foo%5Bbar%5D=test" }
|
|
26
|
-
|
|
27
|
-
context "with unicode chars" do
|
|
28
|
-
let(:data) { { "foo[bar]" => "тест" } }
|
|
29
|
-
it { is_expected.to eq "foo%5Bbar%5D=%D1%82%D0%B5%D1%81%D1%82" }
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
it "rewinds content" do
|
|
33
|
-
content = form_data.read
|
|
34
|
-
expect(form_data.to_s).to eq content
|
|
35
|
-
expect(form_data.read).to eq content
|
|
36
|
-
end
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
describe "#size" do
|
|
40
|
-
it "returns bytesize of multipart data" do
|
|
41
|
-
expect(form_data.size).to eq form_data.to_s.bytesize
|
|
42
|
-
end
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
describe "#read" do
|
|
46
|
-
it "returns multipart data" do
|
|
47
|
-
expect(form_data.read).to eq form_data.to_s
|
|
48
|
-
end
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
describe "#rewind" do
|
|
52
|
-
it "rewinds the multipart data IO" do
|
|
53
|
-
form_data.read
|
|
54
|
-
form_data.rewind
|
|
55
|
-
expect(form_data.read).to eq form_data.to_s
|
|
56
|
-
end
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
describe ".encoder=" do
|
|
60
|
-
before { described_class.encoder = ::JSON.method(:dump) }
|
|
61
|
-
after { described_class.encoder = ::URI.method(:encode_www_form) }
|
|
62
|
-
|
|
63
|
-
it "switches form encoder implementation" do
|
|
64
|
-
expect(form_data.to_s).to eq('{"foo[bar]":"test"}')
|
|
65
|
-
end
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
context "with custom instance level encoder" do
|
|
69
|
-
let(:encoder) { proc { |data| ::JSON.dump(data) } }
|
|
70
|
-
subject(:form_data) do
|
|
71
|
-
HTTP::FormData::Urlencoded.new(data, :encoder => encoder)
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
it "uses encoder passed to initializer" do
|
|
75
|
-
expect(form_data.to_s).to eq('{"foo[bar]":"test"}')
|
|
76
|
-
end
|
|
77
|
-
end
|
|
78
|
-
end
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
RSpec.describe HTTP::FormData do
|
|
4
|
-
describe ".create" do
|
|
5
|
-
subject { HTTP::FormData.create params }
|
|
6
|
-
|
|
7
|
-
context "when form has no files" do
|
|
8
|
-
let(:params) { { :foo => :bar } }
|
|
9
|
-
it { is_expected.to be_a HTTP::FormData::Urlencoded }
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
context "when form has at least one file param" do
|
|
13
|
-
let(:file) { HTTP::FormData::File.new(fixture("the-http-gem.info").to_s) }
|
|
14
|
-
let(:params) { { :foo => :bar, :baz => file } }
|
|
15
|
-
it { is_expected.to be_a HTTP::FormData::Multipart }
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
context "when form has file in an array param" do
|
|
19
|
-
let(:file) { HTTP::FormData::File.new(fixture("the-http-gem.info").to_s) }
|
|
20
|
-
let(:params) { { :foo => :bar, :baz => [file] } }
|
|
21
|
-
it { is_expected.to be_a HTTP::FormData::Multipart }
|
|
22
|
-
end
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
describe ".ensure_hash" do
|
|
26
|
-
subject(:ensure_hash) { HTTP::FormData.ensure_hash data }
|
|
27
|
-
|
|
28
|
-
context "when Hash given" do
|
|
29
|
-
let(:data) { { :foo => :bar } }
|
|
30
|
-
it { is_expected.to eq :foo => :bar }
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
context "when #to_h given" do
|
|
34
|
-
let(:data) { double(:to_h => { :foo => :bar }) }
|
|
35
|
-
it { is_expected.to eq :foo => :bar }
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
context "when nil given" do
|
|
39
|
-
let(:data) { nil }
|
|
40
|
-
it { is_expected.to eq({}) }
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
context "when neither Hash nor #to_h given" do
|
|
44
|
-
let(:data) { double }
|
|
45
|
-
it "fails with HTTP::FormData::Error" do
|
|
46
|
-
expect { ensure_hash }.to raise_error HTTP::FormData::Error
|
|
47
|
-
end
|
|
48
|
-
end
|
|
49
|
-
end
|
|
50
|
-
end
|
data/spec/spec_helper.rb
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "simplecov"
|
|
4
|
-
require "coveralls"
|
|
5
|
-
|
|
6
|
-
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([
|
|
7
|
-
SimpleCov::Formatter::HTMLFormatter,
|
|
8
|
-
Coveralls::SimpleCov::Formatter
|
|
9
|
-
])
|
|
10
|
-
|
|
11
|
-
SimpleCov.start { add_filter "/spec/" }
|
|
12
|
-
|
|
13
|
-
require "http/form_data"
|
|
14
|
-
require "support/fixtures_helper"
|
|
15
|
-
|
|
16
|
-
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
|
17
|
-
RSpec.configure do |config|
|
|
18
|
-
config.expect_with :rspec do |expectations|
|
|
19
|
-
# This option will default to `true` in RSpec 4. It makes the `description`
|
|
20
|
-
# and `failure_message` of custom matchers include text for helper methods
|
|
21
|
-
# defined using `chain`, e.g.:
|
|
22
|
-
# be_bigger_than(2).and_smaller_than(4).description
|
|
23
|
-
# # => "be bigger than 2 and smaller than 4"
|
|
24
|
-
# ...rather than:
|
|
25
|
-
# # => "be bigger than 2"
|
|
26
|
-
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
config.mock_with :rspec do |mocks|
|
|
30
|
-
# Prevents you from mocking or stubbing a method that does not exist on
|
|
31
|
-
# a real object. This is generally recommended, and will default to
|
|
32
|
-
# `true` in RSpec 4.
|
|
33
|
-
mocks.verify_partial_doubles = true
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
# These two settings work together to allow you to limit a spec run
|
|
37
|
-
# to individual examples or groups you care about by tagging them with
|
|
38
|
-
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
|
39
|
-
# get run.
|
|
40
|
-
config.filter_run :focus
|
|
41
|
-
config.run_all_when_everything_filtered = true
|
|
42
|
-
|
|
43
|
-
# Limits the available syntax to the non-monkey patched syntax that is
|
|
44
|
-
# recommended. For more details, see:
|
|
45
|
-
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
|
46
|
-
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
|
47
|
-
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
|
48
|
-
config.disable_monkey_patching!
|
|
49
|
-
|
|
50
|
-
# This setting enables warnings. It's recommended, but in some cases may
|
|
51
|
-
# be too noisy due to issues in dependencies.
|
|
52
|
-
config.warnings = true
|
|
53
|
-
|
|
54
|
-
# Many RSpec users commonly either run the entire suite or an individual
|
|
55
|
-
# file, and it's useful to allow more verbose output when running an
|
|
56
|
-
# individual spec file.
|
|
57
|
-
if config.files_to_run.one?
|
|
58
|
-
# Use the documentation formatter for detailed output,
|
|
59
|
-
# unless a formatter has already been configured
|
|
60
|
-
# (e.g. via a command-line flag).
|
|
61
|
-
config.default_formatter = "doc"
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
# Print the 10 slowest examples and example groups at the
|
|
65
|
-
# end of the spec run, to help surface which specs are running
|
|
66
|
-
# particularly slow.
|
|
67
|
-
config.profile_examples = 10
|
|
68
|
-
|
|
69
|
-
# Run specs in random order to surface order dependencies. If you find an
|
|
70
|
-
# order dependency and want to debug it, you can fix the order by providing
|
|
71
|
-
# the seed, which is printed after each run.
|
|
72
|
-
# --seed 1234
|
|
73
|
-
config.order = :random
|
|
74
|
-
|
|
75
|
-
# Seed global randomization in this process using the `--seed` CLI option.
|
|
76
|
-
# Setting this allows you to use `--seed` to deterministically reproduce
|
|
77
|
-
# test failures related to randomization by passing the same `--seed` value
|
|
78
|
-
# as the one that triggered the failure.
|
|
79
|
-
Kernel.srand config.seed
|
|
80
|
-
|
|
81
|
-
# Include common helpers
|
|
82
|
-
config.include FixturesHelper
|
|
83
|
-
end
|