net-http 0.3.1 → 0.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 76efd58d8b7892a8c7cd99b94ba163b3e7af2c7c213710d09d4f0ff5751fb01c
4
- data.tar.gz: 69b629454de4153eac43b49c1e622889a69a90b8b574be7a91af0668240c7b72
3
+ metadata.gz: a746ed42ea9e1e34ae2ae72383484b19ef4537fcf60a01cd92d17604573bd3b4
4
+ data.tar.gz: 4372f3a8601b83819d8f202af095bfbd2f4e3337e808c6e3488fba37e5b3c477
5
5
  SHA512:
6
- metadata.gz: bf1fe9f92f33a2b2c32ae4ca1c896076ab22df06551b7e669a21c616a2c89d36942a18a3520d3ecb3fd5db387481ac44abbd7c70dc8b39bce4987a681819ce1f
7
- data.tar.gz: 143b8dc3a5f8788c552e17ac45ee20ed0313e16bccb16de94d9a22d96b69a23e24d264c2978388b2b01e7f43d84834d027c65c901e3ed6d8fd668763eb422375
6
+ metadata.gz: 22d42a16ac4309c1098bedf5bece45574cb19aec58b447defebb8f448c09a9398057086e508d8f88ee05fd91e8cf87d39126d973ec83a565fd3fb96e6f1fd7a2
7
+ data.tar.gz: ffd03e1e3db77772c38a31f00a10102c07ca1814c3572ceaec664a0bc03d7c8bac8a8bb5953e6603bf5f0b1d054bce6a565ee8a19810059a78e7731f26807f2d
data/Gemfile CHANGED
@@ -4,4 +4,5 @@ gemspec
4
4
 
5
5
  gem "rake"
6
6
  gem "test-unit"
7
+ gem "test-unit-ruby-core"
7
8
  gem "webrick"
data/Rakefile CHANGED
@@ -7,11 +7,4 @@ Rake::TestTask.new(:test) do |t|
7
7
  t.test_files = FileList["test/**/test_*.rb"]
8
8
  end
9
9
 
10
- task :sync_tool do
11
- require 'fileutils'
12
- FileUtils.cp "../ruby/tool/lib/core_assertions.rb", "./test/lib"
13
- FileUtils.cp "../ruby/tool/lib/envutil.rb", "./test/lib"
14
- FileUtils.cp "../ruby/tool/lib/find_executable.rb", "./test/lib"
15
- end
16
-
17
10
  task :default => :test
@@ -10,9 +10,10 @@ Many code examples here use these example websites:
10
10
 
11
11
  Some examples also assume these variables:
12
12
 
13
- uri = URI('https://jsonplaceholder.typicode.com')
13
+ uri = URI('https://jsonplaceholder.typicode.com/')
14
14
  uri.freeze # Examples may not modify.
15
15
  hostname = uri.hostname # => "jsonplaceholder.typicode.com"
16
+ path = uri.path # => "/"
16
17
  port = uri.port # => 443
17
18
 
18
19
  So that example requests may be written as:
@@ -0,0 +1,3 @@
1
+ This class also includes (indirectly) module Net::HTTPHeader,
2
+ which gives access to its
3
+ {methods for getting headers}[rdoc-ref:Net::HTTPHeader@Getters].
@@ -1,4 +1,4 @@
1
- # frozen_string_literal: false
1
+ # frozen_string_literal: true
2
2
  # for backward compatibility
3
3
 
4
4
  # :enddoc:
@@ -1,4 +1,4 @@
1
- # frozen_string_literal: false
1
+ # frozen_string_literal: true
2
2
  module Net
3
3
  # Net::HTTP exception class.
4
4
  # You cannot use Net::HTTPExceptions directly; instead, you must use
@@ -1,14 +1,18 @@
1
- # frozen_string_literal: false
2
- # HTTPGenericRequest is the parent of the Net::HTTPRequest class.
3
- # Do not use this directly; use a subclass of Net::HTTPRequest.
1
+ # frozen_string_literal: true
4
2
  #
