http-client 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG +6 -0
- data/README.md +13 -5
- data/lib/http-client.rb +1 -1
- data/lib/http/client.rb +5 -5
- data/test/helper.rb +2 -2
- data/test/server.rb +54 -0
- data/test/test_request.rb +77 -4
- metadata +32 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8833622cfb8dd5390e0daae8a669265432903ba5
|
4
|
+
data.tar.gz: 76efa57c394ef3b0ea0073426161c8cb9e7f04db
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 28020a9dd60ca5f2415e4dcb3b44005550147afea1cf39232c962c22c06210b924ba3366b32b2f88137696499817b7abda4c5f1adff55ebaad07f6178df0687c
|
7
|
+
data.tar.gz: 6c2cc5c1e1e9e92e994e4e7164b98771d1eed5f3ceb019a01fc6ece374e7e26c8c759e3e7d72214550ea7985e07528dfd57a99a8b7940aacebd453db6adf112a
|
data/CHANGELOG
CHANGED
data/README.md
CHANGED
@@ -16,7 +16,11 @@ res = HTTP::Client::Request.new(:get, "http://www.example.org", max_redirects: 2
|
|
16
16
|
|
17
17
|
# save a few keystrokes.
|
18
18
|
res = HTTP::Client.get("http://www.example.org/", max_redirects: 2)
|
19
|
-
res = HTTP::Client.post(
|
19
|
+
res = HTTP::Client.post(
|
20
|
+
"http://www.example.org/",
|
21
|
+
files: {pic: "kittens.jpg"},
|
22
|
+
query: {title: "the usual suspects"}
|
23
|
+
)
|
20
24
|
```
|
21
25
|
|
22
26
|
## API
|
@@ -36,20 +40,20 @@ HTTP::Client::Response
|
|
36
40
|
|
37
41
|
### Request parameters
|
38
42
|
|
39
|
-
Required
|
43
|
+
#### Required
|
40
44
|
|
41
45
|
| Name | Type | Description |
|
42
46
|
|------|------|-------------|
|
43
47
|
| verb | Symbol | HTTP verb, one of :get, :head, :put, :post, :delete, :options, :trace. |
|
44
|
-
| uri | String | Remote URI |
|
48
|
+
| uri | String or URI | Remote URI |
|
45
49
|
|
46
|
-
Optional arguments hash
|
50
|
+
#### Optional arguments hash
|
47
51
|
|
48
52
|
| Name | Type | Description |
|
49
53
|
|------|------|-------------|
|
50
54
|
| headers | Hash | Net::HTTP headers, in key-value pairs. |
|
51
55
|
| query | Hash | Net::HTTP query-string in key-value pairs. |
|
52
|
-
| files | Hash | Multi-part file uploads, in key-value pairs of {name => path_to_file} or {name => File} |
|
56
|
+
| files | Hash | Multi-part file uploads, in key-value pairs of {name => path_to_file} or {name => [File]} |
|
53
57
|
| body | String | Request body. |
|
54
58
|
| auth | Hash | Basic-Auth hash. {username: "...", password: "..."} |
|
55
59
|
| timeout | Integer | Fixed timeout for connection, read and ssl handshake in seconds. |
|
@@ -60,6 +64,10 @@ Optional arguments hash:
|
|
60
64
|
| ssl_verify | Integer | OpenSSL verification, HTTP::Client::SSL_VERIFY_PEER or HTTP::Client::SSL_VERIFY_NONE, defaults to SSL_VERIFY_PEER. |
|
61
65
|
| jar | HTTP::CookieJar | Optional cookie jar to use. Relies on HTTP::CookieJar from http-cookie gem. |
|
62
66
|
|
67
|
+
## TODO
|
68
|
+
|
69
|
+
Extensive testing :/
|
70
|
+
|
63
71
|
## License
|
64
72
|
|
65
73
|
MIT
|
data/lib/http-client.rb
CHANGED
data/lib/http/client.rb
CHANGED
@@ -7,7 +7,7 @@ require 'http-cookie'
|
|
7
7
|
|
8
8
|
module HTTP
|
9
9
|
module Client
|
10
|
-
VERSION = '0.1.
|
10
|
+
VERSION = '0.1.1'
|
11
11
|
|
12
12
|
GET = Net::HTTP::Get
|
13
13
|
HEAD = Net::HTTP::Head
|
@@ -32,7 +32,7 @@ module HTTP
|
|
32
32
|
# Create a new HTTP Client Request.
|
33
33
|
#
|
34
34
|
# @param verb [Symbol] HTTP verb, one of :get, :head, :put, :post, :delete, :options, :trace.
|
35
|
-
# @param uri [String]
|
35
|
+
# @param uri [String] or [URI] Remote URI.
|
36
36
|
# @param headers [Hash] Net::HTTP headers, in key-value pairs.
|
37
37
|
# @param query [Hash] Net::HTTP query-string in key-value pairs.
|
38
38
|
# @param files [Hash] Multi-part file uploads, in key-value pairs of {name => path_to_file} or {name => File}
|
@@ -57,7 +57,7 @@ module HTTP
|
|
57
57
|
setup_request_delegate! verb, args
|
58
58
|
|
59
59
|
if body = args[:body]
|
60
|
-
raise ArgumentError, "#{verb} cannot have body" unless
|
60
|
+
raise ArgumentError, "#{verb} cannot have body" unless @delegate.class.const_get(:REQUEST_HAS_BODY)
|
61
61
|
@delegate.body = body
|
62
62
|
end
|
63
63
|
|
@@ -115,7 +115,7 @@ module HTTP
|
|
115
115
|
|
116
116
|
private
|
117
117
|
def parse_uri! uri
|
118
|
-
@uri = URI.parse(uri)
|
118
|
+
@uri = uri.kind_of?(URI) ? uri : URI.parse(uri)
|
119
119
|
case @uri
|
120
120
|
when URI::HTTP, URI::HTTPS
|
121
121
|
# ok
|
@@ -209,7 +209,7 @@ module HTTP
|
|
209
209
|
end
|
210
210
|
|
211
211
|
def headers
|
212
|
-
@headers ||= @response.each_header.entries
|
212
|
+
@headers ||= Hash[@response.each_header.entries]
|
213
213
|
end
|
214
214
|
|
215
215
|
def body
|
data/test/helper.rb
CHANGED
data/test/server.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'webrick'
|
2
|
+
require 'stringio'
|
3
|
+
require 'socket'
|
4
|
+
require 'uri'
|
5
|
+
|
6
|
+
class TestServer
|
7
|
+
attr_reader :port
|
8
|
+
|
9
|
+
def initialize app
|
10
|
+
@app = app
|
11
|
+
@port = find_port
|
12
|
+
end
|
13
|
+
|
14
|
+
def run path = '/'
|
15
|
+
logfile = StringIO.new
|
16
|
+
logger = WEBrick::Log.new logfile
|
17
|
+
accesslog = [[logfile, WEBrick::AccessLog::COMBINED_LOG_FORMAT]]
|
18
|
+
|
19
|
+
@server = WEBrick::HTTPServer.new Port: @port, Logger: logger, AccessLog: accesslog
|
20
|
+
@server.mount_proc(path) do |req, res|
|
21
|
+
code, headers, body = @app.call(req)
|
22
|
+
res.status = code
|
23
|
+
headers.each do |k, v|
|
24
|
+
res[k] = v
|
25
|
+
end
|
26
|
+
res.body = [body].flatten.join
|
27
|
+
end
|
28
|
+
|
29
|
+
Thread.abort_on_exception = true
|
30
|
+
@thread = Thread.new {@server.start}
|
31
|
+
end
|
32
|
+
|
33
|
+
def stop
|
34
|
+
@thread && @thread.kill
|
35
|
+
end
|
36
|
+
|
37
|
+
def root
|
38
|
+
URI.parse("http://127.0.0.1:#{port}/")
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
def find_port
|
43
|
+
@tcp = nil
|
44
|
+
for port in 32768..65535
|
45
|
+
@tcp = TCPServer.new('127.0.0.1', port) rescue nil
|
46
|
+
break if @tcp
|
47
|
+
end
|
48
|
+
|
49
|
+
@tcp or raise RuntimeError 'Unable to find a local TCP port to bind.'
|
50
|
+
port = @tcp.addr[1]
|
51
|
+
@tcp.close
|
52
|
+
port
|
53
|
+
end
|
54
|
+
end
|
data/test/test_request.rb
CHANGED
@@ -19,16 +19,89 @@ describe 'HTTP Client Request' do
|
|
19
19
|
files: {test1: __FILE__, test2: __FILE__},
|
20
20
|
max_redirects: 2,
|
21
21
|
timeout: 10,
|
22
|
-
ssl_verify: HTTP::Client::SSL_VERIFY_NONE
|
22
|
+
ssl_verify: HTTP::Client::SSL_VERIFY_NONE,
|
23
|
+
body: 'Hello world!'
|
23
24
|
)
|
24
25
|
end
|
25
26
|
|
26
|
-
# TODO: mock http endpoint
|
27
27
|
it 'executes a request and returns reponse' do
|
28
|
-
|
28
|
+
app = proc {|req| [200, {}, "Hello World!"]}
|
29
|
+
server = TestServer.new(app)
|
30
|
+
server.run
|
31
|
+
|
32
|
+
response = HTTP::Client.get(server.root)
|
33
|
+
server.stop
|
34
|
+
assert response
|
35
|
+
assert_kind_of Hash, response.headers
|
29
36
|
end
|
30
37
|
|
31
38
|
it 'raises timeout errors' do
|
32
|
-
|
39
|
+
app = proc {|req| sleep }
|
40
|
+
server = TestServer.new(app)
|
41
|
+
server.run
|
42
|
+
assert_raises(Net::ReadTimeout) {HTTP::Client.get(server.root, timeout: 0.2)}
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'handles redirects' do
|
46
|
+
app = proc do |req|
|
47
|
+
case req.path
|
48
|
+
when '/redirect2'
|
49
|
+
[200, {}, "Hello World!"]
|
50
|
+
when '/redirect1'
|
51
|
+
[302, {'Location' => '/redirect2'}, "Moved!"]
|
52
|
+
when '/'
|
53
|
+
[302, {'Location' => '/redirect1'}, "Moved!"]
|
54
|
+
end
|
55
|
+
end
|
56
|
+
server = TestServer.new(app)
|
57
|
+
server.run
|
58
|
+
|
59
|
+
response = HTTP::Client.get(server.root)
|
60
|
+
assert_equal 302, response.code
|
61
|
+
assert_equal %Q{#{server.root + "/redirect1"}}, response.headers['location']
|
62
|
+
|
63
|
+
response = HTTP::Client.get(server.root, max_redirects: 1)
|
64
|
+
assert_equal 302, response.code
|
65
|
+
assert_equal %Q{#{server.root + "/redirect1"}}, response.last_effective_uri.to_s
|
66
|
+
assert_equal %Q{#{server.root + "/redirect2"}}, response.headers['location']
|
67
|
+
|
68
|
+
response = HTTP::Client.get(server.root, max_redirects: 2)
|
69
|
+
assert_equal 200, response.code
|
70
|
+
assert_equal %Q{#{server.root + "/redirect2"}}, response.last_effective_uri.to_s
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'posts url encoded form' do
|
74
|
+
qs = {}
|
75
|
+
app = proc do |req|
|
76
|
+
qs = req.query
|
77
|
+
[200, {}, "OK!"]
|
78
|
+
end
|
79
|
+
server = TestServer.new(app)
|
80
|
+
server.run
|
81
|
+
|
82
|
+
response = HTTP::Client.post(server.root, query: {test1: "test2"})
|
83
|
+
server.stop
|
84
|
+
assert_equal 200, response.code
|
85
|
+
assert_equal "test2", qs["test1"]
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'posts multi-part encoded data' do
|
89
|
+
qs = {}
|
90
|
+
app = proc do |req|
|
91
|
+
qs = req.query
|
92
|
+
[200, {}, "OK!"]
|
93
|
+
end
|
94
|
+
server = TestServer.new(app)
|
95
|
+
server.run
|
96
|
+
|
97
|
+
response = HTTP::Client.post(server.root, files: {this: __FILE__}, query: {test1: "test2"})
|
98
|
+
server.stop
|
99
|
+
|
100
|
+
assert_equal 200, response.code
|
101
|
+
assert_equal "test2", qs["test1"]
|
102
|
+
|
103
|
+
data = IO.read(__FILE__)
|
104
|
+
assert_equal data, qs["this"]
|
105
|
+
assert_equal File.basename(__FILE__), qs["this"].filename # people should stop overloading / extending String!
|
33
106
|
end
|
34
107
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: http-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bharanee Rathna
|
@@ -16,14 +16,20 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '3.
|
19
|
+
version: '3.0'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '3.0'
|
20
23
|
type: :runtime
|
21
24
|
prerelease: false
|
22
25
|
version_requirements: !ruby/object:Gem::Requirement
|
23
26
|
requirements:
|
24
27
|
- - "~>"
|
25
28
|
- !ruby/object:Gem::Version
|
26
|
-
version: '3.
|
29
|
+
version: '3.0'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '3.0'
|
27
33
|
- !ruby/object:Gem::Dependency
|
28
34
|
name: http-cookie
|
29
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -31,6 +37,9 @@ dependencies:
|
|
31
37
|
- - "~>"
|
32
38
|
- !ruby/object:Gem::Version
|
33
39
|
version: '1.0'
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '1.0'
|
34
43
|
type: :runtime
|
35
44
|
prerelease: false
|
36
45
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -38,34 +47,49 @@ dependencies:
|
|
38
47
|
- - "~>"
|
39
48
|
- !ruby/object:Gem::Version
|
40
49
|
version: '1.0'
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '1.0'
|
41
53
|
- !ruby/object:Gem::Dependency
|
42
54
|
name: rake
|
43
55
|
requirement: !ruby/object:Gem::Requirement
|
44
56
|
requirements:
|
45
57
|
- - "~>"
|
46
58
|
- !ruby/object:Gem::Version
|
47
|
-
version: '11.
|
59
|
+
version: '11.0'
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '11.0'
|
48
63
|
type: :development
|
49
64
|
prerelease: false
|
50
65
|
version_requirements: !ruby/object:Gem::Requirement
|
51
66
|
requirements:
|
52
67
|
- - "~>"
|
53
68
|
- !ruby/object:Gem::Version
|
54
|
-
version: '11.
|
69
|
+
version: '11.0'
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '11.0'
|
55
73
|
- !ruby/object:Gem::Dependency
|
56
74
|
name: minitest-reporters
|
57
75
|
requirement: !ruby/object:Gem::Requirement
|
58
76
|
requirements:
|
59
77
|
- - "~>"
|
60
78
|
- !ruby/object:Gem::Version
|
61
|
-
version: '1.
|
79
|
+
version: '1.0'
|
80
|
+
- - ">"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.0'
|
62
83
|
type: :development
|
63
84
|
prerelease: false
|
64
85
|
version_requirements: !ruby/object:Gem::Requirement
|
65
86
|
requirements:
|
66
87
|
- - "~>"
|
67
88
|
- !ruby/object:Gem::Version
|
68
|
-
version: '1.
|
89
|
+
version: '1.0'
|
90
|
+
- - ">"
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '1.0'
|
69
93
|
description: Light weight wrapper around Net::HTTP
|
70
94
|
email:
|
71
95
|
- deepfryed@gmail.com
|
@@ -78,6 +102,7 @@ files:
|
|
78
102
|
- lib/http-client.rb
|
79
103
|
- lib/http/client.rb
|
80
104
|
- test/helper.rb
|
105
|
+
- test/server.rb
|
81
106
|
- test/test_request.rb
|
82
107
|
homepage: http://github.com/deepfryed/http-client
|
83
108
|
licenses:
|