http.rb 0.11.1 → 0.12.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: cbf011f369bb382e03c0ec192cb6ab4d0cad2f0e
4
- data.tar.gz: 0f6803e5073848ba81088f019cd70e3d10be1f66
2
+ SHA256:
3
+ metadata.gz: 64b5868b1582ef96a23021bef26cbfaa4e7ee7a63f28566e20c5f53d6ba91356
4
+ data.tar.gz: 9af58e51678e5b6ba65d55ffdca2d2382cde427fb7e2db2a5999a1e137344969
5
5
  SHA512:
6
- metadata.gz: 0e2584ee7057eebefe3bea61cc7946b92e0a8b11032aceb3c512dff88c62069924233ef82384199f0eb82785d53df3a1b04499c584ac6e8fda2e150f394b9745
7
- data.tar.gz: ec8300cb750bc3afd170a29a99de366b6d821a52d7f1ae26ff5d9ae5cd394dab0094cb90b8f866d7dbb2af43b4a94cf0bdd93b17da5b7bbd43531f39f672f7fc
6
+ metadata.gz: 291e79fc695b67dde10145d69311a79c2529f9cac03a217abbbeb254920c8aa137e0220f0e3f2eba3d46b482757a5c1777197fb4dc758dc0adb67918039a5924
7
+ data.tar.gz: ec2e4b6caf7878518de41ceec6881ebb957dfcca50bc9e3aaa4295b9893ff825459e374c3ebaaf22c6ec1eadb9c6e762459d833f71fbe71397c6b91fb44a217c
data/Gemfile CHANGED
@@ -1,6 +1,7 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  group :test do
4
+ gem 'pry', require: true
4
5
  gem 'rspec', require: false
5
6
  gem 'webmock', require: false
6
7
  end
data/README.md CHANGED
@@ -19,7 +19,7 @@ Perhaps someone will appreciate its relative simplicity, since it is much smalle
19
19
 
20
20
  Add this line to your application's Gemfile:
21
21
 
22
- gem 'HTTP'
22
+ gem 'http.rb'
23
23
 
24
24
  And then execute:
25
25
 
@@ -27,7 +27,7 @@ And then execute:
27
27
 
28
28
  Or install it yourself as:
29
29
 
30
- $ gem install HTTP
30
+ $ gem install http.rb
31
31
 
32
32
 
33
33
  ## Usage
@@ -43,6 +43,10 @@ HTTP.post('http://example.com') # Admittedly doing a POST without providing form
43
43
  HTTP.get('http://example.com', {a: 1, b: 2})
44
44
  HTTP.post('http://example.com', {a: 1, b: 2})
45
45
 
46
+ # With JSON data
47
+
48
+ HTTP.post('http://example.com', {a: 1, b: 2}, {'Content-type' => 'application/json'})
49
+
46
50
  # With custom headers only
47
51
 
48
52
  HTTP.get('http://example.com', {}, {'User-Agent'=>'Custom'})
@@ -89,6 +93,92 @@ end
89
93
 