5
- # Mixes in the Net::HTTPHeader module to provide easier access to HTTP headers.
3
+ # \HTTPGenericRequest is the parent of the Net::HTTPRequest class.
4
+ #
5
+ # Do not use this directly; instead, use a subclass of Net::HTTPRequest.
6
+ #
7
+ # == About the Examples
8
+ #
9
+ # :include: doc/net-http/examples.rdoc
6
10
  #
7
11
  class Net::HTTPGenericRequest
8
12
 
9
13
  include Net::HTTPHeader
10
14
 
11
- def initialize(m, reqbody, resbody, uri_or_path, initheader = nil)
15
+ def initialize(m, reqbody, resbody, uri_or_path, initheader = nil) # :nodoc:
12
16
  @method = m
13
17
  @request_has_body = reqbody
14
18
  @response_has_body = resbody
@@ -19,7 +23,7 @@ class Net::HTTPGenericRequest
19
23
  raise ArgumentError, "no host component for URI" unless (hostname && hostname.length > 0)
20
24
  @uri = uri_or_path.dup
21
25
  host = @uri.hostname.dup
22
- host << ":".freeze << @uri.port.to_s if @uri.port != @uri.default_port
26
+ host << ":" << @uri.port.to_s if @uri.port != @uri.default_port
23
27
  @path = uri_or_path.request_uri
24
28
  raise ArgumentError, "no HTTP request path given" unless @path
25
29
  else
@@ -53,15 +57,47 @@ class Net::HTTPGenericRequest
53
57
  @body_data = nil
54
58
  end
55
59
 
60
+ # Returns the string method name for the request:
61
+ #
62
+ # Net::HTTP::Get.new(uri).method # => "GET"
63
+ # Net::HTTP::Post.new(uri).method # => "POST"
64
+ #
56
65
  attr_reader :method
66
+
67
+ # Returns the string path for the request:
68
+ #
69
+ # Net::HTTP::Get.new(uri).path # => "/"
70
+ # Net::HTTP::Post.new('example.com').path # => "example.com"
71
+ #
57
72
  attr_reader :path
73
+
74
+ # Returns the URI object for the request, or +nil+ if none:
75
+ #
76
+ # Net::HTTP::Get.new(uri).uri
77
+ # # => #<URI::HTTPS https://jsonplaceholder.typicode.com/>
78
+ # Net::HTTP::Get.new('example.com').uri # => nil
79
+ #
58
80
  attr_reader :uri
59
81
 
60
- # Automatically set to false if the user sets the Accept-Encoding header.
61
- # This indicates they wish to handle Content-encoding in responses
62
- # themselves.
82
+ # Returns +false+ if the request's header <tt>'Accept-Encoding'</tt>
83
+ # has been set manually or deleted
84
+ # (indicating that the user intends to handle encoding in the response),
85
+ # +true+ otherwise:
86
+ #
87
+ # req = Net::HTTP::Get.new(uri) # => #<Net::HTTP::Get GET>
88
+ # req['Accept-Encoding'] # => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3"
89
+ # req.decode_content # => true
90
+ # req['Accept-Encoding'] = 'foo'
91
+ # req.decode_content # => false
92
+ # req.delete('Accept-Encoding')
93
+ # req.decode_content # => false
94
+ #
63
95
  attr_reader :decode_content
64
96
 
97
+ # Returns a string representation of the request:
98
+ #
99
+ # Net::HTTP::Post.new(uri).inspect # => "#<Net::HTTP::Post POST>"
100
+ #
65
101
  def inspect
66
102
  "\#<#{self.class} #{@method}>"
67
103
  end
@@ -76,21 +112,45 @@ class Net::HTTPGenericRequest
76
112
  super key, val
77
113
  end
78
114
 
115
+ # Returns whether the request may have a body:
116
+ #
117
+ # Net::HTTP::Post.new(uri).request_body_permitted? # => true
118
+ # Net::HTTP::Get.new(uri).request_body_permitted? # => false
119
+ #
79
120
  def request_body_permitted?
80
121
  @request_has_body
81
122
  end
82
123
 
