http.rb 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1aa084204b33f143035faeb227200b624899c909
4
+ data.tar.gz: fed0d738b0d4482348b74e136388ae96520f9353
5
+ SHA512:
6
+ metadata.gz: 700033cbab49f2cd006e9d35a0c38ad46f90487877e6929957f8339d045d4e9c92f841665839c5617dceb9c71252f1f03837d17840c4611683453f3b3a511ff0
7
+ data.tar.gz: 301840cf8085e1b47ff4a6ec7c099032351099fe9da205a8a6e8a14d3bbde5b2a71b9f2fc5244c7e3687e9424f3e10d4929151d69570bce51e73f7f30318458e
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ group :test do
4
+ gem 'rspec', require: false
5
+ gem 'webmock', require: false
6
+ end
@@ -0,0 +1,99 @@
1
+ # HTTP
2
+
3
+ ## Description
4
+
5
+ This has been a personal library which was begun many years ago, around the middle of 2009 from what I can ascertain, though in various guises it may have been as early as late 2007. Assuming it was the former, it was long enough ago that it actually, sadly (given the diminutive size of this perhaps), predates Faraday by about 6 months (late 2009), http.rb by well over 2 years (late 2011), although it also happens to postdate HTTParty by about a year (mid 2008).
6
+
7
+ Like many others before and after me with their respective libraries, I created it to simplify the heinous interface that is Net::HTTP. At the time of it's original creation I was doing a lot of a webscraping and didn't want a half-dozen line setup to make simple requests. It has stood the test of time, for me personally insofar as the interface remaining simpler than most other similar libraries, though it is also less full featured, but nevertheless for it's tiny size it packs in quite a bit.
8
+
9
+ From inception to early 2013 this was used as a front-end for Mechanize for the purpose of being able to jump out of being a pure web client and to just 'go at' a resource I was after, but still return a Mechanize object so that I could then resume a web client session. Eventually I decided to wrap Net::HTTP and then manually use Nokogiri instead, since I would either use Mechanize largely as is, or I simply wanted to make straight requests without caring about caching or cookies. I may allow HTTP to again return Mechanize objects optionally at some point in future as that had it's benefits, but they weren't great enough for the complications it brought if I remember correctly.
10
+
11
+ Even so, through its various incarnations the goal has basically remained the same: to have a simple way of making HTTP requests in Ruby.
12
+
13
+ I'd never intended to publish this and had intended for it to remain a personal library, but decided to use it for a small project and so as it could be included with ease in a Gemfile it needed to be gemified and well here we are, rightly or wrongly.
14
+
15
+ Perhaps someone will appreciate its relative simplicity, since it is much smaller and the usage simpler than any of the other 'wrapper' libraries mentioned above at least, such that it can be read and comprehended in full in as little as a couple of minutes. It does just enough to do most simple HTTP GET and POST requests as simply as should be.
16
+
17
+
18
+ ## Installation
19
+
20
+ Add this line to your application's Gemfile:
21
+
22
+ gem 'HTTP'
23
+
24
+ And then execute:
25
+
26
+ $ bundle
27
+
28
+ Or install it yourself as:
29
+
30
+ $ gem install HTTP
31
+
32
+
33
+ ## Usage
34
+
35
+ ```Ruby
36
+ # With just a URI
37
+
38
+ HTTP.get('http://example.com')
39
+ HTTP.post('http://example.com') # Admittedly doing a POST without providing form data probably doesn't make much sense.
40
+
41
+ # With arguments only
42
+
43
+ HTTP.get('http://example.com', {a: 1, b: 2})
44
+ HTTP.post('http://example.com', {a: 1, b: 2})
45
+
46
+ # With custom headers only
47
+
48
+ HTTP.get('http://example.com', {}, {'User-Agent'=>'Custom'})
49
+ HTTP.post('http://example.com', {}, {'User-Agent'=>'Custom'})
50
+
51
+ # With options only
52
+
53
+ HTTP.get('http://example.com', {}, {}, {use_ssl: true})
54
+ HTTP.post('http://example.com', {}, {}, {use_ssl: true})
55
+
56
+ # With a block
57
+
58
+ HTTP.get('http://example.com') do |response|
59
+ # Do stuff with a subclass of Net::HTTPResponse here...
60
+ end
61
+ HTTP.post('http://example.com') do |response|
62
+ # Do stuff with a subclass of Net::HTTPResponse here...
63
+ end
64
+
65
+ # With the lot
66
+
67
+ HTTP.get('http://example.com', {a: 1, b: 2}, {'User-Agent'=>'Custom'}, {use_ssl: true}) do |response|
68
+ # Do stuff with a subclass of Net::HTTPResponse here...
69
+ end
70
+ HTTP.post('http://example.com', {a: 1, b: 2}, {'User-Agent'=>'Custom'}, {use_ssl: true}) do |response|
71
+ # Do stuff with a subclass of Net::HTTPResponse here...
72
+ end
73
+
74
+ # Including it in a class
75
+
76
+ class A
77
+ include HTTP
78
+ def a
79
+ get('http://example.com')
80
+ end
81
+ end
82
+
83
+ # Extending a class
84
+
85
+ class A
86
+ extend HTTP
87
+ get('http://example.com')
88
+ end
89
+
90
+ ```
91
+
92
+
93
+ ## Contributing
94
+
95
+ 1. Fork it ( https://github.com/thoran/HTTP/fork )
96
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
97
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
98
+ 4. Push to the branch (`git push origin my-new-feature`)
99
+ 5. Create a new pull request
@@ -0,0 +1,25 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'http.rb'
3
+ s.version = '0.11.0'
4
+ s.date = '2015-03-19'
5
+
6
+ s.summary = "HTTP made easy."
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 \
9
+ Net::HTTP options, and that's it!"
10
+ s.author = 'thoran'
11
+ s.email = 'code@thoran.com'
12
+ s.homepage = "http://github.com/thoran/HTTP"
13
+
14
+ s.files = [
15
+ 'Gemfile',
16
+ 'README.md',
17
+ 'http.rb.gemspec',
18
+ Dir['lib/**/*.rb'],
19
+ Dir['spec/**/*.rb']
20
+ ].flatten
21
+
22
+ s.require_paths = ['lib']
23
+
24
+ s.has_rdoc = false
25
+ end
@@ -0,0 +1,16 @@
1
+ # HTTP.rb
2
+ # HTTP
3
+
4
+ # 20150319
5
+ # 0.11.0
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
+
12
+ lib_dir = File.expand_path(File.join(__FILE__, '..'))
13
+ $LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)
14
+
15
+ require 'HTTP/get'
16
+ require 'HTTP/post'
@@ -0,0 +1,39 @@
1
+ # HTTP/get.rb
2
+ # HTTP.get
3
+
4
+ require 'net/http'
5
+ require 'openssl'
6
+ require 'uri'
7
+
8
+ lib_dir = File.expand_path(File.join(__FILE__, '..', '..'))
9
+ $LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)
10
+
11
+ require 'Hash/x_www_form_urlencode'
12
+ require 'Net/HTTP/Get/set_headers'
13
+ require 'URI/Generic/use_sslQ'
14
+
15
+ module HTTP
16
+
17
+ def get(uri, args = {}, headers = {}, options = {}, &block)
18
+ uri = uri.is_a?(URI) ? uri : URI.parse(uri)
19
+ 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)}
23
+ request_object = Net::HTTP::Get.new(uri.request_uri + '?' + args.x_www_form_urlencode)
24
+ request_object.headers = headers
25
+ request_object.basic_auth(uri.user, uri.password) if uri.user
26
+ response = http.request(request_object)
27
+ if response.code =~ /^3/
28
+ response = get(response.header['location'], {}, {}, options, &block)
29
+ end
30
+ if block_given?
31
+ yield response
32
+ else
33
+ response
34
+ end
35
+ end
36
+
37
+ module_function :get
38
+
39
+ end
@@ -0,0 +1,40 @@
1
+ # HTTP/post.rb
2
+ # HTTP.post
3
+
4
+ require 'net/http'
5
+ require 'openssl'
6
+ require 'uri'
7
+
8
+ lib_dir = File.expand_path(File.join(__FILE__, '..', '..'))
9
+ $LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)
10
+
11
+ require 'HTTP/get'
12
+ require 'Net/HTTP/Post/set_headers'
13
+ require 'URI/Generic/use_sslQ'
14
+
15
+ module HTTP
16
+
17
+ def post(uri, form_data = {}, headers = {}, options = {}, &block)
18
+ uri = uri.is_a?(URI) ? uri : URI.parse(uri)
19
+ 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)}
23
+ request_object = Net::HTTP::Post.new(uri.request_uri)
24
+ request_object.form_data = form_data
25
+ request_object.headers = headers
26
+ request_object.basic_auth(uri.user, uri.password) if uri.user
27
+ response = http.request(request_object)
28
+ if response.code =~ /^3/
29
+ response = get(response.header['location'], {}, {}, options, &block)
30
+ end
31
+ if block_given?
32
+ yield response
33
+ else
34
+ response
35
+ end
36
+ end
37
+
38
+ module_function :post
39
+
40
+ end
@@ -0,0 +1,20 @@
1
+ # Hash/x_www_form_urlencode.rb
2
+ # Hash#x_www_form_urlencode
3
+
4
+ # 20130310
5
+ # 0.0.0
6
+
7
+ # Notes: Extracted from MtGox library.
8
+
9
+ # Todo:
10
+ # 1. I should separate out the functionality for adding '+' characters so as it can do both styles of encoding. That way I could reuse the existing Hash/to_parameter_string method as well...
11
+
12
+ require 'String/url_encode'
13
+
14
+ class Hash
15
+
16
+ def x_www_form_urlencode(joiner = '&')
17
+ inject([]){|a,e| a << "#{e.first.to_s.url_encode.gsub(/ /, '+')}=#{e.last.to_s.url_encode.gsub(/ /, '+')}" unless e.last.nil?; a}.join(joiner)
18
+ end
19
+
20
+ end
@@ -0,0 +1,17 @@
1
+ # Net/HTTP/Get/set_headers.rb
2
+ # Net::HTTP::Get#set_headers
3
+
4
+ # 20130310
5
+ # 0.0.0
6
+
7
+ # Notes:
8
+ # 1. Using the date from when first created in HTTP.get/post as the creation date.
9
+
10
+ class Net::HTTP::Get
11
+
12
+ def set_headers(headers = {})
13
+ headers.each{|k,v| self[k] = v}
14
+ end
15
+ alias_method :headers=, :set_headers
16
+
17
+ end
@@ -0,0 +1,17 @@
1
+ # Net/HTTP/Post/set_headers.rb
2
+ # Net::HTTP::Post#set_headers
3
+
4
+ # 20130309
5
+ # 0.0.0
6
+
7
+ # Notes:
8
+ # 1. Using the date from when first created in HTTP.get/post as the creation date.
9
+
10
+ class Net::HTTP::Post
11
+
12
+ def set_headers(headers = {})
13
+ headers.each{|k,v| self[k] = v}
14
+ end
15
+ alias_method :headers=, :set_headers
16
+
17
+ end
@@ -0,0 +1,18 @@
1
+ # URI/Generic/use_sslQ.rb
2
+ # URI::Generic#use_ssl?
3
+
4
+ # 20141113
5
+ # 0.0.0
6
+
7
+ # Notes:
8
+ # 1. Using the date from when first created in HTTP.get/post as the creation date.
9
+
10
+ module URI
11
+ class Generic
12
+
13
+ def use_ssl?
14
+ scheme == 'https' ? true : false
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,12 @@
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'
@@ -0,0 +1,172 @@
1
+ # spec/HTTP/get_spec.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
+ require 'HTTP/get'
8
+
9
+ WebMock.enable!
10
+ WebMock.disable_net_connect!(allow_localhost: true)
11
+
12
+ describe ".get" do
13
+
14
+ context "with uri-only supplied" do
15
+ before do
16
+ stub_request(:get, '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'}).
18
+ to_return(status: 200, body: '', headers: {})
19
+ end
20
+
21
+ context "uri as a string" do
22
+ let(:uri){'http://example.com/path'}
23
+ let(:parsed_uri){URI.parse(uri)}
24
+ let(:net_http_object){Net::HTTP.new(parsed_uri.host, parsed_uri.port)}
25
+
26
+ it "creates an instance of URI" do
27
+ expect(URI).to receive(:parse).with(uri).and_return(parsed_uri)
28
+ HTTP.get(uri)
29
+ end
30
+
31
+ it "creates a new Net::HTTP object" do
32
+ expect(Net::HTTP).to receive(:new).with(parsed_uri.host, parsed_uri.port).and_return(net_http_object)
33
+ HTTP.get(uri)
34
+ end
35
+ end
36
+
37
+ context "uri as a URI" do
38
+ let(:uri_string){'http://example.com/path'}
39
+ let(:uri){URI.parse(uri_string)}
40
+ let(:net_http_object){Net::HTTP.new(uri.host, uri.port)}
41
+
42
+ it "returns an instance of URI" do
43
+ expect(uri).to eq(uri)
44
+ HTTP.get(uri)
45
+ end
46
+
47
+ it "creates a new Net::HTTP object" do
48
+ expect(Net::HTTP).to receive(:new).with(uri.host, uri.port).and_return(net_http_object)
49
+ HTTP.get(uri)
50
+ end
51
+ end
52
+ end
53
+
54
+ context "with args supplied" do
55
+ let(:uri){'http://example.com/path'}
56
+ let(:parsed_uri){URI.parse(uri)}
57
+ let(:args) do; {a: 1, b: 2}; end
58
+ let(:x_www_form_urlencoded_arguments) do; args.x_www_form_urlencode; end
59
+ let(:get_argument){parsed_uri.request_uri + '?' + x_www_form_urlencoded_arguments}
60
+ let(:request_object){Net::HTTP::Get.new(get_argument)}
61
+
62
+ before do
63
+ stub_request(:get, 'http://example.com/path?a=1&b=2').
64
+ with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
65
+ to_return(status: 200, body: '', headers: {})
66
+ end
67
+
68
+ it "x_www_form_urlencode's the args" do
69
+ expect(args).to receive(:x_www_form_urlencode).and_return(x_www_form_urlencoded_arguments)
70
+ HTTP.get(uri, args)
71
+ end
72
+
73
+ it "creates a new Net::HTTP::Get object" do
74
+ expect(Net::HTTP::Get).to receive(:new).with(get_argument).and_return(request_object)
75
+ HTTP.get(uri, args)
76
+ end
77
+ end
78
+
79
+ context "with headers supplied" do
80
+ let(:uri){'http://example.com/path'}
81
+ let(:parsed_uri){URI.parse(uri)}
82
+ let(:headers) do; {'User-Agent' => 'Rspec'}; end
83
+ let(:get_argument){parsed_uri.request_uri + '?'}
84
+ let(:request_object){Net::HTTP::Get.new(get_argument)}
85
+
86
+ before do
87
+ stub_request(:get, 'http://example.com/path').
88
+ with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Rspec'}).
89
+ to_return(status: 200, body: '', headers: {})
90
+ end
91
+
92
+ it "sets the headers on the request object" do
93
+ allow(Net::HTTP::Get).to receive(:new).with(get_argument).and_return(request_object)
94
+ HTTP.get(uri, {}, headers)
95
+ expect(request_object['User-Agent']).to eq('Rspec')
96
+ end
97
+ end
98
+
99
+ context "with options supplied" do
100
+ let(:uri){'http://example.com/path'}
101
+ let(:parsed_uri){URI.parse(uri)}
102
+ let(:net_http_object){Net::HTTP.new(parsed_uri.host, parsed_uri.port)}
103
+ let(:options) do; {use_ssl: true}; end
104
+
105
+ before do
106
+ stub_request(:get, 'https://example.com:80/path').
107
+ with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
108
+ to_return(status: 200, body: '', headers: {})
109
+ end
110
+
111
+ it "sets the use_ssl option on the Net::HTTP instance" do
112
+ allow(Net::HTTP).to receive(:new).with(parsed_uri.host, parsed_uri.port).and_return(net_http_object)
113
+ HTTP.get(uri, {}, {}, options)
114
+ expect(net_http_object.instance_variable_get(:@use_ssl)).to be_truthy
115
+ end
116
+ end
117
+
118
+ context "with block supplied" do
119
+ let(:uri){'http://example.com/path'}
120
+
121
+ before do
122
+ stub_request(:get, 'http://example.com/path').
123
+ with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
124
+ to_return(status: 200, body: '', headers: {})
125
+ end
126
+
127
+ it "yields an instance of Net::HTTPResponse" do
128
+ expect{|b| HTTP.get(uri, &b)}.to yield_with_args(Net::HTTPResponse)
129
+ HTTP.get(uri){|response|}
130
+ end
131
+ end
132
+
133
+ context "with redirection" do
134
+ let(:request_uri){'http://example.com/path'}
135
+ let(:redirect_uri){'http://redirected.com'}
136
+
137
+ before do
138
+ stub_request(:get, redirect_uri).
139
+ with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
140
+ to_return(status: 200, body: '', headers: {})
141
+ end
142
+
143
+ context "via 301" do
144
+ before do
145
+ stub_request(:get, request_uri).
146
+ 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})
148
+ end
149
+
150
+ it "does a redirect" do
151
+ 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
153
+ HTTP.get(request_uri)
154
+ end
155
+ end
156
+
157
+ context "via 302" do
158
+ before do
159
+ stub_request(:get, request_uri).
160
+ 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})
162
+ end
163
+
164
+ 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
167
+ HTTP.get(request_uri)
168
+ end
169
+ end
170
+ end
171
+
172
+ end
@@ -0,0 +1,173 @@
1
+ # spec/HTTP/post_spec.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
+ require 'HTTP/post'
8
+
9
+ WebMock.enable!
10
+ WebMock.disable_net_connect!(allow_localhost: true)
11
+
12
+ describe ".post" do
13
+
14
+ context "with uri-only supplied" do
15
+ before do
16
+ 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'}).
18
+ to_return(status: 200, body: '', headers: {})
19
+ end
20
+
21
+ context "uri as a string" do
22
+ let(:uri){'http://example.com/path'}
23
+ let(:parsed_uri){URI.parse(uri)}
24
+ let(:net_http_object){Net::HTTP.new(parsed_uri.host, parsed_uri.port)}
25
+
26
+ it "creates an instance of URI" do
27
+ expect(URI).to receive(:parse).with(uri).and_return(parsed_uri)
28
+ HTTP.post(uri)
29
+ end
30
+
31
+ it "creates a new Net::HTTP object" do
32
+ expect(Net::HTTP).to receive(:new).with(parsed_uri.host, parsed_uri.port).and_return(net_http_object)
33
+ HTTP.post(uri)
34
+ end
35
+ end
36
+
37
+ context "uri as a URI" do
38
+ let(:uri_string){'http://example.com/path'}
39
+ let(:uri){URI.parse(uri_string)}
40
+ let(:net_http_object){Net::HTTP.new(uri.host, uri.port)}
41
+
42
+ it "returns an instance of URI" do
43
+ expect(uri).to eq(uri)
44
+ HTTP.post(uri)
45
+ end
46
+
47
+ it "creates a new Net::HTTP object" do
48
+ expect(Net::HTTP).to receive(:new).with(uri.host, uri.port).and_return(net_http_object)
49
+ HTTP.post(uri)
50
+ end
51
+ end
52
+ end
53
+
54
+ context "with form data supplied" do
55
+ let(:uri){'http://example.com/path'}
56
+ 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
59
+ let(:request_uri){parsed_uri.request_uri}
60
+ let(:request_object){Net::HTTP::Post.new(request_uri)}
61
+
62
+ before do
63
+ 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'}).
65
+ to_return(status: 200, body: '', headers: {})
66
+ end
67
+
68
+ it "sets the form data" do
69
+ allow(Net::HTTP::Post).to receive(:new).with(request_uri).and_return(request_object)
70
+ HTTP.post(uri, form_data)
71
+ expect(request_object.body).to eq(encoded_form_data)
72
+ end
73
+
74
+ it "creates a new Net::HTTP::Post object" do
75
+ expect(Net::HTTP::Post).to receive(:new).with(request_uri).and_return(request_object)
76
+ HTTP.post(uri, form_data)
77
+ end
78
+ end
79
+
80
+ context "with headers supplied" do
81
+ let(:uri){'http://example.com/path'}
82
+ let(:parsed_uri){URI.parse(uri)}
83
+ let(:headers) do; {'User-Agent' => 'Rspec'}; end
84
+ let(:request_uri){parsed_uri.request_uri}
85
+ let(:request_object){Net::HTTP::Post.new(request_uri)}
86
+
87
+ before do
88
+ 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'}).
90
+ to_return(status: 200, body: '', headers: {})
91
+ end
92
+
93
+ it "sets the headers on the request object" do
94
+ allow(Net::HTTP::Post).to receive(:new).with(request_uri).and_return(request_object)
95
+ HTTP.post(uri, {}, headers)
96
+ expect(request_object['User-Agent']).to eq('Rspec')
97
+ end
98
+ end
99
+
100
+ context "with options supplied" do
101
+ let(:uri){'http://example.com/path'}
102
+ let(:parsed_uri){URI.parse(uri)}
103
+ let(:net_http_object){Net::HTTP.new(parsed_uri.host, parsed_uri.port)}
104
+ let(:options) do; {use_ssl: true}; end
105
+
106
+ before do
107
+ 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'}).
109
+ to_return(status: 200, body: '', headers: {})
110
+ end
111
+
112
+ it "sets the use_ssl option on the Net::HTTP instance" do
113
+ allow(Net::HTTP).to receive(:new).with(parsed_uri.host, parsed_uri.port).and_return(net_http_object)
114
+ HTTP.post(uri, {}, {}, options)
115
+ expect(net_http_object.instance_variable_get(:@use_ssl)).to be_truthy
116
+ end
117
+ end
118
+
119
+ context "with block supplied" do
120
+ let(:uri){'http://example.com/path'}
121
+
122
+ before do
123
+ 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'}).
125
+ to_return(status: 200, body: '', headers: {})
126
+ end
127
+
128
+ it "yields an instance of Net::HTTPResponse" do
129
+ expect{|b| HTTP.post(uri, &b)}.to yield_with_args(Net::HTTPResponse)
130
+ HTTP.post(uri){|response|}
131
+ end
132
+ end
133
+
134
+ context "with redirection" do
135
+ let(:request_uri){'http://example.com/path'}
136
+ let(:redirect_uri){'http://redirected.com'}
137
+
138
+ before do
139
+ stub_request(:get, redirect_uri).
140
+ with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
141
+ to_return(status: 200, body: '', headers: {})
142
+ end
143
+
144
+ context "via 301" do
145
+ before do
146
+ stub_request(:post, request_uri).
147
+ 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})
149
+ end
150
+
151
+ it "does a redirect" do
152
+ 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
154
+ HTTP.post(request_uri)
155
+ end
156
+ end
157
+
158
+ context "via 302" do
159
+ before do
160
+ stub_request(:post, request_uri).
161
+ 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})
163
+ end
164
+
165
+ it "does a redirect" do
166
+ expect(HTTP).to receive(:post).with(request_uri).and_call_original
167
+ expect(HTTP).to receive(:get).with(redirect_uri, {}, {}, {}).and_call_original
168
+ HTTP.post(request_uri)
169
+ end
170
+ end
171
+ end
172
+
173
+ end
@@ -0,0 +1,7 @@
1
+ # spec/spec_helper.rb
2
+
3
+ lib_dir = File.expand_path(File.join(__FILE__, '..', '..', 'lib'))
4
+ $LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)
5
+
6
+ require 'webmock/rspec'
7
+ WebMock.allow_net_connect!
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: http.rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.11.0
5
+ platform: ruby
6
+ authors:
7
+ - thoran
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-19 00:00:00.000000000 Z
12
+ dependencies: []
13
+ 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!
16
+ email: code@thoran.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - Gemfile
22
+ - README.md
23
+ - http.rb.gemspec
24
+ - lib/HTTP.rb
25
+ - lib/HTTP/get.rb
26
+ - lib/HTTP/post.rb
27
+ - lib/Hash/x_www_form_urlencode.rb
28
+ - lib/Net/HTTP/Get/set_headers.rb
29
+ - lib/Net/HTTP/Post/set_headers.rb
30
+ - lib/URI/Generic/use_sslQ.rb
31
+ - spec/HTTP.rb
32
+ - spec/HTTP/get_spec.rb
33
+ - spec/HTTP/post_spec.rb
34
+ - spec/spec_helper.rb
35
+ homepage: http://github.com/thoran/HTTP
36
+ licenses: []
37
+ metadata: {}
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 2.4.5
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: HTTP made easy.
58
+ test_files: []