http-form_data 1.0.1 → 1.0.2
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/.rubocop.yml +18 -21
- data/.travis.yml +26 -13
- data/CHANGES.md +18 -0
- data/Gemfile +5 -3
- data/Guardfile +2 -0
- data/LICENSE.txt +1 -1
- data/README.md +12 -3
- data/Rakefile +9 -2
- data/appveyor.yml +8 -0
- data/http-form_data.gemspec +5 -4
- data/lib/http/form_data.rb +9 -6
- data/lib/http/form_data/file.rb +18 -10
- data/lib/http/form_data/multipart.rb +4 -4
- data/lib/http/form_data/multipart/param.rb +21 -25
- data/lib/http/form_data/part.rb +38 -0
- data/lib/http/form_data/urlencoded.rb +3 -1
- data/lib/http/form_data/version.rb +3 -1
- data/spec/lib/http/form_data/file_spec.rb +14 -6
- data/spec/lib/http/form_data/multipart_spec.rb +22 -1
- data/spec/lib/http/form_data/part_spec.rb +46 -0
- data/spec/lib/http/form_data/urlencoded_spec.rb +1 -0
- data/spec/lib/http/form_data_spec.rb +2 -0
- data/spec/spec_helper.rb +1 -1
- data/spec/support/fixtures_helper.rb +1 -1
- metadata +8 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f646dc5084304effdd12a91b73151a0ee750db06
|
4
|
+
data.tar.gz: 3514cd9ed1b7c91df984c26dc586d1882ad8196d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3faf8f17dc3f2a25997de4e9014009f706e336ab9f6948ec729a65dd2d6a71d4721e5d56d4e9f53e2f28827219c72ecda5213de18a05b30314e1bc9bf24b22ba
|
7
|
+
data.tar.gz: aa5d4520dfe7a3febf86c6fdcc33db318d7a40089545c86380d65f341c181dcdb2359af5d33e48d45960ba5fc52558550fcfaa7e56435eba474418827b54e546
|
data/.rubocop.yml
CHANGED
@@ -1,3 +1,18 @@
|
|
1
|
+
AllCops:
|
2
|
+
DisplayCopNames: true
|
3
|
+
TargetRubyVersion: 2.4
|
4
|
+
|
5
|
+
## Metrics #####################################################################
|
6
|
+
|
7
|
+
Metrics/BlockLength:
|
8
|
+
Exclude:
|
9
|
+
- "Guardfile"
|
10
|
+
- "spec/**/*"
|
11
|
+
|
12
|
+
Metrics/MethodLength:
|
13
|
+
CountComments: false
|
14
|
+
Max: 15
|
15
|
+
|
1
16
|
## Styles ######################################################################
|
2
17
|
|
3
18
|
Style/AlignParameters:
|
@@ -6,12 +21,6 @@ Style/AlignParameters:
|
|
6
21
|
Style/BracesAroundHashParameters:
|
7
22
|
Enabled: false
|
8
23
|
|
9
|
-
# Broken (2014-12-15). Use `yardstick` gem instead.
|
10
|
-
# See: https://github.com/bbatsov/rubocop/issues/947
|
11
|
-
# TODO: Enable back once cop is fixed.
|
12
|
-
Style/Documentation:
|
13
|
-
Enabled: false
|
14
|
-
|
15
24
|
Style/EmptyLineBetweenDefs:
|
16
25
|
AllowAdjacentOneLineDefs: true
|
17
26
|
|
@@ -31,18 +40,12 @@ Style/Lambda:
|
|
31
40
|
Style/MultilineOperationIndentation:
|
32
41
|
EnforcedStyle: indented
|
33
42
|
|
34
|
-
# A bit useless restriction, that makes impossible aligning code like this:
|
35
|
-
#
|
36
|
-
# redis do |conn|
|
37
|
-
# conn.hset :k1, now
|
38
|
-
# conn.hincrby :k2, 123
|
39
|
-
# end
|
40
|
-
SingleSpaceBeforeFirstArg:
|
41
|
-
Enabled: false
|
42
|
-
|
43
43
|
Style/StringLiterals:
|
44
44
|
EnforcedStyle: double_quotes
|
45
45
|
|
46
|
+
Style/EmptyCaseCondition:
|
47
|
+
Enabled: false
|
48
|
+
|
46
49
|
# Not all trivial readers/writers can be defined with attr_* methods
|
47
50
|
#
|
48
51
|
# class Example < SimpleDelegator
|
@@ -56,9 +59,3 @@ Style/StringLiterals:
|
|
56
59
|
# end
|
57
60
|
Style/TrivialAccessors:
|
58
61
|
Enabled: false
|
59
|
-
|
60
|
-
## Metrics #####################################################################
|
61
|
-
|
62
|
-
Metrics/MethodLength:
|
63
|
-
CountComments: false
|
64
|
-
Max: 15
|
data/.travis.yml
CHANGED
@@ -1,20 +1,33 @@
|
|
1
|
-
bundler_args: --without development doc
|
2
|
-
env:
|
3
|
-
global:
|
4
|
-
- JRUBY_OPTS="$JRUBY_OPTS --debug"
|
5
1
|
language: ruby
|
2
|
+
sudo: false
|
3
|
+
|
4
|
+
before_install:
|
5
|
+
- gem update --system
|
6
|
+
- gem --version
|
7
|
+
- gem install bundler --no-rdoc --no-ri
|
8
|
+
- bundle --version
|
9
|
+
|
10
|
+
install: bundle install --without development doc
|
11
|
+
|
12
|
+
script: bundle exec rake
|
13
|
+
|
14
|
+
env: JRUBY_OPTS="$JRUBY_OPTS --debug"
|
15
|
+
|
6
16
|
rvm:
|
7
|
-
|
17
|
+
# Include JRuby first because it takes the longest
|
18
|
+
- jruby-9.1.8.0
|
8
19
|
- 2.0.0
|
9
20
|
- 2.1
|
10
21
|
- 2.2
|
11
|
-
-
|
12
|
-
-
|
13
|
-
|
14
|
-
- ruby-head
|
22
|
+
- 2.3.4
|
23
|
+
- 2.4.1
|
24
|
+
|
15
25
|
matrix:
|
16
|
-
allow_failures:
|
17
|
-
- rvm: jruby-head
|
18
|
-
- rvm: ruby-head
|
19
26
|
fast_finish: true
|
20
|
-
|
27
|
+
include:
|
28
|
+
- rvm: 2.4.1
|
29
|
+
env: SUITE="rubocop"
|
30
|
+
|
31
|
+
branches:
|
32
|
+
only:
|
33
|
+
- master
|
data/CHANGES.md
CHANGED
@@ -1,3 +1,18 @@
|
|
1
|
+
## 1.0.2 (2017-05-08)
|
2
|
+
|
3
|
+
* [#5](https://github.com/httprb/form_data.rb/issues/5)
|
4
|
+
Allow setting Content-Type non-file parts
|
5
|
+
[@abotalov]
|
6
|
+
|
7
|
+
* [#6](https://github.com/httprb/form_data.rb/issues/6)
|
8
|
+
Creation of file parts without filename
|
9
|
+
[@abotalov]
|
10
|
+
|
11
|
+
* [#11](https://github.com/httprb/form_data.rb/pull/11)
|
12
|
+
Deprecate `HTTP::FormData::File#mime_type`. Use `#content_type` instead.
|
13
|
+
[@ixti]
|
14
|
+
|
15
|
+
|
1
16
|
## 1.0.1 (2015-03-31)
|
2
17
|
|
3
18
|
* Fix usage of URI module.
|
@@ -18,3 +33,6 @@
|
|
18
33
|
## 0.0.1 (2014-12-15)
|
19
34
|
|
20
35
|
* First release ever!
|
36
|
+
|
37
|
+
[@ixti] https://github.com/ixti
|
38
|
+
[@abotalov] https://github.com/abotalov
|
data/Gemfile
CHANGED
@@ -1,23 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
source "https://rubygems.org"
|
2
4
|
|
3
5
|
gem "rake"
|
4
6
|
|
5
7
|
group :development do
|
6
|
-
gem "pry"
|
7
8
|
gem "guard"
|
8
9
|
gem "guard-rspec", :require => false
|
10
|
+
gem "pry"
|
9
11
|
end
|
10
12
|
|
11
13
|
group :test do
|
12
14
|
gem "coveralls"
|
13
15
|
gem "rspec", "~> 3.1"
|
16
|
+
gem "rubocop", "= 0.48.1"
|
14
17
|
gem "simplecov", ">= 0.9"
|
15
|
-
gem "rubocop", "~> 0.28.0"
|
16
18
|
end
|
17
19
|
|
18
20
|
group :doc do
|
19
|
-
gem "yard"
|
20
21
|
gem "redcarpet"
|
22
|
+
gem "yard"
|
21
23
|
end
|
22
24
|
|
23
25
|
# Specify your gem's dependencies in form_data.gemspec
|
data/Guardfile
CHANGED
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -44,16 +44,25 @@ socket << "\r\n"
|
|
44
44
|
socket << form.to_s
|
45
45
|
```
|
46
46
|
|
47
|
+
It's also possible to create a non-file part with Content-Type:
|
48
|
+
|
49
|
+
``` ruby
|
50
|
+
form = HTTP::FormData.create({
|
51
|
+
:username => HTTP::FormData::Part.new('{"a": 1}', content_type: 'application/json'),
|
52
|
+
:avatar_file => HTTP::FormData::File.new("/home/ixti/avatar.png")
|
53
|
+
})
|
54
|
+
```
|
47
55
|
|
48
56
|
## Supported Ruby Versions
|
49
57
|
|
50
58
|
This library aims to support and is [tested against][ci] the following Ruby
|
51
59
|
versions:
|
52
60
|
|
53
|
-
* Ruby 1.9.3
|
54
|
-
* Ruby 2.0.0
|
55
61
|
* Ruby 2.1.x
|
56
62
|
* Ruby 2.2.x
|
63
|
+
* Ruby 2.3.x
|
64
|
+
* Ruby 2.4.x
|
65
|
+
* JRuby 9.1.x.x
|
57
66
|
|
58
67
|
If something doesn't work on one of these versions, it's a bug.
|
59
68
|
|
@@ -80,7 +89,7 @@ dropped.
|
|
80
89
|
|
81
90
|
## Copyright
|
82
91
|
|
83
|
-
Copyright (c) 2015
|
92
|
+
Copyright (c) 2015-2017 Alexey V Zapparov.
|
84
93
|
See [LICENSE.txt][license] for further details.
|
85
94
|
|
86
95
|
|
data/Rakefile
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "bundler/gem_tasks"
|
4
4
|
|
@@ -14,4 +14,11 @@ rescue LoadError
|
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
|
-
|
17
|
+
if ENV["CI"].nil?
|
18
|
+
task :default => %i[spec rubocop]
|
19
|
+
else
|
20
|
+
case ENV["SUITE"]
|
21
|
+
when "rubocop" then task :default => :rubocop
|
22
|
+
else task :default => :spec
|
23
|
+
end
|
24
|
+
end
|
data/appveyor.yml
ADDED
data/http-form_data.gemspec
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
2
3
|
lib = File.expand_path("../lib", __FILE__)
|
3
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
5
|
require "http/form_data/version"
|
@@ -11,15 +12,15 @@ Gem::Specification.new do |spec|
|
|
11
12
|
spec.email = ["ixti@member.fsf.org"]
|
12
13
|
spec.license = "MIT"
|
13
14
|
spec.summary = "http-form_data-#{HTTP::FormData::VERSION}"
|
14
|
-
spec.description = <<-DESC.gsub(/^\s+> /m, "").
|
15
|
+
spec.description = <<-DESC.gsub(/^\s+> /m, "").tr("\n", " ").strip
|
15
16
|
> Utility-belt to build form data request bodies.
|
16
17
|
> Provides support for `application/x-www-form-urlencoded` and
|
17
18
|
> `multipart/form-data` types.
|
18
19
|
DESC
|
19
20
|
|
20
21
|
spec.files = `git ls-files -z`.split("\x0")
|
21
|
-
spec.executables = spec.files.grep(
|
22
|
-
spec.test_files = spec.files.grep(
|
22
|
+
spec.executables = spec.files.grep(%r{^bin\/}).map { |f| File.basename(f) }
|
23
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)\/})
|
23
24
|
spec.require_paths = ["lib"]
|
24
25
|
|
25
26
|
spec.add_development_dependency "bundler", "~> 1.7"
|
data/lib/http/form_data.rb
CHANGED
@@ -1,10 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "http/form_data/part"
|
1
4
|
require "http/form_data/file"
|
2
5
|
require "http/form_data/multipart"
|
3
6
|
require "http/form_data/urlencoded"
|
4
7
|
require "http/form_data/version"
|
5
8
|
|
6
|
-
# http
|
7
|
-
# @see https://github.com/httprb/http
|
9
|
+
# http gem namespace.
|
10
|
+
# @see https://github.com/httprb/http
|
8
11
|
module HTTP
|
9
12
|
# Utility-belt to build form data request bodies.
|
10
13
|
# Provides support for `application/x-www-form-urlencoded` and
|
@@ -26,7 +29,7 @@ module HTTP
|
|
26
29
|
# socket << form.to_s
|
27
30
|
module FormData
|
28
31
|
# CRLF
|
29
|
-
CRLF = "\r\n"
|
32
|
+
CRLF = "\r\n"
|
30
33
|
|
31
34
|
# Generic FormData error.
|
32
35
|
class Error < StandardError; end
|
@@ -55,7 +58,7 @@ module HTTP
|
|
55
58
|
when obj.nil? then {}
|
56
59
|
when obj.is_a?(Hash) then obj
|
57
60
|
when obj.respond_to?(:to_h) then obj.to_h
|
58
|
-
else
|
61
|
+
else raise Error, "#{obj.inspect} is neither Hash nor responds to :to_h"
|
59
62
|
end
|
60
63
|
end
|
61
64
|
|
@@ -67,8 +70,8 @@ module HTTP
|
|
67
70
|
# @return [Boolean]
|
68
71
|
def multipart?(data)
|
69
72
|
data.any? do |_, v|
|
70
|
-
next true if v.is_a? FormData::
|
71
|
-
v.respond_to?(:to_ary) && v.to_ary.any? { |e| e.is_a? FormData::
|
73
|
+
next true if v.is_a? FormData::Part
|
74
|
+
v.respond_to?(:to_ary) && v.to_ary.any? { |e| e.is_a? FormData::Part }
|
72
75
|
end
|
73
76
|
end
|
74
77
|
end
|
data/lib/http/form_data/file.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module HTTP
|
2
4
|
module FormData
|
3
5
|
# Represents file form param.
|
@@ -16,27 +18,33 @@ module HTTP
|
|
16
18
|
# @example Usage with pathname
|
17
19
|
#
|
18
20
|
# FormData::File.new "/home/ixti/avatar.png"
|
19
|
-
class File
|
21
|
+
class File < Part
|
20
22
|
# Default MIME type
|
21
|
-
DEFAULT_MIME = "application/octet-stream"
|
23
|
+
DEFAULT_MIME = "application/octet-stream"
|
22
24
|
|
23
|
-
|
25
|
+
# @deprecated Use #content_type instead
|
26
|
+
alias mime_type content_type
|
24
27
|
|
25
28
|
# @see DEFAULT_MIME
|
26
29
|
# @param [String, StringIO, File] file_or_io Filename or IO instance.
|
27
30
|
# @param [#to_h] opts
|
28
|
-
# @option opts [#to_s] :
|
31
|
+
# @option opts [#to_s] :content_type (DEFAULT_MIME)
|
32
|
+
# Value of Content-Type header
|
29
33
|
# @option opts [#to_s] :filename
|
30
34
|
# When `file` is a String, defaults to basename of `file`.
|
31
35
|
# When `file` is a File, defaults to basename of `file`.
|
32
36
|
# When `file` is a StringIO, defaults to `"stream-{object_id}"`
|
33
37
|
def initialize(file_or_io, opts = {})
|
34
|
-
|
38
|
+
opts = FormData.ensure_hash(opts)
|
35
39
|
|
36
|
-
opts
|
40
|
+
if opts.key? :mime_type
|
41
|
+
warn "[DEPRECATED] :mime_type option deprecated, use :content_type"
|
42
|
+
opts[:content_type] = opts[:mime_type]
|
43
|
+
end
|
37
44
|
|
38
|
-
@
|
39
|
-
@
|
45
|
+
@file_or_io = file_or_io
|
46
|
+
@content_type = opts.fetch(:content_type, DEFAULT_MIME).to_s
|
47
|
+
@filename = opts.fetch :filename do
|
40
48
|
case file_or_io
|
41
49
|
when String then ::File.basename file_or_io
|
42
50
|
when ::File then ::File.basename file_or_io.path
|
@@ -47,7 +55,7 @@ module HTTP
|
|
47
55
|
|
48
56
|
# Returns content size.
|
49
57
|
#
|
50
|
-
# @return [
|
58
|
+
# @return [Integer]
|
51
59
|
def size
|
52
60
|
with_io(&:size)
|
53
61
|
end
|
@@ -67,7 +75,7 @@ module HTTP
|
|
67
75
|
if @file_or_io.is_a?(::File) || @file_or_io.is_a?(StringIO)
|
68
76
|
yield @file_or_io
|
69
77
|
else
|
70
|
-
::File.open(@file_or_io) { |io| yield io }
|
78
|
+
::File.open(@file_or_io, "rb") { |io| yield io }
|
71
79
|
end
|
72
80
|
end
|
73
81
|
end
|
@@ -1,7 +1,7 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
2
3
|
require "securerandom"
|
3
4
|
|
4
|
-
# internal
|
5
5
|
require "http/form_data/multipart/param"
|
6
6
|
|
7
7
|
module HTTP
|
@@ -11,7 +11,7 @@ module HTTP
|
|
11
11
|
# @param [#to_h, Hash] data form data key-value Hash
|
12
12
|
def initialize(data)
|
13
13
|
@parts = Param.coerce FormData.ensure_hash data
|
14
|
-
@boundary = ("-"
|
14
|
+
@boundary = (Array.new(21, "-") << SecureRandom.hex(21)).join("")
|
15
15
|
@content_length = nil
|
16
16
|
end
|
17
17
|
|
@@ -32,7 +32,7 @@ module HTTP
|
|
32
32
|
# Returns form data content size to be used for HTTP request
|
33
33
|
# `Content-Length` header.
|
34
34
|
#
|
35
|
-
# @return [
|
35
|
+
# @return [Integer]
|
36
36
|
def content_length
|
37
37
|
unless @content_length
|
38
38
|
@content_length = head.bytesize + tail.bytesize
|
@@ -1,20 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module HTTP
|
2
4
|
module FormData
|
3
5
|
class Multipart
|
4
6
|
# Utility class to represent multi-part chunks
|
5
7
|
class Param
|
6
8
|
# @param [#to_s] name
|
7
|
-
# @param [FormData::File, #to_s] value
|
9
|
+
# @param [FormData::File, FormData::Part, #to_s] value
|
8
10
|
def initialize(name, value)
|
9
|
-
@name
|
11
|
+
@name = name.to_s
|
12
|
+
|
13
|
+
@part =
|
14
|
+
if value.is_a?(FormData::Part)
|
15
|
+
value
|
16
|
+
else
|
17
|
+
FormData::Part.new(value)
|
18
|
+
end
|
10
19
|
|
11
|
-
|
20
|
+
parameters = { :name => @name }
|
21
|
+
parameters[:filename] = @part.filename if @part.filename
|
22
|
+
parameters = parameters.map { |k, v| "#{k}=#{v.inspect}" }.join("; ")
|
12
23
|
|
13
|
-
|
24
|
+
@header = "Content-Disposition: form-data; #{parameters}"
|
14
25
|
|
15
|
-
|
16
|
-
|
17
|
-
@header
|
26
|
+
return unless @part.content_type
|
27
|
+
|
28
|
+
@header += "#{CRLF}Content-Type: #{@part.content_type}"
|
18
29
|
end
|
19
30
|
|
20
31
|
# Returns body part with headers and data.
|
@@ -34,20 +45,14 @@ module HTTP
|
|
34
45
|
#
|
35
46
|
# @return [String]
|
36
47
|
def to_s
|
37
|
-
"#{@header}#{CRLF * 2}#{@
|
48
|
+
"#{@header}#{CRLF * 2}#{@part}"
|
38
49
|
end
|
39
50
|
|
40
51
|
# Calculates size of a part (headers + body).
|
41
52
|
#
|
42
|
-
# @return [
|
53
|
+
# @return [Integer]
|
43
54
|
def size
|
44
|
-
|
45
|
-
|
46
|
-
if file?
|
47
|
-
size + @value.size
|
48
|
-
else
|
49
|
-
size + @value.to_s.bytesize
|
50
|
-
end
|
55
|
+
@header.bytesize + (CRLF.bytesize * 2) + @part.size
|
51
56
|
end
|
52
57
|
|
53
58
|
# Flattens given `data` Hash into an array of `Param`'s.
|
@@ -67,15 +72,6 @@ module HTTP
|
|
67
72
|
|
68
73
|
params
|
69
74
|
end
|
70
|
-
|
71
|
-
private
|
72
|
-
|
73
|
-
# Tells whenever value is a {FormData::File} or not.
|
74
|
-
#
|
75
|
-
# @return [Boolean]
|
76
|
-
def file?
|
77
|
-
@value.is_a? FormData::File
|
78
|
-
end
|
79
75
|
end
|
80
76
|
end
|
81
77
|
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module HTTP
|
4
|
+
module FormData
|
5
|
+
# Represents a body part of multipart/form-data request.
|
6
|
+
#
|
7
|
+
# @example Usage with String
|
8
|
+
#
|
9
|
+
# body = "Message"
|
10
|
+
# FormData::Part.new body, :content_type => 'foobar.txt; charset="UTF-8"'
|
11
|
+
class Part
|
12
|
+
attr_reader :content_type, :filename
|
13
|
+
|
14
|
+
# @param [#to_s] body
|
15
|
+
# @param [String] content_type Value of Content-Type header
|
16
|
+
# @param [String] filename Value of filename parameter
|
17
|
+
def initialize(body, content_type: nil, filename: nil)
|
18
|
+
@body = body.to_s
|
19
|
+
@content_type = content_type
|
20
|
+
@filename = filename
|
21
|
+
end
|
22
|
+
|
23
|
+
# Returns content size.
|
24
|
+
#
|
25
|
+
# @return [Integer]
|
26
|
+
def size
|
27
|
+
@body.bytesize
|
28
|
+
end
|
29
|
+
|
30
|
+
# Returns content of a file of IO.
|
31
|
+
#
|
32
|
+
# @return [String]
|
33
|
+
def to_s
|
34
|
+
@body
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "uri"
|
2
4
|
|
3
5
|
module HTTP
|
@@ -26,7 +28,7 @@ module HTTP
|
|
26
28
|
# Returns form data content size to be used for HTTP request
|
27
29
|
# `Content-Length` header.
|
28
30
|
#
|
29
|
-
# @return [
|
31
|
+
# @return [Integer]
|
30
32
|
def content_length
|
31
33
|
to_s.bytesize
|
32
34
|
end
|
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
# coding: utf-8
|
2
3
|
|
3
4
|
RSpec.describe HTTP::FormData::File do
|
@@ -28,7 +29,7 @@ RSpec.describe HTTP::FormData::File do
|
|
28
29
|
|
29
30
|
context "when file given as a String" do
|
30
31
|
let(:file) { fixture("the-http-gem.info").to_s }
|
31
|
-
it { is_expected.to eq fixture("the-http-gem.info").read }
|
32
|
+
it { is_expected.to eq fixture("the-http-gem.info").read(:mode => "rb") }
|
32
33
|
end
|
33
34
|
|
34
35
|
context "when file given as StringIO" do
|
@@ -37,9 +38,9 @@ RSpec.describe HTTP::FormData::File do
|
|
37
38
|
end
|
38
39
|
|
39
40
|
context "when file given as File" do
|
40
|
-
let(:file) { fixture("the-http-gem.info").open }
|
41
|
+
let(:file) { fixture("the-http-gem.info").open("rb") }
|
41
42
|
after { file.close }
|
42
|
-
it { is_expected.to eq fixture("the-http-gem.info").read }
|
43
|
+
it { is_expected.to eq fixture("the-http-gem.info").read(:mode => "rb") }
|
43
44
|
end
|
44
45
|
end
|
45
46
|
|
@@ -81,14 +82,21 @@ RSpec.describe HTTP::FormData::File do
|
|
81
82
|
end
|
82
83
|
end
|
83
84
|
|
84
|
-
describe "#
|
85
|
-
subject { described_class.new(StringIO.new, opts).
|
85
|
+
describe "#content_type" do
|
86
|
+
subject { described_class.new(StringIO.new, opts).content_type }
|
86
87
|
|
87
88
|
it { is_expected.to eq "application/octet-stream" }
|
88
89
|
|
89
90
|
context "when it was given with options" do
|
90
|
-
let(:opts) { { :
|
91
|
+
let(:opts) { { :content_type => "application/json" } }
|
91
92
|
it { is_expected.to eq "application/json" }
|
92
93
|
end
|
93
94
|
end
|
95
|
+
|
96
|
+
describe "#mime_type" do
|
97
|
+
it "should be an alias of #content_type" do
|
98
|
+
expect(described_class.instance_method(:mime_type))
|
99
|
+
.to eq(described_class.instance_method(:content_type))
|
100
|
+
end
|
101
|
+
end
|
94
102
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
RSpec.describe HTTP::FormData::Multipart do
|
2
4
|
let(:file) { HTTP::FormData::File.new fixture "the-http-gem.info" }
|
3
5
|
let(:params) { { :foo => :bar, :baz => file } }
|
@@ -6,7 +8,10 @@ RSpec.describe HTTP::FormData::Multipart do
|
|
6
8
|
|
7
9
|
describe "#content_type" do
|
8
10
|
subject { form_data.content_type }
|
9
|
-
|
11
|
+
|
12
|
+
let(:content_type) { %r{^multipart\/form-data; boundary=#{boundary}$} }
|
13
|
+
|
14
|
+
it { is_expected.to match(content_type) }
|
10
15
|
end
|
11
16
|
|
12
17
|
describe "#content_length" do
|
@@ -36,5 +41,21 @@ RSpec.describe HTTP::FormData::Multipart do
|
|
36
41
|
"--#{boundary_value}--"
|
37
42
|
].join("")
|
38
43
|
end
|
44
|
+
|
45
|
+
context "with filename set to nil" do
|
46
|
+
let(:part) { HTTP::FormData::Part.new("s", :filename => nil) }
|
47
|
+
let(:form_data) { HTTP::FormData::Multipart.new(:foo => part) }
|
48
|
+
|
49
|
+
it "doesn't include a filename" do
|
50
|
+
boundary_value = form_data.content_type[/(#{boundary})$/, 1]
|
51
|
+
|
52
|
+
expect(form_data.to_s).to eq [
|
53
|
+
"--#{boundary_value}#{crlf}",
|
54
|
+
"#{disposition 'name' => 'foo'}#{crlf}",
|
55
|
+
"#{crlf}s#{crlf}",
|
56
|
+
"--#{boundary_value}--"
|
57
|
+
].join("")
|
58
|
+
end
|
59
|
+
end
|
39
60
|
end
|
40
61
|
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe HTTP::FormData::Part do
|
4
|
+
let(:body) { "" }
|
5
|
+
let(:opts) { {} }
|
6
|
+
|
7
|
+
describe "#size" do
|
8
|
+
subject { described_class.new(body, opts).size }
|
9
|
+
|
10
|
+
context "when body given as a String" do
|
11
|
+
let(:body) { "привет мир!" }
|
12
|
+
it { is_expected.to eq 20 }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "#to_s" do
|
17
|
+
subject { described_class.new(body, opts).to_s }
|
18
|
+
|
19
|
+
context "when body given as String" do
|
20
|
+
let(:body) { "привет мир!" }
|
21
|
+
it { is_expected.to eq "привет мир!" }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "#filename" do
|
26
|
+
subject { described_class.new(body, opts).filename }
|
27
|
+
|
28
|
+
it { is_expected.to eq nil }
|
29
|
+
|
30
|
+
context "when it was given with options" do
|
31
|
+
let(:opts) { { :filename => "foobar.txt" } }
|
32
|
+
it { is_expected.to eq "foobar.txt" }
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "#content_type" do
|
37
|
+
subject { described_class.new(body, opts).content_type }
|
38
|
+
|
39
|
+
it { is_expected.to eq nil }
|
40
|
+
|
41
|
+
context "when it was given with options" do
|
42
|
+
let(:opts) { { :content_type => "application/json" } }
|
43
|
+
it { is_expected.to eq "application/json" }
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: http-form_data
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aleksey V Zapparov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-05-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -43,17 +43,20 @@ files:
|
|
43
43
|
- LICENSE.txt
|
44
44
|
- README.md
|
45
45
|
- Rakefile
|
46
|
+
- appveyor.yml
|
46
47
|
- http-form_data.gemspec
|
47
48
|
- lib/http/form_data.rb
|
48
49
|
- lib/http/form_data/file.rb
|
49
50
|
- lib/http/form_data/multipart.rb
|
50
51
|
- lib/http/form_data/multipart/param.rb
|
52
|
+
- lib/http/form_data/part.rb
|
51
53
|
- lib/http/form_data/urlencoded.rb
|
52
54
|
- lib/http/form_data/version.rb
|
53
55
|
- spec/fixtures/expected-multipart-body.tpl
|
54
56
|
- spec/fixtures/the-http-gem.info
|
55
57
|
- spec/lib/http/form_data/file_spec.rb
|
56
58
|
- spec/lib/http/form_data/multipart_spec.rb
|
59
|
+
- spec/lib/http/form_data/part_spec.rb
|
57
60
|
- spec/lib/http/form_data/urlencoded_spec.rb
|
58
61
|
- spec/lib/http/form_data_spec.rb
|
59
62
|
- spec/spec_helper.rb
|
@@ -78,17 +81,17 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
78
81
|
version: '0'
|
79
82
|
requirements: []
|
80
83
|
rubyforge_project:
|
81
|
-
rubygems_version: 2.
|
84
|
+
rubygems_version: 2.6.11
|
82
85
|
signing_key:
|
83
86
|
specification_version: 4
|
84
|
-
summary: http-form_data-1.0.
|
87
|
+
summary: http-form_data-1.0.2
|
85
88
|
test_files:
|
86
89
|
- spec/fixtures/expected-multipart-body.tpl
|
87
90
|
- spec/fixtures/the-http-gem.info
|
88
91
|
- spec/lib/http/form_data/file_spec.rb
|
89
92
|
- spec/lib/http/form_data/multipart_spec.rb
|
93
|
+
- spec/lib/http/form_data/part_spec.rb
|
90
94
|
- spec/lib/http/form_data/urlencoded_spec.rb
|
91
95
|
- spec/lib/http/form_data_spec.rb
|
92
96
|
- spec/spec_helper.rb
|
93
97
|
- spec/support/fixtures_helper.rb
|
94
|
-
has_rdoc:
|