http 2.0.2 → 2.0.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cd857e2ad6dc7552b81641ec83c3a60e99087fae
4
- data.tar.gz: b31ec2d4637bc449f29ff01f6df6568672c04cd4
3
+ metadata.gz: 4daefc1ab609a814049d1dbb15c8b4dd1dcbc4af
4
+ data.tar.gz: 8e997cb150a1b40c3f93713a8c0a8e0c3c347e37
5
5
  SHA512:
6
- metadata.gz: 6efddbaf1df00256de1e58fc512357165e343a421a7d67f5b7d0103f241ea0eb77d9a19c33377624d954b68f5b7d016e0d15ffbb5eab0c8acb49d827b3f7f7b4
7
- data.tar.gz: 2a0dec9c76defd8f85d57da8eb4ef46251519bd49be93c57955995e6e91a1b55122ad3d86cd96ba4130e3380871be8e6c329bc0503a5fbf021c8bbca9bee7b03
6
+ metadata.gz: 14aa2d7853ca5d3b0c4a60f3d85ea79ce29f942cae40d60c83cfa9de10845806b521cece91aabff8954f2bfd33ccd78577f39676133d5dee45f7055b0083e599
7
+ data.tar.gz: 9473c747c14dc40eb7ee6a9822bf45b674a8892c8cc92db6cfa9d88fa9bc3d6f03429e72609d6e902fdaed836c38e80fc6ba0d5d58c53a2a688a98fb365deaf1
data/CHANGES.md CHANGED
@@ -1,3 +1,15 @@
1
+ ## 2.0.3 (2016-08-03)
2
+
3
+ * [#365](https://github.com/httprb/http/issues/365)
4
+ Add `HTTP::Response#content_length`
5
+ ([@janko-m])
6
+
7
+ * [#335](https://github.com/httprb/http/issues/335),
8
+ [#360](https://github.com/httprb/http/pull/360)
9
+ Set `Content-Length: 0` header for `nil` bodies.
10
+ ([@britishtea])
11
+
12
+
1
13
  ## 2.0.2 (2016-06-24)
2
14
 
3
15
  * [#353](https://github.com/httprb/http/pull/353)
@@ -537,3 +549,5 @@ end
537
549
  [@mwitek]: https://github.com/mwitek
538
550
  [@tonyta]: https://github.com/tonyta
539
551
  [@jhbabon]: https://github.com/jhbabon
552
+ [@britishtea]: https://github.com/britishtea
553
+ [@janko-m]: https://github.com/janko-m
@@ -23,3 +23,4 @@ In any case, specify following info in description of your issue:
23
23
  - What actually happened
24
24
  - The exception backtrace(s), if any
25
25
  - Version of gem or commit ref you are using
26
+ - Version of ruby you are using
data/Gemfile CHANGED
@@ -23,6 +23,7 @@ group :test do
23
23
  gem "rspec-its"
24
24
  gem "yardstick"
25
25
  gem "certificate_authority", :require => false
26
+ gem "activemodel", "~> 4", :require => false # Used by certificate_authority
26
27
  end
27
28
 
28
29
  group :doc do
@@ -53,6 +53,8 @@ module HTTP
53
53
  def add_body_type_headers
54
54
  if @body.is_a?(String) && !@headers[Headers::CONTENT_LENGTH]
55
55
  @request_header << "#{Headers::CONTENT_LENGTH}: #{@body.bytesize}"
56
+ elsif @body.nil? && !@headers[Headers::CONTENT_LENGTH]
57
+ @request_header << "#{Headers::CONTENT_LENGTH}: 0"
56
58
  elsif @body.is_a?(Enumerable) && CHUNKED != @headers[Headers::TRANSFER_ENCODING]
57
59
  raise(RequestError, "invalid transfer encoding")
58
60
  end
@@ -86,6 +86,22 @@ module HTTP
86
86
  self
87
87
  end
88
88
 
89
+ # Value of the Content-Length header.
90
+ #
91
+ # @return [nil] if Content-Length was not given, or it's value was invalid
92
+ # (not an integer, e.g. empty string or string with non-digits).
93
+ # @return [Integer] otherwise
94
+ def content_length
95
+ value = @headers[Headers::CONTENT_LENGTH]
96
+ return unless value
97
+
98
+ begin
99
+ Integer(value)
100
+ rescue ArgumentError
101
+ nil
102
+ end
103
+ end
104
+
89
105
  # Parsed Content-Type header
90
106
  #
91
107
  # @return [HTTP::ContentType]
@@ -113,7 +129,7 @@ module HTTP
113
129
  #
114
130
  # @param [#to_s] as Parse as given MIME type
115
131
  # instead of the one determined from headers
116
- # @raise [Error] if adapter not found
132
+ # @raise [HTTP::Error] if adapter not found
117
133
  # @return [Object]
118
134
  def parse(as = nil)
119
135
  MimeType[as || mime_type].decode to_s
@@ -26,7 +26,7 @@ module HTTP
26
26
 
27
27
  # Parse the given URI string, returning an HTTP::URI object
28
28
  #
29
- # @param [HTTP::URI, String, #to_str] :uri to parse
29
+ # @param [HTTP::URI, String, #to_str] uri to parse
30
30
  #
31
31
  # @return [HTTP::URI] new URI instance
32
32
  def self.parse(uri)
@@ -37,8 +37,8 @@ module HTTP
37
37
 
38
38
  # Encodes key/value pairs as application/x-www-form-urlencoded
39
39
  #
40
- # @param [#to_hash, #to_ary] :form_values to encode
41
- # @param [TrueClass, FalseClass] :sort should key/value pairs be sorted first?
40
+ # @param [#to_hash, #to_ary] form_values to encode
41
+ # @param [TrueClass, FalseClass] sort should key/value pairs be sorted first?
42
42
  #
43
43
  # @return [String] encoded value
44
44
  def self.form_encode(form_values, sort = false)
@@ -47,14 +47,16 @@ module HTTP
47
47
 
48
48
  # Creates an HTTP::URI instance from the given options
49
49
  #
50
- # @option [String, #to_str] :scheme URI scheme
51
- # @option [String, #to_str] :user for basic authentication
52
- # @option [String, #to_str] :password for basic authentication
53
- # @option [String, #to_str] :host name component
54
- # @option [String, #to_str] :port network port to connect to
55
- # @option [String, #to_str] :path component to request
56
- # @option [String, #to_str] :query component distinct from path
57
- # @option [String, #to_str] :fragment component at the end of the URI
50
+ # @param [Hash, Addressable::URI] options_or_uri
51
+ #
52
+ # @option options_or_uri [String, #to_str] :scheme URI scheme
53
+ # @option options_or_uri [String, #to_str] :user for basic authentication
54
+ # @option options_or_uri [String, #to_str] :password for basic authentication
55
+ # @option options_or_uri [String, #to_str] :host name component
56
+ # @option options_or_uri [String, #to_str] :port network port to connect to
57
+ # @option options_or_uri [String, #to_str] :path component to request
58
+ # @option options_or_uri [String, #to_str] :query component distinct from path
59
+ # @option options_or_uri [String, #to_str] :fragment component at the end of the URI
58
60
  #
59
61
  # @return [HTTP::URI] new URI instance
60
62
  def initialize(options_or_uri = {})
@@ -63,13 +65,14 @@ module HTTP
63
65
  @uri = Addressable::URI.new(options_or_uri)
64
66
  when Addressable::URI
65
67
  @uri = options_or_uri
66
- else raise TypeError, "expected Hash for options, got #{options_or_uri.class}"
68
+ else
69
+ raise TypeError, "expected Hash for options, got #{options_or_uri.class}"
67
70
  end
68
71
  end
69
72
 
70
73
  # Are these URI objects equal? Normalizes both URIs prior to comparison
71
74
  #
72
- # @param [Object] :other URI to compare this one with
75
+ # @param [Object] other URI to compare this one with
73
76
  #
74
77
  # @return [TrueClass, FalseClass] are the URIs equivalent (after normalization)?
75
78
  def ==(other)
@@ -78,7 +81,7 @@ module HTTP
78
81
 
79
82
  # Are these URI objects equal? Does NOT normalizes both URIs prior to comparison
80
83
  #
81
- # @param [Object] :other URI to compare this one with
84
+ # @param [Object] other URI to compare this one with
82
85
  #
83
86
  # @return [TrueClass, FalseClass] are the URIs equivalent?
84
87
  def eql?(other)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HTTP
4
- VERSION = "2.0.2".freeze
4
+ VERSION = "2.0.3".freeze
5
5
  end
@@ -69,6 +69,24 @@ RSpec.describe HTTP::Request::Writer do
69
69
  end
70
70
  end
71
71
 
72
+ context "when body is nil" do
73
+ let(:body) { nil }
74
+
75
+ it "properly sets Content-Length header if needed" do
76
+ writer.stream
77
+ expect(io.string).to start_with "#{headerstart}\r\nContent-Length: 0\r\n\r\n"
78
+ end
79
+
80
+ context "when Content-Length explicitly set" do
81
+ let(:headers) { HTTP::Headers.coerce "Content-Length" => 12 }
82
+
83
+ it "keeps given value" do
84
+ writer.stream
85
+ expect(io.string).to start_with "#{headerstart}\r\nContent-Length: 12\r\n\r\n"
86
+ end
87
+ end
88
+ end
89
+
72
90
  context "when body is a unicode String" do
73
91
  let(:body) { "Привет, мир!" }
74
92
 
@@ -28,6 +28,24 @@ RSpec.describe HTTP::Response do
28
28
  end
29
29
  end
30
30
 
31
+ describe "#content_length" do
32
+ subject { response.content_length }
33
+
34
+ context "without Content-Length header" do
35
+ it { is_expected.to be_nil }
36
+ end
37
+
38
+ context "with Content-Length: 5" do
39
+ let(:headers) { {"Content-Length" => "5"} }
40
+ it { is_expected.to eq 5 }
41
+ end
42
+
43
+ context "with invalid Content-Length" do
44
+ let(:headers) { {"Content-Length" => "foo"} }
45
+ it { is_expected.to be_nil }
46
+ end
47
+ end
48
+
31
49
  describe "mime_type" do
32
50
  subject { response.mime_type }
33
51
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2
4
+ version: 2.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tony Arcieri
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2016-06-24 00:00:00.000000000 Z
14
+ date: 2016-08-03 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: http_parser.rb