90
94
  ```
91
95
 
96
+ ## Allowed values for the options hash (which passes through to Net::HTTP)
97
+ ```Ruby
98
+ ca_file
99
+ # Sets path of a CA certification file in PEM format.
100
+ #
101
+ # The file can contain several CA certificates.
102
+
103
+ ca_path
104
+ # Sets path of a CA certification directory containing certifications in
105
+ # PEM format.
106
+
107
+ cert
108
+ # Sets an OpenSSL::X509::Certificate object as client certificate.
109
+ # (This method is appeared in Michal Rokos's OpenSSL extension).
110
+
111
+ cert_store
112
+ # Sets the X509::Store to verify peer certificate.
113
+
114
+ ciphers
115
+ # Sets the available ciphers. See OpenSSL::SSL::SSLContext#ciphers=
116
+
117
+ close_on_empty_response
118
+
119
+ continue_timeout
120
+ # Number of seconds to wait for one block to be read (via one read(2)
121
+ # call). Any number may be used, including Floats for fractional
122
+ # seconds. If the HTTP object cannot read data in this many seconds,
123
+ # it raises a Net::ReadTimeout exception. The default value is 60 seconds.
124
+
125
+ keep_alive_timeout
126
+ # Seconds to reuse the connection of the previous request.
127
+ # If the idle time is less than this Keep-Alive Timeout,
128
+ # Net::HTTP reuses the TCP/IP socket used by the previous communication.
129
+ # The default value is 2 seconds.
130
+
131
+ key
132
+ # Sets an OpenSSL::PKey::RSA or OpenSSL::PKey::DSA object.
133
+ # (This method is appeared in Michal Rokos's OpenSSL extension.)
134
+
135
+ local_host
136
+ # The local host used to establish the connection.
137
+
138
+ local_port
139
+ # The local port used to establish the connection.
140
+
141
+ open_timeout
142
+ # Number of seconds to wait for the connection to open. Any number
143
+ # may be used, including Floats for fractional seconds. If the HTTP
144
+ # object cannot open a connection in this many seconds, it raises a
145
+ # Net::OpenTimeout exception. The default value is 60 seconds.
146
+
147
+ proxy_address
148
+ proxy_from_env
149
+ proxy_pass
150
+ proxy_port
151
+ proxy_user
152
+
153
+ read_timeout
154
+ # Seconds to wait for 100 Continue response. If the HTTP object does not
155
+ # receive a response in this many seconds it sends the request body. The
156
+ # default value is +nil+.
157
+
158
+ ssl_timeout
159
+ # Sets the SSL timeout seconds.
160
+
161
+ ssl_version
162
+ # Sets the SSL version. See OpenSSL::SSL::SSLContext#ssl_version=
163
+
164
+ use_ssl
165
+ # Turn on/off SSL.
166
+ # This flag must be set before starting session.
167
+ # If you change use_ssl value after session started,
168
+ # a Net::HTTP object raises IOError.
169
+
170
+ verify_callback
171
+ # Sets the verify callback for the server certification verification.
172
+
173
+ verify_depth
174
+ # Sets the maximum depth for the certificate chain verification.
175
+
176
+ verify_mode
177
+ # Sets the flags for server the certification verification at beginning of
178
+ # SSL/TLS session.
179
+ #
180
+ # OpenSSL::SSL::VERIFY_NONE or OpenSSL::SSL::VERIFY_PEER are acceptable.
181
+ ```
92
182
 
93
183
  ## Contributing
94
184
 
data/http.rb.gemspec CHANGED
@@ -1,11 +1,11 @@
1
1
  Gem::Specification.new do |s|
2
- s.name = 'http.rb'
3
- s.version = '0.11.1'
4
- s.date = '2015-05-18'
2
+ s.name = 'http.rb' # I would have preferred 'http', but there's a library called http.rb with the gem name of http. Confusing, eh?
3
+ s.version = '0.12.1'
4
+ s.date = '2025-02-07'
5
5
 
6
6
  s.summary = "HTTP made easy."
7
7
  s.description = "HTTP is the simplest HTTP mezzanine library for Ruby. Supply a URI, \
8
- some optional query arguments, some optional headers, and some other SSL or \
8
+ some optional query arguments, some optional headers, and some \
9
9
  Net::HTTP options, and that's it!"
10
10
  s.author = 'thoran'
11
11
  s.email = 'code@thoran.com'
data/lib/HTTP/get.rb CHANGED
@@ -9,6 +9,7 @@ lib_dir = File.expand_path(File.join(__FILE__, '..', '..'))
9
9
  $LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)
10
10
 
11
11
  require 'Hash/x_www_form_urlencode'
12
+ require 'Net/HTTP/set_options'
12
13
  require 'Net/HTTP/Get/set_headers'
13
14
  require 'URI/Generic/use_sslQ'
14
15
 
@@ -17,15 +18,21 @@ module HTTP
17
18
  def get(uri, args = {}, headers = {}, options = {}, &block)
18
19
  uri = uri.is_a?(URI) ? uri : URI.parse(uri)
19
20
  http = Net::HTTP.new(uri.host, uri.port)
20
- http.use_ssl = options[:use_ssl] || uri.use_ssl?
21
- http.verify_mode = options[:verify_mode] || OpenSSL::SSL::VERIFY_NONE
22
- options.each{|k,v| http.send("#{k}=", v)}
21
+ options[:use_ssl] ||= uri.use_ssl?
22
+ options[:verify_mode] ||= OpenSSL::SSL::VERIFY_NONE
23
+ http.options = options
23
24
  request_object = Net::HTTP::Get.new(uri.request_uri + '?' + args.x_www_form_urlencode)
24
25
  request_object.headers = headers
25
26
  request_object.basic_auth(uri.user, uri.password) if uri.user
26
27
  response = http.request(request_object)
27
28
  if response.code =~ /^3/
28
- response = get(response.header['location'], {}, {}, options, &block)
29
+ redirect_uri = URI.parse(response.header['location'])
30
+ if redirect_uri.scheme
31
+ response = get(response.header['location'], {}, {}, options, &block)
32
+ else
33
+ new_location = "http://#{uri.host}:#{uri.port}#{response.header['location']}"
34
+ response = get(new_location, {}, {}, options, &block)
35
+ end
29
36
  end
30
37
  if block_given?
31
38
  yield response
data/lib/HTTP/post.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # HTTP/post.rb
2
2
  # HTTP.post
3
3
 
4
+ require 'json'
4
5
  require 'net/http'
5
6
  require 'openssl'
6
7
  require 'uri'
@@ -9,24 +10,35 @@ lib_dir = File.expand_path(File.join(__FILE__, '..', '..'))
9
10
  $LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)
10
11
 
11
12
  require 'HTTP/get'
13
+ require 'Net/HTTP/set_options'
12
14
  require 'Net/HTTP/Post/set_headers'
13
15
  require 'URI/Generic/use_sslQ'
14
16
 
15
17
  module HTTP
16
18
 
17
- def post(uri, form_data = {}, headers = {}, options = {}, &block)
19
+ def post(uri, data = {}, headers = {}, options = {}, &block)
18
20
  uri = uri.is_a?(URI) ? uri : URI.parse(uri)
19
21
  http = Net::HTTP.new(uri.host, uri.port)
20
- http.use_ssl = options[:use_ssl] || uri.use_ssl?
21
- http.verify_mode = options[:verify_mode] || OpenSSL::SSL::VERIFY_NONE
22
- options.each{|k,v| http.send("#{k}=", v)}
22
+ options[:use_ssl] ||= uri.use_ssl?
23
+ options[:verify_mode] ||= OpenSSL::SSL::VERIFY_NONE
24
+ http.options = options
23
25
  request_object = Net::HTTP::Post.new(uri.request_uri)
24
- request_object.form_data = form_data
26
+ if headers['Content-Type'] == 'application/json'
27
+ request_object.body = JSON.dump(data)
28
+ else
29
+ request_object.form_data = data
30
+ end
25
31
  request_object.headers = headers
26
32
  request_object.basic_auth(uri.user, uri.password) if uri.user
27
33
  response = http.request(request_object)
28
34
  if response.code =~ /^3/
29
- response = get(response.header['location'], {}, {}, options, &block)
35
+ redirect_uri = URI.parse(response.header['location'])
36
+ if redirect_uri.scheme
37
+ response = get(response.header['location'], {}, {}, options, &block)
38
+ else
39
+ new_location = "http://#{uri.host}:#{uri.port}#{response.header['location']}"
40
+ response = get(new_location, {}, {}, options, &block)
41
+ end
30
42
  end
31
43
  if block_given?
32
44
  yield response
data/lib/HTTP.rb CHANGED
@@ -1,16 +1,6 @@
1
1
  # HTTP.rb
2
2
  # HTTP
3
3
 
4
- # 20150518
5
- # 0.11.1
6
-
7
- # Changes since 0.10:
8
- # 1. Removed HTTP/write.rb, since I wanted to reimplement it using standard File methods and not my custom written File.write. It may stay gone, but I'm not sure yet...
9
- # 2. + HTTP.gemspec
10
- # 3. + README.md
11
- # 0/1
12
- # 4. + String/url_encode, which was left out previously.
13
-
14
4
  lib_dir = File.expand_path(File.join(__FILE__, '..'))
15
5
  $LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)
16
6
 
@@ -0,0 +1,16 @@
1
+ # Net/HTTP/set_options.rb
2
+ # Net::HTTP#set_options
3
+
4
+ # 20171007
5
+ # 0.0.0
6
+
7
+ module Net
8
+ class HTTP
9
+
10
+ def set_options(options = {})
11
+ options.each{|k,v| self.send("#{k}=", v)}
12
+ end
13
+ alias_method :options=, :set_options
14
+
15
+ end
16
+ end
@@ -6,11 +6,7 @@ $LOAD_PATH.unshift(spec_dir) unless $LOAD_PATH.include?(spec_dir)
6
6
  require 'spec_helper'
7
7
  require 'HTTP/get'
8
8
 
9
- WebMock.enable!
10
- WebMock.disable_net_connect!(allow_localhost: true)
11
-
12
9
  describe ".get" do
13
-
14
10
  context "with uri-only supplied" do
15
11
  before do
16
12
  stub_request(:get, 'http://example.com/path').
@@ -144,12 +140,12 @@ describe ".get" do
144
140
  before do
145
141
  stub_request(:get, request_uri).
146
142
  with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
147
- to_return(status: '301', body: '', headers: {'location' => redirect_uri})
143
+ to_return(status: 301, body: '', headers: {'location' => redirect_uri})
148
144
  end
149
145
 
150
146
  it "does a redirect" do
151
147
  expect(HTTP).to receive(:get).once.with(request_uri).and_call_original
152
- expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {}).and_call_original
148
+ expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: 0})
153
149
  HTTP.get(request_uri)
154
150
  end
155
151
  end
@@ -158,15 +154,54 @@ describe ".get" do
158
154
  before do
159
155
  stub_request(:get, request_uri).
160
156
  with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
161
- to_return(status: '302', body: '', headers: {'location' => redirect_uri})
157
+ to_return(status: 302, body: '', headers: {'location' => redirect_uri})
162
158
  end
163
159
 
164
160
  it "does a redirect" do
165
- expect(HTTP).to receive(:get).with(request_uri).and_call_original
166
- expect(HTTP).to receive(:get).with(redirect_uri, {}, {}, {}).and_call_original
161
+ expect(HTTP).to receive(:get).once.with(request_uri).and_call_original
162
+ expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: 0})
167
163
  HTTP.get(request_uri)
168
164
  end
169
165
  end
170
166
  end
171
167
 
168
+ context "with path only redirection" do
169
+ let(:request_uri){'http://example.com/path'}
170
+ let(:redirect_path){'/new_path'}
171
+ let(:redirect_uri){"http://example.com:80#{redirect_path}"}
172
+
173
+ before do
174
+ stub_request(:get, redirect_uri).
175
+ with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
176
+ to_return(status: 200, body: '', headers: {})
177
+ end
178
+
179
+ context "via 301" do
180
+ before do
181
+ stub_request(:get, request_uri).
182
+ with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
183
+ to_return(status: 301, body: '', headers: {'location' => redirect_path})
184
+ end
185
+
186
+ it "does a redirect" do
187
+ expect(HTTP).to receive(:get).once.with(request_uri).and_call_original
188
+ expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: 0})
189
+ HTTP.get(request_uri)
190
+ end
191
+ end
192
+
193
+ context "via 302" do
194
+ before do
195
+ stub_request(:get, request_uri).
196
+ with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
197
+ to_return(status: 302, body: '', headers: {'location' => redirect_path})
198
+ end
199
+
200
+ it "does a redirect" do
201
+ expect(HTTP).to receive(:get).once.with(request_uri).and_call_original
202
+ expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: 0})
203
+ HTTP.get(request_uri)
204
+ end
205
+ end
206
+ end
172
207
  end
@@ -6,15 +6,11 @@ $LOAD_PATH.unshift(spec_dir) unless $LOAD_PATH.include?(spec_dir)
6
6
  require 'spec_helper'
7
7
  require 'HTTP/post'
8
8
 
9
- WebMock.enable!
10
- WebMock.disable_net_connect!(allow_localhost: true)
11
-
12
9
  describe ".post" do
13
-
14
10
  context "with uri-only supplied" do
15
11
  before do
16
12
  stub_request(:post, 'http://example.com/path').
17
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
13
+ with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'Ruby'}).
18
14
  to_return(status: 200, body: '', headers: {})
19
15
  end
20
16
 
@@ -54,26 +50,54 @@ describe ".post" do
54
50
  context "with form data supplied" do
55
51
  let(:uri){'http://example.com/path'}
56
52
  let(:parsed_uri){URI.parse(uri)}
57
- let(:form_data) do; {a: 1, b: 2}; end
58
- let(:encoded_form_data) do; form_data.x_www_form_urlencode; end
53
+ let(:args) do; {a: 1, b: 2}; end
54
+ let(:headers) do; {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'Ruby'}; end
55
+ let(:encoded_form_data){args.x_www_form_urlencode}
59
56
  let(:request_uri){parsed_uri.request_uri}
60
57
  let(:request_object){Net::HTTP::Post.new(request_uri)}
61
58
 
62
59
  before do
63
60
  stub_request(:post, "http://example.com/path").
64
- with(body: {"a"=>"1", "b"=>"2"}, headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
61
+ with(body: encoded_form_data, headers: headers).
65
62
  to_return(status: 200, body: '', headers: {})
66
63
  end
67
64
 
68
65
  it "sets the form data" do
69
66
  allow(Net::HTTP::Post).to receive(:new).with(request_uri).and_return(request_object)
70
- HTTP.post(uri, form_data)
67
+ HTTP.post(uri, args, headers)
71
68
  expect(request_object.body).to eq(encoded_form_data)
72
69
  end
73
70
 
74
71
  it "creates a new Net::HTTP::Post object" do
75
72
  expect(Net::HTTP::Post).to receive(:new).with(request_uri).and_return(request_object)
76
- HTTP.post(uri, form_data)
73
+ HTTP.post(uri, args, headers)
74
+ end
75
+ end
76
+
77
+ context "with JSON data supplied" do
78
+ let(:uri){'http://example.com/path'}
79
+ let(:parsed_uri){URI.parse(uri)}
80
+ let(:args) do; {a: 1, b: 2}; end
81
+ let(:headers) do; {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'}; end
82
+ let(:json_data){JSON.dump(args)}
83
+ let(:request_uri){parsed_uri.request_uri}
84
+ let(:request_object){Net::HTTP::Post.new(request_uri)}
85
+
86
+ before do
87
+ stub_request(:post, "http://example.com/path").
88
+ with(body: json_data, headers: headers).
89
+ to_return(status: 200, body: '', headers: {})
90
+ end
91
+
92
+ it "sets the body" do
93
+ allow(Net::HTTP::Post).to receive(:new).with(request_uri).and_return(request_object)
94
+ HTTP.post(uri, args, headers)
95
+ expect(request_object.body).to eq(json_data)
96
+ end
97
+
98
+ it "creates a new Net::HTTP::Post object" do
99
+ expect(Net::HTTP::Post).to receive(:new).with(request_uri).and_return(request_object)
100
+ HTTP.post(uri, args, headers)
77
101
  end
78
102
  end
79
103
 
@@ -86,7 +110,7 @@ describe ".post" do
86
110
 
87
111
  before do
88
112
  stub_request(:post, 'http://example.com/path').
89
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Rspec'}).
113
+ with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'Rspec'}).
90
114
  to_return(status: 200, body: '', headers: {})
91
115
  end
92
116
 
@@ -105,7 +129,7 @@ describe ".post" do
105
129
 
106
130
  before do
107
131
  stub_request(:post, 'https://example.com:80/path').
108
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
132
+ with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'Ruby'}).
109
133
  to_return(status: 200, body: '', headers: {})
110
134
  end
111
135
 
@@ -121,7 +145,7 @@ describe ".post" do
121
145
 
122
146
  before do
123
147
  stub_request(:post, 'http://example.com/path').
124
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
148
+ with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'Ruby'}).
125
149
  to_return(status: 200, body: '', headers: {})
126
150
  end
127
151
 
@@ -145,12 +169,12 @@ describe ".post" do
145
169
  before do
146
170
  stub_request(:post, request_uri).
147
171
  with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
148
- to_return(status: '301', body: '', headers: {'location' => redirect_uri})
172
+ to_return(status: 301, body: '', headers: {'location' => redirect_uri})
149
173
  end
150
174
 
151
175
  it "does a redirect" do
152
176
  expect(HTTP).to receive(:post).once.with(request_uri).and_call_original
153
- expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {}).and_call_original
177
+ expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: 0})
154
178
  HTTP.post(request_uri)
155
179
  end
156
180
  end
@@ -159,15 +183,54 @@ describe ".post" do
159
183
  before do
160
184
  stub_request(:post, request_uri).
161
185
  with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
162
- to_return(status: '302', body: '', headers: {'location' => redirect_uri})
186
+ to_return(status: 302, body: '', headers: {'location' => redirect_uri})
163
187
  end
164
188
 
165
189
  it "does a redirect" do
166
190
  expect(HTTP).to receive(:post).with(request_uri).and_call_original
167
- expect(HTTP).to receive(:get).with(redirect_uri, {}, {}, {}).and_call_original
191
+ expect(HTTP).to receive(:get).with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: 0})
168
192
  HTTP.post(request_uri)
169
193
  end
170
194
  end
171
195
  end
172
196
 
197
+ context "with path only redirection" do
198
+ let(:request_uri){'http://example.com/path'}
199
+ let(:redirect_path){'/new_path'}
200
+ let(:redirect_uri){"http://example.com:80#{redirect_path}"}
201
+
202
+ before do
203
+ stub_request(:get, redirect_uri).
204
+ with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
205
+ to_return(status: 200, body: '', headers: {})
206
+ end
207
+
208
+ context "via 301" do
209
+ before do
210
+ stub_request(:post, request_uri).
211
+ with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
212
+ to_return(status: 301, body: '', headers: {'location' => redirect_path})
213
+ end
214
+
215
+ it "does a redirect" do
216
+ expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: 0})
217
+ expect(HTTP).to receive(:post).once.with(request_uri).and_call_original
218
+ HTTP.post(request_uri)
219
+ end
220
+ end
221
+
222
+ context "via 302" do
223
+ before do
224
+ stub_request(:post, request_uri).
225
+ with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'Ruby'}).
226
+ to_return(status: 302, body: '', headers: {'location' => redirect_path})
227
+ end
228
+
229
+ it "does a redirect" do
230
+ expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: 0})
231
+ expect(HTTP).to receive(:post).once.with(request_uri).and_call_original
232
+ HTTP.post(request_uri)
233
+ end
234
+ end
235
+ end
173
236
  end
data/spec/spec_helper.rb CHANGED
@@ -4,4 +4,3 @@ lib_dir = File.expand_path(File.join(__FILE__, '..', '..', 'lib'))
4
4
  $LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)
5
5
 
6
6
  require 'webmock/rspec'
7
- WebMock.allow_net_connect!
metadata CHANGED
@@ -1,18 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.1
4
+ version: 0.12.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - thoran
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2015-05-18 00:00:00.000000000 Z
10
+ date: 2025-02-07 00:00:00.000000000 Z
12
11
  dependencies: []
13
12
  description: HTTP is the simplest HTTP mezzanine library for Ruby. Supply a URI, some
14
- optional query arguments, some optional headers, and some other SSL or Net::HTTP
15
- options, and that's it!
13
+ optional query arguments, some optional headers, and some Net::HTTP options,
14
+ and that's it!
16
15
  email: code@thoran.com
17
16
  executables: []
18
17
  extensions: []
@@ -27,16 +26,15 @@ files:
27
26
  - lib/Hash/x_www_form_urlencode.rb
28
27
  - lib/Net/HTTP/Get/set_headers.rb
29
28
  - lib/Net/HTTP/Post/set_headers.rb
29
+ - lib/Net/HTTP/set_options.rb
30
30
  - lib/String/url_encode.rb
31
31
  - lib/URI/Generic/use_sslQ.rb
32
- - spec/HTTP.rb
33
32
  - spec/HTTP/get_spec.rb
34
33
  - spec/HTTP/post_spec.rb
35
34
  - spec/spec_helper.rb
36
35
  homepage: http://github.com/thoran/HTTP
37
36
  licenses: []
38
37
  metadata: {}
39
- post_install_message:
40
38
  rdoc_options: []
41
39
  require_paths:
42
40
  - lib
@@ -51,9 +49,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
51
49
  - !ruby/object:Gem::Version
52
50
  version: '0'
53
51
  requirements: []
54
- rubyforge_project:
55
- rubygems_version: 2.4.5
56
- signing_key:
52
+ rubygems_version: 3.6.3
57
53
  specification_version: 4
58
54
  summary: HTTP made easy.
59
55
  test_files: []
data/spec/HTTP.rb DELETED
@@ -1,12 +0,0 @@
1
- # spec/HTTP.rb
2
-
3
- spec_dir = File.expand_path(File.join(__FILE__, '..'))
4
- $LOAD_PATH.unshift(spec_dir) unless $LOAD_PATH.include?(spec_dir)
5
-
6
- require 'spec_helper'
7
-
8
- require 'HTTP/get'
9
- require 'HTTP/post'
10
-
11
- require 'HTTP/get_spec'
12
- require 'HTTP/post_spec'