124
+ # Returns whether the response may have a body:
125
+ #
126
+ # Net::HTTP::Post.new(uri).response_body_permitted? # => true
127
+ # Net::HTTP::Head.new(uri).response_body_permitted? # => false
128
+ #
83
129
  def response_body_permitted?
84
130
  @response_has_body
85
131
  end
86
132
 
87
- def body_exist?
133
+ def body_exist? # :nodoc:
88
134
  warn "Net::HTTPRequest#body_exist? is obsolete; use response_body_permitted?", uplevel: 1 if $VERBOSE
89
135
  response_body_permitted?
90
136
  end
91
137
 
138
+ # Returns the string body for the request, or +nil+ if there is none:
139
+ #
140
+ # req = Net::HTTP::Post.new(uri)
141
+ # req.body # => nil
142
+ # req.body = '{"title": "foo","body": "bar","userId": 1}'
143
+ # req.body # => "{\"title\": \"foo\",\"body\": \"bar\",\"userId\": 1}"
144
+ #
92
145
  attr_reader :body
93
146
 
147
+ # Sets the body for the request:
148
+ #
149
+ # req = Net::HTTP::Post.new(uri)
150
+ # req.body # => nil
151
+ # req.body = '{"title": "foo","body": "bar","userId": 1}'
152
+ # req.body # => "{\"title\": \"foo\",\"body\": \"bar\",\"userId\": 1}"
153
+ #
94
154
  def body=(str)
95
155
  @body = str
96
156
  @body_stream = nil
@@ -98,8 +158,24 @@ class Net::HTTPGenericRequest
98
158
  str
99
159
  end
100
160
 
161
+ # Returns the body stream object for the request, or +nil+ if there is none:
162
+ #
163
+ # req = Net::HTTP::Post.new(uri) # => #<Net::HTTP::Post POST>
164
+ # req.body_stream # => nil
165
+ # require 'stringio'
166
+ # req.body_stream = StringIO.new('xyzzy') # => #<StringIO:0x0000027d1e5affa8>
167
+ # req.body_stream # => #<StringIO:0x0000027d1e5affa8>
168
+ #
101
169
  attr_reader :body_stream
102
170
 
171
+ # Sets the body stream for the request:
172
+ #
173
+ # req = Net::HTTP::Post.new(uri) # => #<Net::HTTP::Post POST>
174
+ # req.body_stream # => nil
175
+ # require 'stringio'
176
+ # req.body_stream = StringIO.new('xyzzy') # => #<StringIO:0x0000027d1e5affa8>
177
+ # req.body_stream # => #<StringIO:0x0000027d1e5affa8>
178
+ #
103
179
  def body_stream=(input)
104
180
  @body = nil
105
181
  @body_stream = input
@@ -136,15 +212,15 @@ class Net::HTTPGenericRequest
136
212
  return unless @uri
137
213
 
138
214
  if ssl
139
- scheme = 'https'.freeze
215
+ scheme = 'https'
140
216
  klass = URI::HTTPS
141
217
  else
142
- scheme = 'http'.freeze
218
+ scheme = 'http'
143
219
  klass = URI::HTTP
144
220
  end
145
221
 
146
222
  if host = self['host']
147
- host.sub!(/:.*/m, ''.freeze)
223
+ host.sub!(/:.*/m, '')
148
224
  elsif host = @uri.host
149
225
  else
150
226
  host = addr
@@ -240,7 +316,7 @@ class Net::HTTPGenericRequest
240
316
  boundary ||= SecureRandom.urlsafe_base64(40)
241
317
  chunked_p = chunked?
242
318
 
243
- buf = ''
319
+ buf = +''
244
320
  params.each do |key, value, h={}|
245
321
  key = quote_string(key, charset)
246
322
  filename =
@@ -325,7 +401,7 @@ class Net::HTTPGenericRequest
325
401
  if /[\r\n]/ =~ reqline
326
402
  raise ArgumentError, "A Request-Line must not contain CR or LF"
327
403
  end
328
- buf = ""
404
+ buf = +''
329
405
  buf << reqline << "\r\n"
330
406
  each_capitalized do |k,v|
331
407
  buf << "#{k}: #{v}\r\n"