xsr 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 931719af15817041f1826a036bd4315cd05ce1df
4
- data.tar.gz: 26c3470bb7deb98cdce115aad6dc55afdfe31d7f
3
+ metadata.gz: f9e3370abd455ea33763b68734c67f393baabda3
4
+ data.tar.gz: 9c67710ee1481d692ace115a269483b7f66c141c
5
5
  SHA512:
6
- metadata.gz: 674f547fdd8f62a5921ff148023e80d58aac8d58f0c414045512ea688a36ea1065d7f4f7c4b39f1298374bd2c0f56f6d43ac01130c4036ed67c47893144d36ff
7
- data.tar.gz: dea4adc7133a5ba9adc51ea096c6262ea2c6a195fde4a6e72205ef05e9991646e632c00aa719c569781a4ae16dc85c6175409e5d014a9c1513fcdc8787a3e6bc
6
+ metadata.gz: c4caf181848332499d1ccbc4284171fb8de9393c5fb4ba5f48b45d9e683cad1944a34aa514ffa2b1ff46a72837a786e5ebe4547e5232b5a326cee4ce0d5d8627
7
+ data.tar.gz: a5cb61cad99572cdf0cfffdb65a299d3cf37365ace9e93f4be880971193d4acffcacbb7179a4919d67ee5f9f5639fbfc2fdb2b3ec854b9e9c773bffd6ec236b6
data/README.md CHANGED
@@ -97,6 +97,12 @@ resp.success?
97
97
  #=> Response status code is 2xx
98
98
 
99
99
  resp.bad_request?
100
+ #=> Response status code is 400
101
+
102
+ resp.unauthorized?
103
+ #=> Response status code is 401
104
+
105
+ resp.forbidden?
100
106
  #=> Response status code is 403
101
107
 
102
108
  resp.not_found?
@@ -112,6 +118,21 @@ resp.http_response
112
118
  #-> Call http_response to get full Net::HTTPResponse object
113
119
  ```
114
120
 
121
+ ### SSL considerations
122
+ By default, XSR verifies the SSL certificate for the requested server.
123
+
124
+ To use a custom CA Root certificate set ```ca_file```
125
+ ```
126
+ client = XSR::Client.new(ca_file: '/path/to/my_custom.pem')
127
+ client.get('https://api.something.io/get')
128
+ ```
129
+
130
+ In case you want to skip this verfication, set ```skip_cert_check```:
131
+ ```
132
+ client = XSR::Client.new(skip_cert_check: true)
133
+ client.get('https://api.something.io/get')
134
+ ```
135
+
115
136
  ##What's next?
116
137
  I'm not planning to add more features right now, but feel free to fork this repo and add any extra functionality you consider that should be included. Please, submit a PR with proposed changes or fixes.
117
138
  Just keep in mind a minimalist paradigm (https://youtu.be/tXVr2E1vfmk).
data/lib/xsr/client.rb CHANGED
@@ -3,10 +3,14 @@ module XSR
3
3
  class Client
4
4
  attr_accessor :base_url
5
5
  attr_accessor :default_header
6
+ attr_accessor :skip_cert_check
7
+ attr_accessor :ca_file
6
8
 
7
9
  def initialize(options = {})
8
10
  @base_url = options[:base_url]
9
11
  @base_header = options[:header]
12
+ @skip_cert_check = options[:skip_cert_check]
13
+ @ca_file = options[:ca_file]
10
14
  end
11
15
 
12
16
  def post(path, options = {})
@@ -46,7 +50,12 @@ module XSR
46
50
 
47
51
  if uri.scheme == 'https' # Requires SSL?
48
52
  http_session.use_ssl = true
49
- http_session.verify_mode = OpenSSL::SSL::VERIFY_NONE
53
+ if @skip_cert_check || options[:skip_cert_check]
54
+ http_session.verify_mode = OpenSSL::SSL::VERIFY_NONE
55
+ else
56
+ http_session.ca_file = @ca_file || options[:ca_file]
57
+ http_session.verify_mode = OpenSSL::SSL::VERIFY_PEER
58
+ end
50
59
  end
51
60
 
52
61
  XSR::Response.new http_session.start { |http| http.request(req) }
data/makefile CHANGED
@@ -3,3 +3,7 @@
3
3
  test:
4
4
  @echo Running tests... && \
5
5
  RUBYLIB=./lib cutest test/*_test.rb
6
+
7
+ console:
8
+ @echo Running console... && \
9
+ RUBYLIB=./lib irb -r xsr
@@ -0,0 +1,36 @@
1
+ require 'xsr'
2
+ require_relative 'helper'
3
+
4
+ # Tests for SSL requests
5
+ module ClientSSLTest
6
+ prepare do
7
+ @client = XSR::Client.new(base_url: 'https://httpbin.org')
8
+ end
9
+
10
+ test 'https request' do
11
+ resp = @client.get('/get')
12
+
13
+ assert resp.http_response.instance_of?(Net::HTTPOK)
14
+ end
15
+
16
+ test 'https request skipping certificate verification' do
17
+ resp = @client.get('/get', skip_cert_check: true)
18
+
19
+ assert resp.http_response.instance_of?(Net::HTTPOK)
20
+ end
21
+
22
+ test 'client skip certificate verification' do
23
+ client = XSR::Client.new(base_url: 'https://httpbin.org', skip_cert_check: true)
24
+ resp = client.get('/get')
25
+
26
+ assert resp.http_response.instance_of?(Net::HTTPOK)
27
+ end
28
+
29
+ test 'set custom invalid ca pem' do
30
+ @client.ca_file = 'does_not_exist.pem'
31
+
32
+ assert_raise(OpenSSL::SSL::SSLError) do
33
+ @client.get('/get')
34
+ end
35
+ end
36
+ end
data/test/client_test.rb CHANGED
@@ -52,7 +52,7 @@ module ClientTest
52
52
 
53
53
  test 'default headers are passed to request' do
54
54
  h = { 'Some-Header' => 'some_value' }
55
- resp = XSR::Client.new(header: h).get('https://httpbin.org/headers')
55
+ resp = XSR::Client.new(header: h).get('http://httpbin.org/headers')
56
56
 
57
57
  assert resp.http_response.instance_of?(Net::HTTPOK)
58
58
 
@@ -69,10 +69,4 @@ module ClientTest
69
69
  argument = MultiJson.load(resp.http_response.body)['args']['some_arg']
70
70
  assert_equal argument, 'some_value'
71
71
  end
72
-
73
- test 'https request' do
74
- resp = XSR::Client.new.get('https://httpbin.org/get')
75
-
76
- assert resp.http_response.instance_of?(Net::HTTPOK)
77
- end
78
72
  end
data/xsr.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'xsr'
3
- s.version = '1.3.0'
3
+ s.version = '1.4.0'
4
4
  s.summary = 'XSR: eXtremely Simple REST Client'
5
5
  s.description = 'A very simple library to talk to a REST/JSON API'
6
6
  s.authors = ['matiasow']
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xsr
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - matiasow
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-21 00:00:00.000000000 Z
11
+ date: 2015-05-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json
@@ -65,6 +65,7 @@ files:
65
65
  - lib/xsr/client.rb
66
66
  - lib/xsr/response.rb
67
67
  - makefile
68
+ - test/client_ssl_test.rb
68
69
  - test/client_test.rb
69
70
  - test/helper.rb
70
71
  - test/response_test.rb