simple-http 0.1.0 → 0.1.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/lib/simple/http/assertions.rb +16 -0
- data/lib/simple/http/version.rb +1 -1
- data/lib/simple/http.rb +18 -0
- data/test/assertions_test.rb +20 -0
- data/test/basic_auth_test.rb +26 -0
- data/test/redirection_test.rb +18 -0
- data/test/simple_http_test.rb +7 -4
- data/test/test_helper.rb +12 -1
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6297e0992a3d447dd6803a3692d48a737e63420c
|
4
|
+
data.tar.gz: c33f894babece51848969ccccf3a3f6425231cdf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ad178342ff251fcc2702f09432c96406b37095cec930cf7917f33b97be6f08a214f7a5ce77b10acbdc7385369d20699ad3c8745ee237df5129904d396453f882
|
7
|
+
data.tar.gz: 8586f61a9529b83894a76e13020ff7e06840fd70b103676fb76a9f8461eeb524f2a16ea33de44535927325a8456bc6f8b683daf9c6d69b1cee8d4c09cb1e898a
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# Author:: radiospiel (mailto:eno@radiospiel.org)
|
2
|
+
# Copyright:: Copyright (c) 2011-2015 radiospiel
|
3
|
+
# License:: Distributes under the terms of the Modified BSD License, see LICENSE.BSD for details.
|
4
|
+
|
5
|
+
module Simple::HTTP::Assertions
|
6
|
+
def assert_http_error(expected_status, &block)
|
7
|
+
http_error = assert_raises(Simple::HTTP::Error, &block)
|
8
|
+
assert_equal(expected_status, http_error.code)
|
9
|
+
end
|
10
|
+
|
11
|
+
def assert_http_redirects_to(expected_location, &block)
|
12
|
+
http_error = assert_raises(Simple::HTTP::Error, &block)
|
13
|
+
assert_includes([301, 302], http_error.code)
|
14
|
+
assert_equal(expected_location, http_error.response["Location"])
|
15
|
+
end
|
16
|
+
end
|
data/lib/simple/http/version.rb
CHANGED
data/lib/simple/http.rb
CHANGED
@@ -36,6 +36,11 @@ class Simple::HTTP
|
|
36
36
|
# When set, appends this to all request URLs
|
37
37
|
attr :default_params, true
|
38
38
|
|
39
|
+
#
|
40
|
+
# When set, sets Authorization headers to all requests. Must be an
|
41
|
+
# array [ username, password ].
|
42
|
+
attr :basic_auth, true
|
43
|
+
|
39
44
|
def initialize
|
40
45
|
self.follows_redirections = true
|
41
46
|
end
|
@@ -89,7 +94,15 @@ class Simple::HTTP
|
|
89
94
|
end
|
90
95
|
|
91
96
|
uri = URI.parse(url)
|
97
|
+
unless uri.is_a?(URI::HTTP)
|
98
|
+
raise ArgumentError, "Invalid URL: #{url}"
|
99
|
+
end
|
100
|
+
|
92
101
|
http = Net::HTTP.new(uri.host, uri.port)
|
102
|
+
if uri.scheme == "https"
|
103
|
+
http.use_ssl = true
|
104
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
105
|
+
end
|
93
106
|
|
94
107
|
#
|
95
108
|
# build request
|
@@ -146,6 +159,11 @@ class Simple::HTTP
|
|
146
159
|
klass = REQUEST_CLASSES.fetch(method)
|
147
160
|
request = klass.new(uri.request_uri)
|
148
161
|
|
162
|
+
if basic_auth
|
163
|
+
username, password = *basic_auth
|
164
|
+
request.basic_auth(username, password)
|
165
|
+
end
|
166
|
+
|
149
167
|
# set request headers
|
150
168
|
# unless headers && !headers.empty?
|
151
169
|
# # TODO: set headers
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# Author:: radiospiel (mailto:eno@radiospiel.org)
|
2
|
+
# Copyright:: Copyright (c) 2011-2015 radiospiel
|
3
|
+
# License:: Distributes under the terms of the Modified BSD License, see LICENSE.BSD for details.
|
4
|
+
|
5
|
+
require_relative 'test_helper'
|
6
|
+
|
7
|
+
class SimpleHttpTest < Simple::HTTP::TestCase
|
8
|
+
def test_assert_http_error
|
9
|
+
assert_http_error(456) do
|
10
|
+
http.get "http://eu.httpbin.org/status/456"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_assert_http_redirects_to
|
15
|
+
http.follows_redirections = false
|
16
|
+
assert_http_redirects_to "http://eu.httpbin.org" do
|
17
|
+
http.get "http://eu.httpbin.org/redirect-to?url=http://eu.httpbin.org"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# Author:: radiospiel (mailto:eno@radiospiel.org)
|
2
|
+
# Copyright:: Copyright (c) 2011-2015 radiospiel
|
3
|
+
# License:: Distributes under the terms of the Modified BSD License, see LICENSE.BSD for details.
|
4
|
+
|
5
|
+
require_relative 'test_helper'
|
6
|
+
|
7
|
+
class SimpleHttpTest < Simple::HTTP::TestCase
|
8
|
+
def test_basic_auth
|
9
|
+
http.basic_auth = [ "user", "passwd" ]
|
10
|
+
http.get "http://eu.httpbin.org/basic-auth/user/passwd"
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_basic_auth_invalid
|
14
|
+
http.basic_auth = [ "wronguser", "passwd" ]
|
15
|
+
assert_http_error 401 do
|
16
|
+
http.get "http://eu.httpbin.org/basic-auth/user/passwd"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_basic_auth_missing
|
21
|
+
http.basic_auth = [ "wronguser", "passwd" ]
|
22
|
+
assert_http_error 401 do
|
23
|
+
http.get "http://eu.httpbin.org/basic-auth/user/passwd"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# Author:: radiospiel (mailto:eno@radiospiel.org)
|
2
|
+
# Copyright:: Copyright (c) 2011-2015 radiospiel
|
3
|
+
# License:: Distributes under the terms of the Modified BSD License, see LICENSE.BSD for details.
|
4
|
+
|
5
|
+
require_relative 'test_helper'
|
6
|
+
|
7
|
+
class RedirectionTest < Simple::HTTP::TestCase
|
8
|
+
def test_follows_redirections
|
9
|
+
html = http.get "http://eu.httpbin.org/redirect-to?url=http://eu.httpbin.org"
|
10
|
+
assert_match(/title.*httpbin/, html)
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_raises_error_on_invalid_redirect_location
|
14
|
+
assert_raise(ArgumentError) {
|
15
|
+
http.get "http://eu.httpbin.org/redirect-to?url=foo"
|
16
|
+
}
|
17
|
+
end
|
18
|
+
end
|
data/test/simple_http_test.rb
CHANGED
@@ -4,11 +4,14 @@
|
|
4
4
|
|
5
5
|
require_relative 'test_helper'
|
6
6
|
|
7
|
-
class SimpleHttpTest <
|
8
|
-
|
7
|
+
class SimpleHttpTest < Simple::HTTP::TestCase
|
8
|
+
def test_http_google
|
9
|
+
google = http.get "http://google.com"
|
10
|
+
assert_match(/doctype/, google)
|
11
|
+
end
|
9
12
|
|
10
|
-
def
|
11
|
-
google =
|
13
|
+
def test_https_google
|
14
|
+
google = http.get "https://google.com"
|
12
15
|
assert_match(/doctype/, google)
|
13
16
|
end
|
14
17
|
end
|
data/test/test_helper.rb
CHANGED
@@ -8,4 +8,15 @@ require 'test/unit'
|
|
8
8
|
# add_filter "test/*.rb"
|
9
9
|
# end
|
10
10
|
|
11
|
-
require
|
11
|
+
require 'simple/http'
|
12
|
+
require 'simple/http/assertions'
|
13
|
+
|
14
|
+
class Simple::HTTP::TestCase < Test::Unit::TestCase
|
15
|
+
include Simple::HTTP::Assertions
|
16
|
+
|
17
|
+
attr :http
|
18
|
+
|
19
|
+
def setup
|
20
|
+
@http = Simple::HTTP.new
|
21
|
+
end
|
22
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple-http
|
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
|
- radiospiel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-09-
|
11
|
+
date: 2015-09-17 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Simple code for simple HTTP requests
|
14
14
|
email: eno@radiospiel.org
|
@@ -18,10 +18,14 @@ extra_rdoc_files: []
|
|
18
18
|
files:
|
19
19
|
- README.md
|
20
20
|
- lib/simple/http.rb
|
21
|
+
- lib/simple/http/assertions.rb
|
21
22
|
- lib/simple/http/errors.rb
|
22
23
|
- lib/simple/http/expires_in.rb
|
23
24
|
- lib/simple/http/result.rb
|
24
25
|
- lib/simple/http/version.rb
|
26
|
+
- test/assertions_test.rb
|
27
|
+
- test/basic_auth_test.rb
|
28
|
+
- test/redirection_test.rb
|
25
29
|
- test/simple_http_test.rb
|
26
30
|
- test/test_helper.rb
|
27
31
|
homepage: http://github.com/radiospiel/simple-http
|