http.rb 1.7.0 → 1.7.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 +4 -4
- data/CHANGELOG +13 -0
- data/README.md +8 -0
- data/lib/HTTP/VERSION.rb +1 -1
- data/lib/HTTP/request_without_body.rb +1 -1
- data/lib/Thoran/Hash/XWwwFormUrlencode/x_www_form_urlencode.rb +10 -3
- data/test/HTTP/request_without_body_test.rb +48 -0
- data/test/Thoran/Hash/XWwwFormUrlencode/x_www_form_urlencode_test.rb +42 -0
- metadata +3 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 275a68b16a13f177b907eb555faa75187ed2e03bb68e6d70902cfc5fc2f089d4
|
|
4
|
+
data.tar.gz: 5f6b2930739ff00066c4968c0159e179b4f1cf4e419ce8ea4198fe1af2d2a23a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f1ae811bfb12e66bf13461d78b64681095c2f5167d154afd2d93a0dadcf703bb45208fb1cd9dcde634dd836e331cc8db60b314951ba2675672deae477f9f76bd
|
|
7
|
+
data.tar.gz: 21b4a290fe9122f75d64a7d9e3878a3aed962262fdb6e6e434fa61a3b3cd664d2623b8ef36439a45177c120ddfce1693f1a5a7a9f1b76b8a906997e1e6ad85d1
|
data/CHANGELOG
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
# http/CHANGELOG
|
|
2
2
|
|
|
3
|
+
## 20261019
|
|
4
|
+
|
|
5
|
+
1.7.1: Args are joined to a query already in the URI with an ampersand, and an array among them repeats its key.
|
|
6
|
+
|
|
7
|
+
1. ~ HTTP.request_without_body: the args are joined to the URI's query with '&' where it has one. '?' was added regardless, so HTTP.get('http://example.com/p?x=1', {a: 1}) requested '/p?x=1?a=1'.
|
|
8
|
+
2. ~ Thoran::Hash::XWwwFormUrlencode#x_www_form_urlencode: an array value gives a pair for each element, a nil element left out, so {tags: [1, 2]} is 'tags=1&tags=2', as URI.encode_www_form and Net::HTTP#form_data= have it, rather than 'tags=%5B1%2C+2%5D'.
|
|
9
|
+
3. ~ Thoran/Hash/XWwwFormUrlencode/x_www_form_urlencode.rb: /0.3.0/0.3.1/ in the file's own header.
|
|
10
|
+
4. ~ test/HTTP/request_without_body_test.rb: + args with a query in the URI, and an array among the args, through request_without_body and HTTP.get. All fail against 1.7.0.
|
|
11
|
+
5. + test/Thoran/Hash/XWwwFormUrlencode/x_www_form_urlencode_test.rb: pairs, nil values, arrays, an empty array, a nil element, agreement with URI.encode_www_form, the joiner, and the x_www_form_url_encode alias. The array examples fail against 1.7.0.
|
|
12
|
+
6. ~ README.md: + how args meet a query in the URI and an array, under With arguments only.
|
|
13
|
+
7. ~ HTTP::VERSION: /1.7.0/1.7.1/
|
|
14
|
+
|
|
15
|
+
|
|
3
16
|
## 20261018
|
|
4
17
|
|
|
5
18
|
1.7.0: lib/HTTP.rb --> lib/http.rb
|
data/README.md
CHANGED
|
@@ -41,6 +41,14 @@ HTTP.get('http://example.com')
|
|
|
41
41
|
HTTP.get('http://example.com', {a: 1, b: 2})
|
|
42
42
|
HTTP.post('http://example.com', {a: 1, b: 2})
|
|
43
43
|
```
|
|
44
|
+
|
|
45
|
+
For a method without a body, the args are joined to any query the URI already has, and an array repeats its key:
|
|
46
|
+
|
|
47
|
+
```ruby
|
|
48
|
+
HTTP.get('http://example.com/search?q=ruby', {tags: [1, 2]})
|
|
49
|
+
# GET /search?q=ruby&tags=1&tags=2
|
|
50
|
+
```
|
|
51
|
+
|
|
44
52
|
### With JSON data
|
|
45
53
|
|
|
46
54
|
```ruby
|
data/lib/HTTP/VERSION.rb
CHANGED
|
@@ -11,7 +11,7 @@ module HTTP
|
|
|
11
11
|
uri = uri.is_a?(URI) ? uri : URI.parse(uri)
|
|
12
12
|
request_uri = uri.request_uri
|
|
13
13
|
unless args.empty?
|
|
14
|
-
request_uri += '?' + args.x_www_form_urlencode
|
|
14
|
+
request_uri += (uri.query ? '&' : '?') + args.x_www_form_urlencode
|
|
15
15
|
end
|
|
16
16
|
request_object = Net::HTTP.const_get(method.to_s.capitalize).new(request_uri)
|
|
17
17
|
request(uri, request_object, headers, options, &block)
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
# Thoran/Hash/XWwwFormUrlencode/x_www_form_urlencode.rb
|
|
2
2
|
# Thoran::Hash::XWwwFormUrlencode#x_www_form_urlencode
|
|
3
3
|
|
|
4
|
-
#
|
|
5
|
-
# 0.3.
|
|
4
|
+
# 20261019
|
|
5
|
+
# 0.3.1
|
|
6
6
|
|
|
7
7
|
# Changes since 0.2:
|
|
8
8
|
# -/0: (The class name and the snake case name really are consistent now.)
|
|
9
9
|
# 1. /XWwwFormUrlEncode/XWwwFormUrlencode/
|
|
10
10
|
# 2. /url_encode/urlencode/
|
|
11
|
+
# 0/1: An array value repeats its key for each element.
|
|
12
|
+
# 3. ~ x_www_form_urlencode: an array value gives a pair for each of its elements, as URI.encode_www_form does, rather than one pair holding the array's inspect string, '[1, 2]'; a nil element is left out, as a nil value is.
|
|
11
13
|
|
|
12
14
|
require 'Thoran/String/Urlencode/urlencode'
|
|
13
15
|
|
|
@@ -16,7 +18,12 @@ module Thoran
|
|
|
16
18
|
module XWwwFormUrlencode
|
|
17
19
|
|
|
18
20
|
def x_www_form_urlencode(joiner = '&')
|
|
19
|
-
inject([])
|
|
21
|
+
inject([]) do |a,(key, value)|
|
|
22
|
+
(value.is_a?(Array) ? value : [value]).each do |element|
|
|
23
|
+
a << "#{key.to_s.urlencode}=#{element.to_s.urlencode}" unless element.nil?
|
|
24
|
+
end
|
|
25
|
+
a
|
|
26
|
+
end.join(joiner)
|
|
20
27
|
end
|
|
21
28
|
alias_method :x_www_form_url_encode, :x_www_form_urlencode
|
|
22
29
|
|
|
@@ -41,6 +41,54 @@ describe ".request_without_body" do
|
|
|
41
41
|
end
|
|
42
42
|
end
|
|
43
43
|
|
|
44
|
+
describe "with args and a query already in the uri" do
|
|
45
|
+
let(:uri){'http://example.com/path?x=1'}
|
|
46
|
+
|
|
47
|
+
before do
|
|
48
|
+
stub_request(:get, 'http://example.com/path?x=1&a=1').
|
|
49
|
+
to_return(status: 200, body: '', headers: {})
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
it "joins the args to the query with an ampersand" do
|
|
53
|
+
received_arg = nil
|
|
54
|
+
request_object = Net::HTTP::Get.new('/path?x=1&a=1')
|
|
55
|
+
Net::HTTP::Get.stub(:new, ->(arg){received_arg = arg; request_object}) do
|
|
56
|
+
response = HTTP.request_without_body(:get, uri, {a: 1})
|
|
57
|
+
_(received_arg).must_equal('/path?x=1&a=1')
|
|
58
|
+
_(response.success?).must_equal(true)
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
it "is requested so by HTTP.get" do
|
|
63
|
+
response = HTTP.get(uri, {a: 1})
|
|
64
|
+
_(response.success?).must_equal(true)
|
|
65
|
+
assert_requested(:get, 'http://example.com/path?x=1&a=1')
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
describe "with an array among the args" do
|
|
70
|
+
before do
|
|
71
|
+
stub_request(:get, 'http://example.com/path?tags=1&tags=2').
|
|
72
|
+
to_return(status: 200, body: '', headers: {})
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
it "repeats the key for each element" do
|
|
76
|
+
received_arg = nil
|
|
77
|
+
request_object = Net::HTTP::Get.new('/path?tags=1&tags=2')
|
|
78
|
+
Net::HTTP::Get.stub(:new, ->(arg){received_arg = arg; request_object}) do
|
|
79
|
+
response = HTTP.request_without_body(:get, uri, {tags: [1, 2]})
|
|
80
|
+
_(received_arg).must_equal('/path?tags=1&tags=2')
|
|
81
|
+
_(response.success?).must_equal(true)
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
it "is requested so by HTTP.get" do
|
|
86
|
+
response = HTTP.get(uri, {tags: [1, 2]})
|
|
87
|
+
_(response.success?).must_equal(true)
|
|
88
|
+
assert_requested(:get, 'http://example.com/path?tags=1&tags=2')
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
44
92
|
describe "delegation from the named method" do
|
|
45
93
|
it "is reached by HTTP.get with the verb prepended" do
|
|
46
94
|
received_args = nil
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# test/Thoran/Hash/XWwwFormUrlencode/x_www_form_urlencode_test.rb
|
|
2
|
+
|
|
3
|
+
require_relative '../../../helper'
|
|
4
|
+
|
|
5
|
+
describe Thoran::Hash::XWwwFormUrlencode do
|
|
6
|
+
describe "#x_www_form_urlencode" do
|
|
7
|
+
it "joins encoded pairs with an ampersand" do
|
|
8
|
+
_({a: 1, 'b c' => 'd/e'}.x_www_form_urlencode).must_equal('a=1&b+c=d%2Fe')
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it "leaves out a nil value" do
|
|
12
|
+
_({a: 1, b: nil}.x_www_form_urlencode).must_equal('a=1')
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
it "repeats the key for each element of an array" do
|
|
16
|
+
_({tags: [1, 2], a: 3}.x_www_form_urlencode).must_equal('tags=1&tags=2&a=3')
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it "leaves out an empty array" do
|
|
20
|
+
_({tags: [], a: 1}.x_www_form_urlencode).must_equal('a=1')
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it "leaves out a nil element of an array" do
|
|
24
|
+
_({tags: [1, nil, 2]}.x_www_form_urlencode).must_equal('tags=1&tags=2')
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
it "encodes as URI.encode_www_form does" do
|
|
28
|
+
args = {tags: ['x y', 'Ä'], a: '1&2'}
|
|
29
|
+
_(args.x_www_form_urlencode).must_equal(URI.encode_www_form(args))
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
it "joins with the joiner given" do
|
|
33
|
+
_({a: 1, tags: [2, 3]}.x_www_form_urlencode(';')).must_equal('a=1;tags=2;tags=3')
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
describe "#x_www_form_url_encode" do
|
|
38
|
+
it "is an alias of #x_www_form_urlencode" do
|
|
39
|
+
_({tags: [1, 2]}.x_www_form_url_encode).must_equal('tags=1&tags=2')
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: http.rb
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.7.
|
|
4
|
+
version: 1.7.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- thoran
|
|
@@ -124,6 +124,7 @@ files:
|
|
|
124
124
|
- test/HTTP/request_without_body_test.rb
|
|
125
125
|
- test/HTTP/trace_test.rb
|
|
126
126
|
- test/Net/HTTPResponse/StatusPredicates_test.rb
|
|
127
|
+
- test/Thoran/Hash/XWwwFormUrlencode/x_www_form_urlencode_test.rb
|
|
127
128
|
- test/Thoran/String/Urlencode/urlencode_test.rb
|
|
128
129
|
- test/helper.rb
|
|
129
130
|
- test/loading_test.rb
|
|
@@ -145,7 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
145
146
|
- !ruby/object:Gem::Version
|
|
146
147
|
version: '0'
|
|
147
148
|
requirements: []
|
|
148
|
-
rubygems_version: 4.0.
|
|
149
|
+
rubygems_version: 4.0.23
|
|
149
150
|
specification_version: 4
|
|
150
151
|
summary: HTTP made easy.
|
|
151
152
|
test_files: []
|