http.rb 0.12.1 → 0.13.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/CHANGELOG.txt +37 -0
- data/README.md +60 -3
- data/http.rb.gemspec +6 -5
- data/lib/HTTP/VERSION.rb +6 -0
- data/lib/HTTP/get.rb +11 -7
- data/lib/HTTP/post.rb +11 -7
- data/lib/Net/HTTPResponse/StatusPredicates.rb +33 -0
- data/spec/HTTP/get_spec.rb +59 -16
- data/spec/HTTP/post_spec.rb +65 -19
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ae541e50094a51ccb63d737279a67ce0ae4a6a0ee55fe51a05e091da02e170e9
|
4
|
+
data.tar.gz: b1c865b1edd6c6ae58711af7502d10e69da3c31d41d1655438d8bb7c83778bf4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 32a009dc748bef0e9aecf8b225e9bbb8f6bc991f3ffccaa41cb1c43fea085808fcdfb26abed1aa1174ab7332bb297ae3e6202398a8fc8ea20eee9da221e65f21
|
7
|
+
data.tar.gz: 9ddbec00fc0efcb277bf673ba390a13ec76c449db0f5f5189cc6129fdaddf465624fb75e3a0ae7bc47e3e7e423bc5fae42307b83fd50764d59847a5495eda2aa
|
data/CHANGELOG.txt
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# 202503030
|
2
|
+
# 0.13.2: Change repo name to match gem name (/HTTP/http.rb/); + Use HTTP::VERSION; /require/require_relative/
|
3
|
+
1. ~ README.md: /HTTP/http.rb/, /HTTP.rb/http.rb/
|
4
|
+
2. + HTTP::VERSION
|
5
|
+
3. ~ http.rb.gemspec: Use HTTP::VERSION.
|
6
|
+
4. ~ HTTP.get: /require/require_relative/
|
7
|
+
5. ~ HTTP.post: /require/require_relative/
|
8
|
+
|
9
|
+
# 20250304
|
10
|
+
# 0.13.1: /HTTP.rb.gemspec/http.rb.gemspec/
|
11
|
+
1. /HTTP.rb.gemspec/http.rb.gemspec/ (Wonder no more!)
|
12
|
+
|
13
|
+
# 20250304
|
14
|
+
# 0.13.0: Extend Net::HTTPResponse to allow use of predicate methods for statuses and optionally prevent redirections.
|
15
|
+
1. + lib/Net/HTTPResponse/StatusPredicates.rb
|
16
|
+
2. ~ HTTP/get.rb: + require 'Net/HTTPResponse/StatusPredicates'
|
17
|
+
3. ~ HTTP/post.rb: + require 'Net/HTTPResponse/StatusPredicates'
|
18
|
+
4. ~ HTTP.get(): Allow redirects to not be followed with no_redirect option.
|
19
|
+
5. ~ HTTP.post(): Allow redirects to not be followed with no_redirect option.
|
20
|
+
6. ~ spec/HTTP/get_spec.rb: Add success? and redirection? expectations throughout as is correct.
|
21
|
+
7. ~ spec/HTTP/post_spec.rb: Add success? and redirection? expectations throughout as is correct.
|
22
|
+
8. ~ README.md: Demonstrate usage of response status predicates.
|
23
|
+
9. ~ README.md: Demonstrate usage of no_redirect option.
|
24
|
+
10. ~ http.rb.gemspec: /0.12.1/0.13.0/
|
25
|
+
11. /http.rb.gemspec/HTTP.rb.gemspec/ (I wonder if that's going to cause issues for rubygems.org...)
|
26
|
+
12. ~ CHANGELOG.txt
|
27
|
+
|
28
|
+
# 20250207
|
29
|
+
# 0.12.1: Correctly handle POST'ing JSON data.
|
30
|
+
1. ~ HTTP.post(): Check if the Content-Type is 'application/json' and assign the body JSON data otherwise assign the supplied hash to the form data.
|
31
|
+
2. ~ spec/HTTP/post_spec.rb: + spec for when a request is being made with JSON data
|
32
|
+
3. ~ spec/HTTP/post_spec.rb: Assign a headers variable to make the spec more readable.
|
33
|
+
4. ~ spec/HTTP/post_spec.rb: Remove spaces between outer-most and second outer-most blocks.
|
34
|
+
5. ~ spec/HTTP/get_spec.rb: Remove spaces between outer-most and second outer-most blocks.
|
35
|
+
6. ~ README.md: Add # With JSON data section
|
36
|
+
7. ~ http.rb.gemspec: /0.12.0/0.12.1/
|
37
|
+
8. + CHANGELOG.txt
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# http.rb
|
2
2
|
|
3
3
|
## Description
|
4
4
|
|
@@ -19,12 +19,14 @@ 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
|
-
|
22
|
+
gem 'http.rb'
|
23
|
+
|
23
24
|
|
24
25
|
And then execute:
|
25
26
|
|
26
27
|
$ bundle
|
27
28
|
|
29
|
+
|
28
30
|
Or install it yourself as:
|
29
31
|
|
30
32
|
$ gem install http.rb
|
@@ -75,6 +77,56 @@ HTTP.post('http://example.com', {a: 1, b: 2}, {'User-Agent'=>'Custom'}, {use_ssl
|
|
75
77
|
# Do stuff with a subclass of Net::HTTPResponse here...
|
76
78
|
end
|
77
79
|
|
80
|
+
# Preventing redirections
|
81
|
+
|
82
|
+
HTTP.get('http://example.com', {}, {}, {no_redirect: true})
|
83
|
+
# => #<Net::HTTPResponse @code=3xx>
|
84
|
+
|
85
|
+
# Response status predicate methods
|
86
|
+
|
87
|
+
# 1xx
|
88
|
+
response = HTTP.get('http://example.com')
|
89
|
+
response.informational?
|
90
|
+
# => true
|
91
|
+
|
92
|
+
# 2xx
|
93
|
+
response = HTTP.get('http://example.com')
|
94
|
+
response.success?
|
95
|
+
# => true
|
96
|
+
|
97
|
+
# 3xx
|
98
|
+
response = HTTP.get('http://example.com', {}, {}, {no_redirect: true})
|
99
|
+
response.redirection?
|
100
|
+
# => true
|
101
|
+
response.success?
|
102
|
+
# => false
|
103
|
+
|
104
|
+
response = HTTP.get('http://example.com', {}, {}, {no_redirect: false})
|
105
|
+
response.redirection?
|
106
|
+
# => false
|
107
|
+
response.success?
|
108
|
+
# => true
|
109
|
+
|
110
|
+
response = HTTP.get('http://example.com')
|
111
|
+
response.redirection?
|
112
|
+
# => false
|
113
|
+
response.success?
|
114
|
+
# => true
|
115
|
+
|
116
|
+
# 4xx
|
117
|
+
response = HTTP.get('http://example.com')
|
118
|
+
response.client_error?
|
119
|
+
# => true
|
120
|
+
response.error?
|
121
|
+
# => true
|
122
|
+
|
123
|
+
# 5xx
|
124
|
+
response = HTTP.get('http://example.com')
|
125
|
+
response.server_error?
|
126
|
+
# => true
|
127
|
+
response.error?
|
128
|
+
# => true
|
129
|
+
|
78
130
|
# Including it in a class
|
79
131
|
|
80
132
|
class A
|
@@ -93,8 +145,13 @@ end
|
|
93
145
|
|
94
146
|
```
|
95
147
|
|
96
|
-
## Allowed values for the options hash
|
148
|
+
## Allowed values for the options hash
|
149
|
+
#### (These pass through to Net::HTTP, except for `no_redirect`.)
|
150
|
+
|
97
151
|
```Ruby
|
152
|
+
no_redirect
|
153
|
+
# Prevents redirection if a 3xx response is encountered.
|
154
|
+
|
98
155
|
ca_file
|
99
156
|
# Sets path of a CA certification file in PEM format.
|
100
157
|
#
|
data/http.rb.gemspec
CHANGED
@@ -1,7 +1,9 @@
|
|
1
|
+
require_relative './lib/HTTP/VERSION'
|
2
|
+
|
1
3
|
Gem::Specification.new do |s|
|
2
|
-
s.name = 'http.rb'
|
3
|
-
s.version =
|
4
|
-
s.date = '2025-
|
4
|
+
s.name = 'http.rb'
|
5
|
+
s.version = HTTP::VERSION
|
6
|
+
s.date = '2025-03-30'
|
5
7
|
|
6
8
|
s.summary = "HTTP made easy."
|
7
9
|
s.description = "HTTP is the simplest HTTP mezzanine library for Ruby. Supply a URI, \
|
@@ -12,6 +14,7 @@ Gem::Specification.new do |s|
|
|
12
14
|
s.homepage = "http://github.com/thoran/HTTP"
|
13
15
|
|
14
16
|
s.files = [
|
17
|
+
'CHANGELOG.txt',
|
15
18
|
'Gemfile',
|
16
19
|
'README.md',
|
17
20
|
'http.rb.gemspec',
|
@@ -20,6 +23,4 @@ Gem::Specification.new do |s|
|
|
20
23
|
].flatten
|
21
24
|
|
22
25
|
s.require_paths = ['lib']
|
23
|
-
|
24
|
-
s.has_rdoc = false
|
25
26
|
end
|
data/lib/HTTP/VERSION.rb
ADDED
data/lib/HTTP/get.rb
CHANGED
@@ -5,19 +5,18 @@ require 'net/http'
|
|
5
5
|
require 'openssl'
|
6
6
|
require 'uri'
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
require 'Net/HTTP/Get/set_headers'
|
14
|
-
require 'URI/Generic/use_sslQ'
|
8
|
+
require_relative '../Hash/x_www_form_urlencode'
|
9
|
+
require_relative '../Net/HTTP/set_options'
|
10
|
+
require_relative '../Net/HTTP/Get/set_headers'
|
11
|
+
require_relative '../Net/HTTPResponse/StatusPredicates'
|
12
|
+
require_relative '../URI/Generic/use_sslQ'
|
15
13
|
|
16
14
|
module HTTP
|
17
15
|
|
18
16
|
def get(uri, args = {}, headers = {}, options = {}, &block)
|
19
17
|
uri = uri.is_a?(URI) ? uri : URI.parse(uri)
|
20
18
|
http = Net::HTTP.new(uri.host, uri.port)
|
19
|
+
no_redirect = options.delete(:no_redirect)
|
21
20
|
options[:use_ssl] ||= uri.use_ssl?
|
22
21
|
options[:verify_mode] ||= OpenSSL::SSL::VERIFY_NONE
|
23
22
|
http.options = options
|
@@ -26,6 +25,11 @@ module HTTP
|
|
26
25
|
request_object.basic_auth(uri.user, uri.password) if uri.user
|
27
26
|
response = http.request(request_object)
|
28
27
|
if response.code =~ /^3/
|
28
|
+
if block_given? && no_redirect
|
29
|
+
yield response
|
30
|
+
elsif no_redirect
|
31
|
+
return response
|
32
|
+
end
|
29
33
|
redirect_uri = URI.parse(response.header['location'])
|
30
34
|
if redirect_uri.scheme
|
31
35
|
response = get(response.header['location'], {}, {}, options, &block)
|
data/lib/HTTP/post.rb
CHANGED
@@ -6,19 +6,18 @@ require 'net/http'
|
|
6
6
|
require 'openssl'
|
7
7
|
require 'uri'
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
require 'Net/HTTP/Post/set_headers'
|
15
|
-
require 'URI/Generic/use_sslQ'
|
9
|
+
require_relative '../HTTP/get'
|
10
|
+
require_relative '../Net/HTTP/set_options'
|
11
|
+
require_relative '../Net/HTTP/Post/set_headers'
|
12
|
+
require_relative '../Net/HTTPResponse/StatusPredicates'
|
13
|
+
require_relative '../URI/Generic/use_sslQ'
|
16
14
|
|
17
15
|
module HTTP
|
18
16
|
|
19
17
|
def post(uri, data = {}, headers = {}, options = {}, &block)
|
20
18
|
uri = uri.is_a?(URI) ? uri : URI.parse(uri)
|
21
19
|
http = Net::HTTP.new(uri.host, uri.port)
|
20
|
+
no_redirect = options.delete(:no_redirect)
|
22
21
|
options[:use_ssl] ||= uri.use_ssl?
|
23
22
|
options[:verify_mode] ||= OpenSSL::SSL::VERIFY_NONE
|
24
23
|
http.options = options
|
@@ -32,6 +31,11 @@ module HTTP
|
|
32
31
|
request_object.basic_auth(uri.user, uri.password) if uri.user
|
33
32
|
response = http.request(request_object)
|
34
33
|
if response.code =~ /^3/
|
34
|
+
if block_given? && no_redirect
|
35
|
+
yield response
|
36
|
+
elsif no_redirect
|
37
|
+
return response
|
38
|
+
end
|
35
39
|
redirect_uri = URI.parse(response.header['location'])
|
36
40
|
if redirect_uri.scheme
|
37
41
|
response = get(response.header['location'], {}, {}, options, &block)
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Net
|
2
|
+
class HTTPResponse
|
3
|
+
module StatusPredicates
|
4
|
+
def informational?
|
5
|
+
@code =~ /^1/ ? true : false
|
6
|
+
end
|
7
|
+
|
8
|
+
def successful?
|
9
|
+
@code =~ /^2/ ? true : false
|
10
|
+
end
|
11
|
+
alias_method :success?, :successful?
|
12
|
+
alias_method :ok?, :successful?
|
13
|
+
|
14
|
+
def redirection?
|
15
|
+
@code =~ /^3/ ? true : false
|
16
|
+
end
|
17
|
+
|
18
|
+
def client_error?
|
19
|
+
@code =~ /^4/ ? true : false
|
20
|
+
end
|
21
|
+
|
22
|
+
def server_error?
|
23
|
+
@code =~ /^5/ ? true : false
|
24
|
+
end
|
25
|
+
|
26
|
+
def error?
|
27
|
+
client_error? || server_error?
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
Net::HTTPResponse.include(Net::HTTPResponse::StatusPredicates)
|
data/spec/HTTP/get_spec.rb
CHANGED
@@ -21,12 +21,14 @@ describe ".get" do
|
|
21
21
|
|
22
22
|
it "creates an instance of URI" do
|
23
23
|
expect(URI).to receive(:parse).with(uri).and_return(parsed_uri)
|
24
|
-
HTTP.get(uri)
|
24
|
+
response = HTTP.get(uri)
|
25
|
+
expect(response.success?).to eq(true)
|
25
26
|
end
|
26
27
|
|
27
28
|
it "creates a new Net::HTTP object" do
|
28
29
|
expect(Net::HTTP).to receive(:new).with(parsed_uri.host, parsed_uri.port).and_return(net_http_object)
|
29
|
-
HTTP.get(uri)
|
30
|
+
response = HTTP.get(uri)
|
31
|
+
expect(response.success?).to eq(true)
|
30
32
|
end
|
31
33
|
end
|
32
34
|
|
@@ -42,7 +44,8 @@ describe ".get" do
|
|
42
44
|
|
43
45
|
it "creates a new Net::HTTP object" do
|
44
46
|
expect(Net::HTTP).to receive(:new).with(uri.host, uri.port).and_return(net_http_object)
|
45
|
-
HTTP.get(uri)
|
47
|
+
response = HTTP.get(uri)
|
48
|
+
expect(response.success?).to eq(true)
|
46
49
|
end
|
47
50
|
end
|
48
51
|
end
|
@@ -63,12 +66,14 @@ describe ".get" do
|
|
63
66
|
|
64
67
|
it "x_www_form_urlencode's the args" do
|
65
68
|
expect(args).to receive(:x_www_form_urlencode).and_return(x_www_form_urlencoded_arguments)
|
66
|
-
HTTP.get(uri, args)
|
69
|
+
response = HTTP.get(uri, args)
|
70
|
+
expect(response.success?).to eq(true)
|
67
71
|
end
|
68
72
|
|
69
73
|
it "creates a new Net::HTTP::Get object" do
|
70
74
|
expect(Net::HTTP::Get).to receive(:new).with(get_argument).and_return(request_object)
|
71
|
-
HTTP.get(uri, args)
|
75
|
+
response = HTTP.get(uri, args)
|
76
|
+
expect(response.success?).to eq(true)
|
72
77
|
end
|
73
78
|
end
|
74
79
|
|
@@ -87,8 +92,9 @@ describe ".get" do
|
|
87
92
|
|
88
93
|
it "sets the headers on the request object" do
|
89
94
|
allow(Net::HTTP::Get).to receive(:new).with(get_argument).and_return(request_object)
|
90
|
-
HTTP.get(uri, {}, headers)
|
95
|
+
response = HTTP.get(uri, {}, headers)
|
91
96
|
expect(request_object['User-Agent']).to eq('Rspec')
|
97
|
+
expect(response.success?).to eq(true)
|
92
98
|
end
|
93
99
|
end
|
94
100
|
|
@@ -106,8 +112,9 @@ describe ".get" do
|
|
106
112
|
|
107
113
|
it "sets the use_ssl option on the Net::HTTP instance" do
|
108
114
|
allow(Net::HTTP).to receive(:new).with(parsed_uri.host, parsed_uri.port).and_return(net_http_object)
|
109
|
-
HTTP.get(uri, {}, {}, options)
|
115
|
+
response = HTTP.get(uri, {}, {}, options)
|
110
116
|
expect(net_http_object.instance_variable_get(:@use_ssl)).to be_truthy
|
117
|
+
expect(response.success?).to eq(true)
|
111
118
|
end
|
112
119
|
end
|
113
120
|
|
@@ -122,7 +129,6 @@ describe ".get" do
|
|
122
129
|
|
123
130
|
it "yields an instance of Net::HTTPResponse" do
|
124
131
|
expect{|b| HTTP.get(uri, &b)}.to yield_with_args(Net::HTTPResponse)
|
125
|
-
HTTP.get(uri){|response|}
|
126
132
|
end
|
127
133
|
end
|
128
134
|
|
@@ -145,8 +151,9 @@ describe ".get" do
|
|
145
151
|
|
146
152
|
it "does a redirect" do
|
147
153
|
expect(HTTP).to receive(:get).once.with(request_uri).and_call_original
|
148
|
-
expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: 0})
|
149
|
-
HTTP.get(request_uri)
|
154
|
+
expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: 0}).and_call_original
|
155
|
+
response = HTTP.get(request_uri)
|
156
|
+
expect(response.success?).to eq(true)
|
150
157
|
end
|
151
158
|
end
|
152
159
|
|
@@ -159,8 +166,9 @@ describe ".get" do
|
|
159
166
|
|
160
167
|
it "does a redirect" do
|
161
168
|
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})
|
163
|
-
HTTP.get(request_uri)
|
169
|
+
expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: 0}).and_call_original
|
170
|
+
response = HTTP.get(request_uri)
|
171
|
+
expect(response.success?).to eq(true)
|
164
172
|
end
|
165
173
|
end
|
166
174
|
end
|
@@ -185,8 +193,9 @@ describe ".get" do
|
|
185
193
|
|
186
194
|
it "does a redirect" do
|
187
195
|
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)
|
196
|
+
expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: 0}).and_call_original
|
197
|
+
response = HTTP.get(request_uri)
|
198
|
+
expect(response.success?).to eq(true)
|
190
199
|
end
|
191
200
|
end
|
192
201
|
|
@@ -199,8 +208,42 @@ describe ".get" do
|
|
199
208
|
|
200
209
|
it "does a redirect" do
|
201
210
|
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)
|
211
|
+
expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: 0}).and_call_original
|
212
|
+
response = HTTP.get(request_uri)
|
213
|
+
expect(response.success?).to eq(true)
|
214
|
+
end
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
context "no_redirect true" do
|
219
|
+
let(:request_uri){'http://example.com/path'}
|
220
|
+
let(:redirect_uri){'http://redirected.com'}
|
221
|
+
|
222
|
+
context "via 301" do
|
223
|
+
before do
|
224
|
+
stub_request(:get, request_uri).
|
225
|
+
with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
|
226
|
+
to_return(status: 301, body: '', headers: {'location' => redirect_uri})
|
227
|
+
end
|
228
|
+
|
229
|
+
it "doesn't redirect" do
|
230
|
+
expect(HTTP).to receive(:get).once.with(request_uri, {}, {}, {no_redirect: true}).and_call_original
|
231
|
+
response = HTTP.get(request_uri, {}, {}, {no_redirect: true})
|
232
|
+
expect(response.redirection?).to eq(true)
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
context "via 302" do
|
237
|
+
before do
|
238
|
+
stub_request(:get, request_uri).
|
239
|
+
with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
|
240
|
+
to_return(status: 302, body: '', headers: {'location' => redirect_uri})
|
241
|
+
end
|
242
|
+
|
243
|
+
it "doesn't redirect" do
|
244
|
+
expect(HTTP).to receive(:get).once.with(request_uri, {}, {}, {no_redirect: true}).and_call_original
|
245
|
+
response = HTTP.get(request_uri, {}, {}, {no_redirect: true})
|
246
|
+
expect(response.redirection?).to eq(true)
|
204
247
|
end
|
205
248
|
end
|
206
249
|
end
|
data/spec/HTTP/post_spec.rb
CHANGED
@@ -21,12 +21,14 @@ describe ".post" do
|
|
21
21
|
|
22
22
|
it "creates an instance of URI" do
|
23
23
|
expect(URI).to receive(:parse).with(uri).and_return(parsed_uri)
|
24
|
-
HTTP.post(uri)
|
24
|
+
response = HTTP.post(uri)
|
25
|
+
expect(response.success?).to eq(true)
|
25
26
|
end
|
26
27
|
|
27
28
|
it "creates a new Net::HTTP object" do
|
28
29
|
expect(Net::HTTP).to receive(:new).with(parsed_uri.host, parsed_uri.port).and_return(net_http_object)
|
29
|
-
HTTP.post(uri)
|
30
|
+
response = HTTP.post(uri)
|
31
|
+
expect(response.success?).to eq(true)
|
30
32
|
end
|
31
33
|
end
|
32
34
|
|
@@ -37,12 +39,14 @@ describe ".post" do
|
|
37
39
|
|
38
40
|
it "returns an instance of URI" do
|
39
41
|
expect(uri).to eq(uri)
|
40
|
-
HTTP.post(uri)
|
42
|
+
response = HTTP.post(uri)
|
43
|
+
expect(response.success?).to eq(true)
|
41
44
|
end
|
42
45
|
|
43
46
|
it "creates a new Net::HTTP object" do
|
44
47
|
expect(Net::HTTP).to receive(:new).with(uri.host, uri.port).and_return(net_http_object)
|
45
|
-
HTTP.post(uri)
|
48
|
+
response = HTTP.post(uri)
|
49
|
+
expect(response.success?).to eq(true)
|
46
50
|
end
|
47
51
|
end
|
48
52
|
end
|
@@ -64,13 +68,15 @@ describe ".post" do
|
|
64
68
|
|
65
69
|
it "sets the form data" do
|
66
70
|
allow(Net::HTTP::Post).to receive(:new).with(request_uri).and_return(request_object)
|
67
|
-
HTTP.post(uri, args, headers)
|
71
|
+
response = HTTP.post(uri, args, headers)
|
68
72
|
expect(request_object.body).to eq(encoded_form_data)
|
73
|
+
expect(response.success?).to eq(true)
|
69
74
|
end
|
70
75
|
|
71
76
|
it "creates a new Net::HTTP::Post object" do
|
72
77
|
expect(Net::HTTP::Post).to receive(:new).with(request_uri).and_return(request_object)
|
73
|
-
HTTP.post(uri, args, headers)
|
78
|
+
response = HTTP.post(uri, args, headers)
|
79
|
+
expect(response.success?).to eq(true)
|
74
80
|
end
|
75
81
|
end
|
76
82
|
|
@@ -91,13 +97,15 @@ describe ".post" do
|
|
91
97
|
|
92
98
|
it "sets the body" do
|
93
99
|
allow(Net::HTTP::Post).to receive(:new).with(request_uri).and_return(request_object)
|
94
|
-
HTTP.post(uri, args, headers)
|
100
|
+
response = HTTP.post(uri, args, headers)
|
95
101
|
expect(request_object.body).to eq(json_data)
|
102
|
+
expect(response.success?).to eq(true)
|
96
103
|
end
|
97
104
|
|
98
105
|
it "creates a new Net::HTTP::Post object" do
|
99
106
|
expect(Net::HTTP::Post).to receive(:new).with(request_uri).and_return(request_object)
|
100
|
-
HTTP.post(uri, args, headers)
|
107
|
+
response = HTTP.post(uri, args, headers)
|
108
|
+
expect(response.success?).to eq(true)
|
101
109
|
end
|
102
110
|
end
|
103
111
|
|
@@ -116,8 +124,9 @@ describe ".post" do
|
|
116
124
|
|
117
125
|
it "sets the headers on the request object" do
|
118
126
|
allow(Net::HTTP::Post).to receive(:new).with(request_uri).and_return(request_object)
|
119
|
-
HTTP.post(uri, {}, headers)
|
127
|
+
response = HTTP.post(uri, {}, headers)
|
120
128
|
expect(request_object['User-Agent']).to eq('Rspec')
|
129
|
+
expect(response.success?).to eq(true)
|
121
130
|
end
|
122
131
|
end
|
123
132
|
|
@@ -135,8 +144,9 @@ describe ".post" do
|
|
135
144
|
|
136
145
|
it "sets the use_ssl option on the Net::HTTP instance" do
|
137
146
|
allow(Net::HTTP).to receive(:new).with(parsed_uri.host, parsed_uri.port).and_return(net_http_object)
|
138
|
-
HTTP.post(uri, {}, {}, options)
|
147
|
+
response = HTTP.post(uri, {}, {}, options)
|
139
148
|
expect(net_http_object.instance_variable_get(:@use_ssl)).to be_truthy
|
149
|
+
expect(response.success?).to eq(true)
|
140
150
|
end
|
141
151
|
end
|
142
152
|
|
@@ -151,7 +161,6 @@ describe ".post" do
|
|
151
161
|
|
152
162
|
it "yields an instance of Net::HTTPResponse" do
|
153
163
|
expect{|b| HTTP.post(uri, &b)}.to yield_with_args(Net::HTTPResponse)
|
154
|
-
HTTP.post(uri){|response|}
|
155
164
|
end
|
156
165
|
end
|
157
166
|
|
@@ -174,8 +183,9 @@ describe ".post" do
|
|
174
183
|
|
175
184
|
it "does a redirect" do
|
176
185
|
expect(HTTP).to receive(:post).once.with(request_uri).and_call_original
|
177
|
-
expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: 0})
|
178
|
-
HTTP.post(request_uri)
|
186
|
+
expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: 0}).and_call_original
|
187
|
+
response = HTTP.post(request_uri)
|
188
|
+
expect(response.success?).to eq(true)
|
179
189
|
end
|
180
190
|
end
|
181
191
|
|
@@ -188,8 +198,9 @@ describe ".post" do
|
|
188
198
|
|
189
199
|
it "does a redirect" do
|
190
200
|
expect(HTTP).to receive(:post).with(request_uri).and_call_original
|
191
|
-
expect(HTTP).to receive(:get).with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: 0})
|
192
|
-
HTTP.post(request_uri)
|
201
|
+
expect(HTTP).to receive(:get).with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: 0}).and_call_original
|
202
|
+
response = HTTP.post(request_uri)
|
203
|
+
expect(response.success?).to eq(true)
|
193
204
|
end
|
194
205
|
end
|
195
206
|
end
|
@@ -213,9 +224,10 @@ describe ".post" do
|
|
213
224
|
end
|
214
225
|
|
215
226
|
it "does a redirect" do
|
216
|
-
expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: 0})
|
227
|
+
expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: 0}).and_call_original
|
217
228
|
expect(HTTP).to receive(:post).once.with(request_uri).and_call_original
|
218
|
-
HTTP.post(request_uri)
|
229
|
+
response = HTTP.post(request_uri)
|
230
|
+
expect(response.success?).to eq(true)
|
219
231
|
end
|
220
232
|
end
|
221
233
|
|
@@ -227,9 +239,43 @@ describe ".post" do
|
|
227
239
|
end
|
228
240
|
|
229
241
|
it "does a redirect" do
|
230
|
-
expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: 0})
|
242
|
+
expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: 0}).and_call_original
|
231
243
|
expect(HTTP).to receive(:post).once.with(request_uri).and_call_original
|
232
|
-
HTTP.post(request_uri)
|
244
|
+
response = HTTP.post(request_uri)
|
245
|
+
expect(response.success?).to eq(true)
|
246
|
+
end
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
context "no_redirect true" do
|
251
|
+
let(:request_uri){'http://example.com/path'}
|
252
|
+
let(:redirect_uri){'http://redirected.com'}
|
253
|
+
|
254
|
+
context "via 301" do
|
255
|
+
before do
|
256
|
+
stub_request(:post, request_uri).
|
257
|
+
with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
|
258
|
+
to_return(status: 301, body: '', headers: {'location' => redirect_uri})
|
259
|
+
end
|
260
|
+
|
261
|
+
it "doesn't redirect" do
|
262
|
+
expect(HTTP).to receive(:post).once.with(request_uri, {}, {}, {no_redirect: true}).and_call_original
|
263
|
+
response = HTTP.post(request_uri, {}, {}, {no_redirect: true})
|
264
|
+
expect(response.redirection?).to eq(true)
|
265
|
+
end
|
266
|
+
end
|
267
|
+
|
268
|
+
context "via 302" do
|
269
|
+
before do
|
270
|
+
stub_request(:post, request_uri).
|
271
|
+
with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
|
272
|
+
to_return(status: 302, body: '', headers: {'location' => redirect_uri})
|
273
|
+
end
|
274
|
+
|
275
|
+
it "doesn't redirect" do
|
276
|
+
expect(HTTP).to receive(:post).once.with(request_uri, {}, {}, {no_redirect: true}).and_call_original
|
277
|
+
response = HTTP.post(request_uri, {}, {}, {no_redirect: true})
|
278
|
+
expect(response.redirection?).to eq(true)
|
233
279
|
end
|
234
280
|
end
|
235
281
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: http.rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.13.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- thoran
|
8
8
|
bindir: bin
|
9
9
|
cert_chain: []
|
10
|
-
date: 2025-
|
10
|
+
date: 2025-03-30 00:00:00.000000000 Z
|
11
11
|
dependencies: []
|
12
12
|
description: HTTP is the simplest HTTP mezzanine library for Ruby. Supply a URI, some
|
13
13
|
optional query arguments, some optional headers, and some Net::HTTP options,
|
@@ -17,16 +17,19 @@ executables: []
|
|
17
17
|
extensions: []
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
|
+
- CHANGELOG.txt
|
20
21
|
- Gemfile
|
21
22
|
- README.md
|
22
23
|
- http.rb.gemspec
|
23
24
|
- lib/HTTP.rb
|
25
|
+
- lib/HTTP/VERSION.rb
|
24
26
|
- lib/HTTP/get.rb
|
25
27
|
- lib/HTTP/post.rb
|
26
28
|
- lib/Hash/x_www_form_urlencode.rb
|
27
29
|
- lib/Net/HTTP/Get/set_headers.rb
|
28
30
|
- lib/Net/HTTP/Post/set_headers.rb
|
29
31
|
- lib/Net/HTTP/set_options.rb
|
32
|
+
- lib/Net/HTTPResponse/StatusPredicates.rb
|
30
33
|
- lib/String/url_encode.rb
|
31
34
|
- lib/URI/Generic/use_sslQ.rb
|
32
35
|
- spec/HTTP/get_spec.rb
|
@@ -49,7 +52,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
49
52
|
- !ruby/object:Gem::Version
|
50
53
|
version: '0'
|
51
54
|
requirements: []
|
52
|
-
rubygems_version: 3.6.
|
55
|
+
rubygems_version: 3.6.6
|
53
56
|
specification_version: 4
|
54
57
|
summary: HTTP made easy.
|
55
58
|
test_files: []
|