http.rb 0.12.0 → 0.13.1
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 +5 -5
- data/Gemfile +1 -0
- data/README.md +62 -3
- data/http.rb.gemspec +3 -5
- data/lib/HTTP/get.rb +7 -0
- data/lib/HTTP/post.rb +14 -2
- data/lib/HTTP.rb +0 -10
- data/lib/Net/HTTPResponse/StatusPredicates.rb +33 -0
- data/spec/HTTP/get_spec.rb +58 -17
- data/spec/HTTP/post_spec.rb +93 -21
- metadata +4 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1dde44381215bd0f99a698070bbf0a95ca4178fa8a796ed9de6ca7114d9f4cf1
|
4
|
+
data.tar.gz: 9c2186e4d466a1fa2eedb8ad0c305b1d631e22453c0275ac1309d2b7cafedaa6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aebcba1e71b81d0c8f02b51fa88c1655f752a37a8ef987d16a53e2ba2459d29a17757225703de090c7beb9d5e938e08224350b4dd844943252811c9c8c339228
|
7
|
+
data.tar.gz: c5110f64b0918ffb3e855b834af2177e99c3efcec4b57ab217c0536fa522359755204ad2a21b24ce62ffbe99d71695e56f9cbc9a48e29dae4e46abf81fff47a9
|
data/Gemfile
CHANGED
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
|
-
|
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'})
|
@@ -71,6 +75,56 @@ HTTP.post('http://example.com', {a: 1, b: 2}, {'User-Agent'=>'Custom'}, {use_ssl
|
|
71
75
|
# Do stuff with a subclass of Net::HTTPResponse here...
|
72
76
|
end
|
73
77
|
|
78
|
+
# Preventing redirections
|
79
|
+
|
80
|
+
HTTP.get('http://example.com', {}, {}, {no_redirect: true})
|
81
|
+
# => #<Net::HTTPResponse @code=3xx>
|
82
|
+
|
83
|
+
# Response status predicate methods
|
84
|
+
|
85
|
+
# 1xx
|
86
|
+
response = HTTP.get('http://example.com')
|
87
|
+
response.informational?
|
88
|
+
# => true
|
89
|
+
|
90
|
+
# 2xx
|
91
|
+
response = HTTP.get('http://example.com')
|
92
|
+
response.success?
|
93
|
+
# => true
|
94
|
+
|
95
|
+
# 3xx
|
96
|
+
response = HTTP.get('http://example.com', {}, {}, {no_redirect: true})
|
97
|
+
response.redirection?
|
98
|
+
# => true
|
99
|
+
response.success?
|
100
|
+
# => false
|
101
|
+
|
102
|
+
response = HTTP.get('http://example.com', {}, {}, {no_redirect: false})
|
103
|
+
response.redirection?
|
104
|
+
# => false
|
105
|
+
response.success?
|
106
|
+
# => true
|
107
|
+
|
108
|
+
response = HTTP.get('http://example.com')
|
109
|
+
response.redirection?
|
110
|
+
# => false
|
111
|
+
response.success?
|
112
|
+
# => true
|
113
|
+
|
114
|
+
# 4xx
|
115
|
+
response = HTTP.get('http://example.com')
|
116
|
+
response.client_error?
|
117
|
+
# => true
|
118
|
+
response.error?
|
119
|
+
# => true
|
120
|
+
|
121
|
+
# 5xx
|
122
|
+
response = HTTP.get('http://example.com')
|
123
|
+
response.server_error?
|
124
|
+
# => true
|
125
|
+
response.error?
|
126
|
+
# => true
|
127
|
+
|
74
128
|
# Including it in a class
|
75
129
|
|
76
130
|
class A
|
@@ -89,8 +143,13 @@ end
|
|
89
143
|
|
90
144
|
```
|
91
145
|
|
92
|
-
## Allowed values for the options hash
|
146
|
+
## Allowed values for the options hash
|
147
|
+
#### (These pass through to Net::HTTP, except for `no_redirect`.)
|
148
|
+
|
93
149
|
```Ruby
|
150
|
+
no_redirect
|
151
|
+
# Prevents redirection if a 3xx response is encountered.
|
152
|
+
|
94
153
|
ca_file
|
95
154
|
# Sets path of a CA certification file in PEM format.
|
96
155
|
#
|
data/http.rb.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
|
-
s.name = 'http.rb'
|
3
|
-
s.version = '0.
|
4
|
-
s.date = '
|
2
|
+
s.name = 'http.rb'
|
3
|
+
s.version = '0.13.1'
|
4
|
+
s.date = '2025-03-04'
|
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, \
|
@@ -20,6 +20,4 @@ Gem::Specification.new do |s|
|
|
20
20
|
].flatten
|
21
21
|
|
22
22
|
s.require_paths = ['lib']
|
23
|
-
|
24
|
-
s.has_rdoc = false
|
25
23
|
end
|
data/lib/HTTP/get.rb
CHANGED
@@ -11,6 +11,7 @@ $LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)
|
|
11
11
|
require 'Hash/x_www_form_urlencode'
|
12
12
|
require 'Net/HTTP/set_options'
|
13
13
|
require 'Net/HTTP/Get/set_headers'
|
14
|
+
require 'Net/HTTPResponse/StatusPredicates'
|
14
15
|
require 'URI/Generic/use_sslQ'
|
15
16
|
|
16
17
|
module HTTP
|
@@ -18,6 +19,7 @@ module HTTP
|
|
18
19
|
def get(uri, args = {}, headers = {}, options = {}, &block)
|
19
20
|
uri = uri.is_a?(URI) ? uri : URI.parse(uri)
|
20
21
|
http = Net::HTTP.new(uri.host, uri.port)
|
22
|
+
no_redirect = options.delete(:no_redirect)
|
21
23
|
options[:use_ssl] ||= uri.use_ssl?
|
22
24
|
options[:verify_mode] ||= OpenSSL::SSL::VERIFY_NONE
|
23
25
|
http.options = options
|
@@ -26,6 +28,11 @@ module HTTP
|
|
26
28
|
request_object.basic_auth(uri.user, uri.password) if uri.user
|
27
29
|
response = http.request(request_object)
|
28
30
|
if response.code =~ /^3/
|
31
|
+
if block_given? && no_redirect
|
32
|
+
yield response
|
33
|
+
elsif no_redirect
|
34
|
+
return response
|
35
|
+
end
|
29
36
|
redirect_uri = URI.parse(response.header['location'])
|
30
37
|
if redirect_uri.scheme
|
31
38
|
response = get(response.header['location'], {}, {}, options, &block)
|
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'
|
@@ -11,22 +12,33 @@ $LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)
|
|
11
12
|
require 'HTTP/get'
|
12
13
|
require 'Net/HTTP/set_options'
|
13
14
|
require 'Net/HTTP/Post/set_headers'
|
15
|
+
require 'Net/HTTPResponse/StatusPredicates'
|
14
16
|
require 'URI/Generic/use_sslQ'
|
15
17
|
|
16
18
|
module HTTP
|
17
19
|
|
18
|
-
def post(uri,
|
20
|
+
def post(uri, data = {}, headers = {}, options = {}, &block)
|
19
21
|
uri = uri.is_a?(URI) ? uri : URI.parse(uri)
|
20
22
|
http = Net::HTTP.new(uri.host, uri.port)
|
23
|
+
no_redirect = options.delete(:no_redirect)
|
21
24
|
options[:use_ssl] ||= uri.use_ssl?
|
22
25
|
options[:verify_mode] ||= OpenSSL::SSL::VERIFY_NONE
|
23
26
|
http.options = options
|
24
27
|
request_object = Net::HTTP::Post.new(uri.request_uri)
|
25
|
-
|
28
|
+
if headers['Content-Type'] == 'application/json'
|
29
|
+
request_object.body = JSON.dump(data)
|
30
|
+
else
|
31
|
+
request_object.form_data = data
|
32
|
+
end
|
26
33
|
request_object.headers = headers
|
27
34
|
request_object.basic_auth(uri.user, uri.password) if uri.user
|
28
35
|
response = http.request(request_object)
|
29
36
|
if response.code =~ /^3/
|
37
|
+
if block_given? && no_redirect
|
38
|
+
yield response
|
39
|
+
elsif no_redirect
|
40
|
+
return response
|
41
|
+
end
|
30
42
|
redirect_uri = URI.parse(response.header['location'])
|
31
43
|
if redirect_uri.scheme
|
32
44
|
response = get(response.header['location'], {}, {}, options, &block)
|
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,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
@@ -7,7 +7,6 @@ require 'spec_helper'
|
|
7
7
|
require 'HTTP/get'
|
8
8
|
|
9
9
|
describe ".get" do
|
10
|
-
|
11
10
|
context "with uri-only supplied" do
|
12
11
|
before do
|
13
12
|
stub_request(:get, 'http://example.com/path').
|
@@ -22,12 +21,14 @@ describe ".get" do
|
|
22
21
|
|
23
22
|
it "creates an instance of URI" do
|
24
23
|
expect(URI).to receive(:parse).with(uri).and_return(parsed_uri)
|
25
|
-
HTTP.get(uri)
|
24
|
+
response = HTTP.get(uri)
|
25
|
+
expect(response.success?).to eq(true)
|
26
26
|
end
|
27
27
|
|
28
28
|
it "creates a new Net::HTTP object" do
|
29
29
|
expect(Net::HTTP).to receive(:new).with(parsed_uri.host, parsed_uri.port).and_return(net_http_object)
|
30
|
-
HTTP.get(uri)
|
30
|
+
response = HTTP.get(uri)
|
31
|
+
expect(response.success?).to eq(true)
|
31
32
|
end
|
32
33
|
end
|
33
34
|
|
@@ -43,7 +44,8 @@ describe ".get" do
|
|
43
44
|
|
44
45
|
it "creates a new Net::HTTP object" do
|
45
46
|
expect(Net::HTTP).to receive(:new).with(uri.host, uri.port).and_return(net_http_object)
|
46
|
-
HTTP.get(uri)
|
47
|
+
response = HTTP.get(uri)
|
48
|
+
expect(response.success?).to eq(true)
|
47
49
|
end
|
48
50
|
end
|
49
51
|
end
|
@@ -64,12 +66,14 @@ describe ".get" do
|
|
64
66
|
|
65
67
|
it "x_www_form_urlencode's the args" do
|
66
68
|
expect(args).to receive(:x_www_form_urlencode).and_return(x_www_form_urlencoded_arguments)
|
67
|
-
HTTP.get(uri, args)
|
69
|
+
response = HTTP.get(uri, args)
|
70
|
+
expect(response.success?).to eq(true)
|
68
71
|
end
|
69
72
|
|
70
73
|
it "creates a new Net::HTTP::Get object" do
|
71
74
|
expect(Net::HTTP::Get).to receive(:new).with(get_argument).and_return(request_object)
|
72
|
-
HTTP.get(uri, args)
|
75
|
+
response = HTTP.get(uri, args)
|
76
|
+
expect(response.success?).to eq(true)
|
73
77
|
end
|
74
78
|
end
|
75
79
|
|
@@ -88,8 +92,9 @@ describe ".get" do
|
|
88
92
|
|
89
93
|
it "sets the headers on the request object" do
|
90
94
|
allow(Net::HTTP::Get).to receive(:new).with(get_argument).and_return(request_object)
|
91
|
-
HTTP.get(uri, {}, headers)
|
95
|
+
response = HTTP.get(uri, {}, headers)
|
92
96
|
expect(request_object['User-Agent']).to eq('Rspec')
|
97
|
+
expect(response.success?).to eq(true)
|
93
98
|
end
|
94
99
|
end
|
95
100
|
|
@@ -107,8 +112,9 @@ describe ".get" do
|
|
107
112
|
|
108
113
|
it "sets the use_ssl option on the Net::HTTP instance" do
|
109
114
|
allow(Net::HTTP).to receive(:new).with(parsed_uri.host, parsed_uri.port).and_return(net_http_object)
|
110
|
-
HTTP.get(uri, {}, {}, options)
|
115
|
+
response = HTTP.get(uri, {}, {}, options)
|
111
116
|
expect(net_http_object.instance_variable_get(:@use_ssl)).to be_truthy
|
117
|
+
expect(response.success?).to eq(true)
|
112
118
|
end
|
113
119
|
end
|
114
120
|
|
@@ -123,7 +129,6 @@ describe ".get" do
|
|
123
129
|
|
124
130
|
it "yields an instance of Net::HTTPResponse" do
|
125
131
|
expect{|b| HTTP.get(uri, &b)}.to yield_with_args(Net::HTTPResponse)
|
126
|
-
HTTP.get(uri){|response|}
|
127
132
|
end
|
128
133
|
end
|
129
134
|
|
@@ -146,8 +151,9 @@ describe ".get" do
|
|
146
151
|
|
147
152
|
it "does a redirect" do
|
148
153
|
expect(HTTP).to receive(:get).once.with(request_uri).and_call_original
|
149
|
-
expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: 0})
|
150
|
-
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)
|
151
157
|
end
|
152
158
|
end
|
153
159
|
|
@@ -160,8 +166,9 @@ describe ".get" do
|
|
160
166
|
|
161
167
|
it "does a redirect" do
|
162
168
|
expect(HTTP).to receive(:get).once.with(request_uri).and_call_original
|
163
|
-
expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: 0})
|
164
|
-
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)
|
165
172
|
end
|
166
173
|
end
|
167
174
|
end
|
@@ -186,8 +193,9 @@ describe ".get" do
|
|
186
193
|
|
187
194
|
it "does a redirect" do
|
188
195
|
expect(HTTP).to receive(:get).once.with(request_uri).and_call_original
|
189
|
-
expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: 0})
|
190
|
-
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)
|
191
199
|
end
|
192
200
|
end
|
193
201
|
|
@@ -200,10 +208,43 @@ describe ".get" do
|
|
200
208
|
|
201
209
|
it "does a redirect" do
|
202
210
|
expect(HTTP).to receive(:get).once.with(request_uri).and_call_original
|
203
|
-
expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: 0})
|
204
|
-
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)
|
205
214
|
end
|
206
215
|
end
|
207
216
|
end
|
208
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)
|
247
|
+
end
|
248
|
+
end
|
249
|
+
end
|
209
250
|
end
|
data/spec/HTTP/post_spec.rb
CHANGED
@@ -7,7 +7,6 @@ require 'spec_helper'
|
|
7
7
|
require 'HTTP/post'
|
8
8
|
|
9
9
|
describe ".post" do
|
10
|
-
|
11
10
|
context "with uri-only supplied" do
|
12
11
|
before do
|
13
12
|
stub_request(:post, 'http://example.com/path').
|
@@ -22,12 +21,14 @@ describe ".post" do
|
|
22
21
|
|
23
22
|
it "creates an instance of URI" do
|
24
23
|
expect(URI).to receive(:parse).with(uri).and_return(parsed_uri)
|
25
|
-
HTTP.post(uri)
|
24
|
+
response = HTTP.post(uri)
|
25
|
+
expect(response.success?).to eq(true)
|
26
26
|
end
|
27
27
|
|
28
28
|
it "creates a new Net::HTTP object" do
|
29
29
|
expect(Net::HTTP).to receive(:new).with(parsed_uri.host, parsed_uri.port).and_return(net_http_object)
|
30
|
-
HTTP.post(uri)
|
30
|
+
response = HTTP.post(uri)
|
31
|
+
expect(response.success?).to eq(true)
|
31
32
|
end
|
32
33
|
end
|
33
34
|
|
@@ -38,12 +39,14 @@ describe ".post" do
|
|
38
39
|
|
39
40
|
it "returns an instance of URI" do
|
40
41
|
expect(uri).to eq(uri)
|
41
|
-
HTTP.post(uri)
|
42
|
+
response = HTTP.post(uri)
|
43
|
+
expect(response.success?).to eq(true)
|
42
44
|
end
|
43
45
|
|
44
46
|
it "creates a new Net::HTTP object" do
|
45
47
|
expect(Net::HTTP).to receive(:new).with(uri.host, uri.port).and_return(net_http_object)
|
46
|
-
HTTP.post(uri)
|
48
|
+
response = HTTP.post(uri)
|
49
|
+
expect(response.success?).to eq(true)
|
47
50
|
end
|
48
51
|
end
|
49
52
|
end
|
@@ -51,26 +54,58 @@ describe ".post" do
|
|
51
54
|
context "with form data supplied" do
|
52
55
|
let(:uri){'http://example.com/path'}
|
53
56
|
let(:parsed_uri){URI.parse(uri)}
|
54
|
-
let(:
|
55
|
-
let(:
|
57
|
+
let(:args) do; {a: 1, b: 2}; end
|
58
|
+
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
|
59
|
+
let(:encoded_form_data){args.x_www_form_urlencode}
|
56
60
|
let(:request_uri){parsed_uri.request_uri}
|
57
61
|
let(:request_object){Net::HTTP::Post.new(request_uri)}
|
58
62
|
|
59
63
|
before do
|
60
64
|
stub_request(:post, "http://example.com/path").
|
61
|
-
with(body:
|
65
|
+
with(body: encoded_form_data, headers: headers).
|
62
66
|
to_return(status: 200, body: '', headers: {})
|
63
67
|
end
|
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,
|
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,
|
78
|
+
response = HTTP.post(uri, args, headers)
|
79
|
+
expect(response.success?).to eq(true)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
context "with JSON data supplied" do
|
84
|
+
let(:uri){'http://example.com/path'}
|
85
|
+
let(:parsed_uri){URI.parse(uri)}
|
86
|
+
let(:args) do; {a: 1, b: 2}; end
|
87
|
+
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
|
88
|
+
let(:json_data){JSON.dump(args)}
|
89
|
+
let(:request_uri){parsed_uri.request_uri}
|
90
|
+
let(:request_object){Net::HTTP::Post.new(request_uri)}
|
91
|
+
|
92
|
+
before do
|
93
|
+
stub_request(:post, "http://example.com/path").
|
94
|
+
with(body: json_data, headers: headers).
|
95
|
+
to_return(status: 200, body: '', headers: {})
|
96
|
+
end
|
97
|
+
|
98
|
+
it "sets the body" do
|
99
|
+
allow(Net::HTTP::Post).to receive(:new).with(request_uri).and_return(request_object)
|
100
|
+
response = HTTP.post(uri, args, headers)
|
101
|
+
expect(request_object.body).to eq(json_data)
|
102
|
+
expect(response.success?).to eq(true)
|
103
|
+
end
|
104
|
+
|
105
|
+
it "creates a new Net::HTTP::Post object" do
|
106
|
+
expect(Net::HTTP::Post).to receive(:new).with(request_uri).and_return(request_object)
|
107
|
+
response = HTTP.post(uri, args, headers)
|
108
|
+
expect(response.success?).to eq(true)
|
74
109
|
end
|
75
110
|
end
|
76
111
|
|
@@ -89,8 +124,9 @@ describe ".post" do
|
|
89
124
|
|
90
125
|
it "sets the headers on the request object" do
|
91
126
|
allow(Net::HTTP::Post).to receive(:new).with(request_uri).and_return(request_object)
|
92
|
-
HTTP.post(uri, {}, headers)
|
127
|
+
response = HTTP.post(uri, {}, headers)
|
93
128
|
expect(request_object['User-Agent']).to eq('Rspec')
|
129
|
+
expect(response.success?).to eq(true)
|
94
130
|
end
|
95
131
|
end
|
96
132
|
|
@@ -108,8 +144,9 @@ describe ".post" do
|
|
108
144
|
|
109
145
|
it "sets the use_ssl option on the Net::HTTP instance" do
|
110
146
|
allow(Net::HTTP).to receive(:new).with(parsed_uri.host, parsed_uri.port).and_return(net_http_object)
|
111
|
-
HTTP.post(uri, {}, {}, options)
|
147
|
+
response = HTTP.post(uri, {}, {}, options)
|
112
148
|
expect(net_http_object.instance_variable_get(:@use_ssl)).to be_truthy
|
149
|
+
expect(response.success?).to eq(true)
|
113
150
|
end
|
114
151
|
end
|
115
152
|
|
@@ -124,7 +161,6 @@ describe ".post" do
|
|
124
161
|
|
125
162
|
it "yields an instance of Net::HTTPResponse" do
|
126
163
|
expect{|b| HTTP.post(uri, &b)}.to yield_with_args(Net::HTTPResponse)
|
127
|
-
HTTP.post(uri){|response|}
|
128
164
|
end
|
129
165
|
end
|
130
166
|
|
@@ -147,8 +183,9 @@ describe ".post" do
|
|
147
183
|
|
148
184
|
it "does a redirect" do
|
149
185
|
expect(HTTP).to receive(:post).once.with(request_uri).and_call_original
|
150
|
-
expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: 0})
|
151
|
-
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)
|
152
189
|
end
|
153
190
|
end
|
154
191
|
|
@@ -161,8 +198,9 @@ describe ".post" do
|
|
161
198
|
|
162
199
|
it "does a redirect" do
|
163
200
|
expect(HTTP).to receive(:post).with(request_uri).and_call_original
|
164
|
-
expect(HTTP).to receive(:get).with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: 0})
|
165
|
-
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)
|
166
204
|
end
|
167
205
|
end
|
168
206
|
end
|
@@ -186,9 +224,10 @@ describe ".post" do
|
|
186
224
|
end
|
187
225
|
|
188
226
|
it "does a redirect" do
|
189
|
-
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
|
190
228
|
expect(HTTP).to receive(:post).once.with(request_uri).and_call_original
|
191
|
-
HTTP.post(request_uri)
|
229
|
+
response = HTTP.post(request_uri)
|
230
|
+
expect(response.success?).to eq(true)
|
192
231
|
end
|
193
232
|
end
|
194
233
|
|
@@ -200,11 +239,44 @@ describe ".post" do
|
|
200
239
|
end
|
201
240
|
|
202
241
|
it "does a redirect" do
|
203
|
-
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
|
204
243
|
expect(HTTP).to receive(:post).once.with(request_uri).and_call_original
|
205
|
-
HTTP.post(request_uri)
|
244
|
+
response = HTTP.post(request_uri)
|
245
|
+
expect(response.success?).to eq(true)
|
206
246
|
end
|
207
247
|
end
|
208
248
|
end
|
209
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)
|
279
|
+
end
|
280
|
+
end
|
281
|
+
end
|
210
282
|
end
|
metadata
CHANGED
@@ -1,14 +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.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- thoran
|
8
|
-
autorequire:
|
9
8
|
bindir: bin
|
10
9
|
cert_chain: []
|
11
|
-
date:
|
10
|
+
date: 2025-03-04 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
13
|
optional query arguments, some optional headers, and some Net::HTTP options,
|
@@ -28,6 +27,7 @@ files:
|
|
28
27
|
- lib/Net/HTTP/Get/set_headers.rb
|
29
28
|
- lib/Net/HTTP/Post/set_headers.rb
|
30
29
|
- lib/Net/HTTP/set_options.rb
|
30
|
+
- lib/Net/HTTPResponse/StatusPredicates.rb
|
31
31
|
- lib/String/url_encode.rb
|
32
32
|
- lib/URI/Generic/use_sslQ.rb
|
33
33
|
- spec/HTTP/get_spec.rb
|
@@ -36,7 +36,6 @@ files:
|
|
36
36
|
homepage: http://github.com/thoran/HTTP
|
37
37
|
licenses: []
|
38
38
|
metadata: {}
|
39
|
-
post_install_message:
|
40
39
|
rdoc_options: []
|
41
40
|
require_paths:
|
42
41
|
- lib
|
@@ -51,9 +50,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
51
50
|
- !ruby/object:Gem::Version
|
52
51
|
version: '0'
|
53
52
|
requirements: []
|
54
|
-
|
55
|
-
rubygems_version: 2.5.2
|
56
|
-
signing_key:
|
53
|
+
rubygems_version: 3.6.5
|
57
54
|
specification_version: 4
|
58
55
|
summary: HTTP made easy.
|
59
56
|
test_files: []
|