http.rb 1.6.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2483f99ec13020c1e1c780b84c7dcddc68437687b1176e7fb88ba228adf084af
4
- data.tar.gz: 710dd8eed5142ac832553595e581bd2597174dd1131fb14a7c51d4e7f17f4992
3
+ metadata.gz: 275a68b16a13f177b907eb555faa75187ed2e03bb68e6d70902cfc5fc2f089d4
4
+ data.tar.gz: 5f6b2930739ff00066c4968c0159e179b4f1cf4e419ce8ea4198fe1af2d2a23a
5
5
  SHA512:
6
- metadata.gz: dbe519101ddbd07038de07ca7bbcb28bbb2b7c53d0f650df86092433271469da3491326dce2d9c3698f78cb381de5a9f48b57054b0b8b8301adc1118e2cead87
7
- data.tar.gz: 87e66da4b78467b289dafc3a280a7f9592af77e39f6878128c3586a0ea7ade24e2589239e8beb35d43c8e52e31ca79626f04d1ee1f31347b55f5ddf41945145a
6
+ metadata.gz: f1ae811bfb12e66bf13461d78b64681095c2f5167d154afd2d93a0dadcf703bb45208fb1cd9dcde634dd836e331cc8db60b314951ba2675672deae477f9f76bd
7
+ data.tar.gz: 21b4a290fe9122f75d64a7d9e3878a3aed962262fdb6e6e434fa61a3b3cd664d2623b8ef36439a45177c120ddfce1693f1a5a7a9f1b76b8a906997e1e6ad85d1
data/CHANGELOG CHANGED
@@ -1,4 +1,27 @@
1
- # CHANGELOG
1
+ # http/CHANGELOG
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
+
16
+ ## 20261018
17
+
18
+ 1.7.0: lib/HTTP.rb --> lib/http.rb
19
+
20
+ 1. lib/HTTP.rb --> lib/http.rb, so that it resolves the same on case-sensitive filesystems.
21
+ 2. + test/loading_test.rb: lib/ carries http.rb, and require 'http.rb' in a fresh process defines HTTP and HTTP::VERSION.
22
+ 3. ~ test/helper.rb: + skip_webmocks_http_rb_adapter()
23
+ 4. ~ HTTP::VERSION: /1.6.0/1.7.0/
24
+
2
25
 
3
26
  ## 20261011
4
27
 
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
@@ -2,5 +2,5 @@
2
2
  # HTTP::VERSION
3
3
 
4
4
  module HTTP
5
- VERSION = '1.6.0'
5
+ VERSION = '1.7.1'
6
6
  end
@@ -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
- # 20260713
5
- # 0.3.0
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([]){|a,e| a << "#{e.first.to_s.urlencode}=#{e.last.to_s.urlencode}" unless e.last.nil?; a}.join(joiner)
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
 
@@ -1,4 +1,4 @@
1
- # HTTP.rb
1
+ # http.rb
2
2
  # HTTP
3
3
 
4
4
  require 'HTTP/METHODS'
@@ -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
data/test/helper.rb CHANGED
@@ -3,16 +3,19 @@
3
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
- # Skip webmock's http_rb adapter: it auto-requires 'http', which collides
7
- # with this gem's lib/HTTP.rb on case-insensitive filesystems and crashes
8
- # while trying to monkey-patch the wrong HTTP module.
6
+ # webmock's http_rb adapter's require 'http' finds this gem's lib/http.rb,
7
+ # takes HTTP::VERSION for the http gem's, and crashes trying to
8
+ # monkey-patch an HTTP::Client which isn't there.
9
+ def skip_webmocks_http_rb_adapter
10
+ gem 'webmock'
9
11
 
10
- gem 'webmock'
12
+ $LOADED_FEATURES << File.join(
13
+ Gem.loaded_specs['webmock'].full_gem_path,
14
+ 'lib/webmock/http_lib_adapters/http_rb_adapter.rb'
15
+ )
16
+ end
11
17
 
12
- $LOADED_FEATURES << File.join(
13
- Gem.loaded_specs['webmock'].full_gem_path,
14
- 'lib/webmock/http_lib_adapters/http_rb_adapter.rb'
15
- )
18
+ skip_webmocks_http_rb_adapter
16
19
 
17
20
  require 'minitest/autorun'
18
21
  require 'minitest/mock'
@@ -0,0 +1,30 @@
1
+ # test/loading_test.rb
2
+
3
+ require 'minitest/autorun'
4
+
5
+ require_relative '../lib/http'
6
+
7
+ # In its own process, since this one has loaded the file directly and so could
8
+ # not tell what requiring the gem by its own name would bring.
9
+ describe 'loading' do
10
+ def ruby(source)
11
+ lib = File.expand_path('../lib', __dir__)
12
+ IO.popen(['ruby', "-I#{lib}", '-e', source], err: [:child, :out]){|io| io.read}.strip
13
+ end
14
+
15
+ # The gem is http.rb, so require 'http.rb' reads lib/http.rb. A capitalised
16
+ # load file answers that only on a case-insensitive filesystem. Dir.entries
17
+ # reports the name as stored, so this asks what a case-sensitive filesystem
18
+ # would ask.
19
+ it "carries the lowercase file the gem's name asks for" do
20
+ _(Dir.entries(File.expand_path('../lib', __dir__))).must_include 'http.rb'
21
+ end
22
+
23
+ it "defines HTTP upon requiring the gem by name" do
24
+ _(ruby('require "http.rb"; print defined?(HTTP)')).must_equal 'constant'
25
+ end
26
+
27
+ it "defines the version too" do
28
+ _(ruby('require "http.rb"; print HTTP::VERSION')).must_equal HTTP::VERSION
29
+ end
30
+ 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.6.0
4
+ version: 1.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - thoran
@@ -93,7 +93,6 @@ files:
93
93
  - README.md
94
94
  - Rakefile
95
95
  - http.rb.gemspec
96
- - lib/HTTP.rb
97
96
  - lib/HTTP/METHODS.rb
98
97
  - lib/HTTP/REDIRECT.rb
99
98
  - lib/HTTP/RETRY.rb
@@ -108,6 +107,7 @@ files:
108
107
  - lib/Thoran/Hash/XWwwFormUrlencode/x_www_form_urlencode.rb
109
108
  - lib/Thoran/String/Urlencode/urlencode.rb
110
109
  - lib/URI/Generic/use_sslQ.rb
110
+ - lib/http.rb
111
111
  - test/HTTP/METHODS_test.rb
112
112
  - test/HTTP/REDIRECT_test.rb
113
113
  - test/HTTP/RETRY_test.rb
@@ -124,8 +124,10 @@ 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
130
+ - test/loading_test.rb
129
131
  homepage: https://github.com/thoran/http
130
132
  licenses:
131
133
  - MIT
@@ -144,7 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
144
146
  - !ruby/object:Gem::Version
145
147
  version: '0'
146
148
  requirements: []
147
- rubygems_version: 4.0.22
149
+ rubygems_version: 4.0.23
148
150
  specification_version: 4
149
151
  summary: HTTP made easy.
150
152
  test_files: []